Module: Discordrb::API::Channel

Defined in:
lib/discordrb/api/channel.rb

Overview

API calls for Channel

Class Method Summary collapse

Class Method Details

.add_group_user(token, group_channel_id, user_id) ⇒ Object

Add a user to a group channel.


344
345
346
347
348
349
350
351
352
353
354
# File 'lib/discordrb/api/channel.rb', line 344

def add_group_user(token, group_channel_id, user_id)
  Discordrb::API.request(
    :channels_cid_recipients_uid,
    nil,
    :put,
    "#{Discordrb::API.api_base}/channels/#{group_channel_id}/recipients/#{user_id}",
    {}.to_json,
    Authorization: token,
    content_type: :json
  )
end

.bulk_delete_messages(token, channel_id, messages = []) ⇒ Object


128
129
130
131
132
133
134
135
136
137
138
# File 'lib/discordrb/api/channel.rb', line 128

def bulk_delete_messages(token, channel_id, messages = [])
  Discordrb::API.request(
    :channels_cid_messages_bulk_delete,
    channel_id,
    :post,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/bulk-delete",
    { messages: messages }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.create_empty_group(token, bot_user_id) ⇒ Object

Create an empty group channel.


312
313
314
315
316
317
318
319
320
321
322
# File 'lib/discordrb/api/channel.rb', line 312

def create_empty_group(token, bot_user_id)
  Discordrb::API.request(
    :users_uid_channels,
    nil,
    :post,
    "#{Discordrb::API.api_base}/users/#{bot_user_id}/channels",
    {}.to_json,
    Authorization: token,
    content_type: :json
  )
end

.create_group(token, pm_channel_id, user_id) ⇒ Object

Create a group channel.


325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/discordrb/api/channel.rb', line 325

def create_group(token, pm_channel_id, user_id)
  Discordrb::API.request(
    :channels_cid_recipients_uid,
    nil,
    :put,
    "#{Discordrb::API.api_base}/channels/#{pm_channel_id}/recipients/#{user_id}",
    {}.to_json,
    Authorization: token,
    content_type: :json
  )
rescue RestClient::InternalServerError
  raise 'Attempted to add self as a new group channel recipient!'
rescue RestClient::NoContent
  raise 'Attempted to create a group channel with the PM channel recipient!'
rescue RestClient::Forbidden
  raise 'Attempted to add a user to group channel without permission!'
end

.create_invite(token, channel_id, max_age = 0, max_uses = 0, temporary = false, unique = false, reason = nil) ⇒ Object

Create an instant invite from a server or a channel id https://discordapp.com/developers/docs/resources/channel#create-channel-invite


235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/discordrb/api/channel.rb', line 235

def create_invite(token, channel_id, max_age = 0, max_uses = 0, temporary = false, unique = false, reason = nil)
  Discordrb::API.request(
    :channels_cid_invites,
    channel_id,
    :post,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/invites",
    { max_age: max_age, max_uses: max_uses, temporary: temporary, unique: unique }.to_json,
    Authorization: token,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.create_message(token, channel_id, message, tts = false, embed = nil, nonce = nil) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/discordrb/api/channel.rb', line 71

def create_message(token, channel_id, message, tts = false, embed = nil, nonce = nil) # send message
  Discordrb::API.request(
    :channels_cid_messages_mid,
    channel_id,
    :post,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages",
    { content: message, tts: tts, embed: embed, nonce: nonce }.to_json,
    Authorization: token,
    content_type: :json
  )
rescue RestClient::BadRequest => e
  parsed = JSON.parse(e.response.body)
  raise Discordrb::Errors::MessageTooLong, "Message over the character limit (#{message.length} > 2000)" if parsed['content'] && parsed['content'].is_a?(Array) && parsed['content'].first == 'Must be 2000 or fewer in length.'
  raise
end

.create_reaction(token, channel_id, message_id, emoji) ⇒ Object

Create a reaction on a message using this client https://discordapp.com/developers/docs/resources/channel#create-reaction


142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/discordrb/api/channel.rb', line 142

def create_reaction(token, channel_id, message_id, emoji)
  emoji = URI.encode(emoji) unless emoji.ascii_only?
  Discordrb::API.request(
    :channels_cid_messages_mid_reactions_emoji_me,
    channel_id,
    :put,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/@me",
    nil,
    Authorization: token,
    content_type: :json
  )
end

.create_webhook(token, channel_id, name, avatar = nil, reason = nil) ⇒ Object


382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/discordrb/api/channel.rb', line 382

def create_webhook(token, channel_id, name, avatar = nil, reason = nil)
  Discordrb::API.request(
    :channels_cid_webhooks,
    channel_id,
    :post,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/webhooks",
    { name: name, avatar: avatar }.to_json,
    Authorization: token,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.delete(token, channel_id, reason = nil) ⇒ Object


34
35
36
37
38
39
40
41
42
43
# File 'lib/discordrb/api/channel.rb', line 34

def delete(token, channel_id, reason = nil)
  Discordrb::API.request(
    :channels_cid,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}",
    Authorization: token,
    'X-Audit-Log-Reason': reason
  )
end

.delete_all_reactions(token, channel_id, message_id) ⇒ Object

Deletes all reactions on a message from all clients https://discordapp.com/developers/docs/resources/channel#delete-all-reactions


196
197
198
199
200
201
202
203
204
# File 'lib/discordrb/api/channel.rb', line 196

def delete_all_reactions(token, channel_id, message_id)
  Discordrb::API.request(
    :channels_cid_messages_mid_reactions,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions",
    Authorization: token
  )
end

.delete_message(token, channel_id, message_id) ⇒ Object


116
117
118
119
120
121
122
123
124
# File 'lib/discordrb/api/channel.rb', line 116

def delete_message(token, channel_id, message_id)
  Discordrb::API.request(
    :channels_cid_messages_mid,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}",
    Authorization: token
  )
end

.delete_own_reaction(token, channel_id, message_id, emoji) ⇒ Object


157
158
159
160
161
162
163
164
165
166
# File 'lib/discordrb/api/channel.rb', line 157

def delete_own_reaction(token, channel_id, message_id, emoji)
  emoji = URI.encode(emoji) unless emoji.ascii_only?
  Discordrb::API.request(
    :channels_cid_messages_mid_reactions_emoji_me,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/@me",
    Authorization: token
  )
end

.delete_permission(token, channel_id, overwrite_id, reason = nil) ⇒ Object


250
251
252
253
254
255
256
257
258
259
# File 'lib/discordrb/api/channel.rb', line 250

def delete_permission(token, channel_id, overwrite_id, reason = nil)
  Discordrb::API.request(
    :channels_cid_permissions_oid,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/permissions/#{overwrite_id}",
    Authorization: token,
    'X-Audit-Log-Reason': reason
  )
end

.delete_user_reaction(token, channel_id, message_id, emoji, user_id) ⇒ Object


170
171
172
173
174
175
176
177
178
179
# File 'lib/discordrb/api/channel.rb', line 170

def delete_user_reaction(token, channel_id, message_id, emoji, user_id)
  emoji = URI.encode(emoji) unless emoji.ascii_only?
  Discordrb::API.request(
    :channels_cid_messages_mid_reactions_emoji_uid,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}/#{user_id}",
    Authorization: token
  )
