discorb 0.17.1 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/lint.yml +1 -3
  3. data/.github/workflows/validate.yml +21 -0
  4. data/Changelog.md +25 -0
  5. data/Gemfile +6 -0
  6. data/README.md +2 -1
  7. data/Rakefile +205 -98
  8. data/Steepfile +30 -0
  9. data/docs/application_command.md +1 -0
  10. data/docs/events.md +44 -8
  11. data/docs/tutorial.md +7 -7
  12. data/docs/voice_events.md +8 -8
  13. data/examples/commands/message.rb +12 -7
  14. data/examples/commands/permission.rb +2 -1
  15. data/examples/commands/slash.rb +23 -19
  16. data/examples/commands/user.rb +15 -12
  17. data/examples/components/authorization_button.rb +2 -1
  18. data/examples/components/select_menu.rb +4 -1
  19. data/examples/extension/main.rb +1 -0
  20. data/examples/extension/message_expander.rb +1 -0
  21. data/examples/sig/commands/message.rbs +5 -0
  22. data/examples/simple/eval.rb +1 -0
  23. data/examples/simple/ping_pong.rb +1 -0
  24. data/examples/simple/rolepanel.rb +16 -5
  25. data/examples/simple/shard.rb +2 -1
  26. data/examples/simple/wait_for_message.rb +3 -0
  27. data/exe/discorb +3 -3
  28. data/lib/discorb/allowed_mentions.rb +1 -1
  29. data/lib/discorb/app_command/command.rb +16 -13
  30. data/lib/discorb/app_command/handler.rb +21 -6
  31. data/lib/discorb/audit_logs.rb +6 -2
  32. data/lib/discorb/automod.rb +269 -0
  33. data/lib/discorb/channel/guild.rb +5 -4
  34. data/lib/discorb/channel/stage.rb +1 -1
  35. data/lib/discorb/channel/text.rb +14 -23
  36. data/lib/discorb/channel/thread.rb +15 -11
  37. data/lib/discorb/client.rb +15 -15
  38. data/lib/discorb/color.rb +37 -60
  39. data/lib/discorb/common.rb +1 -1
  40. data/lib/discorb/dictionary.rb +1 -1
  41. data/lib/discorb/embed.rb +5 -4
  42. data/lib/discorb/emoji.rb +4 -4
  43. data/lib/discorb/event.rb +2 -2
  44. data/lib/discorb/exe/about.rb +1 -1
  45. data/lib/discorb/exe/new.rb +1 -5
  46. data/lib/discorb/extension.rb +0 -4
  47. data/lib/discorb/flag.rb +2 -2
  48. data/lib/discorb/gateway.rb +38 -589
  49. data/lib/discorb/gateway_events.rb +638 -0
  50. data/lib/discorb/guild.rb +138 -19
  51. data/lib/discorb/guild_template.rb +3 -3
  52. data/lib/discorb/http.rb +47 -25
  53. data/lib/discorb/integration.rb +2 -2
  54. data/lib/discorb/intents.rb +27 -18
  55. data/lib/discorb/interaction/command.rb +14 -10
  56. data/lib/discorb/interaction/response.rb +150 -84
  57. data/lib/discorb/interaction/root.rb +139 -0
  58. data/lib/discorb/invite.rb +1 -1
  59. data/lib/discorb/member.rb +3 -3
  60. data/lib/discorb/message.rb +17 -15
  61. data/lib/discorb/message_meta.rb +2 -3
  62. data/lib/discorb/modules.rb +5 -4
  63. data/lib/discorb/permission.rb +3 -1
  64. data/lib/discorb/presence.rb +4 -2
  65. data/lib/discorb/reaction.rb +2 -2
  66. data/lib/discorb/role.rb +3 -3
  67. data/lib/discorb/shard.rb +1 -1
  68. data/lib/discorb/sticker.rb +5 -5
  69. data/lib/discorb/user.rb +2 -2
  70. data/lib/discorb/voice_state.rb +8 -8
  71. data/lib/discorb/webhook.rb +20 -11
  72. data/lib/discorb.rb +2 -2
  73. data/rbs_collection.lock.yaml +88 -96
  74. data/rbs_collection.yaml +21 -17
  75. data/sig/async.rbs +11 -5
  76. data/sig/discorb/activity.rbs +24 -0
  77. data/sig/discorb/allowed_mentions.rbs +45 -0
  78. data/sig/discorb/app_command/base.rbs +288 -0
  79. data/sig/discorb/app_command/handler.rbs +171 -0
  80. data/sig/discorb/application.rbs +146 -0
  81. data/sig/discorb/asset.rbs +34 -0
  82. data/sig/discorb/attachment.rbs +99 -0
  83. data/sig/discorb/audit_log.rbs +238 -0
  84. data/sig/discorb/automod.rbs +145 -0
  85. data/sig/discorb/avatar.rbs +27 -0
  86. data/sig/discorb/channel/base.rbs +186 -0
  87. data/sig/discorb/channel/category.rbs +57 -0
  88. data/sig/discorb/channel/container.rbs +33 -0
  89. data/sig/discorb/channel/dm.rbs +14 -0
  90. data/sig/discorb/channel/news.rbs +20 -0
  91. data/sig/discorb/channel/stage.rbs +81 -0
  92. data/sig/discorb/channel/text.rbs +158 -0
  93. data/sig/discorb/channel/thread.rbs +196 -0
  94. data/sig/discorb/channel/voice.rbs +43 -0
  95. data/sig/discorb/client.rbs +2496 -0
  96. data/sig/discorb/color.rbs +148 -0
  97. data/sig/discorb/component/base.rbs +29 -0
  98. data/sig/discorb/component/button.rbs +67 -0
  99. data/sig/discorb/component/select_menu.rbs +111 -0
  100. data/sig/discorb/component/text_input.rbs +70 -0
  101. data/sig/discorb/connectable.rbs +8 -0
  102. data/sig/discorb/custom_emoji.rbs +94 -0
  103. data/sig/discorb/dictionary.rbs +87 -0
  104. data/sig/discorb/discord_model.rbs +17 -0
  105. data/sig/discorb/embed.rbs +286 -0
  106. data/sig/discorb/emoji.rbs +14 -0
  107. data/sig/discorb/error.rbs +73 -0
  108. data/sig/discorb/event_handler.rbs +28 -0
  109. data/sig/discorb/extension.rbs +1735 -0
  110. data/sig/discorb/flag.rbs +74 -0
  111. data/sig/discorb/gateway.rbs +486 -0
  112. data/sig/discorb/guild.rbs +872 -0
  113. data/sig/discorb/guild_template.rbs +174 -0
  114. data/sig/discorb/http.rbs +150 -0
  115. data/sig/discorb/image.rbs +22 -0
  116. data/sig/discorb/integration.rbs +118 -0
  117. data/sig/discorb/intents.rbs +98 -0
  118. data/sig/discorb/interaction/autocomplete.rbs +9 -0
  119. data/sig/discorb/interaction/base.rbs +102 -0
  120. data/sig/discorb/interaction/command.rbs +66 -0
  121. data/sig/discorb/interaction/message_component.rbs +139 -0
  122. data/sig/discorb/interaction/modal.rbs +49 -0
  123. data/sig/discorb/interaction/responder.rbs +157 -0
  124. data/sig/discorb/invite.rbs +86 -0
  125. data/sig/discorb/member.rbs +189 -0
  126. data/sig/discorb/message.rbs +474 -0
  127. data/sig/discorb/messageable.rbs +150 -0
  128. data/sig/discorb/partial_emoji.rbs +38 -0
  129. data/sig/discorb/permissions.rbs +156 -0
  130. data/sig/discorb/presence.rbs +239 -0
  131. data/sig/discorb/reaction.rbs +37 -0
  132. data/sig/discorb/role.rbs +151 -0
  133. data/sig/discorb/scheduled_event.rbs +149 -0
  134. data/sig/discorb/shard.rbs +63 -0
  135. data/sig/discorb/snowflake.rbs +58 -0
  136. data/sig/discorb/stage_instance.rbs +69 -0
  137. data/sig/discorb/sticker.rbs +116 -0
  138. data/sig/discorb/system_channel_flag.rbs +17 -0
  139. data/sig/discorb/unicode_emoji.rbs +53 -0
  140. data/sig/discorb/user.rbs +95 -0
  141. data/sig/discorb/utils.rbs +8 -0
  142. data/sig/discorb/voice_region.rbs +30 -0
  143. data/sig/discorb/voice_state.rbs +71 -0
  144. data/sig/discorb/webhook.rbs +338 -0
  145. data/sig/discorb/welcome_screen.rbs +79 -0
  146. data/sig/discorb.rbs +5 -8661
  147. data/sig/manifest.yaml +3 -0
  148. data/sig/override.rbs +21 -0
  149. metadata +80 -3
