ruby-cqhttp 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/Bot/Bot.rb DELETED
@@ -1,137 +0,0 @@
1
- module CQHttp
2
- 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)
5
-
6
- def self.connect(url, debugmode=false)
7
- client = ::CQHttp::Bot::WebSocket.new(url, debugmode)
8
- yield client if block_given?
9
- client.connect
10
- client
11
- end
12
-
13
- class WebSocket
14
- attr_accessor :url
15
- attr_accessor :debugmode
16
- attr_accessor :selfID
17
-
18
- include EventEmitter
19
-
20
- def initialize(url, debugmode)
21
- @url = url
22
- @debugmode = debugmode
23
- end
24
-
25
- def connect
26
- EM.run do
27
- @ws = Faye::WebSocket::Client.new(@url)
28
-
29
- @ws.on :open do
30
- end
31
-
32
- @ws.on :message do |event|
33
- Thread.new { dataParse(event.data)}
34
- end
35
-
36
- @ws.on :close do
37
- emit :close
38
- Utils.log Time.new, '!', '已断开连接'
39
- @ws = nil
40
- exit
41
- end
42
-
43
- @ws.on :error do |event|
44
- emit :error, event
45
- @ws = nil
46
- end
47
- end
48
- end
49
-
50
-
51
- def sendPrivateMessage(msg, user_id)
52
- ret = { action: 'send_private_msg', params: { user_id: user_id, message: msg }, echo: 'BotPrivateMessage' }.to_json
53
- Utils.log Time.new, '↑', "发送至私聊 #{user_id} 的消息: #{msg}"
54
- @ws.send ret
55
- end
56
-
57
- def sendGroupMessage(msg, group_id)
58
- ret = { action: 'send_group_msg', params: { group_id: group_id, message: msg }, echo: 'BotGroupMessage' }.to_json
59
- Utils.log Time.new, '↑', "发送至群 #{group_id} 的消息: #{msg}"
60
- @ws.send ret
61
- end
62
-
63
- private
64
-
65
- def dataParse(data)
66
- msg = JSON.parse(data)
67
- sdr = Sender.new
68
- tar = Target.new
69
- tar.time = msg['time']
70
- if msg['meta_event_type'] == 'lifecycle' && msg['sub_type'] == 'connect'
71
- @selfID = msg['self_id']
72
- Utils.log Time.at(tar.time), '!', "连接成功, BotQQ: #{@selfID}"
73
- emit :logged, @selfID
74
- end
75
- if @debugmode == true # 判断是否为debug模式
76
- puts msg if msg['meta_event_type'] != 'heartbeat' # 过滤心跳
77
- end
78
- case msg['post_type']
79
- #
80
- # 请求事件
81
- #
82
- when 'request'
83
- case msg['request_type']
84
- when 'group'
85
- if msg['sub_type'] == 'invite' # 加群邀请
86
- Utils.log Time.at(tar.time), '↓', "收到用户 #{msg['user_id']} 的加群 #{msg['group_id']} 请求 (#{msg['flag']})"
87
- end
88
- when 'friend' # 加好友邀请
89
- Utils.log Time.at(tar.time), '↓', "收到用户 #{msg['user_id']} 的好友请求 (#{msg['flag']})"
90
- end
91
- emit :request, msg['request_type'], msg['sub_type'], msg['flag']
92
- #
93
- # 提醒事件
94
- #
95
- when 'notice'
96
- case msg['notice_type']
97
- when 'group_decrease' # 群数量减少
98
- if msg['sub_type'] == 'kick_me' # 被踢出
99
- Utils.log Time.at(tar.time), '!', "被 #{msg['operator_id']} 踢出群 #{msg['group_id']}"
100
- end
101
- when 'group_recall'
102
- Utils.log Time.at(tar.time), '!', "群 #{msg['group_id']} 中 #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
103
- when 'friend_recall'
104
- Utils.log Time.at(tar.time), '!', "好友 #{msg['user_id']} 撤回了一条消息 (#{msg['message_id']})"
105
- end
106
- emit :notice, msg['notice_type'], msg
107
-
108
- #
109
- # 消息事件
110
- #
111
- when 'message'
112
- tar.user_id = msg['user_id'] # 用户id
113
- tar.message_id = msg['message_id'] # 消息id
114
- tar.message = msg['message'] # 消息内容
115
- sdr.age = msg['sender']['age'] # 年龄
116
- sdr.nickname = msg['sender']['nickname'] # 原有用户名
117
- sdr.sex = msg['sender']['sex'] # 性别
118
- tar.messagetype = msg['message_type'] # 消息类型
119
- # 下面仅群聊
120
- tar.group_id = msg['group_id'] # 群id
121
- sdr.card = msg['sender']['card'] # 群昵称
122
- sdr.title = msg['sender']['title'] # 头衔
123
- sdr.member_role = msg['sender']['role'] # 群成员地位
124
- sdr.qqlevel = msg['sender']['level'] # 群成员等级
125
- if tar.messagetype == 'group' # 判断是否为群聊
126
- Utils.log Time.at(tar.time), '↓', "收到群 #{tar.group_id} 内 #{sdr.nickname}(#{tar.user_id}) 的消息: #{tar.message} (#{tar.message_id})"
127
- emit :groupMessage, tar.message, sdr, tar
128
- else
129
- Utils.log Time.at(tar.time), '↓', "收到好友 #{sdr.nickname}(#{tar.user_id}) 的消息: #{tar.message} (#{tar.message_id})"
130
- emit :privateMessage, tar.message, sdr, tar
131
- end
132
- emit :message, tar.message, sdr, tar
133
- end
134
- end
135
- end
136
- end
137
- end
data/lib/Bot/Utils.rb DELETED
@@ -1,19 +0,0 @@
1
- module CQHttp
2
- class Utils
3
- class << self
4
- def log(time, status, str)
5
- puts "[#{time.strftime('%Y-%m-%d %H:%M:%S')}][#{status}]: #{str}"
6
- end
7
-
8
- def httpPost(*args)
9
- url = URI.parse args[0]
10
- req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' })
11
- req.body = args[1]
12
- res = Net::HTTP.start(url.hostname, url.port) do |http|
13
- http.request(req)
14
- end
15
- res.body
16
- end
17
- end
18
- end
19
- end
data/lib/ruby-cqhttp.rb DELETED
@@ -1,12 +0,0 @@
1
- require 'faye/websocket'
2
- require 'eventmachine'
3
- require 'json'
4
- require 'event_emitter'
5
- require 'net/http'
6
-
7
- module CQHttp
8
- autoload :Bot, File.expand_path('Bot/Bot', __dir__)
9
- autoload :Api, File.expand_path('Bot/Api', __dir__)
10
- autoload :Utils, File.expand_path('Bot/Utils', __dir__)
11
- end
12
-