end

.edit_message(token, channel_id, message_id, message, mentions = [], embed = nil) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
# File 'lib/discordrb/api/channel.rb', line 102

def edit_message(token, channel_id, message_id, message, mentions = [], embed = nil)
  Discordrb::API.request(
    :channels_cid_messages_mid,
    channel_id,
    :patch,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}",
    { content: message, mentions: mentions, embed: embed }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.get_reactions(token, channel_id, message_id, emoji) ⇒ Object

Get a list of clients who reacted with a specific reaction on a message https://discordapp.com/developers/docs/resources/channel#get-reactions


183
184
185
186
187
188
189
190
191
192
# File 'lib/discordrb/api/channel.rb', line 183

def get_reactions(token, channel_id, message_id, emoji)
  emoji = URI.encode(emoji) unless emoji.ascii_only?
  Discordrb::API.request(
    :channels_cid_messages_mid_reactions_emoji,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}/reactions/#{emoji}",
    Authorization: token
  )
end

.invites(token, channel_id) ⇒ Object


223
224
225
226
227
228
229
230
231
# File 'lib/discordrb/api/channel.rb', line 223

def invites(token, channel_id)
  Discordrb::API.request(
    :channels_cid_invites,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/invites",
    Authorization: token
  )
end

.leave_group(token, group_channel_id) ⇒ Object

Leave a group channel.


369
370
371
372
373
374
375
376
377
378
# File 'lib/discordrb/api/channel.rb', line 369

def leave_group(token, group_channel_id)
  Discordrb::API.request(
    :channels_cid,
    nil,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{group_channel_id}",
    Authorization: token,
    content_type: :json
  )
end

.message(token, channel_id, message_id) ⇒ Object

Get a single message from a channel's history by id https://discordapp.com/developers/docs/resources/channel#get-channel-message


59
60
61
62
63
64
65
66
67
# File 'lib/discordrb/api/channel.rb', line 59

def message(token, channel_id, message_id)
  Discordrb::API.request(
    :channels_cid_messages_mid,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages/#{message_id}",
    Authorization: token
  )
end

