This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLBot.rb
144 lines (129 loc) · 3.79 KB
/
LBot.rb
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require 'websocket-eventmachine-client'
require 'json'
require './plug'
class LBot
def initialize
get_config
end
def get_config
config_text = File.readlines("config.rb").join
config = eval(config_text)
@default_wmsg = config[:welcome_message]
@ws_uri = config[:ws_uri]
@group_config = config[:group_config]
end
def at_qq qq
"[CQ:at,qq=#{qq}] "
end
# 发送私聊消息
def send_private_msg receiver_id, msg
content = {
:action => "send_private_msg",
:params => {
:user_id => receiver_id,
:message => msg
}
}.to_json
@QQ.send content
end
# 发送群消息
def send_group_msg group_id, msg
content = {
:action => "send_group_msg",
:params => {
:group_id => group_id,
:message => msg
}
}.to_json
@QQ.send content
end
# 撤回消息,仅 pro 版本支持
def delete_msg msg_id
content = {
:action => "delete_msg",
:params => {
:message_id => msg_id
}
}.to_json
@QQ.send content
end
# 踢出群成员
def set_group_kick group_id, user_id
content = {
:action => "set_group_kick",
:params => {
:group_id => group_id,
:user_id => user_id,
:reject_add_request => false
}
}.to_json
@QQ.send content
end
# 禁言群成员,单位分钟
def set_group_ban group_id, user_id, minutes
ban_time = 60 * minutes
content = {
:action => "set_group_ban",
:params => {
:group_id => group_id,
:user_id => user_id,
:duration => ban_time
}
}.to_json
@QQ.send content
end
# 取消禁言
def cancel_group_ban group_id, user_id
set_group_ban group_id, user_id, 0
end
# 群成员增加
def deal_group_increase sender_id, group_id
group_config = @group_config[group_id]
wmsg = group_config[:wmsg] if !group_config.nil?
if wmsg.nil?
wmsg = @default_wmsg
end
content = at_qq(sender_id) + wmsg
send_group_msg group_id, content
end
# 解析事件
def deal_raw_msg raw_msg
raw_msg = JSON.parse raw_msg
post_type = raw_msg['post_type']
case post_type
when 'message'
msg = raw_msg['message']
sender_id = raw_msg['user_id']
case raw_msg['message_type']
when 'private'
when 'group'
group_id = raw_msg['group_id']
deal_group_msg sender_id, group_id, raw_msg
when 'discuss'
end
when 'notice'
case raw_msg['notice_type']
when 'group_upload'
when 'group_admin'
when 'group_decrease'
when 'group_increase'
sender_id = raw_msg['user_id']
group_id = raw_msg['group_id']
deal_group_increase sender_id, group_id
end
when 'request'
end
end
def start
EM.run do
@event = WebSocket::EventMachine::Client.connect :uri => @ws_uri + '/event/'
@QQ = WebSocket::EventMachine::Client.connect :uri => @ws_uri + '/api/'
@event.onmessage do |raw_msg, type|
puts raw_msg
deal_raw_msg raw_msg
end
end
end
end
bot = LBot.new
bot.start