data/lib/discorb/guild.rb CHANGED
@@ -179,7 +179,7 @@ module Discorb
179
179
  #
180
180
  # @return [Async::Task<void>] The task.
181
181
  #
182
- def leave!
182
+ def leave
183
183
  Async do
184
184
  @client.http.request(Route.new("/users/@me/guilds/#{@id}", "//users/@me/guilds/:guild_id", :delete)).wait
185
185
  @client.guilds.delete(@id)
@@ -226,9 +226,6 @@ module Discorb
226
226
  :get
227
227
  )
228
228
  ).wait
229
- rescue Discorb::NotFoundError
230
- return nil
231
- else
232
229
  ScheduledEvent.new(@client, event)
233
230
  end
234
231
  end
@@ -343,9 +340,16 @@ module Discorb
343
340
  # @return [Async::Task<Discorb::CustomEmoji>] The emoji with the given id.
344
341
  #
345
342
  def fetch_emoji(id)
346
- _resp, data = @client.http.request(Route.new("/guilds/#{@id}/emojis/#{id}",
347
- "//guilds/:guild_id/emojis/:emoji_id", :get)).wait
348
- @emojis[e[:id]] = CustomEmoji.new(@client, self, data)
343
+ Async do
344
+ _resp, data = @client.http.request(
345
+ Route.new(
346
+ "/guilds/#{@id}/emojis/#{id}",
347
+ "//guilds/:guild_id/emojis/:emoji_id",
348
+ :get
349
+ )
350
+ ).wait
351
+ @emojis[e[:id]] = CustomEmoji.new(@client, self, data)
352
+ end
349
353
  end
