disrb 0.1.0 → 0.1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/disrb.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'net/http'
4
3
  require 'json'
5
4
  require 'async'
6
5
  require 'async/http/endpoint'
@@ -11,9 +10,6 @@ require 'disrb/logger'
11
10
  require 'disrb/user'
12
11
  require 'disrb/message'
13
12
 
14
- # TODO: If there is more than 1 optional parameter in a function, I should change it from setting the default value via
15
- # = to : so that the user can pass only the parameters they want to set
16
-
17
13
  # DiscordApi
18
14
  # The class that contains everything that interacts with the Discord API.
19
15
  class DiscordApi
@@ -25,41 +21,49 @@ class DiscordApi
25
21
  @authorization_token_type = authorization_token_type
26
22
  @authorization_token = authorization_token
27
23
  @authorization_header = "#{authorization_token_type} #{authorization_token}"
28
- url = URI("#{@base_url}/applications/@me")
29
- headers = { 'Authorization': @authorization_header }
30
- @application_id = JSON.parse(Net::HTTP.get(url, headers))['id']
31
24
  @interaction_created = false
32
25
  @interaction = {}
33
26
  if verbosity_level.nil?
34
- @verbosity_level = 3
27
+ @verbosity_level = 4
35
28
  elsif verbosity_level.is_a?(String)
36
29
  case verbosity_level.downcase
37
30
  when 'all'
38
- @verbosity_level = 4
31
+ @verbosity_level = 5
39
32
  when 'info'
40
- @verbosity_level = 3
33
+ @verbosity_level = 4
41
34
  when 'warning'
42
- @verbosity_level = 2
35
+ @verbosity_level = 3
43
36
  when 'error'
37
+ @verbosity_level = 2
38
+ when 'fatal_error'
44
39
  @verbosity_level = 1
45
40
  when 'none'
46
41
  @verbosity_level = 0
47
42
  else
48
43
  Logger2.s_error("Unknown verbosity level: #{verbosity_level}. Defaulting to 'info'.")
49
- @verbosity_level = 3
44
+ @verbosity_level = 4
50
45
  end
51
46
  elsif verbosity_level.is_a?(Integer)
52
- if verbosity_level >= 0 && verbosity_level <= 4
47
+ if verbosity_level >= 0 && verbosity_level <= 5
53
48
  @verbosity_level = verbosity_level
54
49
  else
55
50
  Logger2.s_error("Unknown verbosity level: #{verbosity_level}. Defaulting to 'info'.")
56
- @verbosity_level = 3
51
+ @verbosity_level = 4
57
52
  end
58
53
  else
59
54
  Logger2.s_error("Unknown verbosity level: #{verbosity_level}. Defaulting to 'info'.")
60
- @verbosity_level = 3
55
+ @verbosity_level = 4
61
56
  end
62
57
  @logger = Logger2.new(@verbosity_level)
58
+ url = "#{@base_url}/applications/@me"
59
+ headers = { 'Authorization': @authorization_header }
60
+ response = DiscordApi.get(url, headers)
61
+ if response.status == 200
62
+ @application_id = JSON.parse(response.body)['id']
63
+ else
64
+ @logger.fatal_error("Failed to get application ID with response: #{response.body}")
65
+ exit
66
+ end
63
67
  end
64
68
 
65
69
  def self.handle_query_strings(query_string_hash)
@@ -91,21 +95,25 @@ class DiscordApi
91
95
 
92
96
  def create_guild_application_command(guild_id, name, name_localizations: nil, description: nil,
93
97
  description_localizations: nil, options: nil, default_member_permissions: nil,
94
- default_permission: true, type: 1, nsfw: false)
98
+ default_permission: nil, type: nil, nsfw: nil)
95
99
  output = {}
96
100
  output[:name] = name
97
101
  output[:name_localizations] = name_localizations unless name_localizations.nil?
98
102
  output[:description] = description unless description.nil?
99
103
  output[:description_localizations] = description_localizations unless description_localizations.nil?
100
104
  output[:options] = options unless options.nil?
