discordrb 1.3.0 → 1.3.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of discordrb might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/discordrb/api.rb +271 -0
- data/lib/discordrb/bot.rb +17 -7
- data/lib/discordrb/commands/command-bot.rb +4 -2
- data/lib/discordrb/commands/events.rb +18 -0
- data/lib/discordrb/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bef7f70f8f9ba4e684a19ddd8545909f1e81b609
|
4
|
+
data.tar.gz: 6d8691f3e57f0bff79958ae09cf61aa1b23d6eca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e18a5a8ccd4f31162d5407e69061dace62500b5a620140c7354f325978c61d9a2298d2e9312f855f7ea468f967a93919f55f9c72477357460d17f40932640da4
|
7
|
+
data.tar.gz: 0579638f1f91114752bdaca6afdc2465add207ad7bca65d13c90a38f7da6681ce9a08ece963298304180f4a6dcf05ddb6af512285eb5045571348be31c578139
|
@@ -0,0 +1,271 @@
|
|
1
|
+
require 'rest-client'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module Discordrb::API
|
5
|
+
APIBASE = 'https://discordapp.com/api'
|
6
|
+
module_function
|
7
|
+
|
8
|
+
# Ban a user from a server and delete their messages from the last message_days days
|
9
|
+
def ban_user(token, server_id, user_id, message_days)
|
10
|
+
RestClient.put(
|
11
|
+
"#{APIBASE}/guilds/#{server_id}/bans/#{user_id}?delete-message-days=#{message_days}",
|
12
|
+
Authorization: token
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Get a server's banned users
|
17
|
+
def bans(token, server_id)
|
18
|
+
RestClient.get(
|
19
|
+
"#{APIBASE}/guilds/#{server_id}/bans",
|
20
|
+
Authorization: token
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Login to the server
|
25
|
+
def login(email, password)
|
26
|
+
RestClient.post(
|
27
|
+
"#{APIBASE}/auth/login",
|
28
|
+
email: email,
|
29
|
+
password: password
|
30
|
+
)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Logout from the server
|
34
|
+
def logout(token)
|
35
|
+
RestClient.post(
|
36
|
+
"#{APIBASE}/auth/logout",
|
37
|
+
Authorization: token
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Create a server
|
42
|
+
def create_server(token, name, region)
|
43
|
+
RestClient.post(
|
44
|
+
"#{APIBASE}/guilds",
|
45
|
+
{ 'name' => name, 'region' => region }.to_json,
|
46
|
+
Authorization: token,
|
47
|
+
content_type: :json
|
48
|
+
)
|
49
|
+
end
|
50
|
+
|
51
|
+
# Leave a server
|
52
|
+
def leave_server(server_id)
|
53
|
+
RestClient.delete(
|
54
|
+
"#{APIBASE}/guilds/#{server_id}",
|
55
|
+
Authorization: token
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Get a channel's data
|
60
|
+
def channel(token, channel_id)
|
61
|
+
RestClient.get(
|
62
|
+
"#{APIBASE}/channels/#{channel_id}",
|
63
|
+
Authorization: token
|
64
|
+
)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Create a channel
|
68
|
+
def create_channel(token, server_id, name, type)
|
69
|
+
RestClient.post(
|
70
|
+
"#{APIBASE}/guilds/#{server_id}/channels",
|
71
|
+
{ 'name' => name, 'type' => type }.to_json,
|
72
|
+
Authorization: token,
|
73
|
+
content_type: :json
|
74
|
+
)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Update a channel's data
|
78
|
+
def update_channel(token, channel_id, name, topic, position = 0)
|
79
|
+
RestClient.patch(
|
80
|
+
"#{APIBASE}/channels/#{channel_id}",
|
81
|
+
{ 'name' => name, 'position' => position, 'topic' => topic }.to_json,
|
82
|
+
Authorization: token,
|
83
|
+
content_type: :json
|
84
|
+
)
|
85
|
+
end
|
86
|
+
|
87
|
+
# Delete a channel
|
88
|
+
def delete_channel(token, channel_id)
|
89
|
+
RestClient.delete(
|
90
|
+
"#{APIBASE}/channels/#{channel_id}",
|
91
|
+
Authorization: token
|
92
|
+
)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Join a server using an invite
|
96
|
+
def join_server(token, invite_id)
|
97
|
+
RestClient.post(
|
98
|
+
"#{APIBASE}/invite/#{invite_id}",
|
99
|
+
Authorization: token
|
100
|
+
)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Create a private channel
|
104
|
+
def create_private(token, bot_user_id, user_id)
|
105
|
+
RestClient.post(
|
106
|
+
"#{APIBASE}/users/#{bot_user_id}/channels",
|
107
|
+
{ 'recipient_id' => user_id }.to_json,
|
108
|
+
Authorization: token,
|
109
|
+
content_type: :json
|
110
|
+
)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Create an instant invite from a server or a channel id
|
114
|
+
def create_invite(token, id, max_age = 0, max_uses = 0, temporary = false, xkcd = false)
|
115
|
+
RestClient.post(
|
116
|
+
"#{APIBASE}/channels/#{id}/invites",
|
117
|
+
{ 'max_age' => max_age, 'max_uses' => max_uses, 'temporary' => temporary, 'xkcdpass' => xkcd }.to_json,
|
118
|
+
Authorization: token,
|
119
|
+
content_type: :json
|
120
|
+
)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Send a message to a channel
|
124
|
+
def send_message(token, channel_id, message, mentions = [], tts = false)
|
125
|
+
RestClient.post(
|
126
|
+
"#{APIBASE}/channels/#{channel_id}/messages",
|
127
|
+
{ 'content' => message, 'mentions' => mentions, tts => tts }.to_json,
|
128
|
+
Authorization: token,
|
129
|
+
content_type: :json
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
# Delete a message
|
134
|
+
def delete_message(token, channel_id, message_id)
|
135
|
+
RestClient.delete(
|
136
|
+
"#{APIBASE}/channels/#{channel_id}/messages/#{message_id}",
|
137
|
+
Authorization: token
|
138
|
+
)
|
139
|
+
end
|
140
|
+
|
141
|
+
# Edit a message
|
142
|
+
def edit_message(token, channel_id, message, mentions = [])
|
143
|
+
RestClient.patch(
|
144
|
+
"#{APIBASE}/channels/#{channel_id}/messages",
|
145
|
+
{ 'content' => message, 'mentions' => mentions }.to_json,
|
146
|
+
Authorization: token,
|
147
|
+
content_type: :json
|
148
|
+
)
|
149
|
+
end
|
150
|
+
|
151
|
+
# Acknowledge that a message has been received
|
152
|
+
# The last acknowledged message will be sent in the ready packet,
|
153
|
+
# so this is an easy way to catch up on messages
|
154
|
+
def acknowledge_message(token, channel_id, message_id)
|
155
|
+
RestClient.post(
|
156
|
+
"#{APIBASE}/channels/#{channel_id}/messages/#{message_id}/ack",
|
157
|
+
Authorization: token
|
158
|
+
)
|
159
|
+
end
|
160
|
+
|
161
|
+
# Send a file as a message to a channel
|
162
|
+
def send_file(token, channel_id, file, filename = 'filename')
|
163
|
+
RestClient.post(
|
164
|
+
"#{APIBASE}/channels/#{channel_id}/messages",
|
165
|
+
(filename.to_sym) => file,
|
166
|
+
Authorization: token
|
167
|
+
)
|
168
|
+
end
|
169
|
+
|
170
|
+
# Create a role (parameters such as name and colour will have to be set by update_role afterwards)
|
171
|
+
def create_role(token, server_id)
|
172
|
+
RestClient.post(
|
173
|
+
"#{APIBASE}/guilds/#{server_id}/roles",
|
174
|
+
Authorization: token
|
175
|
+
)
|
176
|
+
end
|
177
|
+
|
178
|
+
# Update a role
|
179
|
+
# Permissions are the Discord defaults; allowed: invite creation, reading/sending messages,
|
180
|
+
# sending TTS messages, embedding links, sending files, reading the history, mentioning everybody,
|
181
|
+
# connecting to voice, speaking and voice activity (push-to-talk isn't mandatory)
|
182
|
+
def update_role(token, server_id, role_id, name, colour, hoist = false, packed_permissions = 36953089)
|
183
|
+
RestClient.patch(
|
184
|
+
"#{APIBASE}/guilds/#{server_id}/roles/#{role_id}",
|
185
|
+
{ 'color' => colour, 'name' => name, 'hoist' => hoist, 'permissions' => packed_permissions }.to_json,
|
186
|
+
Authorization: token,
|
187
|
+
content_type: :json
|
188
|
+
)
|
189
|
+
end
|
190
|
+
|
191
|
+
# Delete a role
|
192
|
+
def delete_role(token, server_id, role_id)
|
193
|
+
RestClient.delete(
|
194
|
+
"#{APIBASE}/guilds/#{server_id}/roles/#{role_id}",
|
195
|
+
Authorization: token
|
196
|
+
)
|
197
|
+
end
|
198
|
+
|
199
|
+
# Update a user's roles
|
200
|
+
def update_user_roles(token, server_id, user_id, roles)
|
201
|
+
RestClient.patch(
|
202
|
+
"#{APIBASE}/guilds/#{server_id}/members/#{user_id}",
|
203
|
+
{ 'roles' => roles }.to_json,
|
204
|
+
Authorization: token,
|
205
|
+
content_type: :json
|
206
|
+
)
|
207
|
+
end
|
208
|
+
|
209
|
+
# Update a user's permission overrides in a channel
|
210
|
+
def update_user_overrides(token, channel_id, user_id, allow, deny)
|
211
|
+
RestClient.put(
|
212
|
+
"#{APIBASE}/channels/#{channel_id}/permissions/#{user_id}",
|
213
|
+
{ 'type' => 'member', 'id' => user_id, 'allow' => allow, 'deny' => deny }.to_json,
|
214
|
+
Authorization: token,
|
215
|
+
content_type: :json
|
216
|
+
)
|
217
|
+
end
|
218
|
+
|
219
|
+
# Update a role's permission overrides in a channel
|
220
|
+
def update_role_overrides(token, channel_id, role_id, allow, deny)
|
221
|
+
RestClient.put(
|
222
|
+
"#{APIBASE}/channels/#{channel_id}/permissions/#{role_id}",
|
223
|
+
{ 'type' => 'role', 'id' => role_id, 'allow' => allow, 'deny' => deny }.to_json,
|
224
|
+
Authorization: token,
|
225
|
+
content_type: :json
|
226
|
+
)
|
227
|
+
end
|
228
|
+
|
229
|
+
# Get the gateway to be used
|
230
|
+
def gateway(token)
|
231
|
+
RestClient.get(
|
232
|
+
"#{APIBASE}/gateway",
|
233
|
+
Authorization: token
|
234
|
+
)
|
235
|
+
end
|
236
|
+
|
237
|
+
# Start typing (needs to be resent every 5 seconds to keep up the typing)
|
238
|
+
def start_typing(token, channel_id)
|
239
|
+
RestClient.post(
|
240
|
+
"#{APIBASE}/channels/#{channel_id}/typing",
|
241
|
+
Authorization: token
|
242
|
+
)
|
243
|
+
end
|
244
|
+
|
245
|
+
# Get user data
|
246
|
+
def user(token, user_id)
|
247
|
+
RestClient.get(
|
248
|
+
"#{APIBASE}/users/#{user_id}",
|
249
|
+
Authorization: token
|
250
|
+
)
|
251
|
+
end
|
252
|
+
|
253
|
+
# Update user data
|
254
|
+
def update_user(token, email, password, new_username, avatar, new_password = nil)
|
255
|
+
RestClient.patch(
|
256
|
+
"#{APIBASE}/users/@me",
|
257
|
+
{ 'avatar' => avatar, 'email' => email, 'new_password' => new_password, 'password' => password, 'username' => new_username }.to_json,
|
258
|
+
Authorization: token,
|
259
|
+
content_type: :json
|
260
|
+
)
|
261
|
+
end
|
262
|
+
|
263
|
+
# Get a list of messages from a channel's history
|
264
|
+
def channel_log(token, channel_id, amount, before = nil, after = nil)
|
265
|
+
RestClient.get(
|
266
|
+
"#{APIBASE}/channels/#{channel_id}/messages?limit=#{amount}#{"&before=#{before}" if before}#{"&after=#{after}" if after}",
|
267
|
+
Authorization: token
|
268
|
+
)
|
269
|
+
end
|
270
|
+
|
271
|
+
end
|
data/lib/discordrb/bot.rb
CHANGED
@@ -17,12 +17,17 @@ require 'discordrb/events/guild-role-create'
|
|
17
17
|
require 'discordrb/events/guild-role-delete'
|
18
18
|
require 'discordrb/events/guild-role-update'
|
19
19
|
|
20
|
+
require 'discordrb/api'
|
21
|
+
|
20
22
|
require 'discordrb/exceptions'
|
21
23
|
require 'discordrb/data'
|
22
24
|
|
23
25
|
module Discordrb
|
24
26
|
class Bot
|
25
27
|
include Discordrb::Events
|
28
|
+
|
29
|
+
attr_reader :bot_user
|
30
|
+
|
26
31
|
def initialize(email, password, debug = false)
|
27
32
|
# Make sure people replace the login details in the example files...
|
28
33
|
if email.end_with? "example.com"
|
@@ -66,7 +71,7 @@ module Discordrb
|
|
66
71
|
debug("Obtaining data for channel with id #{id}")
|
67
72
|
return @channels[id] if @channels[id]
|
68
73
|
|
69
|
-
response =
|
74
|
+
response = API.channel(@token, id)
|
70
75
|
channel = Channel.new(JSON.parse(response), self)
|
71
76
|
@channels[id] = channel
|
72
77
|
end
|
@@ -79,11 +84,16 @@ module Discordrb
|
|
79
84
|
'recipient_id' => id
|
80
85
|
}
|
81
86
|
|
82
|
-
response =
|
87
|
+
response = API.create_private(@token, @bot_user.id, id)
|
83
88
|
channel = Channel.new(JSON.parse(response), self)
|
84
89
|
@private_channels[id] = channel
|
85
90
|
end
|
86
91
|
|
92
|
+
def join(invite)
|
93
|
+
invite = invite[invite.rindex('/')+1..-1] if invite.start_with?('http') || invite.start_with?('discord.gg')
|
94
|
+
API.join_server(@token, invite)
|
95
|
+
end
|
96
|
+
|
87
97
|
def user(id)
|
88
98
|
@users[id]
|
89
99
|
end
|
@@ -98,7 +108,7 @@ module Discordrb
|
|
98
108
|
'content' => content.to_s,
|
99
109
|
'mentions' => []
|
100
110
|
}
|
101
|
-
|
111
|
+
API.send_message(@token, channel_id, content)
|
102
112
|
end
|
103
113
|
|
104
114
|
def debug=(debug)
|
@@ -308,7 +318,7 @@ module Discordrb
|
|
308
318
|
login_attempts = login_attempts || 0
|
309
319
|
|
310
320
|
# Login
|
311
|
-
login_response =
|
321
|
+
login_response = API.login(@email, @password)
|
312
322
|
raise HTTPStatusException.new(login_response.code) if login_response.code >= 400
|
313
323
|
|
314
324
|
# Parse response
|
@@ -336,7 +346,7 @@ module Discordrb
|
|
336
346
|
|
337
347
|
def get_gateway
|
338
348
|
# Get updated websocket_hub
|
339
|
-
response =
|
349
|
+
response = API.gateway(@token)
|
340
350
|
JSON.parse(response)["url"]
|
341
351
|
end
|
342
352
|
|
@@ -489,8 +499,8 @@ module Discordrb
|
|
489
499
|
"v" => 2, # Another identifier
|
490
500
|
"token" => @token,
|
491
501
|
"properties" => { # I'm unsure what these values are for exactly, but they don't appear to impact bot functionality in any way.
|
492
|
-
"$os" => "
|
493
|
-
"$browser" => "",
|
502
|
+
"$os" => "#{RUBY_PLATFORM}",
|
503
|
+
"$browser" => "discordrb",
|
494
504
|
"$device" => "discordrb",
|
495
505
|
"$referrer" => "",
|
496
506
|
"$referring_domain" => ""
|
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'discordrb/bot'
|
2
2
|
require 'discordrb/data'
|
3
3
|
require 'discordrb/commands/parser'
|
4
|
+
require 'discordrb/commands/events'
|
4
5
|
|
5
6
|
# Specialized bot to run commands
|
6
7
|
|
@@ -92,6 +93,7 @@ module Discordrb::Commands
|
|
92
93
|
return
|
93
94
|
end
|
94
95
|
if permission?(user(event.user.id), command.attributes[:permission_level], event.server.id)
|
96
|
+
event.command = command
|
95
97
|
command.call(event, arguments, chained)
|
96
98
|
else
|
97
99
|
event.respond "You don't have permission to execute command `#{name}`!"
|
@@ -106,13 +108,13 @@ module Discordrb::Commands
|
|
106
108
|
|
107
109
|
def create_message(data)
|
108
110
|
message = Discordrb::Message.new(data, self)
|
109
|
-
event =
|
111
|
+
event = CommandEvent.new(message, self)
|
110
112
|
|
111
113
|
if message.content.start_with? @prefix
|
112
114
|
chain = message.content[@prefix.length..-1]
|
113
115
|
debug("Parsing command chain #{chain}")
|
114
116
|
result = (@attributes[:advanced_functionality]) ? CommandChain.new(chain, self).execute(event) : simple_execute(chain, event)
|
115
|
-
|
117
|
+
result = event.saved_message + (result || '')
|
116
118
|
event.respond result if result
|
117
119
|
end
|
118
120
|
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'discordrb/events/message'
|
2
|
+
|
3
|
+
module Discordrb::Commands
|
4
|
+
class CommandEvent < Discordrb::Events::MessageEvent
|
5
|
+
attr_reader :bot, :saved_message
|
6
|
+
attr_accessor :command
|
7
|
+
|
8
|
+
def initialize(message, bot)
|
9
|
+
super(message, bot)
|
10
|
+
@saved_message = ''
|
11
|
+
end
|
12
|
+
|
13
|
+
def <<(message)
|
14
|
+
@saved_message += "#{message}\n"
|
15
|
+
nil
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/discordrb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: discordrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.3.
|
4
|
+
version: 1.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- meew0
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faye-websocket
|
@@ -99,8 +99,10 @@ files:
|
|
99
99
|
- examples/ping.rb
|
100
100
|
- examples/pm.rb
|
101
101
|
- lib/discordrb.rb
|
102
|
+
- lib/discordrb/api.rb
|
102
103
|
- lib/discordrb/bot.rb
|
103
104
|
- lib/discordrb/commands/command-bot.rb
|
105
|
+
- lib/discordrb/commands/events.rb
|
104
106
|
- lib/discordrb/commands/parser.rb
|
105
107
|
- lib/discordrb/data.rb
|
106
108
|
- lib/discordrb/endpoints/endpoints.rb
|