350
354
 
351
355
  #
@@ -359,13 +363,17 @@ module Discorb
359
363
  # @return [Async::Task<Discorb::CustomEmoji>] The created emoji.
360
364
  #
361
365
  def create_emoji(name, image, roles: [])
362
- _resp, data = @client.http.request(Route.new("/guilds/#{@id}/emojis", "//guilds/:guild_id/emojis", :post),
363
- {
364
- name: name,
365
- image: image.to_s,
366
- roles: roles.map { |r| Discorb::Utils.try(r, :id) },
367
- }).wait
368
- @emojis[data[:id]] = CustomEmoji.new(@client, self, data)
366
+ Async do
367
+ _resp, data = @client.http.request(
368
+ Route.new("/guilds/#{@id}/emojis", "//guilds/:guild_id/emojis", :post),
369
+ {
370
+ name: name,
371
+ image: image.to_s,
372
+ roles: roles.map { |r| Discorb::Utils.try(r, :id) },
373
+ }
374
+ ).wait
375
+ @emojis[data[:id]] = CustomEmoji.new(@client, self, data)
376
+ end
369
377
  end
370
378
 
371
379
  #
@@ -378,7 +386,7 @@ module Discorb
378
386
  Async do
379
387
  _resp, data = @client.http.request(Route.new("/guilds/#{@id}/webhooks", "//guilds/:guild_id/webhooks",
380
388
  :get)).wait
381
- data.map { |webhook| Webhook.new([@client, webhook]) }
389
+ data.map { |webhook| Webhook.from_data(@client, webhook) }
382
390
  end
383
391
  end
384
392
 