101
- output[:default_permission] = default_permission
102
- output[:type] = type
103
- output[:nsfw] = nsfw
105
+ output[:default_permission] = default_permission unless default_permission.nil?
106
+ output[:type] = type unless type.nil?
107
+ output[:nsfw] = nsfw unless nsfw.nil?
104
108
  output[:default_member_permissions] = default_member_permissions unless default_member_permissions.nil?
105
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands")
109
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands"
106
110
  data = JSON.generate(output)
107
111
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
108
- Net::HTTP.post(url, data, headers)
112
+ response = DiscordApi.post(url, data, headers)
113
+ return response unless response.status != 201 || response.status != 200
114
+
115
+ @logger.error("Failed to create guild application command in guild with ID #{guild_id}. Response: #{response.body}")
116
+ response
109
117
  end
110
118
 
111
119
  def create_guild_application_commands(application_commands_array)
@@ -124,24 +132,28 @@ class DiscordApi
124
132
 
125
133
  def create_global_application_command(name, name_localizations: nil, description: nil,
126
134
  description_localizations: nil, options: nil,
127
- default_member_permissions: nil, default_permission: true,
128
- integration_types: nil, contexts: nil, type: 1, nsfw: false)
135
+ default_member_permissions: nil, default_permission: nil,
136
+ integration_types: nil, contexts: nil, type: nil, nsfw: nil)
129
137
  output = {}
130
138
  output[:name] = name
131
139
  output[:name_localizations] = name_localizations unless name_localizations.nil?
132
140
  output[:description] = description unless description.nil?
133
141
  output[:description_localizations] = description_localizations unless description_localizations.nil?
134
142
  output[:options] = options unless options.nil?
135
- output[:default_permission] = default_permission
136
- output[:type] = type
137
- output[:nsfw] = nsfw
143
+ output[:default_permission] = default_permission unless default_permission.nil?
144
+ output[:type] = type unless type.nil?
145
+ output[:nsfw] = nsfw unless nsfw.nil?
138
146
  output[:default_member_permissions] = default_member_permissions unless default_member_permissions.nil?
139
147
  output[:integration_types] = integration_types unless integration_types.nil?
140
148
  output[:contexts] = contexts unless contexts.nil?
141
- url = URI("#{@base_url}/applications/#{@application_id}/commands")
149
+ url = "#{@base_url}/applications/#{@application_id}/commands"
142
150
  data = JSON.generate(output)
143
151
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
144
- Net::HTTP.post(url, data, headers)
152
+ response = DiscordApi.post(url, data, headers)
153
+ return response unless response.status != 201 || response.status != 200
154
+
155
+ @logger.error("Failed to create global application command. Response: #{response.body}")
156
+ response
145
157
  end
146
158
 
147
159
  def create_global_application_commands(application_commands_array)
@@ -158,112 +170,181 @@ class DiscordApi
158
170
  end
159
171
  end
160
172
 
161
- def edit_global_application_command(command_id, name = nil, name_localizations = nil, description = nil,
162
- description_localizations = nil, options = nil, default_member_permissions = nil,
163
- default_permission: true, integration_types: nil, contexts: nil, nsfw: nil)
173
+ def edit_global_application_command(command_id, name: nil, name_localizations: nil, description: nil,
174
+ description_localizations: nil, options: nil, default_member_permissions: nil,
175
+ default_permission: nil, integration_types: nil, contexts: nil, nsfw: nil)
176
+ if args[1..].all?(&:nil?)
177
+ @logger.warn("No modifications provided for global application command with ID #{command_id}. Skipping.")
178
+ return nil
179
+ end
164
180
  output = {}
165
181
  output[:name] = name
166
182
  output[:name_localizations] = name_localizations unless name_localizations.nil?
167
183
  output[:description] = description unless description.nil?
168
184
  output[:description_localizations] = description_localizations unless description_localizations.nil?
169
185
  output[:options] = options unless options.nil?
