discorb 0.16.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (199) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +1 -0
  3. data/.github/workflows/build_main.yml +2 -2
  4. data/.github/workflows/build_version.yml +1 -1
  5. data/.github/workflows/codeql-analysis.yml +1 -1
  6. data/.github/workflows/lint-push.yml +3 -5
  7. data/.github/workflows/lint.yml +2 -4
  8. data/.github/workflows/spec.yml +33 -0
  9. data/.github/workflows/validate.yml +21 -0
  10. data/.gitignore +2 -0
  11. data/.lefthook/commit-msg/validator.rb +5 -0
  12. data/.rspec +2 -0
  13. data/.rspec_parallel +2 -0
  14. data/.rubocop.yml +43 -6
  15. data/Changelog.md +30 -1
  16. data/Gemfile +20 -8
  17. data/Rakefile +226 -98
  18. data/Steepfile +28 -0
  19. data/bin/console +3 -3
  20. data/docs/Examples.md +1 -1
  21. data/docs/application_command.md +156 -47
  22. data/docs/cli/irb.md +2 -2
  23. data/docs/cli/new.md +14 -9
  24. data/docs/cli/run.md +7 -11
  25. data/docs/cli.md +17 -10
  26. data/docs/events.md +247 -213
  27. data/docs/extension.md +1 -2
  28. data/docs/faq.md +0 -1
  29. data/docs/tutorial.md +18 -18
  30. data/docs/voice_events.md +106 -106
  31. data/examples/commands/message.rb +68 -0
  32. data/examples/commands/permission.rb +19 -0
  33. data/examples/commands/slash.rb +48 -0
  34. data/examples/commands/user.rb +54 -0
  35. data/examples/components/authorization_button.rb +4 -3
  36. data/examples/components/select_menu.rb +6 -3
  37. data/examples/extension/main.rb +2 -1
  38. data/examples/extension/message_expander.rb +6 -2
  39. data/examples/sig/commands/message.rbs +5 -0
  40. data/examples/simple/eval.rb +3 -2
  41. data/examples/simple/ping_pong.rb +2 -1
  42. data/examples/simple/rolepanel.rb +17 -6
  43. data/examples/simple/shard.rb +3 -2
  44. data/examples/simple/wait_for_message.rb +4 -1
  45. data/exe/discorb +33 -18
  46. data/lefthook.yml +45 -0
  47. data/lib/discorb/allowed_mentions.rb +2 -1
  48. data/lib/discorb/app_command/command.rb +130 -72
  49. data/lib/discorb/app_command/common.rb +25 -0
  50. data/lib/discorb/app_command/handler.rb +130 -33
  51. data/lib/discorb/app_command.rb +2 -1
  52. data/lib/discorb/application.rb +1 -0
  53. data/lib/discorb/asset.rb +1 -2
  54. data/lib/discorb/attachment.rb +1 -1
  55. data/lib/discorb/audit_logs.rb +19 -10
  56. data/lib/discorb/automod.rb +269 -0
  57. data/lib/discorb/channel/base.rb +108 -0
  58. data/lib/discorb/channel/category.rb +32 -0
  59. data/lib/discorb/channel/container.rb +44 -0
  60. data/lib/discorb/channel/dm.rb +28 -0
  61. data/lib/discorb/channel/guild.rb +246 -0
  62. data/lib/discorb/channel/stage.rb +140 -0
  63. data/lib/discorb/channel/text.rb +336 -0
  64. data/lib/discorb/channel/thread.rb +325 -0
  65. data/lib/discorb/channel/voice.rb +79 -0
  66. data/lib/discorb/channel.rb +2 -1165
  67. data/lib/discorb/client.rb +50 -36
  68. data/lib/discorb/color.rb +37 -60
  69. data/lib/discorb/common.rb +2 -1
  70. data/lib/discorb/components/button.rb +2 -1
  71. data/lib/discorb/components/select_menu.rb +4 -2
  72. data/lib/discorb/components/text_input.rb +12 -2
  73. data/lib/discorb/components.rb +1 -1
  74. data/lib/discorb/dictionary.rb +1 -1
  75. data/lib/discorb/embed.rb +26 -10
  76. data/lib/discorb/emoji.rb +31 -4
  77. data/lib/discorb/emoji_table.rb +4969 -3
  78. data/lib/discorb/event.rb +29 -4
  79. data/lib/discorb/exe/about.rb +2 -1
  80. data/lib/discorb/exe/irb.rb +2 -4
  81. data/lib/discorb/exe/new.rb +89 -26
  82. data/lib/discorb/exe/run.rb +8 -22
  83. data/lib/discorb/exe/setup.rb +25 -12
  84. data/lib/discorb/exe/show.rb +4 -3
  85. data/lib/discorb/extend.rb +1 -0
  86. data/lib/discorb/extension.rb +6 -7
  87. data/lib/discorb/flag.rb +13 -2
  88. data/lib/discorb/gateway.rb +79 -589
  89. data/lib/discorb/gateway_events.rb +638 -0
  90. data/lib/discorb/guild.rb +318 -67
  91. data/lib/discorb/guild_template.rb +11 -5
  92. data/lib/discorb/http.rb +53 -24
  93. data/lib/discorb/integration.rb +4 -1
  94. data/lib/discorb/intents.rb +28 -19
  95. data/lib/discorb/interaction/autocomplete.rb +28 -16
  96. data/lib/discorb/interaction/command.rb +42 -14
  97. data/lib/discorb/interaction/components.rb +5 -2
  98. data/lib/discorb/interaction/modal.rb +0 -1
  99. data/lib/discorb/interaction/response.rb +125 -26
  100. data/lib/discorb/interaction/root.rb +13 -13
  101. data/lib/discorb/interaction.rb +1 -0
  102. data/lib/discorb/invite.rb +5 -2
  103. data/lib/discorb/member.rb +28 -8
  104. data/lib/discorb/message.rb +60 -25
  105. data/lib/discorb/message_meta.rb +3 -3
  106. data/lib/discorb/modules.rb +59 -16
  107. data/lib/discorb/presence.rb +2 -0
  108. data/lib/discorb/rate_limit.rb +7 -2
  109. data/lib/discorb/reaction.rb +2 -2
  110. data/lib/discorb/role.rb +20 -5
  111. data/lib/discorb/shard.rb +1 -1
  112. data/lib/discorb/sticker.rb +9 -8
  113. data/lib/discorb/user.rb +4 -3
  114. data/lib/discorb/utils/colored_puts.rb +1 -0
  115. data/lib/discorb/voice_state.rb +6 -2
  116. data/lib/discorb/webhook.rb +64 -31
  117. data/lib/discorb.rb +7 -5
  118. data/po/yard.pot +20 -20
  119. data/rbs_collection.lock.yaml +88 -0
  120. data/rbs_collection.yaml +21 -0
  121. data/sig/async.rbs +11 -0
  122. data/sig/discorb/activity.rbs +23 -0
  123. data/sig/discorb/allowed_mentions.rbs +44 -0
  124. data/sig/discorb/app_command/base.rbs +282 -0
  125. data/sig/discorb/app_command/handler.rbs +171 -0
  126. data/sig/discorb/application.rbs +142 -0
  127. data/sig/discorb/asset.rbs +32 -0
  128. data/sig/discorb/attachment.rbs +91 -0
  129. data/sig/discorb/audit_log.rbs +231 -0
  130. data/sig/discorb/automod.rbs +128 -0
  131. data/sig/discorb/avatar.rbs +26 -0
  132. data/sig/discorb/channel/base.rbs +179 -0
  133. data/sig/discorb/channel/category.rbs +56 -0
  134. data/sig/discorb/channel/container.rbs +29 -0
  135. data/sig/discorb/channel/dm.rbs +14 -0
  136. data/sig/discorb/channel/news.rbs +20 -0
  137. data/sig/discorb/channel/stage.rbs +77 -0
  138. data/sig/discorb/channel/text.rbs +158 -0
  139. data/sig/discorb/channel/thread.rbs +185 -0
  140. data/sig/discorb/channel/voice.rbs +41 -0
  141. data/sig/discorb/client.rbs +2495 -0
  142. data/sig/discorb/color.rbs +142 -0
  143. data/sig/discorb/component/base.rbs +28 -0
  144. data/sig/discorb/component/button.rbs +65 -0
  145. data/sig/discorb/component/select_menu.rbs +107 -0
  146. data/sig/discorb/component/text_input.rbs +69 -0
  147. data/sig/discorb/connectable.rbs +8 -0
  148. data/sig/discorb/custom_emoji.rbs +90 -0
  149. data/sig/discorb/dictionary.rbs +85 -0
  150. data/sig/discorb/discord_model.rbs +15 -0
  151. data/sig/discorb/embed.rbs +279 -0
  152. data/sig/discorb/emoji.rbs +13 -0
  153. data/sig/discorb/error.rbs +73 -0
  154. data/sig/discorb/event_handler.rbs +27 -0
  155. data/sig/discorb/extension.rbs +1734 -0
  156. data/sig/discorb/flag.rbs +72 -0
  157. data/sig/discorb/gateway.rbs +481 -0
  158. data/sig/discorb/guild.rbs +870 -0
  159. data/sig/discorb/guild_template.rbs +174 -0
  160. data/sig/discorb/http.rbs +147 -0
  161. data/sig/discorb/image.rbs +20 -0
  162. data/sig/discorb/integration.rbs +118 -0
  163. data/sig/discorb/intents.rbs +97 -0
  164. data/sig/discorb/interaction/autocomplete.rbs +9 -0
  165. data/sig/discorb/interaction/base.rbs +66 -0
  166. data/sig/discorb/interaction/command.rbs +66 -0
  167. data/sig/discorb/interaction/message_component.rbs +140 -0
  168. data/sig/discorb/interaction/modal.rbs +50 -0
  169. data/sig/discorb/interaction/responder.rbs +157 -0
  170. data/sig/discorb/invite.rbs +86 -0
  171. data/sig/discorb/member.rbs +187 -0
  172. data/sig/discorb/message.rbs +469 -0
  173. data/sig/discorb/messageable.rbs +153 -0
  174. data/sig/discorb/partial_emoji.rbs +35 -0
  175. data/sig/discorb/permissions.rbs +149 -0
  176. data/sig/discorb/presence.rbs +237 -0
  177. data/sig/discorb/reaction.rbs +33 -0
  178. data/sig/discorb/role.rbs +145 -0
  179. data/sig/discorb/scheduled_event.rbs +148 -0
  180. data/sig/discorb/shard.rbs +62 -0
  181. data/sig/discorb/snowflake.rbs +56 -0
  182. data/sig/discorb/stage_instance.rbs +63 -0
  183. data/sig/discorb/sticker.rbs +116 -0
  184. data/sig/discorb/system_channel_flag.rbs +17 -0
  185. data/sig/discorb/unicode_emoji.rbs +49 -0
  186. data/sig/discorb/user.rbs +93 -0
  187. data/sig/discorb/utils.rbs +8 -0
  188. data/sig/discorb/voice_region.rbs +30 -0
  189. data/sig/discorb/voice_state.rbs +71 -0
  190. data/sig/discorb/webhook.rbs +327 -0
  191. data/sig/discorb/welcome_screen.rbs +78 -0
  192. data/sig/discorb.rbs +6 -7230
  193. data/sig/manifest.yaml +3 -0
  194. data/sig/override.rbs +19 -0
  195. data/template-replace/files/css/common.css +4 -0
  196. metadata +102 -6
  197. data/examples/commands/bookmarker.rb +0 -42
  198. data/examples/commands/hello.rb +0 -10
  199. data/examples/commands/inspect.rb +0 -25