@@ -698,7 +706,7 @@ module Discorb
698
706
  "//guilds/:guild_id/members", :get)).wait
699
707
  ret += data.map { |m| Member.new(@client, @id, m[:user], m) }
700
708
  after = data.last[:user][:id]
701
- break if data.length != 1000
709
+ break if data.length != 100
702
710
  end
703
711
  ret
704
712
  end
@@ -1122,6 +1130,100 @@ module Discorb
1122
1130
  end
1123
1131
  end
1124
1132
 
1133
+ #
1134
+ # Fetch the automod rules in the guild.
1135
+ # @async
1136
+ #
1137
+ # @return [Async::Task<Array<Discorb::AutoModRule>>] The automod rules.
1138
+ #
1139
+ def fetch_automod_rules
1140
+ Async do
1141
+ _resp, data = @client.http.request(
1142
+ Route.new("/guilds/#{@id}/auto-moderation/rules", "//guilds/:guild_id/auto-moderation/rules", :get)
1143
+ )
1144
+ data.map { |d| AutoModRule.new(@client, d) }
1145
+ end
1146
+ end
1147
+
1148
+ alias fetch_automod_rule_list fetch_automod_rules
1149
+
1150
+ #
1151
+ # Fetch the automod rule by ID.
1152
+ #
1153
+ # @param [#to_s] id The ID of the automod rule.
1154
+ #
1155
+ # @return [Async::Task<Array<Discord::AutoModRule>>] The automod rule.
1156
+ #
1157
+ def fetch_automod_rule(id)
1158
+ Async do
1159
+ _resp, data = @client.http.request(
1160
+ Route.new(
1161
+ "/guilds/#{@id}/auto-moderation/rules/#{id}",
1162
+ "//guilds/:guild_id/auto-moderation/rules/:rule_id",
1163
+ :get
1164
+ )
1165
+ ).wait
1166
+ AutoModRule.new(@client, data)
1167
+ end
1168
+ end
1169
+
1170
+ #
1171
+ # Create a new automod rule in the guild.
1172
+ # @async
1173
+ #
1174
+ # @param [String] name The name of the rule.
1175
+ # @param [Symbol] trigger_type The trigger type of the rule. See {Discorb::AutoModRule::TRIGGER_TYPES}.
1176
+ # @param [Array<Discorb::AutoModRule::Action>] actions The actions of the rule.
1177
+ # @param [Symbol] event_type The event type of the rule. See {Discorb::AutoModRule::EVENT_TYPES}.
1178
+ # @param [Boolean] enabled Whether the rule is enabled or not.
1179
+ # @param [Array<Discorb::Role>] exempt_roles The roles that are exempt from the rule.
1180
+ # @param [Array<Discorb::Channel>] exempt_channels The channels that are exempt from the rule.
1181
+ # @param [Array<String>] keyword_filter The keywords to filter.
1182
+ # @param [Symbol] presets The preset of the rule. See {Discorb::AutoModRule::PRESET_TYPES}.
1183
+ # @param [String] reason The reason for creating the rule.
1184
+ #
1185
+ # @return [Async::Task<Discorb::AutoModRule>] The automod rule.
1186
+ #
1187
+ def create_automod_rule(
1188
+ name,
1189
+ trigger_type,
1190
+ actions,
1191
+ event_type = :message_send,
1192
+ enabled: false,
1193
+ exempt_roles: [],
1194
+ exempt_channels: [],
1195
+ keyword_filter: nil,
1196
+ presets: nil,
1197
+ reason: nil
1198
+ )
1199
+ Async do
1200
+ payload = {
1201
+ name: name,
1202
+ event_type: Discorb::AutoModRule::EVENT_TYPES.key(event_type),
1203
+ trigger_type: Discorb::AutoModRule::TRIGGER_TYPES.key(trigger_type),
1204
+ metadata: {
1205
+ keyword_filter: keyword_filter,
1206
+ presets: presets && Discorb::AutoModRule::PRESET_TYPES.key(presets),
1207
+ },
1208
+ actions: actions.map(&:to_hash),
1209
+ enabled: enabled,
1210
+ exempt_roles: exempt_roles.map(&:id),
1211
+ exempt_channels: exempt_channels.map(&:id),
1212
+ }
1213
+
1214
+ _resp, data = @client.http.request(
1215
+ Route.new(
1216
+ "/guilds/#{@id}/auto-moderation/rules",
1217
+ "//guilds/:guild_id/auto-moderation/rules",
1218
+ :post
1219
+ ),
1220
+ payload,
1221
+ audit_log_reason: reason
1222
+ )
1223
+ Discorb::AutoModRule.new(@client, data)
1224
+ end
1225
+ end
1226
+
1125
1227
  #