170
- output[:default_permission] = default_permission
171
- output[:nsfw] = nsfw
186
+ output[:default_permission] = default_permission unless default_permission.nil?
187
+ output[:nsfw] = nsfw unless nsfw.nil?
172
188
  output[:default_member_permissions] = default_member_permissions unless default_member_permissions.nil?
173
189
  output[:integration_types] = integration_types unless integration_types.nil?
174
190
  output[:contexts] = contexts unless contexts.nil?
175
- url = URI("#{@base_url}/applications/#{@application_id}/commands/#{command_id}")
191
+ url = "#{@base_url}/applications/#{@application_id}/commands/#{command_id}"
176
192
  data = JSON.generate(output)
177
193
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
178
- Net::HTTP.patch(url, data, headers)
194
+ response = DiscordApi.patch(url, data, headers)
195
+ return response unless response.status != 200
196
+
197
+ @logger.error("Failed to edit global application command with ID #{command_id}. Response: #{response.body}")
198
+ response
179
199
  end
180
200
 
181
- def edit_guild_application_command(guild_id, command_id, name = nil, name_localizations = nil, description = nil,
182
- description_localizations = nil, options = nil, default_member_permissions = nil,
183
- default_permission: true, nsfw: nil)
201
+ def edit_guild_application_command(guild_id, command_id, name: nil, name_localizations: nil, description: nil,
202
+ description_localizations: nil, options: nil, default_member_permissions: nil,
203
+ default_permission: nil, nsfw: nil)
204
+ if args[2..].all?(&:nil?)
205
+ @logger.warn("No modifications provided for guild application command with command ID #{command_id}. Skipping.")
206
+ return nil
207
+ end
184
208
  output = {}
185
209
  output[:name] = name
186
210
  output[:name_localizations] = name_localizations unless name_localizations.nil?
187
211
  output[:description] = description unless description.nil?
188
212
  output[:description_localizations] = description_localizations unless description_localizations.nil?
189
213
  output[:options] = options unless options.nil?
190
- output[:default_permission] = default_permission
191
- output[:nsfw] = nsfw
214
+ output[:default_permission] = default_permission unless default_permission.nil?
215
+ output[:nsfw] = nsfw unless nsfw.nil?
192
216
  output[:default_member_permissions] = default_member_permissions unless default_member_permissions.nil?
193
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}")
217
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}"
194
218
  data = JSON.generate(output)
195
219
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
196
- Net::HTTP.patch(url, data, headers)
220
+ response = DiscordApi.patch(url, data, headers)
221
+ return response unless response.status != 200
222
+
223
+ @logger.error("Failed to edit guild application command with ID #{command_id}. Response: #{response.body}")
224
+ response
197
225
  end
198
226
 
199
227
  def delete_global_application_command(command_id)
200
- url = URI("#{@base_url}/applications/#{@application_id}/commands/#{command_id}")
228
+ url = "#{@base_url}/applications/#{@application_id}/commands/#{command_id}"
201
229
  headers = { 'Authorization': @authorization_header }
202
- Net::HTTP.delete(url, headers)
230
+ response = DiscordApi.delete(url, headers)
231
+ return response unless response.status != 204
232
+
233
+ @logger.error("Failed to delete global application command with ID #{command_id}. Response: #{response.body}")
234
+ response
203
235
  end
204
236
 
205
237
  def delete_guild_application_command(guild_id, command_id)
206
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}")
238
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}"
207
239
  headers = { 'Authorization': @authorization_header }
208
- Net::HTTP.delete(url, headers)
240
+ response = DiscordApi.delete(url, headers)
241
+ return response unless response.status != 204
242
+
243
+ @logger.error("Failed to delete guild application command with ID #{command_id} in guild with ID #{guild_id}. " \
244
+ "Response: #{response.body}")
209
245
  end
210
246
 
211
- def get_guild_application_commands(guild_id, with_localizations: false)
212
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands?with_localizations=" \
213
- "#{with_localizations}")
247
+ def get_guild_application_commands(guild_id, with_localizations: nil)
248
+ query_string_hash = {}
249
+ query_string_hash[:with_localizations] = with_localizations unless with_localizations.nil?
250
+ query_string = DiscordApi.handle_query_strings(query_string_hash)
251
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands#{query_string}"
214
252
  headers = { 'Authorization': @authorization_header }
