ruby-cqhttp 0.0.6 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.bundle/config +2 -0
- data/.gitignore +12 -2
- data/.rubocop.yml +27 -0
- data/Gemfile +13 -11
- data/README.md +70 -59
- data/icon.png +0 -0
- data/lib/Core/Core.rb +19 -0
- data/lib/Core/Http/API.rb +183 -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 +141 -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
- data/ruby-cqhttp.gemspec +14 -16
- data/tests/client.rb +21 -0
- data/tests/rainbows.conf +3 -0
- data/tests/server.rb +77 -0
- metadata +29 -31
- data/lib/Bot/Api.rb +0 -115
- data/lib/Bot/Bot.rb +0 -137
- data/lib/Bot/Utils.rb +0 -42
- data/lib/ruby-cqhttp.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 925802d93d0690ccc22b020a3236b52c837b3d0961d55a9962cd9bb4f6d55b69
|
4
|
+
data.tar.gz: ac87d2efe8fc65a2f8a3b6c5216306b69c7c59b25c68c3f1c9067b4b2138c87a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1e82a77d99140324a348d12d5aac54a5dffa137c577ba9260773607849dfa596934cd79a86ad6f98d35f1ec4ad8b4020eac8049e1bf2b1a38908804d517fc07
|
7
|
+
data.tar.gz: 483be6c942157ed36fe9f206e2a6b14265dd563a6441e5effc36e9ffa9d18d03dbc3060932c17d2f5f7a34d2ca36936c4677619d9db2fb2395517b3330da2454
|
data/.bundle/config
ADDED
data/.gitignore
CHANGED
data/.rubocop.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.2
|
3
|
+
NewCops: enable
|
4
|
+
Naming/VariableName:
|
5
|
+
Enabled: false
|
6
|
+
Naming/MethodName:
|
7
|
+
Enabled: false
|
8
|
+
Naming/FileName:
|
9
|
+
Enabled: false
|
10
|
+
Style/AsciiComments:
|
11
|
+
Enabled: false
|
12
|
+
Style/FrozenStringLiteralComment:
|
13
|
+
Enabled: false
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
Layout/LineLength:
|
17
|
+
Enabled: false
|
18
|
+
Naming/MethodParameterName:
|
19
|
+
Enabled: false
|
20
|
+
Metrics/MethodLength:
|
21
|
+
Enabled: false
|
22
|
+
Metrics/AbcSize:
|
23
|
+
Enabled: false
|
24
|
+
Metrics/ClassLength:
|
25
|
+
Enabled: false
|
26
|
+
Metrics/CyclomaticComplexity:
|
27
|
+
Enabled: false
|
data/Gemfile
CHANGED
@@ -1,11 +1,13 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
source
|
4
|
-
|
5
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
6
|
-
|
7
|
-
gem '
|
8
|
-
gem 'eventmachine'
|
9
|
-
gem '
|
10
|
-
gem '
|
11
|
-
gem '
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
gem 'event_emitter'
|
8
|
+
gem 'eventmachine', platform: :ruby
|
9
|
+
gem 'faye-websocket'
|
10
|
+
gem 'json'
|
11
|
+
gem 'puma'
|
12
|
+
gem 'rack'
|
13
|
+
gem 'solargraph'
|
data/README.md
CHANGED
@@ -1,59 +1,70 @@
|
|
1
|
-
#
|
2
|
-
|
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)
|
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
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
|
1
|
+
# ruby-cqhttp
|
2
|
+
|
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/)
|
5
|
+
|
6
|
+
![图标](https://raw.githubusercontent.com/fantasyzhjk/ruby-cqhttp/main/icon.png)
|
7
|
+
|
8
|
+
一个基于 OneBot 标准的 QQ 机器人框架
|
9
|
+
|
10
|
+
用 Ruby 写 QQ 机器人!
|
11
|
+
|
12
|
+
本库还在快速迭代更新中。。(咕了
|
13
|
+
|
14
|
+
## 使用
|
15
|
+
|
16
|
+
安装
|
17
|
+
|
18
|
+
$ gem install ruby-cqhttp
|
19
|
+
|
20
|
+
或者
|
21
|
+
|
22
|
+
在 `Gemfile` 中添加
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'ruby-cqhttp'
|
26
|
+
```
|
27
|
+
|
28
|
+
然后运行
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
## 示例
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'onebot-ruby'
|
36
|
+
|
37
|
+
logger = Onebot::Logging::Logger.new().setLoggerLevel(Logger::INFO) # 如果需要 logger 可以直接建立
|
38
|
+
api = Onebot::Http::API.new().setLogger(logger)
|
39
|
+
|
40
|
+
Onebot::Core.connect url: "ws://127.0.0.1:6700", logger: logger do |bot|
|
41
|
+
bot.on :logged do |botQQ|
|
42
|
+
logger.log('我开了欸')
|
43
|
+
end
|
44
|
+
|
45
|
+
bot.on :message do |data|
|
46
|
+
logger.log('我收到消息了欸')
|
47
|
+
# 复读
|
48
|
+
bot.sendMessage(data.message, data)
|
49
|
+
end
|
50
|
+
|
51
|
+
# 获取并发出好友撤回的消息
|
52
|
+
bot.on :notice do |notice_type, data|
|
53
|
+
if notice_type == 'friend_recall'
|
54
|
+
req = bot.get_msg(data.message_id)
|
55
|
+
bot.sendPrivateMessage req.message, req.sender.user_id
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# 自动同意群邀请和好友请求
|
60
|
+
bot.on :request do |request_type, data|
|
61
|
+
if request_type == 'group'
|
62
|
+
api.acceptGroupRequest(data.flag, data.sub_type) if data.sub_type == 'invite'
|
63
|
+
elsif request_type == 'friend'
|
64
|
+
api.acceptFriendRequest(data.flag)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
**具体使用方法请查看 tests*
|
data/icon.png
ADDED
Binary file
|
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,183 @@
|
|
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 apiIp [String]
|
15
|
+
# @param apiPort [Number]
|
16
|
+
# @return [URI]
|
17
|
+
def initialize(apiIp: '127.0.0.1', apiPort: 5700)
|
18
|
+
@url = URI::HTTP.build(host: apiIp, port: apiPort)
|
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
|
+
# @param url [URI]
|
32
|
+
# @return [Hash]
|
33
|
+
def setGroupName(group_id, group_name)
|
34
|
+
data = sendReq('set_group_name', { group_id: group_id.to_i, group_name: })
|
35
|
+
if data['status'] == 'ok'
|
36
|
+
@logger.log '设置群头像成功'
|
37
|
+
else
|
38
|
+
@logger.log '设置群头像失败', Logger::WARN
|
39
|
+
end
|
40
|
+
data['data']
|
41
|
+
end
|
42
|
+
|
43
|
+
# 获取图片信息
|
44
|
+
#
|
45
|
+
# @param file [String]
|
46
|
+
# @param url [URI]
|
47
|
+
# @return [Hash]
|
48
|
+
def getImage(file)
|
49
|
+
data = sendReq('get_image', { file: })
|
50
|
+
if data['status'] == 'ok'
|
51
|
+
@logger.log '下载图片成功'
|
52
|
+
else
|
53
|
+
@logger.log '下载图片失败', Logger::WARN
|
54
|
+
end
|
55
|
+
data['data']
|
56
|
+
end
|
57
|
+
|
58
|
+
# 获取消息
|
59
|
+
#
|
60
|
+
# @param message_id [Number]
|
61
|
+
# @param url [URI]
|
62
|
+
# @return [Hash]
|
63
|
+
def get_msg(message_id)
|
64
|
+
data = sendReq('get_msg', { message_id: })
|
65
|
+
if data['status'] == 'ok'
|
66
|
+
@logger.log '消息获取成功'
|
67
|
+
else
|
68
|
+
@logger.log '消息获取失败', Logger::WARN
|
69
|
+
end
|
70
|
+
data['data']
|
71
|
+
end
|
72
|
+
|
73
|
+
# 发送私聊消息
|
74
|
+
#
|
75
|
+
# @param msg [String]
|
76
|
+
# @param user_id [Number]
|
77
|
+
# @param url [URI]
|
78
|
+
# @return [Hash]
|
79
|
+
def sendPrivateMessage(msg, user_id)
|
80
|
+
data = sendReq('send_private_msg', { user_id:, message: msg })
|
81
|
+
if data['status'] == 'ok'
|
82
|
+
message_id = data['data']['message_id']
|
83
|
+
@logger.log "发送至私聊 #{user_id} 的消息: #{msg} (#{message_id})"
|
84
|
+
else
|
85
|
+
@logger.log '发送消息失败', Logger::WARN
|
86
|
+
end
|
87
|
+
data['data']
|
88
|
+
end
|
89
|
+
|
90
|
+
# 发送群聊消息
|
91
|
+
#
|
92
|
+
# @param msg [String]
|
93
|
+
# @param group_id [Number]
|
94
|
+
# @param url [URI]
|
95
|
+
# @return [Hash]
|
96
|
+
def sendGroupMessage(msg, group_id)
|
97
|
+
data = sendReq('send_group_msg', { group_id:, message: msg })
|
98
|
+
if data['status'] == 'ok'
|
99
|
+
message_id = data['data']['message_id']
|
100
|
+
@logger.log "发送至群 #{group_id} 的消息: #{msg} (#{message_id})"
|
101
|
+
else
|
102
|
+
@logger.log '发送消息失败', Logger::WARN
|
103
|
+
end
|
104
|
+
data['data']
|
105
|
+
end
|
106
|
+
|
107
|
+
# 接受好友邀请
|
108
|
+
#
|
109
|
+
# @param flag [String]
|
110
|
+
# @param reason [String]
|
111
|
+
# @param url [URI]
|
112
|
+
# @return [Boolean]
|
113
|
+
def acceptFriendRequest(flag, reason = nil)
|
114
|
+
data = sendReq('set_friend_add_request', { flag:, approve: true, remark: reason })
|
115
|
+
if data['status'] == 'ok'
|
116
|
+
@logger.log '已通过好友请求'
|
117
|
+
true
|
118
|
+
else
|
119
|
+
@logger.log '请求通过失败', Logger::WARN
|
120
|
+
false
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# 拒绝好友邀请
|
125
|
+
#
|
126
|
+
# @param flag [String]
|
127
|
+
# @param url [URI]
|
128
|
+
# @return [Boolean]
|
129
|
+
def refuseFriendRequest(flag)
|
130
|
+
data = sendReq('set_friend_add_request', { flag:, approve: false })
|
131
|
+
if data['status'] == 'ok'
|
132
|
+
@logger.log '已拒绝好友请求'
|
133
|
+
true
|
134
|
+
else
|
135
|
+
@logger.log '请求拒绝失败', Logger::WARN
|
136
|
+
false
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
# 接受加群请求
|
141
|
+
#
|
142
|
+
# @param flag [String]
|
143
|
+
# @param sub_type [String]
|
144
|
+
# @param url [URI]
|
145
|
+
# @return [Boolean]
|
146
|
+
def acceptGroupRequest(flag, sub_type)
|
147
|
+
data = sendReq('set_group_add_request', { flag:, sub_type:, approve: true })
|
148
|
+
if data['status'] == 'ok'
|
149
|
+
@logger.log '已通过加群请求'
|
150
|
+
true
|
151
|
+
else
|
152
|
+
@logger.log '请求通过失败', Logger::WARN
|
153
|
+
false
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
# 拒绝加群请求
|
158
|
+
#
|
159
|
+
# @param flag [String]
|
160
|
+
# @param sub_type [String]
|
161
|
+
# @param reason [String]
|
162
|
+
# @param url [URI]
|
163
|
+
# @return [Boolean]
|
164
|
+
def refuseGroupRequest(flag, sub_type, reason = nil)
|
165
|
+
data = sendReq('set_group_add_request', { flag:, sub_type:, approve: false, reason: })
|
166
|
+
if data['status'] == 'ok'
|
167
|
+
@logger.log '已拒绝加群请求'
|
168
|
+
true
|
169
|
+
else
|
170
|
+
@logger.log '请求拒绝失败', Logger::WARN
|
171
|
+
false
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
private
|
176
|
+
|
177
|
+
def sendReq(action, params, url = @url)
|
178
|
+
url.path = action
|
179
|
+
JSON.parse(Utils.httpPost(url, params.to_json))
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
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
|