1126
1228
  # Represents a vanity invite.
1127
1229
  #
@@ -1279,7 +1381,7 @@ module Discorb
1279
1381
  #
1280
1382
  # @return [String] The url of the banner.
1281
1383
  #
1282
- def banner(guild_id, style: "banner")
1384
+ def banner(guild_id, style: :banner)
1283
1385
  "#{Discorb::API_BASE_URL}/guilds/#{guild_id}/widget.png&style=#{style}"
1284
1386
  end
1285
1387
  end
@@ -1339,7 +1441,7 @@ module Discorb
1339
1441
  @nsfw_level = NSFW_LEVELS[data[:nsfw_level]]
1340
1442
  return unless is_create_event
1341
1443
 
1342
- @stickers = data[:stickers].nil? ? [] : data[:stickers].map { |s| Sticker::GuildSticker.new(self, s) }
1444
+ @stickers = data[:stickers].nil? ? [] : data[:stickers].map { |s| Sticker::GuildSticker.new(@client, s) }
1343
1445
  @joined_at = Time.iso8601(data[:joined_at])
1344
1446
  @large = data[:large]
1345
1447
  @member_count = data[:member_count]
@@ -1407,7 +1509,24 @@ module Discorb
1407
1509
  @client = client
1408
1510
  @description = data[:description]
1409
1511
  @guild = guild
1410
- @channels = data[:channels].map { |c| WelcomeScreen::Channel.new(client, c, nil) }
1512
+ @channels = data[:channels].map do |c|
1513
+ WelcomeScreen::Channel.new(
1514
+ client.channels[c[:channel_id]],
1515
+ c,
1516
+ c[:emoji_name] &&
1517
+ if c[:emoji_id]
1518
+ (client.emojis[c[:emoji_id]] ||
1519
+ Discorb::PartialEmoji.new(
1520
+ {
1521
+ name: c[:emoji_name],
1522
+ id: c[:emoji_id],
1523
+ }
1524
+ ))
1525
+ else
1526
+ Discorb::UnicodeEmoji.new(c[:emoji_name])
1527
+ end
1528
+ )
1529
+ end
1411
1530
  end
1412
1531
 
1413
1532
  #
@@ -19,7 +19,7 @@ module Discorb
19
19
  attr_reader :created_at
20
20
  # @return [Time] The time this template was last updated.
21
21
  attr_reader :updated_at
22
- # @return [Discorb::Guild] The guild where the template was created.
22
+ # @return [Discorb::Snowflake] The ID of guild where the template was created.
23
23
  attr_reader :source_guild_id
24
24
  # @return [Discorb::GuildTemplate::TemplateGuild] The guild where the template was created.
25
25
  attr_reader :serialized_source_guild
@@ -93,14 +93,14 @@ module Discorb
93
93
  #
94
94
  # @return [Async::Task<void>] The task.
95
95
  #
96
- def delete!
96
+ def delete
97
97
  Async do
98
98
  @client.http.request(Route.new("/guilds/#{@source_guild_id}/templates/#{@code}",
99
99
  "//guilds/:guild_id/templates/:code", :delete)).wait
100
100
  end
101
101
  end
102
102
 
103
- alias destroy! delete!
103
+ alias destroy delete
104
104
 
105
105
  #
106
106
  # Represents a guild in guild template.
data/lib/discorb/http.rb CHANGED
@@ -27,7 +27,6 @@ module Discorb
27
27
  #
28
28
  # @param [Discorb::Route] path The path to the resource.
