Module: Discordrb::API

Defined in:
lib/discordrb/api.rb

Overview

List of methods representing endpoints in Discord's API

Constant Summary collapse

APIBASE =

The base URL of the Discord REST API.

'https://discordapp.com/api'

Class Method Summary collapse

Class Method Details

.acknowledge_message(token, channel_id, message_id) ⇒ Object

Acknowledge that a message has been received The last acknowledged message will be sent in the ready packet, so this is an easy way to catch up on messages



209
210
211
212
213
214
215
# File 'lib/discordrb/api.rb', line 209

def acknowledge_message(token, channel_id, message_id)
  RestClient.post(
    "#{APIBASE}/channels/#{channel_id}/messages/#{message_id}/ack",
    nil,
    Authorization: token
  )
end

.ban_user(token, server_id, user_id, message_days) ⇒ Object

Ban a user from a server and delete their messages from the last message_days days



12
13
14
15
16
17
# File 'lib/discordrb/api.rb', line 12

def ban_user(token, server_id, user_id, message_days)
  RestClient.put(
    "#{APIBASE}/guilds/#{server_id}/bans/#{user_id}?delete-message-days=#{message_days}",
    Authorization: token
  )
end

.bans(token, server_id) ⇒ Object

Get a server's banned users



36
37
38
39
40
41
# File 'lib/discordrb/api.rb', line 36

def bans(token, server_id)
  RestClient.get(
    "#{APIBASE}/guilds/#{server_id}/bans",
    Authorization: token
  )
end

.channel(token, channel_id) ⇒ Object

Get a channel's data



98
99
100
101
102
103
# File 'lib/discordrb/api.rb', line 98

def channel(token, channel_id)
  RestClient.get(
    "#{APIBASE}/channels/#{channel_id}",
    Authorization: token
  )
end

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

Get a list of messages from a channel's history



322
323
324
325
326
327
# File 'lib/discordrb/api.rb', line 322

def channel_log(token, channel_id, amount, before = nil, after = nil)
  RestClient.get(
    "#{APIBASE}/channels/#{channel_id}/messages?limit=#{amount}#{"&before=#{before}" if before}#{"&after=#{after}" if after}",
    Authorization: token
  )
end

.create_channel(token, server_id, name, type) ⇒ Object

Create a channel



106
107
108
109
110
111
112
113
# File 'lib/discordrb/api.rb', line 106