@@ -21,8 +21,17 @@ module Discorb
21
21
  #
22
22
  # @return [Async::Task<Discorb::Message>] The message sent.
23
23
  #
24
- def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil,
25
- reference: nil, components: nil, attachment: nil, attachments: nil)
24
+ def post(
25
+ content = nil,
26
+ tts: false,
27
+ embed: nil,
28
+ embeds: nil,
29
+ allowed_mentions: nil,
30
+ reference: nil,
31
+ components: nil,
32
+ attachment: nil,
33
+ attachments: nil
34
+ )
26
35
  Async do
27
36
  payload = {}
28
37
  payload[:content] = content if content
@@ -35,7 +44,7 @@ module Discorb
35
44
  payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
36
45
  payload[:allowed_mentions] =
37
46
  allowed_mentions ? allowed_mentions.to_hash(@client.allowed_mentions) : @client.allowed_mentions.to_hash
38
- payload[:message_reference] = reference.to_reference if reference
47
+ payload[:message_reference] = reference.to_reference.to_hash if reference
39
48
  payload[:components] = Component.to_payload(components) if components
40
49
  attachments ||= attachment ? [attachment] : []
41
50
 
@@ -47,7 +56,10 @@ module Discorb
47
56
  }
48
57
  end
49
58
 