29
29
  # @param [String, Hash] body The body of the request. Defaults to an empty string.
30
- # @param [Hash] headers The headers to send with the request.
31
30
  # @param [String] audit_log_reason The audit log reason to send with the request.
32
31
  # @param [Hash] kwargs The keyword arguments.
33
32
  #
@@ -36,18 +35,28 @@ module Discorb
36
35
  #
37
36
  # @raise [Discorb::HTTPError] The request was failed.
38
37
  #
39
- def request(path, body = "", headers: nil, audit_log_reason: nil, **kwargs)
38
+ def request(path, body = "", audit_log_reason: nil, **kwargs)
40
39
  Async do |_task|
41
40
  @ratelimit_handler.wait(path)
42
41
  resp = if %i[post patch put].include? path.method
43
- http.send(path.method, get_path(path), get_body(body), get_headers(headers, body, audit_log_reason),
44
- **kwargs)
42
+ http.send(
43
+ path.method,
44
+ get_path(path),
45
+ get_body(body),
46
+ get_headers(body, audit_log_reason),
47
+ **kwargs,
48
+ )
45
49
  else
46
- http.send(path.method, get_path(path), get_headers(headers, body, audit_log_reason), **kwargs)
50
+ http.send(
51
+ path.method,
52
+ get_path(path),
53
+ get_headers(body, audit_log_reason),
54
+ **kwargs,
55
+ )
47
56
  end
48
57
  data = get_response_data(resp)
49
58
  @ratelimit_handler.save(path, resp)
50
- handle_response(resp, data, path, body, headers, audit_log_reason, kwargs)
59
+ handle_response(resp, data, path, body, nil, audit_log_reason, kwargs)
51
60
  end
52
61
  end
53
62
 
@@ -58,7 +67,6 @@ module Discorb
58
67
  # @param [Discorb::Route] path The path to the resource.
59
68
  # @param [String, Hash] body The body of the request.
60
69
  # @param [Array<Discorb::Attachment>] files The files to upload.
61
- # @param [Hash] headers The headers to send with the request.
62
70
  # @param [String] audit_log_reason The audit log reason to send with the request.
63
71
  # @param [Hash] kwargs The keyword arguments.
64
72
  #
@@ -67,14 +75,15 @@ module Discorb
67
75
  #
68
76
  # @raise [Discorb::HTTPError] The request was failed.
69
77
  #
70
- def multipart_request(path, body, files, headers: nil, audit_log_reason: nil, **kwargs)
78
+ def multipart_request(path, body, files, audit_log_reason: nil, **kwargs)
71
79
  Async do |_task|
72
80
  @ratelimit_handler.wait(path)
73
81
  req = Net::HTTP.const_get(path.method.to_s.capitalize).new(
74
82
  get_path(path),
75
- get_headers(headers, body, audit_log_reason),
83
+ get_headers(body, audit_log_reason),
76
84
  **kwargs,
77
85
  )
86
+ # @type var data: Array[untyped]
78
87
  data = [
79
88
  ["payload_json", get_body(body)],
80
89
  ]
@@ -83,10 +92,13 @@ module Discorb
83
92
 
84
93
  if file.created_by == :discord
85
94
  request_io = StringIO.new(
86
- cdn_http.get(URI.parse(file.url).path, {
87
- "Content-Type" => nil,
88
- "User-Agent" => Discorb::USER_AGENT,
89
- }).body
95
+ cdn_http.get(
96
+ (URI.parse(file.url).path || raise("Could not parse url")),
97
+ {
98
+ "Content-Type" => nil,
99
+ "User-Agent" => Discorb::USER_AGENT,
100
+ }
101
+ ).body
90
102
  )
91
103
  data << ["files[#{i}]", request_io, { filename: file.filename, content_type: file.content_type }]
92
104
  else
@@ -97,10 +109,11 @@ module Discorb
97
109
  session = Net::HTTP.new("discord.com", 443)
98
110
  session.use_ssl = true
99
111
  resp = session.request(req)
