discorb 0.13.3 → 0.13.4

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/discorb/http.rb CHANGED
@@ -17,10 +17,11 @@ module Discorb
17
17
  end
18
18
 
19
19
  #
20
- # Execute a GET request.
20
+ # Execute a request.
21
21
  # @async
22
22
  #
23
- # @param [String] path The path to the resource.
23
+ # @param [Discorb::Route] path The path to the resource.
24
+ # @param [String, Hash] body The body of the request. Defaults to an empty string.
24
25
  # @param [Hash] headers The headers to send with the request.
25
26
  # @param [String] audit_log_reason The audit log reason to send with the request.
26
27
  # @param [Hash] kwargs The keyword arguments.
@@ -30,46 +31,25 @@ module Discorb
30
31
  #
31
32
  # @raise [Discorb::HTTPError] The request was failed.
32
33
  #
33
- def get(path, headers: nil, audit_log_reason: nil, **kwargs)
34
+ def request(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
34
35
  Async do |task|
35
- @ratelimit_handler.wait("GET", path)
36
- resp = http.get(get_path(path), get_headers(headers, "", audit_log_reason), **kwargs)
37
- data = get_response_data(resp)
38
- @ratelimit_handler.save("GET", path, resp)
39
- handle_response(:patch, resp, data, path, nil, headers, audit_log_reason, kwargs)
40
- end
41
- end
42
-
43
- #
44
- # Execute a POST request.
45
- # @async
46
- #
47
- # @param [String] path The path to the resource.
48
- # @param [String, Hash] body The body of the request.
49
- # @param [Hash] headers The headers to send with the request.
50
- # @param [String] audit_log_reason The audit log reason to send with the request.
51
- # @param [Hash] kwargs The keyword arguments.
52
- #
53
- # @return [Async::Task<Array(Net::HTTPResponse, Hash)>] The response and as JSON.
54
- # @return [Async::Task<Array(Net::HTTPResponse, nil)>] The response was 204.
55
- #
56
- # @raise [Discorb::HTTPError] The request was failed.
57
- #
58
- def post(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
59
- Async do |task|
60
- @ratelimit_handler.wait("POST", path)
61
- resp = http.post(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs)
36
+ @ratelimit_handler.wait(path)
37
+ if %i[post patch put].include? path.method
38
+ resp = http.send(path.method, get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs)
39
+ else
40
+ resp = http.send(path.method, get_path(path), get_headers(headers, body, audit_log_reason), **kwargs)
41
+ end
62
42
  data = get_response_data(resp)
63
- @ratelimit_handler.save("POST", path, resp)
64
- handle_response(:post, resp, data, path, body, headers, audit_log_reason, kwargs)
43
+ @ratelimit_handler.save(path, resp)
44
+ handle_response(resp, data, path, body, headers, audit_log_reason, kwargs)
65
45
  end
66
46
  end
67
47
 
68
48
  #
69
- # Execute a multipart POST request.
49
+ # Execute a multipart request.
70
50
  # @async
71
51
  #
72
- # @param [String] path The path to the resource.
52
+ # @param [Discorb::Route] path The path to the resource.
73
53
  # @param [String, Hash] body The body of the request.
74
54
  # @param [Array<Discorb::File>] files The files to upload.
75
55
  # @param [Hash] headers The headers to send with the request.
@@ -81,10 +61,10 @@ module Discorb
81
61
  #
82
62
  # @raise [Discorb::HTTPError] The request was failed.
83
63
  #
84
- def multipart_post(path, body = "", files, headers: nil, audit_log_reason: nil, **kwargs)
64
+ def multipart_request(path, body = "", files, headers: nil, audit_log_reason: nil, **kwargs)
85
65
  Async do |task|
86
- @ratelimit_handler.wait("POST", path)
87
- req = Net::HTTP::Post.new(get_path(path), get_headers(headers, body, audit_log_reason), **kwargs)
66
+ @ratelimit_handler.wait(path)
67
+ req = Net::HTTP.const_get(path.method.to_s.capitalize).new(get_path(path), get_headers(headers, body, audit_log_reason), **kwargs)
88
68
  data = [
89
69
  ["payload_json", get_body(body)],
90
70
  ]
@@ -97,117 +77,8 @@ module Discorb
97
77
  session.use_ssl = true
98
78
  resp = session.request(req)
99
79
  data = get_response_data(resp)
100
- @ratelimit_handler.save("POST", path, resp)
101
- handle_response(:post, resp, data, path, body, headers, audit_log_reason, kwargs)
102
- end
103
- end
104
-
105
- #
106
- # Execute a PATCH request.
107
- # @async
108
- #
109
- # @param [String] path The path to the resource.
110
- # @param [String, Hash] body The body of the request.
111
- # @param [Hash] headers The headers to send with the request.
112
- # @param [String] audit_log_reason The audit log reason to send with the request.
113
- # @param [Hash] kwargs The keyword arguments.
114
- #
115
- # @return [Async::Task<Array(Net::HTTPResponse, Hash)>] The response and as JSON.
116
- # @return [Async::Task<Array(Net::HTTPResponse, nil)>] The response was 204.
117
- #
118
- # @raise [Discorb::HTTPError] The request was failed.
119
- #
120
- def patch(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
121
- Async do |task|
122
- @ratelimit_handler.wait("PATCH", path)
123
- resp = http.patch(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs)
124
- data = get_response_data(resp)
125
- @ratelimit_handler.save("PATCH", path, resp)
126
- handle_response(:patch, resp, data, path, body, headers, audit_log_reason, kwargs)
127
- end
128
- end
129
-
130
- #
131
- # Execute a PATCH request.
132
- # @async
133
- #
134
- # @param [String] path The path to the resource.
135
- # @param [String, Hash] body The body of the request.
136
- # @param [Hash] headers The headers to send with the request.
137
- # @param [String] audit_log_reason The audit log reason to send with the request.
138
- # @param [Hash] kwargs The keyword arguments.
139
- #
140
- # @return [Async::Task<Array(Net::HTTPResponse, Hash)>] The response and as JSON.
141
- # @return [Async::Task<Array(Net::HTTPResponse, nil)>] The response was 204.
142
- #
143
- # @raise [Discorb::HTTPError] The request was failed.
144
- #
145
- def multipart_patch(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
146
- Async do |task|
147
- @ratelimit_handler.wait("PATCH", path)
148
- req = Net::HTTP::Patch.new(get_path(path), get_headers(headers, body, audit_log_reason), **kwargs)
149
- data = [
150
- ["payload_json", get_body(body)],
151
- ]
152
- files&.each_with_index do |file, i|
153
- data << ["files[#{i}]", file.io, { filename: file.filename, content_type: file.content_type }]
154
- end
155
- req.set_form(data, "multipart/form-data")
156
- session = Net::HTTP.new("discord.com", 443)
157
- session.use_ssl = true
158
- resp = session.request(req)
159
- data = get_response_data(resp)
160
- @ratelimit_handler.save("PATCH", path, resp)
161
- handle_response(:patch, resp, data, path, body, headers, audit_log_reason, kwargs)
162
- end
163
- end
164
-
165
- #
166
- # Execute a PUT request.
167
- # @async
168
- #
169
- # @param [String] path The path to the resource.
170
- # @param [String, Hash] body The body of the request.
171
- # @param [Hash] headers The headers to send with the request.
172
- # @param [String] audit_log_reason The audit log reason to send with the request.
173
- # @param [Hash] kwargs The keyword arguments.
174
- #
175
- # @return [Async::Task<Array(Net::HTTPResponse, Hash)>] The response and as JSON.
176
- # @return [Async::Task<Array(Net::HTTPResponse, nil)>] The response was 204.
177
- #
178
- # @raise [Discorb::HTTPError] The request was failed.
179
- #
180
- def put(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
181
- Async do |task|
182
- @ratelimit_handler.wait("PUT", path)
183
- resp = http.put(get_path(path), get_body(body), get_headers(headers, body, audit_log_reason), **kwargs)
184
- data = get_response_data(resp)
185
- @ratelimit_handler.save("PUT", path, resp)
186
- handle_response(:put, resp, data, path, body, headers, audit_log_reason, kwargs)
187
- end
188
- end
189
-
190
- #
191
- # Execute a DELETE request.
192
- # @async
193
- #
194
- # @param [String] path The path to the resource.
195
- # @param [Hash] headers The headers to send with the request.
196
- # @param [String] audit_log_reason The audit log reason to send with the request.
197
- # @param [Hash] kwargs The keyword arguments.
198
- #
199
- # @return [Async::Task<Array(Net::HTTPResponse, Hash)>] The response and as JSON.
200
- # @return [Async::Task<Array(Net::HTTPResponse, nil)>] The response was 204.
201
- #
202
- # @raise [Discorb::HTTPError] The request was failed.
203
- #
204
- def delete(path, headers: nil, audit_log_reason: nil, **kwargs)
205
- Async do
206
- @ratelimit_handler.wait("DELETE", path)
207
- resp = http.delete(get_path(path), get_headers(headers, "", audit_log_reason))
208
- data = get_response_data(resp)
209
- @ratelimit_handler.save("DELETE", path, resp)
210
- handle_response(:delete, resp, data, path, nil, headers, audit_log_reason, kwargs)
80
+ @ratelimit_handler.save(path, resp)
81
+ handle_response(resp, data, path, body, headers, audit_log_reason, kwargs)
211
82
  end
212
83
  end
213
84
 
@@ -217,16 +88,12 @@ module Discorb
217
88
 
218
89
  private
219
90
 
220
- def handle_response(method, resp, data, path, body, headers, audit_log_reason, kwargs)
91
+ def handle_response(resp, data, path, body, headers, audit_log_reason, kwargs)
221
92
  case resp.code
222
93
  when "429"
223
- @client.log.info("Rate limit exceeded for #{method} #{path}, waiting #{data[:retry_after]} seconds")
94
+ @client.log.info("Rate limit exceeded for #{path.method} #{path.url}, waiting #{data[:retry_after]} seconds")
224
95
  sleep(data[:retry_after])
225
- if body
226
- __send__(method, path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
227
- else
228
- __send__(method, path, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
229
- end
96
+ request(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
230
97
  when "400"
231
98
  raise BadRequestError.new(resp, data)
232
99
  when "401"
@@ -263,10 +130,10 @@ module Discorb
263
130
  end
264
131
 
265
132
  def get_path(path)
266
- full_path = if path.start_with?("https://")
267
- path
133
+ full_path = if path.url.start_with?("https://")
134
+ path.url
268
135
  else
269
- API_BASE_URL + path
136
+ API_BASE_URL + path.url
270
137
  end
271
138
  uri = URI(full_path)
272
139
  full_path.sub(uri.scheme + "://" + uri.host, "")
@@ -65,7 +65,7 @@ module Discorb
65
65
  #
66
66
  def delete!(reason: nil)
67
67
  Async do
68
- @client.http.delete("/guilds/#{@guild}/integrations/#{@id}", audit_log_reason: reason).wait
68
+ @client.http.request(Route.new("/guilds/#{@guild}/integrations/#{@id}", "//guilds/:guild_id/integrations/:integration_id", :delete), audit_log_reason: reason).wait
69
69
  end
70
70
  end
71
71
 
@@ -27,7 +27,7 @@ module Discorb
27
27
 
28
28
  # @private
29
29
  def send_complete_result(val)
30
- @client.http.post("/interactions/#{@id}/#{@token}/callback", {
30
+ @client.http.request(Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post), {
31
31
  type: 8,
32
32
  data: {
33
33
  choices: val.map do |vk, vv|
@@ -15,7 +15,7 @@ module Discorb
15
15
  #
16
16
  def defer_source(ephemeral: false)
17
17
  Async do
18
- @client.http.post("/interactions/#{@id}/#{@token}/callback", {
18
+ @client.http.request(Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post), {
19
19
  type: 5,
20
20
  data: {
21
21
  flags: (ephemeral ? 1 << 6 : 0),
@@ -51,14 +51,14 @@ module Discorb
51
51
  payload[:flags] = (ephemeral ? 1 << 6 : 0)
52
52
 
53
53
  ret = if @responded
54
- _resp, data = @client.http.post("/webhooks/#{@application_id}/#{@token}", payload).wait
54
+ _resp, data = @client.http.request(Route.new("/webhooks/#{@application_id}/#{@token}", "//webhooks/:webhook_id/:token", :post), payload).wait
55
55
  webhook = Webhook::URLWebhook.new("/webhooks/#{@application_id}/#{@token}")
56
56
  Webhook::Message.new(webhook, data, @client)
57
57
  elsif @defered
58
- @client.http.patch("/webhooks/#{@application_id}/#{@token}/messages/@original", payload).wait
58
+ @client.http.request(Route.new("/webhooks/#{@application_id}/#{@token}/messages/@original", "//webhooks/:webhook_id/:token/messages/@original", :patch), payload).wait
59
59
  CallbackMessage.new(@client, payload, @application_id, @token)
60
60
  else
61
- @client.http.post("/interactions/#{@id}/#{@token}/callback", { type: 4, data: payload }).wait
61
+ @client.http.request(Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post), { type: 4, data: payload }).wait
62
62
  CallbackMessage.new(@client, payload, @application_id, @token)
63
63
  end
64
64
  @responded = true
@@ -103,7 +103,7 @@ module Discorb
103
103
  payload[:attachments] = attachments.map(&:to_hash) if attachments != Discorb::Unset
104
104
  files = [file] if file != Discorb::Unset
105
105
  files = [] if files == Discorb::Unset
106
- @client.http.multipart_patch("/webhooks/#{@application_id}/#{@token}/messages/@original", payload, files, headers: headers).wait
106
+ @client.http.multipart_request(Route.new("/webhooks/#{@application_id}/#{@token}/messages/@original", "//webhooks/:webhook_id/:token/messages/@original", :patch), payload, files, headers: headers).wait
107
107
  end
108
108
  end
109
109
 
@@ -118,7 +118,7 @@ module Discorb
118
118
  #
119
119
  def delete!
120
120
  Async do
121
- @client.http.delete("/webhooks/#{@application_id}/#{@token}/messages/@original").wait
121
+ @client.http.request(Route.new("/webhooks/#{@application_id}/#{@token}/messages/@original", "//webhooks/:webhook_id/:token/messages/@original", :delete)).wait
122
122
  end
123
123
  end
124
124
  end
@@ -138,7 +138,7 @@ module Discorb
138
138
  #
139
139
  def defer_update(ephemeral: false)
140
140
  Async do
141
- @client.http.post("/interactions/#{@id}/#{@token}/callback", {
141
+ @client.http.request(Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post), {
142
142
  type: 6,
143
143
  data: {
144
144
  flags: (ephemeral ? 1 << 6 : 0),
@@ -176,7 +176,7 @@ module Discorb
176
176
  payload[:allowed_mentions] = allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
177
177
  payload[:components] = Component.to_payload(components) if components
178
178
  payload[:flags] = (ephemeral ? 1 << 6 : 0)
179
- @client.http.post("/interactions/#{@id}/#{@token}/callback", { type: 7, data: payload }).wait
179
+ @client.http.request(Route.new("/interactions/#{@id}/#{@token}/callback", "//interactions/:interaction_id/:token/callback", :post), { type: 7, data: payload }).wait
180
180
  end
181
181
  end
182
182
  end
@@ -105,7 +105,7 @@ module Discorb
105
105
  #
106
106
  def delete!(reason: nil)
107
107
  Async do
108
- @client.http.delete("/invites/#{@code}", audit_log_reason: reason)
108
+ @client.http.request(Route.new("/invites/#{@code}", "//invites/:code", :delete), audit_log_reason: reason)
109
109
  end
110
110
  end
111
111
 
data/lib/discorb/log.rb CHANGED
@@ -71,10 +71,11 @@ module Discorb
71
71
 
72
72
  time = Time.now.iso8601
73
73
  if @colorize_log
74
- @out.puts("\e[90m#{time}\e[0m #{color}#{name.ljust(5)}\e[0m #{message}")
74
+ @out.write("\e[90m#{time}\e[0m #{color}#{name.ljust(5)}\e[0m #{message}\n")
75
75
  else
76
- @out.puts("#{time} #{name.ljust(5)} #{message}")
76
+ @out.write("#{time} #{name.ljust(5)} #{message}\n")
77
77
  end
78
+ @out.flush
78
79
  end
79
80
  end
80
81
  end
@@ -152,7 +152,7 @@ module Discorb
152
152
  #
153
153
  def add_role(role, reason: nil)
154
154
  Async do
155
- @client.http.put("/guilds/#{@guild_id}/members/#{@id}/roles/#{role.is_a?(Role) ? role.id : role}", nil, audit_log_reason: reason).wait
155
+ @client.http.request(Route.new("/guilds/#{@guild_id}/members/#{@id}/roles/#{role.is_a?(Role) ? role.id : role}", "//guilds/:guild_id/members/:user_id/roles/:role_id", :put), nil, audit_log_reason: reason).wait
156
156
  end
157
157
  end
158
158
 
@@ -167,7 +167,7 @@ module Discorb
167
167
  #
168
168
  def remove_role(role, reason: nil)
169
169
  Async do
170
- @client.http.delete("/guilds/#{@guild_id}/members/#{@id}/roles/#{role.is_a?(Role) ? role.id : role}", audit_log_reason: reason).wait
170
+ @client.http.request(Route.new("/guilds/#{@guild_id}/members/#{@id}/roles/#{role.is_a?(Role) ? role.id : role}", "//guilds/:guild_id/members/:user_id/roles/:role_id", :delete), audit_log_reason: reason).wait
171
171
  end
172
172
  end
173
173
 
@@ -200,7 +200,7 @@ module Discorb
200
200
  communication_disabled_until = timeout_until if timeout_until != Discorb::Unset
201
201
  payload[:communication_disabled_until] = communication_disabled_until&.iso8601 if communication_disabled_until != Discorb::Unset
202
202
  payload[:channel_id] = channel&.id if channel != Discorb::Unset
203
- @client.http.patch("/guilds/#{@guild_id}/members/#{@id}", payload, audit_log_reason: reason).wait
203
+ @client.http.request(Route.new("/guilds/#{@guild_id}/members/#{@id}", "//guilds/:guild_id/members/:user_id", :patch), payload, audit_log_reason: reason).wait
204
204
  end
205
205
  end
206
206
 
@@ -379,7 +379,7 @@ module Discorb
379
379
  #
380
380
  def add_reaction(emoji)
381
381
  Async do
382
- @client.http.put("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", nil).wait
382
+ @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", "//channels/:channel_id/messages/:message_id/reactions/:emoji/@me", :put), nil).wait
383
383
  end
384
384
  end
385
385
 
@@ -395,7 +395,7 @@ module Discorb
395
395
  #
396
396
  def remove_reaction(emoji)
397
397
  Async do
398
- @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me").wait
398
+ @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/@me", "//channels/:channel_id/messages/:message_id/reactions/:emoji/@me", :delete)).wait
399
399
  end
400
400
  end
401
401
 
@@ -412,7 +412,7 @@ module Discorb
412
412
  #
413
413
  def remove_reaction_of(emoji, member)
414
414
  Async do
415
- @client.http.delete("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/#{member.is_a?(Member) ? member.id : member}").wait
415
+ @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}/#{member.is_a?(Member) ? member.id : member}", "//channels/:channel_id/messages/:message_id/reactions/:emoji/:user_id", :delete)).wait
416
416
  end
417
417
  end
418
418
 
@@ -434,7 +434,7 @@ module Discorb
434
434
  after = 0
435
435
  users = []
436
436
  loop do
437
- _resp, data = @client.http.get("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=100&after=#{after}").wait
437
+ _resp, data = @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=100&after=#{after}", "//channels/:channel_id/messages/:message_id/reactions/:emoji", :get)).wait
438
438
  break if data.empty?
439
439
 
440
440
  users += data.map { |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) }
@@ -445,7 +445,7 @@ module Discorb
445
445
  end
446
446
  next users
447
447
  else
448
- _resp, data = @client.http.get("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=#{limit}&after=#{after}").wait
448
+ _resp, data = @client.http.request(Route.new("/channels/#{@channel_id}/messages/#{@id}/reactions/#{emoji.to_uri}?limit=#{limit}&after=#{after}", "//channels/:channel_id/messages/:message_id/reactions/:emoji", :get)).wait
449
449
  next data.map { |r| guild&.members&.[](r[:id]) || @client.users[r[:id]] || User.new(@client, r) }
450
450
  end
451
451
  end
@@ -38,7 +38,7 @@ module Discorb
38
38
  payload[:message_reference] = reference.to_reference if reference
39
39
  payload[:components] = Component.to_payload(components) if components
40
40
  files = [file]
41
- _resp, data = @client.http.multipart_post("/channels/#{channel_id.wait}/messages", payload, files).wait
41
+ _resp, data = @client.http.multipart_request(Route.new("/channels/#{channel_id.wait}/messages", "//channels/:channel_id/messages", :post), payload, files).wait
42
42
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
43
43
  end
44
44
  end
@@ -74,7 +74,7 @@ module Discorb
74
74
  allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
75
75
  payload[:components] = Component.to_payload(components) if components
76
76
  payload[:flags] = (supress ? 1 << 2 : 0) unless supress.nil?
77
- @client.http.patch("/channels/#{channel_id.wait}/messages/#{message_id}", payload).wait
77
+ @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id", :patch), payload).wait
78
78
  end
79
79
  end
80
80
 
@@ -89,7 +89,7 @@ module Discorb
89
89
  #
90
90
  def delete_message!(message_id, reason: nil)
91
91
  Async do
92
- @client.http.delete("/channels/#{channel_id.wait}/messages/#{message_id}", audit_log_reason: reason).wait
92
+ @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id", :delete), audit_log_reason: reason).wait
93
93
  end
94
94
  end
95
95
 
@@ -106,7 +106,7 @@ module Discorb
106
106
  #
107
107
  def fetch_message(id)
108
108
  Async do
109
- _resp, data = @client.http.get("/channels/#{channel_id.wait}/messages/#{id}").wait
109
+ _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{id}", "//channels/:channel_id/messages/:message_id", :get)).wait
110
110
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
111
111
  end
112
112
  end
@@ -130,7 +130,7 @@ module Discorb
130
130
  after: Discorb::Utils.try(around, :id),
131
131
  around: Discorb::Utils.try(before, :id),
132
132
  }.filter { |_k, v| !v.nil? }.to_h
133
- _resp, messages = @client.http.get("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}").wait
133
+ _resp, messages = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages", :get)).wait
134
134
  messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
135
135
  end
136
136
  end
@@ -143,7 +143,7 @@ module Discorb
143
143
  #
144
144
  def fetch_pins
145
145
  Async do
146
- _resp, data = @client.http.get("/channels/#{channel_id.wait}/pins").wait
146
+ _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/pins", "//channels/:channel_id/pins", :get)).wait
147
147
  data.map { |pin| Message.new(@client, pin) }
148
148
  end
149
149
  end
@@ -159,7 +159,7 @@ module Discorb
159
159
  #
160
160
  def pin_message(message, reason: nil)
161
161
  Async do
162
- @client.http.put("/channels/#{channel_id.wait}/pins/#{message.id}", {}, audit_log_reason: reason).wait
162
+ @client.http.request(Route.new("/channels/#{channel_id.wait}/pins/#{message.id}", "//channels/:channel_id/pins/:message_id", :put), {}, audit_log_reason: reason).wait
163
163
  end
164
164
  end
165
165
 
@@ -174,7 +174,7 @@ module Discorb
174
174
  #
175
175
  def unpin_message(message, reason: nil)
176
176
  Async do
177
- @client.http.delete("/channels/#{channel_id.wait}/pins/#{message.id}", audit_log_reason: reason).wait
177
+ @client.http.request(Route.new("/channels/#{channel_id.wait}/pins/#{message.id}", "//channels/:channel_id/pins/:message_id", :delete), audit_log_reason: reason).wait
178
178
  end
179
179
  end
180
180
 
@@ -195,7 +195,7 @@ module Discorb
195
195
  begin
196
196
  post_task = Async do
197
197
  loop do
198
- @client.http.post("/channels/#{@id}/typing", {})
198
+ @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {})
199
199
  sleep(5)
200
200
  end
201
201
  end
@@ -205,7 +205,7 @@ module Discorb
205
205
  end
206
206
  else
207
207
  Async do |task|
208
- @client.http.post("/channels/#{@id}/typing", {})
208
+ @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {})
209
209
  end
210
210
  end
211
211
  end
@@ -175,12 +175,12 @@ module Discorb
175
175
  # Represents the assets of an activity.
176
176
  #
177
177
  class Asset < DiscordModel
178
- # @return [String] The large image ID of the asset.
178
+ # @return [String] The large image ID or URL of the asset.
179
179
  attr_reader :large_image
180
180
  alias large_id large_image
181
181
  # @return [String] The large text of the activity.
182
182
  attr_reader :large_text
183
- # @return [String] The small image ID of the activity.
183
+ # @return [String] The small image ID or URL of the activity.
184
184
  attr_reader :small_image
185
185
  alias small_id small_image
186
186
  # @return [String] The small text of the activity.
@@ -9,8 +9,8 @@ module Discorb
9
9
  # @private
10
10
  def initialize(client)
11
11
  @client = client
12
- @current_ratelimits = {}
13
12
  @path_ratelimit_bucket = {}
13
+ @path_ratelimit_hash = {}
14
14
  @global = false
15
15
  end
16
16
 
@@ -21,12 +21,10 @@ module Discorb
21
21
  #
22
22
  # Wait for the rate limit to reset.
23
23
  #
24
- # @param [String] method The HTTP method.
25
- # @param [String] path The path.
24
+ # @param [Discorb::Route] path The path.
26
25
  #
27
- def wait(method, path)
28
- return if path.start_with?("https://")
29
-
26
+ def wait(path)
27
+ # return if path.url.start_with?("https://")
30
28
  if @global && @global > Time.now.to_f
31
29
  time = @global - Time.now.to_f
32
30
  @client.log.info("global rate limit reached, waiting #{time} seconds")
@@ -34,36 +32,34 @@ module Discorb
34
32
  @global = false
35
33
  end
36
34
 
37
- return unless hash = @path_ratelimit_bucket[method + path]
35
+ return unless hash = @path_ratelimit_hash[path.identifier]
38
36
 
39
- return unless b = @current_ratelimits[hash]
37
+ return unless bucket = @path_ratelimit_bucket[hash + path.major_param]
40
38
 
41
- if b[:reset_at] < Time.now.to_f
42
- @current_ratelimits.delete(hash)
39
+ if bucket[:reset_at] < Time.now.to_f
40
+ @path_ratelimit_bucket.delete(path.identifier + path.major_param)
43
41
  return
44
42
  end
45
- return if b[:remaining] > 0
43
+ return if bucket[:remaining] > 0
46
44
 
47
- time = b[:reset_at] - Time.now.to_f
48
- @client.log.info("rate limit for #{method} #{path} reached, waiting #{time} seconds")
45
+ time = bucket[:reset_at] - Time.now.to_f
46
+ @client.log.info("rate limit for #{path.identifier} with #{path.major_param} reached, waiting #{time.round(4)} seconds")
49
47
  sleep(time)
50
48
  end
51
49
 
52
50
  #
53
51
  # Save the rate limit.
54
52
  #
55
- # @param [String] method The HTTP method.
56
53
  # @param [String] path The path.
57
54
  # @param [Net::HTTPResponse] resp The response.
58
55
  #
59
- def save(method, path, resp)
56
+ def save(path, resp)
60
57
  if resp["X-Ratelimit-Global"] == "true"
61
58
  @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after]
62
59
  end
63
60
  return unless resp["X-RateLimit-Remaining"]
64
-
65
- @path_ratelimit_bucket[method + path] = resp["X-RateLimit-Bucket"]
66
- @current_ratelimits[resp["X-RateLimit-Bucket"]] = {
61
+ @path_ratelimit_hash[path.identifier] = resp["X-Ratelimit-Bucket"]
62
+ @path_ratelimit_bucket[resp["X-Ratelimit-Bucket"] + path.major_param] = {
67
63
  remaining: resp["X-RateLimit-Remaining"].to_i,
68
64
  reset_at: Time.now.to_f + resp["X-RateLimit-Reset-After"].to_f,
69
65
  }
data/lib/discorb/role.rb CHANGED
@@ -99,7 +99,7 @@ module Discorb
99
99
  #
100
100
  def move(position, reason: nil)
101
101
  Async do
102
- @client.http.patch("/guilds/#{@guild.id}/roles", { id: @id, position: position }, audit_log_reason: reason).wait
102
+ @client.http.request(Route.new("/guilds/#{@guild.id}/roles", "//guilds/:guild_id/roles", :patch), { id: @id, position: position }, audit_log_reason: reason).wait
103
103
  end
104
104
  end
105
105
 
@@ -133,7 +133,7 @@ module Discorb
133
133
  payload[:unicode_emoji] = icon.to_s
134
134
  end
135
135
  end
136
- @client.http.patch("/guilds/#{@guild.id}/roles/#{@id}", payload, audit_log_reason: reason).wait
136
+ @client.http.request(Route.new("/guilds/#{@guild.id}/roles/#{@id}", "//guilds/:guild_id/roles/:role_id", :patch), payload, audit_log_reason: reason).wait
137
137
  end
138
138
  end
139
139
 
@@ -148,7 +148,7 @@ module Discorb
148
148
  #
149
149
  def delete!(reason: nil)
150
150
  Async do
151
- @client.http.delete("/guilds/#{@guild.id}/roles/#{@id}", audit_log_reason: reason).wait
151
+ @client.http.request(Route.new("/guilds/#{@guild.id}/roles/#{@id}", "//guilds/:guild_id/roles/:role_id", :delete), audit_log_reason: reason).wait
152
152
  end
153
153
  end
154
154
 
@@ -83,7 +83,7 @@ module Discorb
83
83
  payload[:name] = name unless name == Discorb::Unset
84
84
  payload[:description] = description unless description == Discorb::Unset
85
85
  payload[:tags] = tag.name unless tag == Discorb::Unset
86
- @client.http.patch("/guilds/#{@guild_id}/stickers/#{@id}", payload, audit_log_reason: reason).wait
86
+ @client.http.request(Route.new("/guilds/#{@guild_id}/stickers/#{@id}", "//guilds/:guild_id/stickers/:sticker_id", :patch), payload, audit_log_reason: reason).wait
87
87
  end
88
88
  end
89
89
 
@@ -97,7 +97,7 @@ module Discorb
97
97
  #
98
98
  def delete!(reason: nil)
99
99
  Async do
100
- @client.http.delete("/guilds/#{@guild_id}/stickers/#{@id}", audit_log_reason: reason).wait
100
+ @client.http.request(Route.new("/guilds/#{@guild_id}/stickers/#{@id}", "//guilds/:guild_id/stickers/:sticker_id", :delete), audit_log_reason: reason).wait
101
101
  end
102
102
  end
103
103