215
- Net::HTTP.get(url, headers)
253
+ response = DiscordApi.get(url, headers)
254
+ return response unless response.status != 200
255
+
256
+ @logger.error("Failed to get guild application commands for guild with ID #{guild_id}. Response: #{response.body}")
257
+ response
216
258
  end
217
259
 
218
260
  def get_global_application_commands(with_localizations: false)
219
- url = URI("#{@base_url}/applications/#{@application_id}/commands?with_localizations=#{with_localizations}")
261
+ query_string_hash = {}
262
+ query_string_hash[:with_localizations] = with_localizations unless with_localizations.nil?
263
+ query_string = DiscordApi.handle_query_strings(query_string_hash)
264
+ url = "#{@base_url}/applications/#{@application_id}/commands#{query_string}"
220
265
  headers = { 'Authorization': @authorization_header }
221
- Net::HTTP.get(url, headers)
266
+ response = DiscordApi.get(url, headers)
267
+ return response unless response.status != 200
268
+
269
+ @logger.error("Failed to get global application commands. Response: #{response.body}")
270
+ response
222
271
  end
223
272
 
224
273
  def get_global_application_command(command_id)
225
- url = URI("#{@base_url}/applications/#{@application_id}/commands/#{command_id}")
274
+ url = "#{@base_url}/applications/#{@application_id}/commands/#{command_id}"
226
275
  headers = { 'Authorization': @authorization_header }
227
- Net::HTTP.get(url, headers)
276
+ response = DiscordApi.get(url, headers)
277
+ return response unless response.status != 200
278
+
279
+ @logger.error("Failed to get global application command with ID #{command_id}. Response: #{response.body}")
280
+ response
228
281
  end
229
282
 
230
283
  def get_guild_application_command(guild_id, command_id)
231
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}")
284
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}"
232
285
  headers = { 'Authorization': @authorization_header }
233
- Net::HTTP.get(url, headers)
286
+ response = DiscordApi.get(url, headers)
287
+ return response unless response.status != 200
288
+
289
+ @logger.error("Failed to get guild application command with ID #{command_id}. Response: #{response.body}")
290
+ response
234
291
  end
235
292
 
236
293
  def bulk_overwrite_global_application_commands(commands)
237
- url = URI("#{@base_url}/applications/#{@application_id}/commands")
294
+ url = "#{@base_url}/applications/#{@application_id}/commands"
238
295
  data = JSON.generate(commands)
239
296
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
240
- Net::HTTP.put(url, data, headers)
297
+ response = DiscordApi.put(url, data, headers)
298
+ return response unless response.status != 200
299
+
300
+ @logger.error("Failed to bulk overwrite global application commands. Response: #{response.body}")
301
+ response
241
302
  end
242
303
 
243
304
  def bulk_overwrite_guild_application_commands(guild_id, commands)
244
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands")
305
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands"
245
306
  data = JSON.generate(commands)
246
307
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
247
- Net::HTTP.put(url, data, headers)
308
+ response = DiscordApi.put(url, data, headers)
309
+ return response unless response.status != 200
310
+
311
+ @logger.error("Failed to bulk overwrite guild application commands in guild with ID #{guild_id}. " \
312
+ "Response: #{response.body}")
313
+ response
248
314
  end
249
315
 
250
316
  def get_guild_application_command_permissions(guild_id)
251
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/permissions")
317
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/permissions"
252
318
  headers = { 'Authorization': @authorization_header }
253
- Net::HTTP.get(url, headers)
319
+ response = DiscordApi.get(url, headers)
320
+ return response unless response.status != 200
321
+
322
+ @logger.error("Failed to get guild application command permissions for guild with ID #{guild_id}. " \
323
+ "Response: #{response.body}")
324
+ response
254
325
  end
255
326
 
256
327
  def get_application_command_permissions(guild_id, command_id)