100
- files&.then { _1.filter(&:will_close).each { |f| f.io.close } }
101
- data = get_response_data(resp)
112
+ resp_data = get_response_data(resp)
102
113
  @ratelimit_handler.save(path, resp)
103
- handle_response(resp, data, path, body, headers, audit_log_reason, kwargs)
114
+ response = handle_response(resp, resp_data, path, body, files, audit_log_reason, kwargs)
115
+ files&.then { _1.filter(&:will_close).each { |f| f.io.close } }
116
+ response
104
117
  end
105
118
  end
106
119
 
@@ -110,12 +123,16 @@ module Discorb
110
123
 
111
124
  private
112
125
 
113
- def handle_response(resp, data, path, body, headers, audit_log_reason, kwargs)
126
+ def handle_response(resp, data, path, body, files, audit_log_reason, kwargs)
114
127
  case resp.code
115
128
  when "429"
116
129
  @client.logger.info("Rate limit exceeded for #{path.method} #{path.url}, waiting #{data[:retry_after]} seconds")
117
130
  sleep(data[:retry_after])
118
- request(path, body, headers: headers, audit_log_reason: audit_log_reason, **kwargs).wait
131
+ if files
132
+ multipart_request(path, body, files, audit_log_reason: audit_log_reason, **kwargs).wait
133
+ else
134
+ request(path, body, audit_log_reason: audit_log_reason, **kwargs).wait
135
+ end
119
136
  when "400"
120
137
  raise BadRequestError.new(resp, data)
121
138
  when "401"
@@ -129,14 +146,19 @@ module Discorb
129
146
  end
130
147
  end
131
148
 
132
- def get_headers(headers, body = "", audit_log_reason = nil)
149
+ def get_headers(body = "", audit_log_reason = nil)
133
150
  ret = if body.nil? || body == ""
134
- { "User-Agent" => USER_AGENT, "authorization" => "Bot #{@client.token}" }
151
+ {
152
+ "User-Agent" => USER_AGENT,
153
+ "authorization" => "Bot #{@client.token}",
154
+ }
135
155
  else
136
- { "User-Agent" => USER_AGENT, "authorization" => "Bot #{@client.token}",
137
- "content-type" => "application/json", }
156
+ {
157
+ "User-Agent" => USER_AGENT,
158
+ "authorization" => "Bot #{@client.token}",
159
+ "content-type" => "application/json",
160
+ }
138
161
  end
139
- ret.merge!(headers) if !headers.nil? && headers.length.positive?
140
162
  ret["X-Audit-Log-Reason"] = audit_log_reason unless audit_log_reason.nil?
141
163
  ret
142
164
  end
@@ -153,7 +175,7 @@ module Discorb
153
175
 
154
176
  def get_path(path)
155
177
  full_path = if path.url.start_with?("https://")
156
- path.url
178
+ path.url
157
179
  else
158
180
  API_BASE_URL + path.url
159
181
  end
@@ -166,7 +188,7 @@ module Discorb
166
188
  data = JSON.parse(resp.body, symbolize_names: true)
167
189
  rescue JSON::ParserError, TypeError
168
190
  data = if resp.body.nil? || resp.body.empty?
169
- nil
191
+ {}
170
192
  else
171
193
  resp.body
172
194
  end
@@ -72,7 +72,7 @@ module Discorb
72
72
  #
73
73
  # @return [Async::Task<void>] The task.
74
74
  #
75
- def delete!(reason: nil)
75
+ def delete(reason: nil)
76
76
  Async do
