ruby-cqhttp 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dc16893adf2a71ea90af191c3d83c6bbea44417d58b661914f10d1a38284b6fa
4
- data.tar.gz: b25414575ff09da58a404ec81fca6832a195f9935b3a5c0e9da1f629db002479
3
+ metadata.gz: 9e05ea3a7529706ede1358efa6735e455e8ca125242beab7107145ba78032f9b
4
+ data.tar.gz: 9aeeb36d26f766ee178aff75920e60256a8b7d38f9b26731ac89dc9c0f07eefc
5
5
  SHA512:
6
- metadata.gz: 83bd2c919fd51fe8f356fc91ab2c4a2e04b4f778ff70b492fe0f5d18b07165fb3bf201caecfc818ecca812fd1c7f1e200e6d6b6407233b7d9e34aeeadefbd9e2
7
- data.tar.gz: d0dc12c9582ca47c100886c80c4baa2eed56e44a44b42f09d8a1f7b80da01b61619ed7aa652546c9902d9c6c737742987835e6680d83466905bbbb1fbc936938
6
+ metadata.gz: 0fbf62999904bee4c4c3811be5ab6ad315e0ef1b10a057ac4d6ea117a982c3a353c9b614283369bb58103c1f80dcedd67c6d126417fe4603c3ed5c41b8958ac6
7
+ data.tar.gz: 9d5b13fb23d8f0334cf57aea46ed33e60978c517026755f8bf6880c7ac6c5fd13115ec08f378af20a8505cec64d3444a2e1b81b7f9e21b2cd2d537d480cbf159
data/.gitignore CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/README.md CHANGED
@@ -1,10 +1,14 @@
1
1
  # RUBY-CQHTTP
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/ruby-cqhttp.svg)](https://badge.fury.io/rb/ruby-cqhttp)
4
- [![yard docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/gems/ruby-cqhttp)
4
+ [![yard docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://rubydoc.info/github/fantasyzhjk/ruby-cqhttp)
5
+
6
+ 一个基于 OneBot 标准的 QQ 机器人框架
5
7
 
6
8
  用 Ruby 写 QQ 机器人!
7
9
 
10
+ 本库还在快速迭代更新中。。
11
+
8
12
  ## 使用
9
13
 
10
14
  安装
@@ -16,15 +20,13 @@
16
20
  在 `Gemfile` 中添加
17
21
 
18
22
  ```ruby
19
- gem 'eventmachine'
23
+ gem 'ruby-cqhttp'
20
24
  ```
21
25
 
22
26
  然后运行
23
27
 
24
28
  $ bundle
25
29
 
26
- 安装
27
-
28
30
  ## 示例
29
31
 
30
32
  ```ruby
@@ -48,12 +50,12 @@ CQHttp::Bot.connect host: '127.0.0.1', port: 6700 do |bot|
48
50
  end
49
51
 
50
52
  # 自动同意群邀请和好友请求
51
- bot.on :request do |request_type, sub_type, flag|
53
+ bot.on :request do |request_type, data|
52
54
  if request_type == 'group'
53
- CQHttp::Api.acceptGroupRequest(flag, sub_type) if sub_type == 'invite'
55
+ CQHttp::Api.acceptGroupRequest(data['flag'], data['sub_type']) if data['sub_type'] == 'invite'
54
56
  elsif request_type == 'friend'
55
- CQHttp::Api.acceptFriendRequest(flag)
57
+ CQHttp::Api.acceptFriendRequest(data['flag'])
56
58
  end
57
59
  end
58
60
  end
59
- ```
61
+ ```
data/lib/Bot/Api.rb CHANGED
@@ -1,10 +1,29 @@
1
1
  module CQHttp
2
+ # OneBot标准API
3
+ # [OneBot文档] (https://github.com/howmanybots/onebot)
4
+ #
5
+ # Example:
6
+ # CQHttp::Api.getImage file
2
7
  class Api
8
+ # @return [URI] HTTP API链接
3
9
  attr_accessor :apiUrl
4
10
  class << self
11
+
12
+ # 设置API地址
13
+ #
14
+ # @param apiIp [String]
15
+ # @param apiPort [Number]
16
+ # @return [URI]
5
17
  def setUrl(apiIp:'127.0.0.1', apiPort:5700)
6
18
  @apiUrl = URI::HTTP.build(host: apiIp, port: apiPort)
7
19
  end
20
+
21
+ # 设置群名
22
+ #
23
+ # @param group_id [Number]
24
+ # @param group_name [String]
25
+ # @param url [URI]
26
+ # @return [Hash]
8
27
  def setGroupName(group_id, group_name, url=@apiUrl)
9
28
  url.path = "/set_group_name"
10
29
  ret = { group_id: group_id.to_i, group_name: group_name }.to_json
@@ -14,33 +33,49 @@ module CQHttp
14
33
  else
15
34
  Utils.log '设置群头像失败', Logger::WARN
16
35
  end
36
+ return data['data']
17
37
  end
18
38
 
19
- def getImage(file, url=@apiUrl) # UNFINSHED
39
+ # 下载图片(未完成)
40
+ #
41
+ # @param file [String]
42
+ # @param url [URI]
43
+ # @return [Hash]
44
+ def getImage(file, url=@apiUrl)
20
45
  url.path = "/get_image"
21
46
  ret = { file: file }.to_json
22
47
  data = JSON.parse(Utils.httpPost(url, ret))
23
48
  if data['status'] == 'ok'
24
49
  Utils.log '下载图片成功'
25
- return data['data']
26
50
  else
27
51
  Utils.log '下载图片失败', Logger::WARN
28
52
  end
53
+ return data['data']
29
54
  end
30
55
 
31
- def get_msg(message_id, url=@apiUrl) # UNFINSHED
56
+ # 获取消息
57
+ #
58
+ # @param message_id [Number]
59
+ # @param url [URI]
60
+ # @return [Hash]
61
+ def get_msg(message_id, url=@apiUrl)
32
62
  url.path = "/get_msg"
33
63
  ret = { message_id: message_id }.to_json
34
64
  data = JSON.parse(Utils.httpPost(url, ret))
35
65
  if data['status'] == 'ok'
36
66
  Utils.log '消息获取成功'
37
- return data['data']
38
67
  else
39
68
  Utils.log '消息获取失败', Logger::WARN
40
69
  end
41
-
70
+ return data['data']
42
71
  end
43
72
 
73
+ # 发送私聊消息
74
+ #
75
+ # @param msg [String]
76
+ # @param user_id [Number]
77
+ # @param url [URI]
78
+ # @return [Hash]
44
79
  def sendPrivateMessage(msg, user_id, url=@apiUrl)
45
80
  url.path = "/send_private_msg"
46
81
  ret = { user_id: user_id, message: msg }.to_json
@@ -48,12 +83,18 @@ module CQHttp
48
83
  if data['status'] == 'ok'
49
84
  message_id = data['data']['message_id']
50
85
  Utils.log "发送至私聊 #{user_id} 的消息: #{msg} (#{message_id})"
51
- return message_id
52
86
  else
53
87
  Utils.log '发送消息失败', Logger::WARN
54
88
  end
89
+ return data['data']
55
90
  end
56
91
 
92
+ # 发送群聊消息
93
+ #
94
+ # @param msg [String]
95
+ # @param group_id [Number]
96
+ # @param url [URI]
97
+ # @return [Hash]
57
98
  def sendGroupMessage(msg, group_id, url=@apiUrl)
58
99
  url.path = "/send_group_msg"
59
100
  ret = { group_id: group_id, message: msg }.to_json
@@ -61,53 +102,85 @@ module CQHttp
61
102
  if data['status'] == 'ok'
62
103
  message_id = data['data']['message_id']
63
104
  Utils.log "发送至群 #{group_id} 的消息: #{msg} (#{message_id})"
64
- return message_id
65
105
  else
66
106
  Utils.log '发送消息失败', Logger::WARN
67
107
  end
108
+ return data['data']
68
109
  end
69
110
 
70
- def acceptFriendRequest(flag, url=@apiUrl)
111
+ # 接受好友邀请
112
+ #
113
+ # @param flag [String]
114
+ # @param reason [String]
115
+ # @param url [URI]
116
+ # @return [Boolean]
117
+ def acceptFriendRequest(flag, reason=nil, url=@apiUrl)
71
118
  url.path = "/set_friend_add_request"
72
- ret = { flag: flag, approve: true }.to_json
119
+ ret = { flag: flag, approve: true, remark: reason }.to_json
73
120
  data = JSON.parse(Utils.httpPost(url, ret))
74
121
  if data['status'] == 'ok'
75
122
  Utils.log '已通过好友请求'
123
+ true
76
124
  else
77
125
  Utils.log '请求通过失败', Logger::WARN
126
+ false
78
127
  end
79
128
  end
80
129
 
130
+ # 拒绝好友邀请
131
+ #
132
+ # @param flag [String]
133
+ # @param url [URI]
134
+ # @return [Boolean]
81
135
  def refuseFriendRequest(flag, url=@apiUrl)
82
136
  url.path = "/set_friend_add_request"
83
137
  ret = { flag: flag, approve: false }.to_json
84
138
  user_id = JSON.parse(Utils.httpPost(url, ret))
85
139
  if data['status'] == 'ok'
86
140
  Utils.log '已拒绝好友请求'
141
+ true
87
142
  else
88
143
  Utils.log '请求拒绝失败', Logger::WARN
144
+ false
89
145
  end
90
146
  end
91
147
 
148
+ # 接受加群请求
149
+ #
150
+ # @param flag [String]
151
+ # @param sub_type [String]
152
+ # @param url [URI]
153
+ # @return [Boolean]
92
154
  def acceptGroupRequest(flag, sub_type, url=@apiUrl)
93
155
  url.path = "/set_group_add_request"
94
156
  ret = { flag: flag, sub_type: sub_type, approve: true }.to_json
95
157
  data = JSON.parse(Utils.httpPost(url, ret))
96
158
  if data['status'] == 'ok'
97
159
  Utils.log '已通过加群请求'
160
+ true
98
161
  else
99
162
  Utils.log '请求通过失败', Logger::WARN
163
+ false
100
164
  end
101
165
  end
102
166
 
103
- def refuseGroupRequest(flag, sub_type, url=@apiUrl)
167
+ # 拒绝加群请求
168
+ #
169
+ # @param flag [String]
170
+ # @param sub_type [String]
171
+ # @param reason [String]
172
+ # @param url [URI]
173
+ # @return [Boolean]
174
+ def refuseGroupRequest(flag, sub_type, reason=nil, url=@apiUrl)
104
175
  url.path = "/set_group_add_request"
105
- ret = { flag: flag, sub_type: sub_type, approve: false }.to_json
176
+ ret = { flag: flag, sub_type: sub_type, approve: false, reason: reason }.to_json
106
177
  data = JSON.parse(Utils.httpPost(url, ret))
107
178
  if data['status'] == 'ok'
108
179
  Utils.log '已拒绝加群请求'
180
+ true
109
181
  else
110
182
  Utils.log '请求拒绝失败', Logger::WARN
183
+ false
111
184
  end
112
185
  end
113
186
  end
data/lib/Bot/Bot.rb CHANGED
@@ -1,7 +1,55 @@
1
1
  module CQHttp
2
+ # 消息处理,ws连接
3
+ #
4
+ # Example:
5
+ # CQHttp::Bot.connect host: host, port: port {|bot| ... }
2
6
  class Bot
3
- Sender = Struct.new(:age, :member_role, :card, :qqlevel, :nickname, :title, :sex)
4
- Target = Struct.new(:messagetype, :time, :group_id, :user_id, :message_id, :message)
7
+ # 发送人信息
8
+ #
9
+ # @!attribute age
10
+ # @return [Number] 年龄
11
+ # @!attribute member_role
12
+ # @return [String] 角色,owner 或 admin 或 member
13
+ # @!attribute card
14
+ # @return [String] 群名片/备注
15
+ # @!attribute user_id
16
+ # @return [Number] 发送者 QQ 号
17
+ # @!attribute qqlevel
18
+ # @return [String] 成员等级
19
+ # @!attribute nickname
20
+ # @return [String] 昵称
21
+ # @!attribute title
22
+ # @return [String] 专属头衔
23
+ # @!attribute sex
24
+ # @return [String] 性别,male 或 female 或 unknown
25
+ Sender = Struct.new(:age, :member_role, :card, :user_id, :qqlevel, :nickname, :title, :sex)
26
+ # 消息事件数据
27
+ #
28
+ # @!attribute messagetype
29
+ # @return [String] 成员等级
30
+ # @!attribute time
31
+ # @return [Number] 事件发生的时间戳
32
+ # @!attribute group_id
33
+ # @return [Number] 群号
34
+ # @!attribute user_id
35
+ # @return [Number] 发送者 QQ 号
36
+ # @!attribute message_id
37
+ # @return [Number] 消息 ID
38
+ # @!attribute message
39
+ # @return [String] 消息内容
40
+ # @!attribute raw_message
41
+ # @return [String] 原始消息内容
42
+ # @!attribute sub_type
43
+ # @return [String] 消息子类型,私聊中如果是好友则是 friend,如果是群临时会话则是 group,群聊中正常消息是 normal,匿名消息是 anonymous,系统提示(如「管理员已禁止群内匿名聊天」)是 notice
44
+ # @!attribute anonymous
45
+ # @return [Hash] 匿名信息,如果不是匿名消息则为 null
46
+ Target = Struct.new(:messagetype, :time, :group_id, :user_id, :message_id, :message, :raw_message, :sub_type, :anonymous)
47
+
48
+ # 新建连接
49
+ #
50
+ # @param host [String]
51
+ # @param port [Number]
52
+ # @return [WebSocket]
5
53
  def self.connect(host:, port:)
6
54
  url = URI::WS.build(host: host, port: port)
7
55
  Api.setUrl()
@@ -12,22 +60,30 @@ module CQHttp
12
60
  client
13
61
  end
14
62
 
63
+ # WebSocket连接处理部分
15
64
  class WebSocket
65
+ # @return [URI] WS URL
16
66
  attr_accessor :url
67
+ # @return [Faye::WebSocket::Client] WS Conn
68
+ attr_accessor :ws
69
+ # @return [Number] self QQ id
17
70
  attr_accessor :selfID
18
71
 
19
72
  include EventEmitter
20
73
 
74
+ # 设置 WS URL
21
75
  def initialize(url)
76
+ @queueList = {}
22
77
  @url = url
23
78
  end
24
79
 
80
+ # 连接 WS
25
81
  def connect
26
82
  EM.run do
27
83
  @ws = Faye::WebSocket::Client.new(@url.to_s)
28
84
 
29
85
  @ws.on :message do |event|
30
- Thread.new { dataParse(event.data)}
86
+ Thread.new { dataParse(event.data) }
31
87
  end
32
88
 
33
89
  @ws.on :close do |event|
@@ -44,26 +100,68 @@ module CQHttp
44
100
  end
45
101
  end
46
102
 
47
-
103
+ # 发送私聊消息
104
+ #
105
+ # @param msg [String]
106
+ # @param user_id [Number]
107
+ # @return [Hash]
48
108
  def sendPrivateMessage(msg, user_id)
49
- ret = { action: 'send_private_msg', params: { user_id: user_id, message: msg }, echo: 'BotPrivateMessage' }.to_json
50
- Utils.log "发送至私聊 #{user_id} 的消息: #{msg}"
51
- @ws.send ret
109
+ echo = Time.now.to_i.to_s
110
+ params = { action: 'send_private_msg', params: { user_id: user_id, message: msg }, echo: echo }.to_json
111
+ @ws.send params
112
+ @queueList[echo] = Queue.new
113
+ ret = @queueList[echo].pop
114
+ if parseRet(ret)
115
+ Utils.log "发送至私聊 #{user_id} 的消息: #{msg} (#{ret['data']['message_id']})"
116
+ else
117
+ Utils.log "发送消息失败,错误码: #{ret['msg']}, 错误消息: #{ret['wording']}", Logger::WARN
118
+ end
119
+ return ret['data']
52
120
  end
53
121
 
122
+ # 发送群聊消息
123
+ #
124
+ # @param msg [String]
125
+ # @param group_id [Number]
126
+ # @return [Hash]
54
127
  def sendGroupMessage(msg, group_id)
55
- ret = { action: 'send_group_msg', params: { group_id: group_id, message: msg }, echo: 'BotGroupMessage' }.to_json
56
- Utils.log "发送至群 #{group_id} 的消息: #{msg}"
57
- @ws.send ret
128
+ echo = Time.now.to_i.to_s
129
+ params = { action: 'send_group_msg', params: { group_id: group_id, message: msg }, echo: echo }.to_json
130
+ @ws.send params
131
+ @queueList[echo] = Queue.new
132
+ ret = @queueList[echo].pop
133
+ @queueList.delete(echo)
134
+ if parseRet(ret)
135
+ Utils.log "发送至群 #{group_id} 的消息: #{msg} (#{ret['data']['message_id']})"
136
+ else
137
+ Utils.log "发送消息失败,错误码: #{ret['msg']}, 错误消息: #{ret['wording']}", Logger::WARN
138
+ end
139
+ return ret['data']
58
140
  end
59
141
 
142
+ # 发送消息
143
+ # 根据 target [Struct] 自动选择
144
+ #
145
+ # @param msg [String]
146
+ # @param target [Struct]
147
+ # @return [Hash]
60
148
  def sendMessage(msg, target)
61
- sendGroupMessage msg, target.group_id if target.messagetype == 'group'
62
- sendPrivateMessage msg, target.user_id if target.messagetype == 'private'
149
+ return sendGroupMessage msg, target.group_id if target.messagetype == 'group'
150
+ return sendPrivateMessage msg, target.user_id if target.messagetype == 'private'
63
151
  end
64
152
 
65
153
  private
66
154
 
155
+ #
156
+ # 解析API返回
157
+ #
158
+ def parseRet(ret)
159
+ return true if ret['status'] == 'ok'
160
+ return false if ret['status'] == 'failed'
161
+ end
162
+ #
163
+ # 消息解析部分
164
+ #
67
165
  def dataParse(data)
68
166
  msg = JSON.parse(data)
69
167
  sdr = Sender.new
@@ -74,7 +172,13 @@ module CQHttp
74
172
  Utils.log "连接成功, BotQQ: #{@selfID}"
75
173
  emit :logged, @selfID
76
174
  end
77
- Utils.log msg, Logger::DEBUG if msg['meta_event_type'] != 'heartbeat' # 过滤心跳
175
+ Utils.log data, Logger::DEBUG if msg['meta_event_type'] != 'heartbeat' # 过滤心跳
176
+ #
177
+ # 函数回调
178
+ #
179
+ if msg.include?('echo')
180
+ @queueList[msg['echo']] << msg
181
+ end
78
182
  case msg['post_type']
79
183
  #
80
184
  # 请求事件
@@ -82,25 +186,35 @@ module CQHttp
82
186
  when 'request'
83
187
  case msg['request_type']
84
188
  when 'group'
85
- if msg['sub_type'] == 'invite' # 加群邀请
86
- Utils.log "收到用户 #{msg['user_id']} 的加群 #{msg['group_id']} 请求 (#{msg['flag']})"
87
- end
189
+ Utils.log "收到用户 #{msg['user_id']} 加群 #{msg['group_id']} 的请求 (#{msg['flag']})" if msg['sub_type'] == 'add' # 加群请求
190
+ Utils.log "收到用户 #{msg['user_id']} 的加群 #{msg['group_id']} 请求 (#{msg['flag']})" if msg['sub_type'] == 'invite' # 加群邀请
88
191
  when 'friend' # 加好友邀请
89
192
  Utils.log "收到用户 #{msg['user_id']} 的好友请求 (#{msg['flag']})"
90
193
  end
91
- emit :request, msg['request_type'], msg['sub_type'], msg['flag']
194
+ emit :request, msg['request_type'], msg
92
195
  #
93
196
  # 提醒事件
94
197
  #
95
198
  when 'notice'
96
199
  case msg['notice_type']
97
- when 'group_decrease' # 群数量减少
98
- if msg['sub_type'] == 'kick_me' # 被踢出
99
- Utils.log " #{msg['operator_id']} 踢出群 #{msg['group_id']}"
100
- end
101
- when 'group_recall'
102
- Utils.log "#{msg['group_id']} #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
103
- when 'friend_recall'
200
+ when 'group_admin' # 群管理员变动
201
+ Utils.log "群 #{msg['group_id']} 内 #{msg['user_id']} 成了管理员" if msg['sub_type'] == 'set' # 设置管理员
202
+ Utils.log " #{msg['group_id']} #{msg['user_id']} 没了管理员" if msg['sub_type'] == 'unset' # 取消管理员
203
+ when 'group_increase' # 群成员增加
204
+ Utils.log "#{msg['operator_id']} 已同意 #{msg['user_id']} 进入了群 #{msg['group_id']}" if msg['sub_type'] == 'approve' # 管理员已同意入群
205
+ Utils.log "#{msg['operator_id']} 邀请 #{msg['user_id']} 进入了群 #{msg['group_id']}" if msg['sub_type'] == 'invite' # 管理员邀请入群
206
+ when 'group_decrease' # 群成员减少
207
+ Utils.log "被 #{msg['operator_id']} 踢出了群 #{msg['group_id']}" if msg['sub_type'] == 'kick_me' # 登录号被踢
208
+ Utils.log "#{msg['user_id']} 被 #{msg['operator_id']} 踢出了群 #{msg['group_id']}" if msg['sub_type'] == 'kick' # 成员被踢
209
+ Utils.log "#{msg['operator_id']} 退出了群 #{msg['group_id']}" if msg['sub_type'] == 'leave' # 主动退群
210
+ when 'group_ban' # 群禁言
211
+ Utils.log "群 #{msg['group_id']} 内 #{msg['user_id']} 被 #{msg['operator_id']} 禁言了 #{msg['duration']} 秒" if msg['sub_type'] == 'ban' # 禁言
212
+ Utils.log "群 #{msg['group_id']} 内 #{msg['user_id']} 被 #{msg['operator_id']} 解除禁言" if msg['sub_type'] == 'lift_ban' # 解除禁言
213
+ when 'friend_add' # 好友添加
214
+ Utils.log "#{msg['user_id']} 成了你的好友"
215
+ when 'group_recall' # 群消息撤回
216
+ Utils.log "群 #{msg['group_id']} 内 #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
217
+ when 'friend_recall' # 好友消息撤回
104
218
  Utils.log "好友 #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
105
219
  end
106
220
  emit :notice, msg['notice_type'], msg
@@ -110,14 +224,18 @@ module CQHttp
110
224
  #
111
225
  when 'message'
112
226
  tar.user_id = msg['user_id'] # 用户id
227
+ sdr.user_id = msg['sender']['user_id'] # 用户id
113
228
  tar.message_id = msg['message_id'] # 消息id
114
229
  tar.message = msg['message'] # 消息内容
230
+ tar.raw_message = msg['raw_message'] # 消息内容
115
231
  sdr.age = msg['sender']['age'] # 年龄
116
232
  sdr.nickname = msg['sender']['nickname'] # 原有用户名
117
233
  sdr.sex = msg['sender']['sex'] # 性别
118
234
  tar.messagetype = msg['message_type'] # 消息类型
235
+ tar.sub_type = msg['sub_type'] # 消息子类型
119
236
  # 下面仅群聊
120
237
  tar.group_id = msg['group_id'] # 群id
238
+ tar.anonymous = msg['anonymous'] # 匿名信息
121
239
  sdr.card = msg['sender']['card'] # 群昵称
122
240
  sdr.title = msg['sender']['title'] # 头衔
123
241
  sdr.member_role = msg['sender']['role'] # 群成员地位
data/lib/Bot/Utils.rb CHANGED
@@ -1,34 +1,50 @@
1
1
  module CQHttp
2
+ # 各种工具包
3
+ #
4
+ # Example:
5
+ # CQHttp::Utils.log str, Logger::INFO
2
6
  class Utils
7
+ # @!method stdLogger
8
+ # @return [Logger] 终端Logger
9
+ # @!method fileLogger
10
+ # @return [Logger] 文件Logger
11
+ # @!method loggerFile
12
+ # @return [String] Logger文件地址
3
13
  attr_accessor :stdLogger, :fileLogger, :loggerFile
4
14
  class << self
5
- def setLogger(logger)
6
- logger.level = 'INFO'
7
- logger.formatter = proc do |severity, datetime, progname, msg|
8
- if progname == nil
9
- "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{severity}]: #{msg.gsub(/\n/,'\n').gsub(/\r/,'\r') }\n"
10
- else
11
- "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{progname}][#{severity}]: #{msg.gsub(/\n/,'\n').gsub(/\r/,'\r') }\n"
12
- end
13
- end
14
- logger
15
- end
15
+
16
+ # 初始化日志
17
+ #
18
+ # @param loggerFile [String]
16
19
  def initLogger(loggerFile=nil)
17
20
  @loggerFile = loggerFile
18
21
  @stdLogger = setLogger(Logger.new(STDOUT))
19
22
  @fileLogger = setLogger(Logger.new(@loggerFile, 'daily')) if @loggerFile
20
23
  end
21
-
24
+
25
+ # 设置日志等级
26
+ #
27
+ # @param loggerLevel [String]
22
28
  def setLoggerLevel(loggerLevel)
23
29
  @stdLogger.level = loggerLevel
24
30
  @fileLogger.level = loggerLevel if @loggerFile
25
31
  end
26
-
32
+
33
+ # 输出日志
34
+ #
35
+ # @param str [String]
36
+ # @param severity [Logger::INFO, Logger::DEBUG, Logger::WARN, Logger::ERROR]
37
+ # @param app [String]
27
38
  def log(str, severity=Logger::INFO, app="RUBY-CQHTTP")
28
39
  @stdLogger.log(severity, str, app)
29
40
  @fileLogger.log(severity, str, app) if @loggerFile
30
41
  end
31
-
42
+
43
+ # post发包
44
+ #
45
+ # @param url [URI]
46
+ # @param ret [String]
47
+ # @return [String]
32
48
  def httpPost(url, ret)
33
49
  req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' })
34
50
  req.body = ret
@@ -37,6 +53,67 @@ module CQHttp
37
53
  end
38
54
  res.body
39
55
  end
56
+ alias post httpPost
57
+
58
+ # 消息转义
59
+ # &amp; -> &
60
+ # &#91; -> [
61
+ # &#93; -> ]
62
+ #
63
+ # @param msg [String]
64
+ # @return [String]
65
+ def msg_change(msg)
66
+ msg.gsub!('&amp;','&')
67
+ msg.gsub!('&#91;','[')
68
+ msg.gsub!('&#93;',']')
69
+ msg
70
+ end
71
+
72
+ # 消息反转义
73
+ # & -> &amp;
74
+ # [ -> &#91;
75
+ # ] -> &#93;
76
+ #
77
+ # @param msg [String]
78
+ # @return [String]
79
+ def msg_change!(msg)
80
+ msg.gsub!('&','&amp;')
81
+ msg.gsub!('[','&#91;')
82
+ msg.gsub!(']','&#93;')
83
+ msg
84
+ end
85
+
86
+ # CQ码解析
87
+ #
88
+ # @param cqmsg [String]
89
+ # @return [Hash]
90
+ def cq_parse(cqmsg)
91
+ cqary = []
92
+ cqmsg.scan(/\[CQ:(.*?),(.*?)\]/m).each do |matches|
93
+ cqcode = { type: matches[0], data: {} }
94
+ matches[1].split(',').each do |arg|
95
+ args = arg.split('=')
96
+ cqcode[:data][args[0].to_sym] = args[1]
97
+ end
98
+ cqary << cqcode
99
+ end
100
+ cqary
101
+ end
102
+
103
+ private
104
+
105
+ # 设置logger
106
+ def setLogger(logger)
107
+ logger.level = 'INFO'
108
+ logger.formatter = proc do |severity, datetime, progname, msg|
109
+ if progname == nil
110
+ "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{severity}]: #{msg.to_s.gsub(/\n/,'\n').gsub(/\r/,'\r') }\n"
111
+ else
112
+ "[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{progname}][#{severity}]: #{msg.to_s.gsub(/\n/,'\n').gsub(/\r/,'\r') }\n"
113
+ end
114
+ end
115
+ logger
116
+ end
40
117
  end
41
118
  end
42
119
  end
data/lib/ruby-cqhttp.rb CHANGED
@@ -7,6 +7,7 @@ require 'uri/ws'
7
7
  require 'uri'
8
8
  require 'logger'
9
9
 
10
+ # 一个基于 OneBot 标准的 QQ 机器人框架
10
11
  module CQHttp
11
12
  autoload :Bot, File.expand_path('Bot/Bot', __dir__)
12
13
  autoload :Api, File.expand_path('Bot/Api', __dir__)
data/ruby-cqhttp.gemspec CHANGED
@@ -3,10 +3,9 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'ruby-cqhttp'
6
- s.version = '0.0.6'
7
- s.date = '2021-03-23'
8
- s.summary = 'ruby-cqhttp for osucat'
9
- s.description = 'ruby-cqhttp for osucat'
6
+ s.version = '0.0.7'
7
+ s.summary = '一个基于 OneBot 标准的 QQ 机器人框架'
8
+ s.description = '一个基于 OneBot 标准的 QQ 机器人框架'
10
9
  s.authors = ['fantasyzhjk']
11
10
  s.email = 'fantasyzhjk@outlook.com'
12
11
  s.platform = Gem::Platform::RUBY
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-cqhttp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - fantasyzhjk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-23 00:00:00.000000000 Z
11
+ date: 2021-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -94,7 +94,7 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: ruby-cqhttp for osucat
97
+ description: 一个基于 OneBot 标准的 QQ 机器人框架
98
98
  email: fantasyzhjk@outlook.com
99
99
  executables: []
100
100
  extensions: []
@@ -127,8 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
127
127
  - !ruby/object:Gem::Version
128
128
  version: '0'
129
129
  requirements: []
130
- rubygems_version: 3.1.2
130
+ rubygems_version: 3.1.4
131
131
  signing_key:
132
132
  specification_version: 4
133
- summary: ruby-cqhttp for osucat
133
+ summary: 一个基于 OneBot 标准的 QQ 机器人框架
134
134
  test_files: []