mij-discord 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.travis.yml +5 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE +21 -0
  6. data/README.md +35 -0
  7. data/Rakefile +10 -0
  8. data/bin/console +7 -0
  9. data/bin/setup +6 -0
  10. data/lib/mij-discord.rb +56 -0
  11. data/lib/mij-discord/bot.rb +579 -0
  12. data/lib/mij-discord/cache.rb +298 -0
  13. data/lib/mij-discord/core/api.rb +228 -0
  14. data/lib/mij-discord/core/api/channel.rb +416 -0
  15. data/lib/mij-discord/core/api/invite.rb +43 -0
  16. data/lib/mij-discord/core/api/server.rb +465 -0
  17. data/lib/mij-discord/core/api/user.rb +144 -0
  18. data/lib/mij-discord/core/errors.rb +106 -0
  19. data/lib/mij-discord/core/gateway.rb +505 -0
  20. data/lib/mij-discord/data.rb +65 -0
  21. data/lib/mij-discord/data/application.rb +38 -0
  22. data/lib/mij-discord/data/channel.rb +404 -0
  23. data/lib/mij-discord/data/embed.rb +115 -0
  24. data/lib/mij-discord/data/emoji.rb +62 -0
  25. data/lib/mij-discord/data/invite.rb +87 -0
  26. data/lib/mij-discord/data/member.rb +174 -0
  27. data/lib/mij-discord/data/message.rb +206 -0
  28. data/lib/mij-discord/data/permissions.rb +121 -0
  29. data/lib/mij-discord/data/role.rb +99 -0
  30. data/lib/mij-discord/data/server.rb +359 -0
  31. data/lib/mij-discord/data/user.rb +173 -0
  32. data/lib/mij-discord/data/voice.rb +68 -0
  33. data/lib/mij-discord/events.rb +133 -0
  34. data/lib/mij-discord/events/basic.rb +80 -0
  35. data/lib/mij-discord/events/channel.rb +50 -0
  36. data/lib/mij-discord/events/member.rb +66 -0
  37. data/lib/mij-discord/events/message.rb +150 -0
  38. data/lib/mij-discord/events/server.rb +102 -0
  39. data/lib/mij-discord/logger.rb +20 -0
  40. data/lib/mij-discord/version.rb +5 -0
  41. data/mij-discord.gemspec +31 -0
  42. metadata +154 -0