def create_channel(token, server_id, name, type)
  RestClient.post(
    "#{APIBASE}/guilds/#{server_id}/channels",
    { 'name' => name, 'type' => type }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.create_invite(token, channel_id, max_age = 0, max_uses = 0, temporary = false, xkcd = false) ⇒ Object

Create an instant invite from a server or a channel id



161
162
163
164
165
166
167
168
# File 'lib/discordrb/api.rb', line 161

def create_invite(token, channel_id, max_age = 0, max_uses = 0, temporary = false, xkcd = false)
  RestClient.post(
    "#{APIBASE}/channels/#{channel_id}/invites",
    { 'max_age' => max_age, 'max_uses' => max_uses, 'temporary' => temporary, 'xkcdpass' => xkcd }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.create_private(token, bot_user_id, user_id) ⇒ Object

Create a private channel



151
152
153
154
155
156
157
158
# File 'lib/discordrb/api.rb', line 151

def create_private(token, bot_user_id, user_id)
  RestClient.post(
    "#{APIBASE}/users/#{bot_user_id}/channels",
    { 'recipient_id' => user_id }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.create_role(token, server_id) ⇒ Object

Create a role (parameters such as name and colour will have to be set by update_role afterwards)



227
228
229
230
231
232
233
# File 'lib/discordrb/api.rb', line 227

def create_role(token, server_id)
  RestClient.post(
    "#{APIBASE}/guilds/#{server_id}/roles",
    nil,
    Authorization: token
  )
end

.create_server(token, name, region = :london) ⇒ Object

Create a server



62
63
64
65
66
67
68
69
# File 'lib/discordrb/api.rb', line 62

def create_server(token, name, region = :london)
  RestClient.post(
    "#{APIBASE}/guilds",
    { 'name' => name, 'region' => region.to_s }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.delete_channel(token, channel_id) ⇒ Object

Delete a channel



126
127
128
129
130
131
# File 'lib/discordrb/api.rb', line 126

def delete_channel(token, channel_id)
  RestClient.delete(
    "#{APIBASE}/channels/#{channel_id}",
    Authorization: token
  )
end

.delete_invite(token, code) ⇒ Object

Delete an invite by code



171
172
173
174
175
176
# File 'lib/discordrb/api.rb', line 171

def delete_invite(token, code)
  RestClient.delete(
    "#{APIBASE}/invites/#{code}",
    Authorization: token
  )
end

.delete_message(token, channel_id, message_id) ⇒ Object

Delete a message



189
190
191
192
193
194
# File 'lib/discordrb/api.rb', line 189

def delete_message(token, channel_id, message_id)
  RestClient.delete(
    "#{APIBASE}/channels/#{channel_id}/messages/#{message_id}",
    Authorization: token
  )
end

.delete_role(token, server_id, role_id) ⇒ Object

Delete a role



249
250
251
252
253
254
# File 'lib/discordrb/api.rb', line 249

def delete_role(token, server_id, role_id)
  RestClient.delete(
    "#{APIBASE}/guilds/#{server_id}/roles/#{role_id}",
    Authorization: token
  )
end

.delete_server(token, server_id) ⇒ Object

Delete a server



82
83
84
85
86
87
# File 'lib/discordrb/api.rb', line 82

def delete_server(token, server_id)
  RestClient.delete(
    "#{APIBASE}/guilds/#{server_id}",
    Authorization: token
  )
end

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

Edit a message



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

def edit_message(token, channel_id, message_id, message, mentions = [])
  RestClient.patch(
    "#{APIBASE}/channels/#{channel_id}/messages/#{message_id}",
    { 'content' => message, 'mentions' => mentions }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.gateway(token) ⇒ Object

Get the gateway to be used



287
288
289
290
291
292
# File 'lib/discordrb/api.rb', line 287

def gateway(token)
  RestClient.get(
    "#{APIBASE}/gateway",
    Authorization: token
  )
end

.join_server(token, invite_code) ⇒ Object

Join a server using an invite



134
135
136
137
138
139
140
# File 'lib/discordrb/api.rb', line 134

def join_server(token, invite_code)
  RestClient.post(
    "#{APIBASE}/invite/#{invite_code}",
    nil,
    Authorization: token
  )
end

.kick_user(token, server_id, user_id) ⇒ Object

Kick a user from a server



28
29
30
31
32
33
# File 'lib/discordrb/api.rb', line 28

def kick_user(token, server_id, user_id)
  RestClient.delete(
    "#{APIBASE}/guilds/#{server_id}/members/#{user_id}",
    Authorization: token
  )
end

.leave_server(server_id) ⇒ Object

Leave a server



90
91
92
93
94
95
# File 'lib/discordrb/api.rb', line 90

def leave_server(server_id)
  RestClient.delete(
    "#{APIBASE}/guilds/#{server_id}",
    Authorization: token
  )
end

.login(email, password) ⇒ Object

Login to the server



44
45
46
47
48
49
50
# File 'lib/discordrb/api.rb', line 44

def (email, password)
  RestClient.post(
    "#{APIBASE}/auth/login",
    email: email,
    password: password
  )
end

.logout(token) ⇒ Object

Logout from the server



53
54
55
56
57
58
59
# File 'lib/discordrb/api.rb', line 53

def logout(token)
  RestClient.post(
    "#{APIBASE}/auth/logout",
    nil,
    Authorization: token
  )
end

.resolve_invite(token, invite_code) ⇒ Object

Resolve an invite



143
144
145
146
147
148
# File 'lib/discordrb/api.rb', line 143

def resolve_invite(token, invite_code)
  RestClient.get(
    "#{APIBASE}/invite/#{invite_code}",
    Authorization: token
  )
end

.send_file(token, channel_id, file) ⇒ Object

Send a file as a message to a channel



218
219
220
221
222
223
224
# File 'lib/discordrb/api.rb', line 218

def send_file(token, channel_id, file)
  RestClient.post(
    "#{APIBASE}/channels/#{channel_id}/messages",
    { file: file },
    Authorization: token
  )
end

.send_message(token, channel_id, message, mentions = [], tts = false) ⇒ Object

Send a message to a channel



179
180
181
182
183
184
185
186
# File 'lib/discordrb/api.rb', line 179

def send_message(token, channel_id, message, mentions = [], tts = false)
  RestClient.post(
    "#{APIBASE}/channels/#{channel_id}/messages",
    { 'content' => message, 'mentions' => mentions, tts => tts }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.start_typing(token, channel_id) ⇒ Object

Start typing (needs to be resent every 5 seconds to keep up the typing)



295
296
297
298
299
300
301
# File 'lib/discordrb/api.rb', line 295

def start_typing(token, channel_id)
  RestClient.post(
    "#{APIBASE}/channels/#{channel_id}/typing",
    nil,
    Authorization: token
  )
end

.unban_user(token, server_id, user_id) ⇒ Object

Unban a user from a server



20
21
22
23
24
25
# File 'lib/discordrb/api.rb', line 20

def unban_user(token, server_id, user_id)
  RestClient.delete(
    "#{APIBASE}/guilds/#{server_id}/bans/#{user_id}",
    Authorization: token
  )
end

.update_channel(token, channel_id, name, topic, position = 0) ⇒ Object

Update a channel's data



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

def update_channel(token, channel_id, name, topic, position = 0)
  RestClient.patch(
    "#{APIBASE}/channels/#{channel_id}",
    { 'name' => name, 'position' => position, 'topic' => topic }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_role(token, server_id, role_id, name, colour, hoist = false, packed_permissions = 36_953_089) ⇒ Object

Update a role Permissions are the Discord defaults; allowed: invite creation, reading/sending messages, sending TTS messages, embedding links, sending files, reading the history, mentioning everybody, connecting to voice, speaking and voice activity (push-to-talk isn't mandatory)



239
240
241
242
243
244
245
246
# File 'lib/discordrb/api.rb', line 239

def update_role(token, server_id, role_id, name, colour, hoist = false, packed_permissions = 36_953_089)
  RestClient.patch(
    "#{APIBASE}/guilds/#{server_id}/roles/#{role_id}",
    { 'color' => colour, 'name' => name, 'hoist' => hoist, 'permissions' => packed_permissions }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_role_overrides(token, channel_id, role_id, allow, deny) ⇒ Object

Update a role's permission overrides in a channel



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

def update_role_overrides(token, channel_id, role_id, allow, deny)
  RestClient.put(
    "#{APIBASE}/channels/#{channel_id}/permissions/#{role_id}",
    { 'type' => 'role', 'id' => role_id, 'allow' => allow, 'deny' => deny }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_server(token, server_id, name, region, icon, afk_channel_id, afk_timeout) ⇒ Object

Update a server



72
73
74
75
76
77
78
79
# File 'lib/discordrb/api.rb', line 72

def update_server(token, server_id, name, region, icon, afk_channel_id, afk_timeout)
  RestClient.patch(
    "#{APIBASE}/guilds/#{server_id}",
    { 'name' => name, 'region' => region, 'icon' => icon, 'afk_channel_id' => afk_channel_id, 'afk_timeout' => afk_timeout }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_user(token, email, password, new_username, avatar, new_password = nil) ⇒ Object

Update user data



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

def update_user(token, email, password, new_username, avatar, new_password = nil)
  RestClient.patch(
    "#{APIBASE}/users/@me",
    { 'avatar' => avatar, 'email' => email, 'new_password' => new_password, 'password' => password, 'username' => new_username }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_user_overrides(token, channel_id, user_id, allow, deny) ⇒ Object

Update a user's permission overrides in a channel



267
268
269
270
271
272
273
274
# File 'lib/discordrb/api.rb', line 267

def update_user_overrides(token, channel_id, user_id, allow, deny)
  RestClient.put(
    "#{APIBASE}/channels/#{channel_id}/permissions/#{user_id}",
    { 'type' => 'member', 'id' => user_id, 'allow' => allow, 'deny' => deny }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_user_roles(token, server_id, user_id, roles) ⇒ Object

Update a user's roles



257
258
259
260
261
262
263
264
# File 'lib/discordrb/api.rb', line 257

def update_user_roles(token, server_id, user_id, roles)
  RestClient.patch(
    "#{APIBASE}/guilds/#{server_id}/members/#{user_id}",
    { 'roles' => roles }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.user(token, user_id) ⇒ Object

Get user data



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

def user(token, user_id)
  RestClient.get(
    "#{APIBASE}/users/#{user_id}",
    Authorization: token
  )
end