50
- _resp, data = @client.http.multipart_request(Route.new("/channels/#{channel_id.wait}/messages", "//channels/:channel_id/messages", :post), payload, attachments).wait
59
+ _resp, data = @client.http.multipart_request(
60
+ Route.new("/channels/#{channel_id.wait}/messages", "//channels/:channel_id/messages",
61
+ :post), payload, attachments
62
+ ).wait
51
63
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
52
64
  end
53
65
  end
@@ -70,8 +82,16 @@ module Discorb
70
82
  #
71
83
  # @return [Async::Task<void>] The task.
72
84
  #
73
- def edit_message(message_id, content = Discorb::Unset, embed: Discorb::Unset, embeds: Discorb::Unset, allowed_mentions: Discorb::Unset,
74
- attachments: Discorb::Unset, components: Discorb::Unset, supress: Discorb::Unset)
85
+ def edit_message(
86
+ message_id,
87
+ content = Discorb::Unset,
88
+ embed: Discorb::Unset,
89
+ embeds: Discorb::Unset,
90
+ allowed_mentions: Discorb::Unset,
91
+ attachments: Discorb::Unset,
92
+ components: Discorb::Unset,
93
+ supress: Discorb::Unset
94
+ )
75
95
  Async do
76
96
  payload = {}
77
97
  payload[:content] = content if content != Discorb::Unset
@@ -81,8 +101,11 @@ module Discorb
81
101
  embeds
82
102
  end
83
103
  payload[:embeds] = tmp_embed.map(&:to_hash) if tmp_embed
84
- payload[:allowed_mentions] =
85
- allowed_mentions == Discorb::Unset ? @client.allowed_mentions.to_hash : allowed_mentions.to_hash(@client.allowed_mentions)
104
+ payload[:allowed_mentions] = if allowed_mentions == Discorb::Unset
105
+ @client.allowed_mentions.to_hash
106
+ else
107
+ allowed_mentions.to_hash(@client.allowed_mentions)
108
+ end
86
109
  payload[:components] = Component.to_payload(components) if components != Discorb::Unset