257
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}/permissions")
328
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}/permissions"
258
329
  headers = { 'Authorization': @authorization_header }
259
- Net::HTTP.get(url, headers)
330
+ response = DiscordApi.get(url, headers)
331
+ return response unless response.status != 200
332
+
333
+ @logger.error("Failed to get appliaction command permissions for command with ID #{command_id} in guild with ID " \
334
+ "#{guild_id}. Response: #{response.body}")
335
+ response
260
336
  end
261
337
 
262
338
  def edit_application_command_permissions(guild_id, command_id, permissions)
263
- url = URI("#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}/permissions")
339
+ url = "#{@base_url}/applications/#{@application_id}/guilds/#{guild_id}/commands/#{command_id}/permissions"
264
340
  data = JSON.generate(permissions)
265
341
  headers = { 'Authorization': @authorization_header, 'Content-Type': 'application/json' }
266
- Net::HTTP.put(url, data, headers)
342
+ response = DiscordApi.put(url, data, headers)
343
+ return response unless response.status != 200
344
+
345
+ @logger.error("Failed to edit application command permissions for command with ID #{command_id} in guild with ID " \
346
+ "#{guild_id}. Response: #{response.body}")
347
+ response
267
348
  end
268
349
 
269
350
  def connect_gateway(activities: nil, os: nil, browser: nil, device: nil, intents: nil, presence_since: nil,
@@ -346,7 +427,13 @@ class DiscordApi
346
427
  rescue_connection, sequence, resume_gateway_url, session_id = nil
347
428
  loop do
348
429
  url = if rescue_connection.nil?
349
- "#{JSON.parse(Net::HTTP.get(URI("#{@base_url}/gateway")))['url']}/?v=#{@api_version}&encoding=json"
430
+ response = DiscordApi.get("#{@base_url}/gateway")
431
+ if response.status == 200
432
+ "#{JSON.parse(response)['url']}/?v=#{@api_version}&encoding=json"
433
+ else
434
+ @logger.fatal_error("Failed to get gateway URL. Response: #{response.body}")
435
+ exit
436
+ end
350
437
  else
351
438
  "#{rescue_connection[:resume_gateway_url]}/?v=#{@api_version}&encoding=json"
352
439
  end
@@ -595,7 +682,6 @@ class DiscordApi
595
682
  intents.reduce(0) { |acc, n| acc | n }
596
683
  end
597
684
 
598
- # TODO: Transition from Net::HTTP to below wrapper methods
599
685
  def self.get(url, headers = nil)
600
686
  split_url = url.split(%r{(http[^/]+)(/.*)}).reject(&:empty?)
601
687
  @logger.error("Empty/invalid URL provided: #{url}. Cannot perform GET request.") if split_url.empty?
@@ -634,4 +720,30 @@ class DiscordApi
634
720
  conn.post('', data)
635
721
  end
636
722
  end
723
+
724
+ def self.patch(url, data, headers = nil)
725
+ split_url = url.split(%r{(http[^/]+)(/.*)}).reject(&:empty?)
726
+ @logger.error("Empty/invalid URL provided: #{url}. Cannot perform PATCH request.") if split_url.empty?
727
+ host = split_url[0]
728
+ path = split_url[1] if split_url[1]
729
+ conn = Faraday.new(url: host, headers: headers)
730
+ if path
731
+ conn.patch(path, data)
732
+ else
733
+ conn.patch('', data)
734
+ end
735
+ end
736
+
737
+ def self.put(url, data, headers = nil)
738
+ split_url = url.split(%r{(http[^/]+)(/.*)}).reject(&:empty?)
739
+ @logger.error("Empty/invalid URL provided: #{url}. Cannot perform PUT request.") if split_url.empty?
740
+ host = split_url[0]
741
+ path = split_url[1] if split_url[1]
742
+ conn = Faraday.new(url: host, headers: headers)
743
+ if path
744
+ conn.put(path, data)
745
+ else
746
+ conn.put('', data)
747
+ end
748
+ end
637
749
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: disrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - hoovad