@@ -0,0 +1,416 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MijDiscord::Core::API::Channel
4
+ class << self
5
+ # Get a channel's data
6
+ # https://discordapp.com/developers/docs/resources/channel#get-channel
7
+ def resolve(token, channel_id)
8
+ MijDiscord::Core::API.request(
9
+ :channels_cid,
10
+ channel_id,
11
+ :get,
12
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}",
13
+ Authorization: token
14
+ )
15
+ end
16
+
17
+ # Update a channel's data
18
+ # https://discordapp.com/developers/docs/resources/channel#modify-channel
19
+ def update(token, channel_id, name, topic, nsfw, parent_id, position, bitrate, user_limit, reason = nil)
20
+ MijDiscord::Core::API.request(
21
+ :channels_cid,
22
+ channel_id,
23
+ :patch,
24
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}",
25
+ {
26
+ name: name, topic: topic, nsfw: nsfw,
27
+ parent_id: parent_id, position: position,
28
+ bitrate: bitrate, user_limit: user_limit
29
+ }.delete_if {|_, v| v.nil? }.to_json,
30
+ Authorization: token,
31
+ content_type: :json,
32
+ 'X-Audit-Log-Reason': reason
33
+ )
34
+ end
35
+
36
+ # Delete a channel
37
+ # https://discordapp.com/developers/docs/resources/channel#deleteclose-channel
38
+ def delete(token, channel_id, reason = nil)
39
+ MijDiscord::Core::API.request(
40
+ :channels_cid,
41
+ channel_id,
42
+ :delete,
43
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}",
44
+ Authorization: token,
45
+ 'X-Audit-Log-Reason': reason
46
+ )
47
+ end
48
+
49
+ # Get a list of messages from a channel's history
50
+ # https://discordapp.com/developers/docs/resources/channel#get-channel-messages
51
+ def messages(token, channel_id, amount, before = nil, after = nil, around = nil)
52
+ MijDiscord::Core::API.request(
53
+ :channels_cid_messages,
54
+ channel_id,
55
+ :get,
56
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages?limit=#{amount}#{"&before=#{before}" if before}#{"&after=#{after}" if after}#{"&around=#{around}" if around}",
57
+ Authorization: token
58
+ )
59
+ end
60
+
61
+ # Get a single message from a channel's history by id
62
+ # https://discordapp.com/developers/docs/resources/channel#get-channel-message
63
+ def message(token, channel_id, message_id)
64
+ MijDiscord::Core::API.request(
65
+ :channels_cid_messages_mid,
66
+ channel_id,
67
+ :get,
68
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}",
69
+ Authorization: token
70
+ )
71
+ end
72
+
73
+ # Send a message to a channel
74
+ # https://discordapp.com/developers/docs/resources/channel#create-message
75
+ def create_message(token, channel_id, message, tts = false, embed = nil, mentions = [])
76
+ MijDiscord::Core::API.request(
77
+ :channels_cid_messages_mid,
78
+ channel_id,
79
+ :post,
80
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages",
81
+ { content: message, mentions: mentions, tts: tts, embed: embed }.to_json,
82
+ Authorization: token,
83
+ content_type: :json
84
+ )
85
+ rescue RestClient::BadRequest => e
86
+ parsed = JSON.parse(e.response.body)
87
+ if (content = parsed['content']).is_a?(Array) && content.first == 'Must be 2000 or fewer characters long.'
88
+ raise MijDiscord::Core::Errors::MessageTooLong, "Message over the character limit (#{message.length} > 2000)"
89
+ end
90
+ raise
91
+ end
92
+
93
+ # Send a file as a message to a channel
94
+ # https://discordapp.com/developers/docs/resources/channel#upload-file
95
+ def upload_file(token, channel_id, file, caption = nil, tts = false)
96
+ MijDiscord::Core::API.request(
97
+ :channels_cid_messages_mid,
98
+ channel_id,
99
+ :post,
100
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages",
101
+ { file: file, content: caption, tts: tts },
102
+ Authorization: token
103
+ )
104
+ end
105
+
106
+ # Edit a message
107
+ # https://discordapp.com/developers/docs/resources/channel#edit-message
108
+ def edit_message(token, channel_id, message_id, message, mentions = [], embed = nil)
109
+ MijDiscord::Core::API.request(
110
+ :channels_cid_messages_mid,
111
+ channel_id,
112
+ :patch,
113
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}",
114
+ { content: message, mentions: mentions, embed: embed }.to_json,
115
+ Authorization: token,
116
+ content_type: :json
117
+ )
118
+ end
119
+
120
+ # Delete a message
121
+ # https://discordapp.com/developers/docs/resources/channel#delete-message
122
+ def delete_message(token, channel_id, message_id)
123
+ MijDiscord::Core::API.request(
124
+ :channels_cid_messages_mid,
125
+ channel_id,
126
+ :delete,
127
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}",
128
+ Authorization: token
129
+ )
130
+ end
131
+
132
+ # Delete messages in bulk
133
+ # https://discordapp.com/developers/docs/resources/channel#bulk-delete-messages
134
+ def bulk_delete_messages(token, channel_id, messages = [])
135
+ MijDiscord::Core::API.request(
136
+ :channels_cid_messages_bulk_delete,
137
+ channel_id,
138
+ :post,
139
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/bulk-delete",
140
+ { messages: messages }.to_json,
141
+ Authorization: token,
142
+ content_type: :json
143
+ )
144
+ end
145
+
146
+ # Create a reaction on a message using this client
147
+ # https://discordapp.com/developers/docs/resources/channel#create-reaction
148
+ def create_reaction(token, channel_id, message_id, emoji)
149
+ emoji = URI.encode(emoji) unless emoji.ascii_only?
150
+ MijDiscord::Core::API.request(
151
+ :channels_cid_messages_mid_reactions_emoji_me,
152
+ channel_id,
153
+ :put,
154
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/@me",
155
+ nil,
156
+ Authorization: token,
157
+ content_type: :json,
158
+ header_bypass_delay: 0.25
159
+ )
160
+ end
161
+
162
+ # Delete this client's own reaction on a message
163
+ # https://discordapp.com/developers/docs/resources/channel#delete-own-reaction
164
+ def delete_own_reaction(token, channel_id, message_id, emoji)
165
+ emoji = URI.encode(emoji) unless emoji.ascii_only?
166
+ MijDiscord::Core::API.request(
167
+ :channels_cid_messages_mid_reactions_emoji_me,
168
+ channel_id,
169
+ :delete,
170
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/@me",
171
+ Authorization: token,
172
+ header_bypass_delay: 0.25
173
+ )
174
+ end
175
+
176
+ # Delete another client's reaction on a message
177
+ # https://discordapp.com/developers/docs/resources/channel#delete-user-reaction
178
+ def delete_user_reaction(token, channel_id, message_id, emoji, user_id)
179
+ emoji = URI.encode(emoji) unless emoji.ascii_only?
180
+ MijDiscord::Core::API.request(
181
+ :channels_cid_messages_mid_reactions_emoji_uid,
182
+ channel_id,
183
+ :delete,
184
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/#{user_id}",
185
+ Authorization: token,
186
+ header_bypass_delay: 0.25
187
+ )
188
+ end
189
+
190
+ # Get a list of clients who reacted with a specific reaction on a message
191
+ # https://discordapp.com/developers/docs/resources/channel#get-reactions
192
+ def get_reactions(token, channel_id, message_id, emoji)
193
+ emoji = URI.encode(emoji) unless emoji.ascii_only?
194
+ MijDiscord::Core::API.request(
195
+ :channels_cid_messages_mid_reactions_emoji,
196
+ channel_id,
197
+ :get,
198
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}",
199
+ Authorization: token
200
+ )
201
+ end
202
+
203
+ # Deletes all reactions on a message from all clients
204
+ # https://discordapp.com/developers/docs/resources/channel#delete-all-reactions
205
+ def delete_all_reactions(token, channel_id, message_id)
206
+ MijDiscord::Core::API.request(
207
+ :channels_cid_messages_mid_reactions,
208
+ channel_id,
209
+ :delete,
210
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/messages/#{message_id}/reactions",
211
+ Authorization: token
212
+ )
213
+ end
214
+
215
+ # Update a channels permission for a role or member
216
+ # https://discordapp.com/developers/docs/resources/channel#edit-channel-permissions
217
+ def update_permission(token, channel_id, overwrite_id, allow, deny, type, reason = nil)
218
+ MijDiscord::Core::API.request(
219
+ :channels_cid_permissions_oid,
220
+ channel_id,
221
+ :put,
222
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/permissions/#{overwrite_id}",
223
+ { type: type, id: overwrite_id, allow: allow, deny: deny }.to_json,
224
+ Authorization: token,
225
+ content_type: :json,
226
+ 'X-Audit-Log-Reason': reason
227
+ )
228
+ end
229
+
230
+ # Get a channel's invite list
231
+ # https://discordapp.com/developers/docs/resources/channel#get-channel-invites
232
+ def invites(token, channel_id)
233
+ MijDiscord::Core::API.request(
234
+ :channels_cid_invites,
235
+ channel_id,
236
+ :get,
237
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/invites",
238
+ Authorization: token
239
+ )
240
+ end
241
+
242
+ # Create an instant invite from a server or a channel id
243
+ # https://discordapp.com/developers/docs/resources/channel#create-channel-invite
244
+ def create_invite(token, channel_id, max_age = 0, max_uses = 0, temporary = false, unique = false, reason = nil)
245
+ MijDiscord::Core::API.request(
246
+ :channels_cid_invites,
247
+ channel_id,
248
+ :post,
249
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/invites",
250
+ { max_age: max_age, max_uses: max_uses, temporary: temporary, unique: unique }.to_json,
251
+ Authorization: token,
252
+ content_type: :json,
253
+ 'X-Audit-Log-Reason': reason
254
+ )
255
+ end
256
+
257
+ # Delete channel permission
258
+ # https://discordapp.com/developers/docs/resources/channel#delete-channel-permission
259
+ def delete_permission(token, channel_id, overwrite_id, reason = nil)
260
+ MijDiscord::Core::API.request(
261
+ :channels_cid_permissions_oid,
262
+ channel_id,
263
+ :delete,
264
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/permissions/#{overwrite_id}",
265
+ Authorization: token,
266
+ 'X-Audit-Log-Reason': reason
267
+ )
268
+ end
269
+
270
+ # Start typing (needs to be resent every 5 seconds to keep up the typing)
271
+ # https://discordapp.com/developers/docs/resources/channel#trigger-typing-indicator
272
+ def start_typing(token, channel_id)
273
+ MijDiscord::Core::API.request(
274
+ :channels_cid_typing,
275
+ channel_id,
276
+ :post,
277
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/typing",
278
+ nil,
279
+ Authorization: token
280
+ )
281
+ end
282
+
283
+ # Get a list of pinned messages in a channel
284
+ # https://discordapp.com/developers/docs/resources/channel#get-pinned-messages
285
+ def pinned_messages(token, channel_id)
286
+ MijDiscord::Core::API.request(
287
+ :channels_cid_pins,
288
+ channel_id,
289
+ :get,
290
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/pins",
291
+ Authorization: token
292
+ )
293
+ end
294
+
295
+ # Pin a message
296
+ # https://discordapp.com/developers/docs/resources/channel#add-pinned-channel-message
297
+ def pin_message(token, channel_id, message_id)
298
+ MijDiscord::Core::API.request(
299
+ :channels_cid_pins_mid,
300
+ channel_id,
301
+ :put,
302
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/pins/#{message_id}",
303
+ nil,
304
+ Authorization: token
305
+ )
306
+ end
307
+
308
+ # Unpin a message
309
+ # https://discordapp.com/developers/docs/resources/channel#delete-pinned-channel-message
310
+ def unpin_message(token, channel_id, message_id)
311
+ MijDiscord::Core::API.request(
312
+ :channels_cid_pins_mid,
313
+ channel_id,
314
+ :delete,
315
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/pins/#{message_id}",
316
+ Authorization: token
317
+ )
318
+ end
319
+
320
+ # Create an empty group channel.
321
+ def create_empty_group(token, bot_user_id)
322
+ MijDiscord::Core::API.request(
323
+ :users_uid_channels,
324
+ nil,
325
+ :post,
326
+ "#{MijDiscord::Core::API::APIBASE_URL}/users/#{bot_user_id}/channels",
327
+ {}.to_json,
328
+ Authorization: token,
329
+ content_type: :json
330
+ )
331
+ end
332
+
333
+ # Create a group channel.
334
+ def create_group(token, pm_channel_id, user_id)
335
+ MijDiscord::Core::API.request(
336
+ :channels_cid_recipients_uid,
337
+ nil,
338
+ :put,
339
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{pm_channel_id}/recipients/#{user_id}",
340
+ {}.to_json,
341
+ Authorization: token,
342
+ content_type: :json
343
+ )
344
+ rescue RestClient::InternalServerError
345
+ raise 'Attempted to add self as a new group channel recipient!'
346
+ rescue RestClient::NoContent
347
+ raise 'Attempted to create a group channel with the PM channel recipient!'
348
+ rescue RestClient::Forbidden
349
+ raise 'Attempted to add a user to group channel without permission!'
350
+ end
351
+
352
+ # Add a user to a group channel.
353
+ def add_group_user(token, group_channel_id, user_id)
354
+ MijDiscord::Core::API.request(
355
+ :channels_cid_recipients_uid,
356
+ nil,
357
+ :put,
358
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{group_channel_id}/recipients/#{user_id}",
359
+ {}.to_json,
360
+ Authorization: token,
361
+ content_type: :json
362
+ )
363
+ end
364
+
365
+ # Remove a user from a group channel.
366
+ def remove_group_user(token, group_channel_id, user_id)
367
+ MijDiscord::Core::API.request(
368
+ :channels_cid_recipients_uid,
369
+ nil,
370
+ :delete,
371
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{group_channel_id}/recipients/#{user_id}",
372
+ Authorization: token,
373
+ content_type: :json
374
+ )
375
+ end
376
+
377
+ # Leave a group channel.
378
+ def leave_group(token, group_channel_id)
379
+ MijDiscord::Core::API.request(
380
+ :channels_cid,
381
+ nil,
382
+ :delete,
383
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{group_channel_id}",
384
+ Authorization: token,
385
+ content_type: :json
386
+ )
387
+ end
388
+
389
+ # Create a webhook
390
+ # https://discordapp.com/developers/docs/resources/webhook#create-webhook
391
+ def create_webhook(token, channel_id, name, avatar = nil, reason = nil)
392
+ MijDiscord::Core::API.request(
393
+ :channels_cid_webhooks,
394
+ channel_id,
395
+ :post,
396
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/webhooks",
397
+ { name: name, avatar: avatar }.to_json,
398
+ Authorization: token,
399
+ content_type: :json,
400
+ 'X-Audit-Log-Reason': reason
401
+ )
402
+ end
403
+
404
+ # Get channel webhooks
405
+ # https://discordapp.com/developers/docs/resources/webhook#get-channel-webhooks
406
+ def webhooks(token, channel_id)
407
+ MijDiscord::Core::API.request(
408
+ :channels_cid_webhooks,
409
+ channel_id,
410
+ :get,
411
+ "#{MijDiscord::Core::API::APIBASE_URL}/channels/#{channel_id}/webhooks",
412
+ Authorization: token
413
+ )
414
+ end
415
+ end
416
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MijDiscord::Core::API::Invite
4
+ class << self
5
+ # Resolve an invite
6
+ # https://discordapp.com/developers/docs/resources/invite#get-invite
7
+ def resolve(token, invite_code)
8
+ MijDiscord::Core::API.request(
9
+ :invite_code,
10
+ nil,
11
+ :get,
12
+ "#{MijDiscord::Core::API::APIBASE_URL}/invite/#{invite_code}",
13
+ Authorization: token
14
+ )
15
+ end
16
+
17
+ # Delete an invite by code
18
+ # https://discordapp.com/developers/docs/resources/invite#delete-invite
19
+ def delete(token, code, reason = nil)
20
+ MijDiscord::Core::API.request(
21
+ :invites_code,
22
+ nil,
23
+ :delete,
24
+ "#{MijDiscord::Core::API::APIBASE_URL}/invites/#{code}",
25
+ Authorization: token,
26
+ 'X-Audit-Log-Reason': reason
27
+ )
28
+ end
29
+
30
+ # Join a server using an invite
31
+ # https://discordapp.com/developers/docs/resources/invite#accept-invite
32
+ def accept(token, invite_code)
33
+ MijDiscord::Core::API.request(
34
+ :invite_code,
35
+ nil,
36
+ :post,
37
+ "#{MijDiscord::Core::API::APIBASE_URL}/invite/#{invite_code}",
38
+ nil,
39
+ Authorization: token
40
+ )
41
+ end
42
+ end
43
+ end