87
110
  payload[:flags] = (supress ? 1 << 2 : 0) if supress != Discorb::Unset
88
111
  if attachments != Discorb::Unset
@@ -95,7 +118,8 @@ module Discorb
95
118
  end
96
119
  end
97
120
  @client.http.multipart_request(
98
- Route.new("/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id", :patch),
121
+ Route.new("/channels/#{channel_id.wait}/messages/#{message_id}",
122
+ "//channels/:channel_id/messages/:message_id", :patch),
99
123
  payload,
100
124
  attachments == Discorb::Unset ? [] : attachments
101
125
  ).wait
@@ -113,7 +137,12 @@ module Discorb
113
137
  #
114
138
  def delete_message!(message_id, reason: nil)
115
139
  Async do
116
- @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id", :delete), audit_log_reason: reason).wait
140
+ @client.http.request(
141
+ Route.new(
142
+ "/channels/#{channel_id.wait}/messages/#{message_id}", "//channels/:channel_id/messages/:message_id",
143
+ :delete
144
+ ), {}, audit_log_reason: reason,
145
+ ).wait
117
146
  end
118
147
  end
119
148
 
@@ -130,7 +159,8 @@ module Discorb
130
159
  #
131
160
  def fetch_message(id)
132
161
  Async do
133
- _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{id}", "//channels/:channel_id/messages/:message_id", :get)).wait
162
+ _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages/#{id}",
163
+ "//channels/:channel_id/messages/:message_id", :get)).wait
134
164
  Message.new(@client, data.merge({ guild_id: @guild_id.to_s }))
135
165
  end
136
166
  end
@@ -154,7 +184,12 @@ module Discorb
154
184
  after: Discorb::Utils.try(around, :id),
155
185
  around: Discorb::Utils.try(before, :id),
156
186
  }.filter { |_k, v| !v.nil? }.to_h
157
- _resp, messages = @client.http.request(Route.new("/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages", :get)).wait
187
+ _resp, messages = @client.http.request(
188
+ Route.new(
189
+ "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}", "//channels/:channel_id/messages",
190
+ :get
191
+ )
192
+ ).wait
158
193
  messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
159
194
  end
160
195
  end
@@ -167,7 +202,8 @@ module Discorb
167
202
  #
168
203
  def fetch_pins
169
204
  Async do
170
- _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/pins", "//channels/:channel_id/pins", :get)).wait
205
+ _resp, data = @client.http.request(Route.new("/channels/#{channel_id.wait}/pins",
206
+ "//channels/:channel_id/pins", :get)).wait
171
207
  data.map { |pin| Message.new(@client, pin) }
172
208
  end
173
209
  end
@@ -183,7 +219,10 @@ module Discorb
183
219
  #
184
220
  def pin_message(message, reason: nil)
185
221
  Async do
186
- @client.http.request(Route.new("/channels/#{channel_id.wait}/pins/#{message.id}", "//channels/:channel_id/pins/:message_id", :put), {}, audit_log_reason: reason).wait
222
+ @client.http.request(
223
+ Route.new("/channels/#{channel_id.wait}/pins/#{message.id}", "//channels/:channel_id/pins/:message_id",
224
+ :put), {}, audit_log_reason: reason,
225
+ ).wait
187
226
  end
188
227
  end
189
228
 
@@ -198,7 +237,10 @@ module Discorb
198
237
  #
199
238
  def unpin_message(message, reason: nil)
200
239
  Async do
201
- @client.http.request(Route.new("/channels/#{channel_id.wait}/pins/#{message.id}", "//channels/:channel_id/pins/:message_id", :delete), audit_log_reason: reason).wait
240
+ @client.http.request(
241
+ Route.new("/channels/#{channel_id.wait}/pins/#{message.id}", "//channels/:channel_id/pins/:message_id",
242
+ :delete), {}, audit_log_reason: reason,
243
+ ).wait
202
244
  end
203
245
  end
204
246
 
@@ -223,10 +265,11 @@ module Discorb
223
265
  sleep(5)
224
266
  end
225
267
  end
226
- yield
268
+ ret = yield
227
269
  ensure
228
270
  post_task.stop
229
271
  end
272
+ ret
230
273
  else
231
274
  Async do |_task|
232
275
  @client.http.request(Route.new("/channels/#{@id}/typing", "//channels/:channel_id/typing", :post), {})
@@ -141,6 +141,8 @@ module Discorb
141
141
  "#{@emoji} #{@state}"
142
142
  when :competing
143
143
  "Competing in #{@name}"
144
+ else
145
+ raise "Unknown activity type: #{@type}"
144
146
  end
145
147
  end
146
148
 
@@ -48,7 +48,8 @@ module Discorb
48
48
  return if (bucket[:remaining]).positive?