.messages(token, channel_id, amount, before = nil, after = nil, around = nil) ⇒ Object


47
48
49
50
51
52
53
54
55
# File 'lib/discordrb/api/channel.rb', line 47

def messages(token, channel_id, amount, before = nil, after = nil, around = nil)
  Discordrb::API.request(
    :channels_cid_messages,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages?limit=#{amount}#{"&before=#{before}" if before}#{"&after=#{after}" if after}#{"&around=#{around}" if around}",
    Authorization: token
  )
end

.pin_message(token, channel_id, message_id) ⇒ Object


288
289
290
291
292
293
294
295
296
297
# File 'lib/discordrb/api/channel.rb', line 288

def pin_message(token, channel_id, message_id)
  Discordrb::API.request(
    :channels_cid_pins_mid,
    channel_id,
    :put,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/pins/#{message_id}",
    nil,
    Authorization: token
  )
end

.pinned_messages(token, channel_id) ⇒ Object


276
277
278
279
280
281
282
283
284
# File 'lib/discordrb/api/channel.rb', line 276

def pinned_messages(token, channel_id)
  Discordrb::API.request(
    :channels_cid_pins,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/pins",
    Authorization: token
  )
end

.remove_group_user(token, group_channel_id, user_id) ⇒ Object

Remove a user from a group channel.


357
358
359
360
361
362
363
364
365
366
# File 'lib/discordrb/api/channel.rb', line 357

def remove_group_user(token, group_channel_id, user_id)
  Discordrb::API.request(
    :channels_cid_recipients_uid,
    nil,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{group_channel_id}/recipients/#{user_id}",
    Authorization: token,
    content_type: :json
  )
end

.resolve(token, channel_id) ⇒ Object


7
8
9
10
11
12
13
14
15
# File 'lib/discordrb/api/channel.rb', line 7

def resolve(token, channel_id)
  Discordrb::API.request(
    :channels_cid,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}",
    Authorization: token
  )
end

.start_typing(token, channel_id) ⇒ Object

Start typing (needs to be resent every 5 seconds to keep up the typing) https://discordapp.com/developers/docs/resources/channel#trigger-typing-indicator


263
264
265
266
267
268
269
270
271
272
# File 'lib/discordrb/api/channel.rb', line 263

def start_typing(token, channel_id)
  Discordrb::API.request(
    :channels_cid_typing,
    channel_id,
    :post,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/typing",
    nil,
    Authorization: token
  )
end

.unpin_message(token, channel_id, message_id) ⇒ Object


301
302
303
304
305
306
307
308
309
# File 'lib/discordrb/api/channel.rb', line 301

def unpin_message(token, channel_id, message_id)
  Discordrb::API.request(
    :channels_cid_pins_mid,
    channel_id,
    :delete,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/pins/#{message_id}",
    Authorization: token
  )
end

.update(token, channel_id, name, topic, position, bitrate, user_limit, nsfw, reason = nil) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/discordrb/api/channel.rb', line 19

def update(token, channel_id, name, topic, position, bitrate, user_limit, nsfw, reason = nil)
  Discordrb::API.request(
    :channels_cid,
    channel_id,
    :patch,
    "#{Discordrb::API.api_base}/channels/#{channel_id}",
    { name: name, position: position, topic: topic, bitrate: bitrate, user_limit: user_limit, nsfw: nsfw }.to_json,
    Authorization: token,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.update_permission(token, channel_id, overwrite_id, allow, deny, type, reason = nil) ⇒ Object


208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/discordrb/api/channel.rb', line 208

def update_permission(token, channel_id, overwrite_id, allow, deny, type, reason = nil)
  Discordrb::API.request(
    :channels_cid_permissions_oid,
    channel_id,
    :put,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/permissions/#{overwrite_id}",
    { type: type, id: overwrite_id, allow: allow, deny: deny }.to_json,
    Authorization: token,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.upload_file(token, channel_id, file, caption: nil, tts: false) ⇒ Object


89
90
91
92
93
94
95
96
97
98
# File 'lib/discordrb/api/channel.rb', line 89

def upload_file(token, channel_id, file, caption: nil, tts: false)
  Discordrb::API.request(
    :channels_cid_messages_mid,
    channel_id,
    :post,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/messages",
    { file: file, content: caption, tts: tts },
    Authorization: token
  )
end

.webhooks(token, channel_id) ⇒ Object


397
398
399
400
401
402
403
404
405
# File 'lib/discordrb/api/channel.rb', line 397

def webhooks(token, channel_id)
  Discordrb::API.request(
    :channels_cid_webhooks,
    channel_id,
    :get,
    "#{Discordrb::API.api_base}/channels/#{channel_id}/webhooks",
    Authorization: token
  )
end