77
77
  @client.http.request(
78
78
  Route.new("/guilds/#{@guild}/integrations/#{@id}", "//guilds/:guild_id/integrations/:integration_id",
@@ -81,7 +81,7 @@ module Discorb
81
81
  end
82
82
  end
83
83
 
84
- alias destroy! delete!
84
+ alias destroy delete
85
85
 
86
86
  private
87
87
 
@@ -23,6 +23,8 @@ module Discorb
23
23
  dm_typing: 1 << 14,
24
24
  message_content: 1 << 15,
25
25
  scheduled_events: 1 << 16,
26
+ automod_configuration: 1 << 20,
27
+ automod_execution: 1 << 21,
26
28
  }.freeze
27
29
 
28
30
  #
@@ -44,29 +46,34 @@ module Discorb
44
46
  # @param dm_typing [Boolean] Whether dm typing related events are enabled.
45
47
  # @param message_content [Boolean] Whether message content will be sent with events.
46
48
  # @param scheduled_events [Boolean] Whether events related scheduled events are enabled.
47
- #
49
+ # @param automod_configuration [Boolean] Whether automod configuration related events are enabled.
50
+ # @param automod_execution [Boolean] Whether automod execution related events are enabled.
48
51
  # @note You must enable privileged intents to use `members` and/or `presences` intents.
49
52
  # @note Message Content Intent is not required to use `message_content` intents for now,
50
53
  # this will be required in September 1, 2022. [Learn More](https://support-dev.discord.com/hc/en-us/articles/4404772028055).
51
54
  # You should specify `message_content` intent for preventing unexpected changes in the future.
52
55
  #
53
- def initialize(guilds: true,
54
- members: false,
55
- bans: true,
56
- emojis: true,
57
- integrations: true,
58
- webhooks: true,
59
- invites: true,
60
- voice_states: true,
61
- presences: false,
62
- messages: true,
63
- reactions: true,
64
- typing: true,
65
- dm_messages: true,
66
- dm_reactions: true,
67
- dm_typing: true,
68
- message_content: nil,
69
- scheduled_events: true)
56
+ def initialize(
57
+ guilds: true,
58
+ members: false,
59
+ bans: true,
60
+ emojis: true,
61
+ integrations: true,
62
+ webhooks: true,
63
+ invites: true,
64
+ voice_states: true,
65
+ presences: false,
66
+ messages: true,
67
+ reactions: true,
68
+ typing: true,
69
+ dm_messages: true,
70
+ dm_reactions: true,
71
+ dm_typing: true,
72
+ message_content: nil,
73
+ scheduled_events: true,
74
+ automod_configuration: true,
75
+ automod_execution: true
76
+ )
70
77
  @raw_value = {
71
78
  guilds: guilds,
72
79
  members: members,
@@ -85,6 +92,8 @@ module Discorb
85
92
  dm_typing: dm_typing,
86
93
  message_content: message_content,
87
94
  scheduled_events: scheduled_events,
95
+ automod_configuration: automod_configuration,
96
+ automod_execution: automod_execution,
88
97
  }
89
98
  end
90
99
 
@@ -83,21 +83,25 @@ module Discorb
83
83
  when 3, 4, 5, 10
84
84
  option[:value]
85
85
  when 6
86
- members[option[:value]] || guild.members[option[:value]] || guild.fetch_member(option[:value]).wait
86
+ members[option[:value]] || guild && (guild.members[option[:value]] ||
87
+ guild.fetch_member(option[:value]).wait)
87
88
  when 7
88
- guild.channels[option[:value]] || guild.fetch_channels.wait.find do |channel|
89
- channel.id == option[:value]
89
+ if guild
90
+ guild.channels[option[:value]] || guild.fetch_channels.wait.find do |channel|
91
+ channel.id == option[:value]
92
+ end
90
93
  end
91
94
  when 8
92
- guild.roles[option[:value]] || guild.fetch_roles.wait.find { |role| role.id == option[:value] }
95
+ guild && (guild.roles[option[:value]] ||
96
+ guild.fetch_roles.wait.find { |role| role.id == option[:value] })
93
97
  when 9
94
98
  members[option[:value]] ||
95
- guild.members[option[:value]] ||
96
- guild.roles[option[:value]] ||
97
- guild.fetch_member(option[:value]).wait ||
98
- guild.fetch_roles.wait.find do |role|
99
- role.id == option[:value]
100
- end
99
+ guild && (guild.members[option[:value]] ||
100
+ guild.roles[option[:value]] ||
101
+ guild.fetch_member(option[:value]).wait ||
102
+ guild.fetch_roles.wait.find do |role|
103
+ role.id == option[:value]
104
+ end)
101
105
  when 11
102
106
  attachments[option[:value]]
103
107
  end