49
49
 
50
50
  time = bucket[:reset_at] - Time.now.to_f
51
- @client.logger.info("rate limit for #{path.identifier} with #{path.major_param} reached, waiting #{time.round(4)} seconds")
51
+ @client.logger.info("rate limit for #{path.identifier} with #{path.major_param} reached, " \
52
+ "waiting #{time.round(4)} seconds")
52
53
  sleep(time)
53
54
  end
54
55
 
@@ -59,8 +60,12 @@ module Discorb
59
60
  # @param [Net::HTTPResponse] resp The response.
60
61
  #
61
62
  def save(path, resp)
62
- @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after] if resp["X-Ratelimit-Global"] == "true"
63
+ if resp["X-Ratelimit-Global"] == "true"
64
+ @global = Time.now.to_f + JSON.parse(resp.body,
65
+ symbolize_names: true)[:retry_after]
66
+ end
63
67
  return unless resp["X-RateLimit-Remaining"]
68
+
64
69
  @path_ratelimit_hash[path.identifier] = resp["X-Ratelimit-Bucket"]
65
70
  @path_ratelimit_bucket[resp["X-Ratelimit-Bucket"] + path.major_param] = {
66
71
  remaining: resp["X-RateLimit-Remaining"].to_i,
@@ -7,7 +7,7 @@ module Discorb
7
7
  class Reaction < DiscordModel
8
8
  # @return [Integer] The number of users that have reacted with this emoji.
9
9
  attr_reader :count
10
- # @return [Discorb::Emoji] The emoji that was reacted with.
10
+ # @return [Discorb::Emoji, Discorb::PartialEmoji] The emoji that was reacted with.
11
11
  attr_reader :emoji
12
12
  # @return [Discorb::Message] The message that this reaction is on.
13
13
  attr_reader :message
@@ -33,7 +33,7 @@ module Discorb
33
33
  #
34
34
  # @param (see Message#fetch_reacted_users)
35
35
  #
36
- # @return [Array<Discorb::User>] The users that reacted with this emoji.
36
+ # @return [Async::Task<Array<Discorb::User>>] The users that reacted with this emoji.
37
37
  #
38
38
  def fetch_users(...)
39
39
  message.fetch_reacted_users(@emoji, ...)
data/lib/discorb/role.rb CHANGED
@@ -69,7 +69,7 @@ module Discorb
69
69
  # @return [Integer] -1 if the other role is higher, 0 if they are equal, 1 if the other role is lower.
70
70
  #
71
71
  def <=>(other)
72
- return false unless other.is_a?(Role)
72
+ return nil unless other.is_a?(Role)
73
73
 
74
74
  @position <=> other.position
75
75
  end
@@ -106,7 +106,8 @@ module Discorb
106
106
  #
107
107
  def move(position, reason: nil)
108
108
  Async do
109
- @client.http.request(Route.new("/guilds/#{@guild.id}/roles", "//guilds/:guild_id/roles", :patch), { id: @id, position: position }, audit_log_reason: reason).wait
109
+ @client.http.request(Route.new("/guilds/#{@guild.id}/roles", "//guilds/:guild_id/roles", :patch),
110
+ { id: @id, position: position }, audit_log_reason: reason).wait
110
111
  end
111
112
  end
112
113
 
@@ -125,7 +126,15 @@ module Discorb
125
126
  #
126
127
  # @return [Async::Task<void>] The task.
127
128
  #
128
- def edit(name: Discorb::Unset, position: Discorb::Unset, color: Discorb::Unset, hoist: Discorb::Unset, mentionable: Discorb::Unset, icon: Discorb::Unset, reason: nil)
129
+ def edit(
130
+ name: Discorb::Unset,
131
+ position: Discorb::Unset,
132
+ color: Discorb::Unset,
133
+ hoist: Discorb::Unset,
134
+ mentionable: Discorb::Unset,
135
+ icon: Discorb::Unset,
136
+ reason: nil
137
+ )
129
138
  Async do
130
139
  payload = {}
131
140
  payload[:name] = name if name != Discorb::Unset
@@ -140,7 +149,10 @@ module Discorb
140
149
  payload[:unicode_emoji] = icon.to_s
141
150
  end
142
151
  end
143
- @client.http.request(Route.new("/guilds/#{@guild.id}/roles/#{@id}", "//guilds/:guild_id/roles/:role_id", :patch), payload, audit_log_reason: reason).wait
152
+ @client.http.request(
153
+ Route.new("/guilds/#{@guild.id}/roles/#{@id}", "//guilds/:guild_id/roles/:role_id",
154
+ :patch), payload, audit_log_reason: reason,
155
+ ).wait
144
156
  end
145
157
  end
146
158
 
@@ -155,7 +167,10 @@ module Discorb
155
167
  #
156
168
  def delete!(reason: nil)
157
169
  Async do
158
- @client.http.request(Route.new("/guilds/#{@guild.id}/roles/#{@id}", "//guilds/:guild_id/roles/:role_id", :delete), audit_log_reason: reason).wait
170
+ @client.http.request(
171
+ Route.new("/guilds/#{@guild.id}/roles/#{@id}", "//guilds/:guild_id/roles/:role_id",
172
+ :delete), {}, audit_log_reason: reason,
173
+ ).wait
159
174
  end
160
175
  end
161
176
 
data/lib/discorb/shard.rb CHANGED
@@ -41,7 +41,7 @@ module Discorb
41
41
  Thread.current.thread_variable_set("shard", self)
42
42
  if @index.positive?
43
43
  Thread.stop
44
- sleep 5 # Somehow discord disconnects the shard without a little sleep.
44
+ sleep 5 # Somehow discord disconnects the shard without a little sleep.
45
45
  end
46
46
  client.send(:main_loop, id)
47
47
  end
@@ -17,7 +17,7 @@ module Discorb
17
17
  attr_reader :format
18
18
  # @return [String] The URL of the sticker.
19
19
  attr_reader :description
20
- # @return [Discorb::Sticker] The ID of the sticker pack.
20
+ # @return [Discorb::Snowflake] The ID of the sticker pack.
21
21
  attr_reader :pack_id
22
22
  # @return [Integer] The sort value of the sticker.
23
23
  attr_reader :sort_value
@@ -85,7 +85,10 @@ module Discorb
85
85
  payload[:name] = name unless name == Discorb::Unset
86
86
  payload[:description] = description unless description == Discorb::Unset
87
87
  payload[:tags] = tag.name unless tag == Discorb::Unset
88
- @client.http.request(Route.new("/guilds/#{@guild_id}/stickers/#{@id}", "//guilds/:guild_id/stickers/:sticker_id", :patch), payload, audit_log_reason: reason).wait
88
+ @client.http.request(
89
+ Route.new("/guilds/#{@guild_id}/stickers/#{@id}", "//guilds/:guild_id/stickers/:sticker_id",
90
+ :patch), payload, audit_log_reason: reason,
91
+ ).wait
89
92
  end
90
93
  end
91
94
 
@@ -99,7 +102,10 @@ module Discorb
99
102
  #
100
103
  def delete!(reason: nil)
101
104
  Async do
102
- @client.http.request(Route.new("/guilds/#{@guild_id}/stickers/#{@id}", "//guilds/:guild_id/stickers/:sticker_id", :delete), audit_log_reason: reason).wait
105
+ @client.http.request(
106
+ Route.new("/guilds/#{@guild_id}/stickers/#{@id}", "//guilds/:guild_id/stickers/:sticker_id",
107
+ :delete), {}, audit_log_reason: reason,
108
+ ).wait
103
109
  end
104
110
  end
105
111
 
@@ -161,10 +167,5 @@ module Discorb
161
167
  @user = data[:user] && (@client.users[data[:user][:id]] || User.new(@client, data[:user]))
162
168
  end
163
169
  end
164
-
165
- class << self
166
- # @private
167
- attr_reader :sticker_type, :sticker_format
168
- end
169
170
  end
170
171
  end
data/lib/discorb/user.rb CHANGED
@@ -16,7 +16,7 @@ module Discorb
16
16
  attr_reader :flag
17
17
  # @return [String] The user's discriminator.
18
18
  attr_reader :discriminator
19
- # @return [Discorb::Asset] The user's avatar.
19
+ # @return [Discorb::Asset, Discorb::DefaultAvatar] The user's avatar.
20
20
  attr_reader :avatar
21
21
  # @return [Boolean] Whether the user is a bot.
22
22
  attr_reader :bot
@@ -95,7 +95,8 @@ module Discorb
95
95
  Async do
96
96
  next @dm_channel_id if @dm_channel_id
97
97
 
98
- _resp, dm_channel = @client.http.request(Route.new("/users/@me/channels", "//users/@me/channels", :post), { recipient_id: @id }).wait
98
+ _resp, dm_channel = @client.http.request(Route.new("/users/@me/channels", "//users/@me/channels", :post),
99
+ { recipient_id: @id }).wait
99
100
  @dm_channel_id = dm_channel[:id]
100
101
  @dm_channel_id
101
102
  end
@@ -146,7 +147,7 @@ module Discorb
146
147
  @flag = User::Flag.new(data[:public_flags] | (data[:flags] || 0))
147
148
  @discriminator = data[:discriminator]
148
149
  @avatar = data[:avatar] ? Asset.new(self, data[:avatar]) : DefaultAvatar.new(data[:discriminator])
149
- @bot = data[:bot]
150
+ @bot = data[:bot] || false
150
151
  @raw_data = data
151
152
  @client.users[@id] = self unless data[:no_cache]
152
153
  @created_at = @id.timestamp
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  # @private
3
4
  def sputs(text)
4
5
  puts "\e[92m#{text}\e[m"
@@ -197,7 +197,8 @@ module Discorb
197
197
  payload[:topic] = topic if topic != Discorb::Unset
198
198
  payload[:privacy_level] = PRIVACY_LEVEL.key(privacy_level) if privacy_level != Discorb::Unset
199
199
  @client.http.request(
200
- Route.new("/stage-instances/#{@channel_id}", "//stage-instances/:channel_id", :patch), payload, audit_log_reason: reason,
200
+ Route.new("/stage-instances/#{@channel_id}", "//stage-instances/:channel_id",
201
+ :patch), payload, audit_log_reason: reason,
201
202
  ).wait
202
203
  self
203
204
  end
@@ -214,7 +215,10 @@ module Discorb
214
215
  #
215
216
  def delete!(reason: nil)
216
217
  Async do
217
- @client.http.request(Route.new("/stage-instances/#{@channel_id}", "//stage-instances/:stage_instance_id", :delete), audit_log_reason: reason).wait
218
+ @client.http.request(
219
+ Route.new("/stage-instances/#{@channel_id}", "//stage-instances/:stage_instance_id",
220
+ :delete), {}, audit_log_reason: reason,
221
+ ).wait
218
222
  self
219
223
  end
220
224
  end
@@ -56,8 +56,8 @@ module Discorb
56
56
  # @param [Discorb::Embed] embed The embed to send.
57
57
  # @param [Array<Discorb::Embed>] embeds The embeds to send.
58
58
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
59
- # @param [Discorb::File] file The file to send.
60
- # @param [Array<Discorb::File>] files The files to send.
59
+ # @param [Discorb::Attachment] attachment The attachment to send.
60
+ # @param [Array<Discorb::Attachment>] attachment The attachments to send.
61
61
  # @param [String] username The username of the message.
62
62
  # @param [String] avatar_url The avatar URL of the message.
63
63
  # @param [Boolean] wait Whether to wait for the message to be sent.
@@ -65,8 +65,18 @@ module Discorb
65
65
  # @return [Discorb::Webhook::Message] The message that was sent.
66
66
  # @return [Async::Task<nil>] If `wait` is false.
67
67
  #
68
- def post(content = nil, tts: false, embed: nil, embeds: nil, allowed_mentions: nil,
69
- file: nil, files: nil, username: nil, avatar_url: Discorb::Unset, wait: true)
68
+ def post(
69
+ content = nil,
70
+ tts: false,
71
+ embed: nil,
72
+ embeds: nil,
73
+ allowed_mentions: nil,
74
+ attachment: nil,
75
+ attachments: nil,
76
+ username: nil,
77
+ avatar_url: Discorb::Unset,
78
+ wait: true
79
+ )
70
80
  Async do
71
81
  payload = {}
72
82
  payload[:content] = content if content
@@ -80,8 +90,16 @@ module Discorb
80
90
  payload[:allowed_mentions] = allowed_mentions&.to_hash
81
91
  payload[:username] = username if username
82
92
  payload[:avatar_url] = avatar_url if avatar_url != Discorb::Unset
83
- files = [file]
84
- _resp, data = @http.multipart_request(Route.new("#{url}?wait=#{wait}", "//webhooks/:webhook_id/:token", :post), files, payload, headers: headers).wait
93
+ attachments = [attachment] if attachment
94
+ _resp, data = @http.multipart_request(
95
+ Route.new(
96
+ "#{url}?wait=#{wait}",
97
+ "//webhooks/:webhook_id/:token",
98
+ :post
99
+ ),
100
+ attachments,
101
+ payload
102
+ ).wait
85
103
  data && Webhook::Message.new(self, data)
86
104
  end
87
105
  end
@@ -136,8 +154,8 @@ module Discorb
136
154
  # @param [Discorb::Embed] embed The new embed of the message.
137
155
  # @param [Array<Discorb::Embed>] embeds The new embeds of the message.
138
156
  # @param [Array<Discorb::Attachment>] attachments The attachments to remain.
139
- # @param [Discorb::File] file The file to send.
140
- # @param [Array<Discorb::File>] files The files to send.
157
+ # @param [Discorb::Attachment] file The file to send.
158
+ # @param [Array<Discorb::Attachment>] files The files to send.
141
159
  # @param [Discorb::AllowedMentions] allowed_mentions The allowed mentions to send.
142
160
  #
143
161
  # @return [Async::Task<void>] The task.
@@ -157,7 +175,10 @@ module Discorb
157
175
  payload[:attachments] = attachments.map(&:to_hash) if attachments != Discorb::Unset
158
176
  payload[:allowed_mentions] = allowed_mentions if allowed_mentions != Discorb::Unset
159
177
  files = [file] if file != Discorb::Unset
160
- _resp, data = @http.multipart_request(Route.new("#{url}/messages/#{Utils.try(message, :id)}", "//webhooks/:webhook_id/:token/messages/:message_id", :patch), payload, files).wait
178
+ _resp, data = @http.multipart_request(
179
+ Route.new("#{url}/messages/#{Utils.try(message, :id)}", "//webhooks/:webhook_id/:token/messages/:message_id",
180
+ :patch), payload, files
181
+ ).wait
161
182
  message.send(:_set_data, data)
162
183
  message
163
184
  end
@@ -172,10 +193,12 @@ module Discorb
172
193
  #
173
194
  def delete_message!(message)
174
195
  Async do
175
- @http.request(Route.new(
176
- "#{url}/messages/#{Utils.try(message, :id)}",
177
- "//webhooks/:webhook_id/:token/messages/:message_id", :delete
178
- )).wait
196
+ @http.request(
197
+ Route.new(
198
+ "#{url}/messages/#{Utils.try(message, :id)}",
199
+ "//webhooks/:webhook_id/:token/messages/:message_id", :delete
200
+ )
201
+ ).wait
179
202
  message
180
203
  end
181
204
  end
@@ -191,11 +214,12 @@ module Discorb
191
214
  # Initializes the webhook from URL.
192
215
  #
193
216
  # @param [String] url The URL of the webhook.
217
+ # @param [Discorb::Client] client The client to associate with the webhook.
194
218
  #
195
- def initialize(url)
219
+ def initialize(url, client: nil)
196
220
  @url = url
197
221
  @token = ""
198
- @http = Discorb::HTTP.new(self)
222
+ @http = Discorb::HTTP.new(client || Discorb::Client.new)
199
223
  end
200
224
  end
201
225
 
@@ -443,29 +467,38 @@ module Discorb
443
467
  # Creates URLWebhook.
444
468
  #
445
469
  # @param [String] url The URL of the webhook.
470
+ # @param [Discorb::Client] client The client to associate with the webhook.
446
471
  #
447
472
  # @return [Discorb::Webhook::URLWebhook] The URLWebhook.
448
473
  #
449
- def new(url)
450
- if self != Webhook
451
- return super(*url) if url.is_a?(Array)
452
-
453
- return super
454
- end
455
- if url.is_a?(String)
456
- URLWebhook.new(url)
474
+ def new(url, client: nil)
475
+ if self == Webhook
476
+ URLWebhook.new(url, client: client)
457
477
  else
458
- case url[1][:type]
459
- when 1
460
- IncomingWebhook
461
- when 2
462
- FollowerWebhook
463
- when 3
464
- ApplicationWebhook
465
- end.new(url)
478
+ super
466
479
  end
467
480
  end
468
481
 
482
+ #
483
+ # Creates Webhook with discord data.
484
+ # @private
485
+ #
486
+ # @param [Discorb::Client] client The client.
487
+ # @param [Hash] data The data of the webhook.
488
+ #
489
+ # @return [Discorb::Webhook] The Webhook.
490
+ #
491
+ def from_data(client, data)
492
+ case data[:type]
493
+ when 1
494
+ IncomingWebhook
495
+ when 2
496
+ FollowerWebhook
497
+ when 3
498
+ ApplicationWebhook
499
+ end.new(client, data)
500
+ end
501
+
469
502
  def from_url(url)
470
503
  URLWebhook.new(url)
471
504
  end
data/lib/discorb.rb CHANGED
@@ -9,7 +9,8 @@ module Discorb
9
9
  # @private
10
10
  #
11
11
  # @!macro [new] async
12
- # @note This is an asynchronous method, it will return a `Async::Task` object. Use `Async::Task#wait` to get the result.
12
+ # @note This is an asynchronous method, it will return a `Async::Task` object.
13
+ # Use `Async::Task#wait` to get the result.
13
14
  #
14
15
  # @!macro [new] client_cache
15
16
  # @note This method returns an object from client cache. it will return `nil` if the object is not in cache.
@@ -19,7 +20,8 @@ module Discorb
19
20
  # @note You must enable `GUILD_MEMBERS` intent to use this method.
20
21
  #
21
22
  # @!macro edit
22
- # @note The arguments of this method are defaultly set to `Discorb::Unset`. Specify value to set the value, if not don't specify or specify `Discorb::Unset`.
23
+ # @note The arguments of this method are defaultly set to `Discorb::Unset`.
24
+ # Specify value to set the value, if not don't specify or specify `Discorb::Unset`.
23
25
  #
24
26
  # @!macro http
25
27
  # @note This method calls HTTP request.
@@ -42,12 +44,12 @@ module Discorb
42
44
  end
43
45
 
44
46
  require_order = %w[common flag dictionary error rate_limit http intents emoji_table modules] +
45
- %w[message_meta allowed_mentions] +
47
+ %w[channel/container message_meta allowed_mentions] +
46
48
  %w[user member guild emoji channel embed message] +
47
- %w[application audit_logs color components event event_handler] +
49
+ %w[application audit_logs color components event event_handler automod] +
48
50
  %w[attachment guild_template image integration interaction invite permission] +
49
51
  %w[presence reaction role sticker utils voice_state webhook] +
50
- %w[gateway_requests gateway app_command] +
52
+ %w[gateway_requests gateway_events gateway app_command] +
51
53
  %w[asset extension shard client extend]
52
54
  require_order.each do |name|
53
55
  require_relative "discorb/#{name}.rb"