ruby-cqhttp 0.0.7 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +21 -0
- data/README.md +71 -61
- data/lib/Core/Core.rb +19 -0
- data/lib/Core/Http/API.rb +174 -0
- data/lib/Core/Logging/EventLogger.rb +101 -0
- data/lib/Core/Logging/Logger.rb +58 -0
- data/lib/Core/Utils.rb +85 -0
- data/lib/Core/WebSocket/API.rb +137 -0
- data/lib/Core/WebSocket/Bot.rb +69 -0
- data/lib/Core/WebSocket/Client.rb +44 -0
- data/lib/Core/WebSocket/Server.rb +35 -0
- data/lib/onebot-ruby.rb +44 -0
- metadata +92 -27
- data/.gitignore +0 -2
- data/Gemfile +0 -11
- data/lib/Bot/Api.rb +0 -188
- data/lib/Bot/Bot.rb +0 -255
- data/lib/Bot/Utils.rb +0 -119
- data/lib/ruby-cqhttp.rb +0 -17
- data/ruby-cqhttp.gemspec +0 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f4ded7c0bb8d8255401a9d13b5c98eafdba3324f7221bf6ef4e1c0bf7646ba8e
|
4
|
+
data.tar.gz: 2cc88a1e903da48f2b74859f3f7bc8a7c5cc2292d70061f219d528c1b90ead2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8e2d94cbf60ef8697ac9a8fef81a46051939f2daac915ddb4b8b6d31193ef4cd87acfb27686c4762680e0ffbfe53653d4e9ccec2a304fbac9f8b5c604073a795
|
7
|
+
data.tar.gz: 5925ec6144d391cded1963e7790907dece0a8c945e269ce0c19c2a8f5e930395e98443c607a871690db8fef6306d4e6ad4722566fb70e79b13631597cb70f5e2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Zh_Jk
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,61 +1,71 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
[![
|
4
|
-
[![
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
#
|
53
|
-
bot.on :
|
54
|
-
if
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
1
|
+
# ruby-cqhttp
|
2
|
+
|
3
|
+
[![Badge](https://img.shields.io/badge/OneBot-11-black?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHAAAABwCAMAAADxPgR5AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAAxQTFRF////29vbr6+vAAAAk1hCcwAAAAR0Uk5T////AEAqqfQAAAKcSURBVHja7NrbctswDATQXfD//zlpO7FlmwAWIOnOtNaTM5JwDMa8E+PNFz7g3waJ24fviyDPgfhz8fHP39cBcBL9KoJbQUxjA2iYqHL3FAnvzhL4GtVNUcoSZe6eSHizBcK5LL7dBr2AUZlev1ARRHCljzRALIEog6H3U6bCIyqIZdAT0eBuJYaGiJaHSjmkYIZd+qSGWAQnIaz2OArVnX6vrItQvbhZJtVGB5qX9wKqCMkb9W7aexfCO/rwQRBzsDIsYx4AOz0nhAtWu7bqkEQBO0Pr+Ftjt5fFCUEbm0Sbgdu8WSgJ5NgH2iu46R/o1UcBXJsFusWF/QUaz3RwJMEgngfaGGdSxJkE/Yg4lOBryBiMwvAhZrVMUUvwqU7F05b5WLaUIN4M4hRocQQRnEedgsn7TZB3UCpRrIJwQfqvGwsg18EnI2uSVNC8t+0QmMXogvbPg/xk+Mnw/6kW/rraUlvqgmFreAA09xW5t0AFlHrQZ3CsgvZm0FbHNKyBmheBKIF2cCA8A600aHPmFtRB1XvMsJAiza7LpPog0UJwccKdzw8rdf8MyN2ePYF896LC5hTzdZqxb6VNXInaupARLDNBWgI8spq4T0Qb5H4vWfPmHo8OyB1ito+AysNNz0oglj1U955sjUN9d41LnrX2D/u7eRwxyOaOpfyevCWbTgDEoilsOnu7zsKhjRCsnD/QzhdkYLBLXjiK4f3UWmcx2M7PO21CKVTH84638NTplt6JIQH0ZwCNuiWAfvuLhdrcOYPVO9eW3A67l7hZtgaY9GZo9AFc6cryjoeFBIWeU+npnk/nLE0OxCHL1eQsc1IciehjpJv5mqCsjeopaH6r15/MrxNnVhu7tmcslay2gO2Z1QfcfX0JMACG41/u0RrI9QAAAABJRU5ErkJggg==)](https://github.com/botuniverse/onebot-11)
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/ruby-cqhttp.svg)](https://badge.fury.io/rb/ruby-cqhttp)
|
5
|
+
[![yard docs](http://img.shields.io/badge/yard-docs-blue.svg)](https://www.rubydoc.info/github/fantasyzhjk/ruby-cqhttp)
|
6
|
+
|
7
|
+
![图标](https://raw.githubusercontent.com/fantasyzhjk/ruby-cqhttp/main/icon.png)
|
8
|
+
|
9
|
+
一个基于 OneBot 标准的 QQ 机器人框架
|
10
|
+
|
11
|
+
用 Ruby 写 QQ 机器人!
|
12
|
+
|
13
|
+
本库还在快速迭代更新中。。(咕了
|
14
|
+
|
15
|
+
## 使用
|
16
|
+
|
17
|
+
安装
|
18
|
+
|
19
|
+
$ gem install ruby-cqhttp
|
20
|
+
|
21
|
+
或者
|
22
|
+
|
23
|
+
在 `Gemfile` 中添加
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'ruby-cqhttp'
|
27
|
+
```
|
28
|
+
|
29
|
+
然后运行
|
30
|
+
|
31
|
+
$ bundle
|
32
|
+
|
33
|
+
## 示例
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'onebot-ruby'
|
37
|
+
|
38
|
+
logger = Onebot::Logging::Logger.new().setLoggerLevel(Logger::INFO) # 如果需要 logger 可以直接建立
|
39
|
+
api = Onebot::Http::API.new().setLogger(logger)
|
40
|
+
|
41
|
+
Onebot::Core.connect url: "ws://127.0.0.1:6700", logger: logger do |bot|
|
42
|
+
bot.on :logged do |botQQ|
|
43
|
+
logger.log('我开了欸')
|
44
|
+
end
|
45
|
+
|
46
|
+
bot.on :message do |data|
|
47
|
+
logger.log('我收到消息了欸')
|
48
|
+
# 复读
|
49
|
+
bot.sendMessage(data.message, data)
|
50
|
+
end
|
51
|
+
|
52
|
+
# 获取并发出好友撤回的消息
|
53
|
+
bot.on :notice do |notice_type, data|
|
54
|
+
if notice_type == 'friend_recall'
|
55
|
+
req = bot.get_msg(data.message_id)
|
56
|
+
bot.sendPrivateMessage req.message, req.sender.user_id
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# 自动同意群邀请和好友请求
|
61
|
+
bot.on :request do |request_type, data|
|
62
|
+
if request_type == 'group'
|
63
|
+
api.acceptGroupRequest(data.flag, data.sub_type) if data.sub_type == 'invite'
|
64
|
+
elsif request_type == 'friend'
|
65
|
+
api.acceptFriendRequest(data.flag)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
**具体使用方法请查看 examples*
|
data/lib/Core/Core.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
module Onebot
|
2
|
+
# 消息处理,ws连接
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
# Onebot::Core.connect host: host, port: port {|bot| ... }
|
6
|
+
class Core
|
7
|
+
# 新建连接
|
8
|
+
#
|
9
|
+
# @param host [String]
|
10
|
+
# @param port [Number]
|
11
|
+
# @return [WebSocket]
|
12
|
+
def self.connect(url:, logger: nil, protocols: nil, options: {})
|
13
|
+
client = ::Onebot::WebSocket::Client.new(url:, logger:)
|
14
|
+
yield client if block_given?
|
15
|
+
client.connect(protocols, options)
|
16
|
+
client
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
module Onebot
|
2
|
+
# OneBot标准API
|
3
|
+
# [OneBot文档] (https://github.com/howmanybots/onebot)
|
4
|
+
#
|
5
|
+
# Example:
|
6
|
+
# Onebot::Api.getImage file
|
7
|
+
module Http
|
8
|
+
class API
|
9
|
+
# @return [URI] HTTP API链接
|
10
|
+
attr_accessor :url
|
11
|
+
|
12
|
+
# (初始化) 设置API地址
|
13
|
+
#
|
14
|
+
# @param host [String]
|
15
|
+
# @param port [Number]
|
16
|
+
# @return [URI]
|
17
|
+
def initialize(host: '127.0.0.1', port: 5700)
|
18
|
+
@url = URI::HTTP.build(host: host, port: port)
|
19
|
+
@logger = Logging::EventLogger.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def setLogger(logger)
|
23
|
+
@logger = Logging::EventLogger.new(logger)
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# 设置群名
|
28
|
+
#
|
29
|
+
# @param group_id [Number]
|
30
|
+
# @param group_name [String]
|
31
|
+
# @return [Hash]
|
32
|
+
def setGroupName(group_id, group_name)
|
33
|
+
data = sendReq('set_group_name', { group_id: group_id.to_i, group_name: })
|
34
|
+
if data['status'] == 'ok'
|
35
|
+
@logger.log '设置群头像成功'
|
36
|
+
else
|
37
|
+
@logger.log '设置群头像失败', Logger::WARN
|
38
|
+
end
|
39
|
+
data['data']
|
40
|
+
end
|
41
|
+
|
42
|
+
# 获取图片信息
|
43
|
+
#
|
44
|
+
# @param file [String]
|
45
|
+
# @return [Hash]
|
46
|
+
def getImage(file)
|
47
|
+
data = sendReq('get_image', { file: })
|
48
|
+
if data['status'] == 'ok'
|
49
|
+
@logger.log '下载图片成功'
|
50
|
+
else
|
51
|
+
@logger.log '下载图片失败', Logger::WARN
|
52
|
+
end
|
53
|
+
data['data']
|
54
|
+
end
|
55
|
+
|
56
|
+
# 获取消息
|
57
|
+
#
|
58
|
+
# @param message_id [Number]
|
59
|
+
# @return [Hash]
|
60
|
+
def get_msg(message_id)
|
61
|
+
data = sendReq('get_msg', { message_id: })
|
62
|
+
if data['status'] == 'ok'
|
63
|
+
@logger.log '消息获取成功'
|
64
|
+
else
|
65
|
+
@logger.log '消息获取失败', Logger::WARN
|
66
|
+
end
|
67
|
+
data['data']
|
68
|
+
end
|
69
|
+
|
70
|
+
# 发送私聊消息
|
71
|
+
#
|
72
|
+
# @param msg [String]
|
73
|
+
# @param user_id [Number]
|
74
|
+
# @return [Hash]
|
75
|
+
def sendPrivateMessage(msg, user_id)
|
76
|
+
data = sendReq('send_private_msg', { user_id:, message: msg })
|
77
|
+
if data['status'] == 'ok'
|
78
|
+
message_id = data['data']['message_id']
|
79
|
+
@logger.log "发送至私聊 #{user_id} 的消息: #{msg} (#{message_id})"
|
80
|
+
else
|
81
|
+
@logger.log '发送消息失败', Logger::WARN
|
82
|
+
end
|
83
|
+
data['data']
|
84
|
+
end
|
85
|
+
|
86
|
+
# 发送群聊消息
|
87
|
+
#
|
88
|
+
# @param msg [String]
|
89
|
+
# @param group_id [Number]
|
90
|
+
# @return [Hash]
|
91
|
+
def sendGroupMessage(msg, group_id)
|
92
|
+
data = sendReq('send_group_msg', { group_id:, message: msg })
|
93
|
+
if data['status'] == 'ok'
|
94
|
+
message_id = data['data']['message_id']
|
95
|
+
@logger.log "发送至群 #{group_id} 的消息: #{msg} (#{message_id})"
|
96
|
+
else
|
97
|
+
@logger.log '发送消息失败', Logger::WARN
|
98
|
+
end
|
99
|
+
data['data']
|
100
|
+
end
|
101
|
+
|
102
|
+
# 接受好友邀请
|
103
|
+
#
|
104
|
+
# @param flag [String]
|
105
|
+
# @param reason [String]
|
106
|
+
# @return [Boolean]
|
107
|
+
def acceptFriendRequest(flag, reason = nil)
|
108
|
+
data = sendReq('set_friend_add_request', { flag:, approve: true, remark: reason })
|
109
|
+
if data['status'] == 'ok'
|
110
|
+
@logger.log '已通过好友请求'
|
111
|
+
true
|
112
|
+
else
|
113
|
+
@logger.log '请求通过失败', Logger::WARN
|
114
|
+
false
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# 拒绝好友邀请
|
119
|
+
#
|
120
|
+
# @param flag [String]
|
121
|
+
# @return [Boolean]
|
122
|
+
def refuseFriendRequest(flag)
|
123
|
+
data = sendReq('set_friend_add_request', { flag:, approve: false })
|
124
|
+
if data['status'] == 'ok'
|
125
|
+
@logger.log '已拒绝好友请求'
|
126
|
+
true
|
127
|
+
else
|
128
|
+
@logger.log '请求拒绝失败', Logger::WARN
|
129
|
+
false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
# 接受加群请求
|
134
|
+
#
|
135
|
+
# @param flag [String]
|
136
|
+
# @param sub_type [String]
|
137
|
+
# @return [Boolean]
|
138
|
+
def acceptGroupRequest(flag, sub_type)
|
139
|
+
data = sendReq('set_group_add_request', { flag:, sub_type:, approve: true })
|
140
|
+
if data['status'] == 'ok'
|
141
|
+
@logger.log '已通过加群请求'
|
142
|
+
true
|
143
|
+
else
|
144
|
+
@logger.log '请求通过失败', Logger::WARN
|
145
|
+
false
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
# 拒绝加群请求
|
150
|
+
#
|
151
|
+
# @param flag [String]
|
152
|
+
# @param sub_type [String]
|
153
|
+
# @param reason [String]
|
154
|
+
# @return [Boolean]
|
155
|
+
def refuseGroupRequest(flag, sub_type, reason = nil)
|
156
|
+
data = sendReq('set_group_add_request', { flag:, sub_type:, approve: false, reason: })
|
157
|
+
if data['status'] == 'ok'
|
158
|
+
@logger.log '已拒绝加群请求'
|
159
|
+
true
|
160
|
+
else
|
161
|
+
@logger.log '请求拒绝失败', Logger::WARN
|
162
|
+
false
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
private
|
167
|
+
|
168
|
+
def sendReq(action, params, url = @url)
|
169
|
+
url.path = '/' << action
|
170
|
+
JSON.parse(Utils.httpPost(url, params.to_json))
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
module Onebot
|
2
|
+
module Logging
|
3
|
+
class EventLogger
|
4
|
+
def initialize(logger = nil)
|
5
|
+
@logger = logger
|
6
|
+
end
|
7
|
+
|
8
|
+
def log(str, severity = ::Logger::INFO, app = 'Onebot')
|
9
|
+
return if @logger.nil?
|
10
|
+
|
11
|
+
@logger.log(str, severity, app)
|
12
|
+
end
|
13
|
+
|
14
|
+
#
|
15
|
+
# 消息解析部分
|
16
|
+
#
|
17
|
+
def dataParse(msg)
|
18
|
+
return if @logger.nil?
|
19
|
+
|
20
|
+
# 连接成功
|
21
|
+
@selfID = msg.self_id if msg.meta_event_type == 'lifecycle' && msg.sub_type == 'connect'
|
22
|
+
@logger.log(msg.to_json, ::Logger::DEBUG) if msg.meta_event_type != 'heartbeat' # 过滤心跳
|
23
|
+
case msg.post_type
|
24
|
+
#
|
25
|
+
# 请求事件
|
26
|
+
#
|
27
|
+
when 'request'
|
28
|
+
onReq(msg)
|
29
|
+
#
|
30
|
+
# 提醒事件
|
31
|
+
#
|
32
|
+
when 'notice'
|
33
|
+
onNotice(msg)
|
34
|
+
#
|
35
|
+
# 消息事件
|
36
|
+
#
|
37
|
+
when 'message'
|
38
|
+
onMsg(msg)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def onMsg(msg)
|
45
|
+
if msg.message_type == 'group' # 判断是否为群聊
|
46
|
+
@logger.log "收到群 #{msg.group_id} 内 #{msg.sender.nickname}(#{msg.user_id}) 的消息: #{msg.raw_message} (#{msg.message_id})"
|
47
|
+
else
|
48
|
+
@logger.log "收到好友 #{msg.sender.nickname}(#{msg.user_id}) 的消息: #{msg.raw_message} (#{msg.message_id})"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def onReq(msg)
|
53
|
+
case msg.request_type
|
54
|
+
when 'group'
|
55
|
+
if msg.sub_type == 'add'
|
56
|
+
@logger.log "收到用户 #{msg.user_id} 加群 #{msg.group_id} 的请求 (#{msg.flag})" # 加群请求
|
57
|
+
end
|
58
|
+
if msg.sub_type == 'invite'
|
59
|
+
@logger.log "收到用户 #{msg.user_id} 的加群 #{msg.group_id} 请求 (#{msg.flag})" # 加群邀请
|
60
|
+
end
|
61
|
+
when 'friend' # 加好友邀请
|
62
|
+
@logger.log "收到用户 #{msg.user_id} 的好友请求 (#{msg.flag})"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def onNotice(msg)
|
67
|
+
case msg.notice_type
|
68
|
+
when 'group_admin' # 群管理员变动
|
69
|
+
@logger.log "群 #{msg.group_id} 内 #{msg.user_id} 成了管理员" if msg.sub_type == 'set' # 设置管理员
|
70
|
+
@logger.log "群 #{msg.group_id} 内 #{msg.user_id} 没了管理员" if msg.sub_type == 'unset' # 取消管理员
|
71
|
+
when 'group_increase' # 群成员增加
|
72
|
+
if msg.sub_type == 'approve'
|
73
|
+
@logger.log "#{msg.operator_id} 已同意 #{msg.user_id} 进入了群 #{msg.group_id}" # 管理员已同意入群
|
74
|
+
end
|
75
|
+
if msg.sub_type == 'invite'
|
76
|
+
@logger.log "#{msg.operator_id} 邀请 #{msg.user_id} 进入了群 #{msg.group_id}" # 管理员邀请入群
|
77
|
+
end
|
78
|
+
when 'group_decrease' # 群成员减少
|
79
|
+
@logger.log "被 #{msg.operator_id} 踢出了群 #{msg.group_id}" if msg.sub_type == 'kick_me' # 登录号被踢
|
80
|
+
if msg.sub_type == 'kick'
|
81
|
+
@logger.log "#{msg.user_id} 被 #{msg.operator_id} 踢出了群 #{msg.group_id}" # 成员被踢
|
82
|
+
end
|
83
|
+
@logger.log "#{msg.operator_id} 退出了群 #{msg.group_id}" if msg.sub_type == 'leave' # 主动退群
|
84
|
+
when 'group_ban' # 群禁言
|
85
|
+
if msg.sub_type == 'ban'
|
86
|
+
@logger.log "群 #{msg.group_id} 内 #{msg.user_id} 被 #{msg.operator_id} 禁言了 #{msg.duration} 秒" # 禁言
|
87
|
+
end
|
88
|
+
if msg.sub_type == 'lift_ban'
|
89
|
+
@logger.log "群 #{msg.group_id} 内 #{msg.user_id} 被 #{msg.operator_id} 解除禁言" # 解除禁言
|
90
|
+
end
|
91
|
+
when 'friend_add' # 好友添加
|
92
|
+
@logger.log "#{msg.user_id} 成了你的好友"
|
93
|
+
when 'group_recall' # 群消息撤回
|
94
|
+
@logger.log "群 #{msg.group_id} 内 #{msg.user_id} 撤回了一条消息 (#{msg.message_id})"
|
95
|
+
when 'friend_recall' # 好友消息撤回
|
96
|
+
@logger.log "好友 #{msg.user_id} 撤回了一条消息 (#{msg.message_id})"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Onebot
|
2
|
+
module Logging
|
3
|
+
class Logger
|
4
|
+
# @!method stdLogger
|
5
|
+
# @return [Logger] 终端Logger
|
6
|
+
# @!method fileLogger
|
7
|
+
# @return [Logger] 文件Logger
|
8
|
+
# @!method loggerFile
|
9
|
+
# @return [String] Logger文件地址
|
10
|
+
attr_accessor :stdLogger, :fileLogger, :loggerFile
|
11
|
+
|
12
|
+
# 初始化日志
|
13
|
+
#
|
14
|
+
# @param loggerFile [String]
|
15
|
+
def initialize(loggerFile = nil)
|
16
|
+
@loggerFile = loggerFile
|
17
|
+
@stdLogger = setLogger(::Logger.new($stdout))
|
18
|
+
@fileLogger = setLogger(::Logger.new(@loggerFile, 'daily')) if @loggerFile
|
19
|
+
end
|
20
|
+
|
21
|
+
# 设置日志等级
|
22
|
+
#
|
23
|
+
# @param loggerLevel [String]
|
24
|
+
def setLoggerLevel(loggerLevel)
|
25
|
+
@stdLogger.level = loggerLevel
|
26
|
+
@fileLogger.level = loggerLevel if @loggerFile
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
# 输出日志
|
31
|
+
#
|
32
|
+
# @param str [String]
|
33
|
+
# @param severity [Logger::INFO, Logger::DEBUG, Logger::WARN, Logger::ERROR]
|
34
|
+
# @param app [String]
|
35
|
+
def log(str, severity = ::Logger::INFO, app = 'Onebot')
|
36
|
+
@stdLogger.log(severity, str, app)
|
37
|
+
@fileLogger.log(severity, str, app) if @loggerFile
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
# 设置logger
|
43
|
+
def setLogger(logger)
|
44
|
+
logger.level = 'INFO'
|
45
|
+
logger.formatter = proc do |severity, datetime, progname, msg|
|
46
|
+
if progname.nil?
|
47
|
+
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{severity}]: #{msg.to_s.gsub(/\n/, '\n').gsub(/\r/, '\r')}\n"
|
48
|
+
else
|
49
|
+
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S')}][#{progname}][#{severity}]: #{msg.to_s.gsub(/\n/, '\n').gsub(
|
50
|
+
/\r/, '\r'
|
51
|
+
)}\n"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
logger
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/Core/Utils.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
module Onebot
|
2
|
+
# 各种工具包
|
3
|
+
#
|
4
|
+
# Example:
|
5
|
+
# Onebot::Utils.log str, Logger::INFO
|
6
|
+
module Utils
|
7
|
+
extend self
|
8
|
+
# post发包
|
9
|
+
#
|
10
|
+
# @param url [URI]
|
11
|
+
# @param ret [String]
|
12
|
+
# @return [String]
|
13
|
+
def httpPost(url, ret)
|
14
|
+
req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' })
|
15
|
+
req.body = ret
|
16
|
+
res = Net::HTTP.start(url.hostname, url.port) do |http|
|
17
|
+
http.request(req)
|
18
|
+
end
|
19
|
+
res.body
|
20
|
+
end
|
21
|
+
|
22
|
+
alias post httpPost
|
23
|
+
# 消息转义
|
24
|
+
# & -> &
|
25
|
+
# [ -> [
|
26
|
+
# ] -> ]
|
27
|
+
#
|
28
|
+
# @param msg [String]
|
29
|
+
# @return [String]
|
30
|
+
def cqEscape(msg)
|
31
|
+
msg.gsub!('&', '&')
|
32
|
+
msg.gsub!('[', '[')
|
33
|
+
msg.gsub!(']', ']')
|
34
|
+
msg
|
35
|
+
end
|
36
|
+
|
37
|
+
# 消息反转义
|
38
|
+
# & -> &
|
39
|
+
# [ -> [
|
40
|
+
# ] -> ]
|
41
|
+
#
|
42
|
+
# @param msg [String]
|
43
|
+
# @return [String]
|
44
|
+
def cqUnescape(msg)
|
45
|
+
msg.gsub!('&', '&')
|
46
|
+
msg.gsub!('[', '[')
|
47
|
+
msg.gsub!(']', ']')
|
48
|
+
msg
|
49
|
+
end
|
50
|
+
|
51
|
+
# CQ码解析, 将字符串格式转换成 Onebot v11 的消息段数组格式
|
52
|
+
#
|
53
|
+
# @param cqmsg [String]
|
54
|
+
# @return [Array]
|
55
|
+
def cqParse(cqmsg)
|
56
|
+
msgary = []
|
57
|
+
cqary = cqmsg.scan(/\[CQ:(.*?),(.*?)\]/m)
|
58
|
+
isCode = false
|
59
|
+
i = 0
|
60
|
+
temp = ''
|
61
|
+
cqmsg.each_char do |c|
|
62
|
+
if isCode
|
63
|
+
if c == ']'
|
64
|
+
isCode = false
|
65
|
+
matches = cqary[i]
|
66
|
+
cqcode = { type: matches[0], data: {} }
|
67
|
+
matches[1].split(',').each do |arg|
|
68
|
+
args = arg.split('=')
|
69
|
+
cqcode[:data][args[0].to_sym] = args[1]
|
70
|
+
end
|
71
|
+
msgary << cqcode
|
72
|
+
end
|
73
|
+
elsif c == '['
|
74
|
+
msgary << { type: 'text', data: { text: cqEscape(temp) } }
|
75
|
+
temp = ''
|
76
|
+
isCode = true
|
77
|
+
else
|
78
|
+
temp << c
|
79
|
+
end
|
80
|
+
end
|
81
|
+
msgary << { type: 'text', data: { text: cqEscape(temp) } } unless temp.empty?
|
82
|
+
msgary
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|