discorb 0.19.0 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/build_version.yml +2 -2
  3. data/.rubocop.yml +12 -75
  4. data/Changelog.md +10 -0
  5. data/Rakefile +482 -454
  6. data/lib/discorb/allowed_mentions.rb +68 -72
  7. data/lib/discorb/app_command/command.rb +466 -398
  8. data/lib/discorb/app_command/common.rb +65 -25
  9. data/lib/discorb/app_command/handler.rb +304 -266
  10. data/lib/discorb/app_command.rb +5 -5
  11. data/lib/discorb/application.rb +198 -197
  12. data/lib/discorb/asset.rb +101 -101
  13. data/lib/discorb/attachment.rb +134 -119
  14. data/lib/discorb/audit_logs.rb +412 -385
  15. data/lib/discorb/automod.rb +279 -269
  16. data/lib/discorb/channel/base.rb +107 -108
  17. data/lib/discorb/channel/category.rb +32 -32
  18. data/lib/discorb/channel/container.rb +44 -44
  19. data/lib/discorb/channel/dm.rb +26 -28
  20. data/lib/discorb/channel/guild.rb +311 -246
  21. data/lib/discorb/channel/stage.rb +156 -140
  22. data/lib/discorb/channel/text.rb +430 -336
  23. data/lib/discorb/channel/thread.rb +374 -325
  24. data/lib/discorb/channel/voice.rb +85 -79
  25. data/lib/discorb/channel.rb +5 -5
  26. data/lib/discorb/client.rb +635 -621
  27. data/lib/discorb/color.rb +178 -182
  28. data/lib/discorb/common.rb +168 -164
  29. data/lib/discorb/components/button.rb +107 -106
  30. data/lib/discorb/components/select_menu.rb +157 -145
  31. data/lib/discorb/components/text_input.rb +103 -106
  32. data/lib/discorb/components.rb +68 -66
  33. data/lib/discorb/dictionary.rb +135 -135
  34. data/lib/discorb/embed.rb +404 -398
  35. data/lib/discorb/emoji.rb +309 -302
  36. data/lib/discorb/emoji_table.rb +16099 -8857
  37. data/lib/discorb/error.rb +131 -131
  38. data/lib/discorb/event.rb +360 -314
  39. data/lib/discorb/event_handler.rb +39 -39
  40. data/lib/discorb/exe/about.rb +17 -17
  41. data/lib/discorb/exe/irb.rb +72 -67
  42. data/lib/discorb/exe/new.rb +323 -315
  43. data/lib/discorb/exe/run.rb +69 -68
  44. data/lib/discorb/exe/setup.rb +57 -55
  45. data/lib/discorb/exe/show.rb +12 -12
  46. data/lib/discorb/extend.rb +25 -45
  47. data/lib/discorb/extension.rb +89 -83
  48. data/lib/discorb/flag.rb +126 -128
  49. data/lib/discorb/gateway.rb +984 -804
  50. data/lib/discorb/gateway_events.rb +670 -638
  51. data/lib/discorb/gateway_requests.rb +45 -48
  52. data/lib/discorb/guild.rb +2115 -1626
  53. data/lib/discorb/guild_template.rb +280 -241
  54. data/lib/discorb/http.rb +247 -232
  55. data/lib/discorb/image.rb +42 -42
  56. data/lib/discorb/integration.rb +169 -161
  57. data/lib/discorb/intents.rb +161 -163
  58. data/lib/discorb/interaction/autocomplete.rb +76 -62
  59. data/lib/discorb/interaction/command.rb +279 -224
  60. data/lib/discorb/interaction/components.rb +114 -104
  61. data/lib/discorb/interaction/modal.rb +36 -32
  62. data/lib/discorb/interaction/response.rb +379 -336
  63. data/lib/discorb/interaction/root.rb +271 -257
  64. data/lib/discorb/interaction.rb +5 -5
  65. data/lib/discorb/invite.rb +154 -153
  66. data/lib/discorb/member.rb +344 -311
  67. data/lib/discorb/message.rb +615 -544
  68. data/lib/discorb/message_meta.rb +197 -186
  69. data/lib/discorb/modules.rb +371 -290
  70. data/lib/discorb/permission.rb +305 -291
  71. data/lib/discorb/presence.rb +352 -346
  72. data/lib/discorb/rate_limit.rb +81 -76
  73. data/lib/discorb/reaction.rb +55 -54
  74. data/lib/discorb/role.rb +272 -240
  75. data/lib/discorb/shard.rb +76 -74
  76. data/lib/discorb/sticker.rb +193 -171
  77. data/lib/discorb/user.rb +205 -188
  78. data/lib/discorb/utils/colored_puts.rb +16 -16
  79. data/lib/discorb/utils.rb +12 -16
  80. data/lib/discorb/voice_state.rb +305 -281
  81. data/lib/discorb/webhook.rb +537 -507
  82. data/lib/discorb.rb +62 -56
  83. data/sig/discorb/application.rbs +2 -0
  84. data/sig/discorb/automod.rbs +10 -1
  85. data/sig/discorb/guild.rbs +2 -0
  86. data/sig/discorb/message.rbs +2 -0
  87. data/sig/discorb/user.rbs +22 -20
  88. metadata +2 -2
@@ -1,246 +1,311 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- #
5
- # Represents a channel in guild.
6
- # @abstract
7
- #
8
- class GuildChannel < Channel
9
- # @return [Integer] The position of the channel as integer.
10
- attr_reader :position
11
- # @return [Hash{Discorb::Role, Discorb::Member => PermissionOverwrite}] The permission overwrites of the channel.
12
- attr_reader :permission_overwrites
13
-
14
- # @!attribute [r] mention
15
- # @return [String] The mention of the channel.
16
- #
17
- # @!attribute [r] parent
18
- # @macro client_cache
19
- # @return [Discorb::CategoryChannel] The parent of channel.
20
- # @return [nil] If the channel is not a child of category.
21
- #
22
- # @!attribute [r] guild
23
- # @return [Discorb::Guild] The guild of channel.
24
- # @macro client_cache
25
-
26
- include Comparable
27
- @channel_type = nil
28
-
29
- #
30
- # Compares position of two channels.
31
- #
32
- # @param [Discorb::GuildChannel] other The channel to compare.
33
- #
34
- # @return [-1, 0, 1] -1 if the channel is at lower than the other, 1 if the channel is at highter than the other.
35
- #
36
- def <=>(other)
37
- return nil unless other.respond_to?(:position)
38
-
39
- @position <=> other.position
40
- end
41
-
42
- #
43
- # Checks if the channel is same as another.
44
- #
45
- # @param [Discorb::GuildChannel] other The channel to check.
46
- #
47
- # @return [Boolean] `true` if the channel is same as another.
48
- #
49
- def ==(other)
50
- return false unless other.respond_to?(:id)
51
-
52
- @id == other.id
53
- end
54
-
55
- #
56
- # Stringifies the channel.
57
- #
58
- # @return [String] The name of the channel with `#`.
59
- #
60
- def to_s
61
- "##{@name}"
62
- end
63
-
64
- def mention
65
- "<##{@id}>"
66
- end
67
-
68
- def parent
69
- return nil unless @parent_id
70
-
71
- @client.channels[@parent_id]
72
- end
73
-
74
- alias category parent
75
-
76
- def guild
77
- @client.guilds[@guild_id]
78
- end
79
-
80
- def inspect
81
- "#<#{self.class} \"##{@name}\" id=#{@id}>"
82
- end
83
-
84
- #
85
- # Deletes the channel.
86
- # @async
87
- #
88
- # @param [String] reason The reason of deleting the channel.
89
- #
90
- # @return [Async::Task<self>] The deleted channel.
91
- #
92
- def delete(reason: nil)
93
- Async do
94
- @client.http.request(Route.new(base_url.wait.to_s, "//webhooks/:webhook_id/:token", :delete), {},
95
- audit_log_reason: reason).wait
96
- @deleted = true
97
- self
98
- end
99
- end
100
-
101
- alias close delete
102
- alias destroy delete
103
-
104
- #
105
- # Moves the channel to another position.
106
- # @async
107
- #
108
- # @param [Integer] position The position to move the channel.
109
- # @param [Boolean] lock_permissions Whether to lock the permissions of the channel.
110
- # @param [Discorb::CategoryChannel] parent The parent of channel.
111
- # @param [String] reason The reason of moving the channel.
112
- #
113
- # @return [Async::Task<self>] The moved channel.
114
- #
115
- def move(position, lock_permissions: false, parent: Discorb::Unset, reason: nil)
116
- Async do
117
- # @type var payload: Hash[Symbol, untyped]
118
- payload = {
119
- position: position,
120
- }
121
- payload[:lock_permissions] = lock_permissions
122
- payload[:parent_id] = parent&.id if parent != Discorb::Unset
123
- @client.http.request(Route.new("/guilds/#{@guild_id}/channels", "//guilds/:guild_id/channels", :patch),
124
- payload, audit_log_reason: reason).wait
125
- end
126
- end
127
-
128
- #
129
- # Set the channel's permission overwrite.
130
- # @async
131
- #
132
- # @param [Discorb::Role, Discorb::Member] target The target of the overwrite.
133
- # @param [String] reason The reason of setting the overwrite.
134
- # @param [{Symbol => Boolean}] perms The permission overwrites to replace.
135
- #
136
- # @return [Async::Task<void>] The task.
137
- #
138
- def set_permissions(target, reason: nil, **perms)
139
- Async do
140
- allow_value = @permission_overwrites[target]&.allow_value.to_i
141
- deny_value = @permission_overwrites[target]&.deny_value.to_i
142
- perms.each do |perm, value|
143
- allow_value[Discorb::Permission.bits[perm]] = 1 if value == true
144
- deny_value[Discorb::Permission.bits[perm]] = 1 if value == false
145
- end
146
- payload = {
147
- allow: allow_value,
148
- deny: deny_value,
149
- type: target.is_a?(Member) ? 1 : 0,
150
- }
151
- @client.http.request(
152
- Route.new("/channels/#{@id}/permissions/#{target.id}", "//channels/:channel_id/permissions/:target_id",
153
- :put), payload, audit_log_reason: reason,
154
- ).wait
155
- end
156
- end
157
-
158
- alias modify_permissions set_permissions
159
- alias modify_permisssion set_permissions
160
- alias edit_permissions set_permissions
161
- alias edit_permission set_permissions
162
-
163
- #
164
- # Delete the channel's permission overwrite.
165
- # @async
166
- #
167
- # @param [Discorb::Role, Discorb::Member] target The target of the overwrite.
168
- # @param [String] reason The reason of deleting the overwrite.
169
- #
170
- # @return [Async::Task<void>] The task.
171
- #
172
- def delete_permissions(target, reason: nil)
173
- Async do
174
- @client.http.request(
175
- Route.new("/channels/#{@id}/permissions/#{target.id}", "//channels/:channel_id/permissions/:target_id",
176
- :delete), {}, audit_log_reason: reason,
177
- ).wait
178
- end
179
- end
180
-
181
- alias delete_permission delete_permissions
182
- alias destroy_permissions delete_permissions
183
- alias destroy_permission delete_permissions
184
-
185
- #
186
- # Fetch the channel's invites.
187
- # @async
188
- #
189
- # @return [Async::Task<Array<Discorb::Invite>>] The invites in the channel.
190
- #
191
- def fetch_invites
192
- Async do
193
- _resp, data = @client.http.request(Route.new("/channels/#{@id}/invites", "//channels/:channel_id/invites",
194
- :get)).wait
195
- data.map { |invite| Invite.new(@client, invite, false) }
196
- end
197
- end
198
-
199
- #
200
- # Create an invite in the channel.
201
- # @async
202
- #
203
- # @param [Integer] max_age The max age of the invite.
204
- # @param [Integer] max_uses The max uses of the invite.
205
- # @param [Boolean] temporary Whether the invite is temporary.
206
- # @param [Boolean] unique Whether the invite is unique.
207
- # @note if it's `false` it may return existing invite.
208
- # @param [String] reason The reason of creating the invite.
209
- #
210
- # @return [Async::Task<Invite>] The created invite.
211
- #
212
- def create_invite(max_age: nil, max_uses: nil, temporary: false, unique: false, reason: nil)
213
- Async do
214
- _resp, data = @client.http.request(
215
- Route.new("/channels/#{@id}/invites", "//channels/:channel_id/invites", :post), {
216
- max_age: max_age,
217
- max_uses: max_uses,
218
- temporary: temporary,
219
- unique: unique,
220
- }, audit_log_reason: reason,
221
- ).wait
222
- Invite.new(@client, data, false)
223
- end
224
- end
225
-
226
- private
227
-
228
- def _set_data(data)
229
- @guild_id = data[:guild_id]
230
- @position = data[:position]
231
- @permission_overwrites = if data[:permission_overwrites]
232
- data[:permission_overwrites].to_h do |ow|
233
- [
234
- (ow[:type] == 1 ? guild.roles : guild.members)[ow[:id]],
235
- PermissionOverwrite.new(ow[:allow].to_i, ow[:deny].to_i),
236
- ]
237
- end
238
- else
239
- {}
240
- end
241
- @parent_id = data[:parent_id]
242
-
243
- super
244
- end
245
- end
246
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents a channel in guild.
6
+ # @abstract
7
+ #
8
+ class GuildChannel < Channel
9
+ # @return [Integer] The position of the channel as integer.
10
+ attr_reader :position
11
+ # @return [Hash{Discorb::Role, Discorb::Member => PermissionOverwrite}] The permission overwrites of the channel.
12
+ attr_reader :permission_overwrites
13
+
14
+ # @!attribute [r] mention
15
+ # @return [String] The mention of the channel.
16
+ #
17
+ # @!attribute [r] parent
18
+ # @macro client_cache
19
+ # @return [Discorb::CategoryChannel] The parent of channel.
20
+ # @return [nil] If the channel is not a child of category.
21
+ #
22
+ # @!attribute [r] guild
23
+ # @return [Discorb::Guild] The guild of channel.
24
+ # @macro client_cache
25
+
26
+ include Comparable
27
+ @channel_type = nil
28
+
29
+ #
30
+ # Compares position of two channels.
31
+ #
32
+ # @param [Discorb::GuildChannel] other The channel to compare.
33
+ #
34
+ # @return [-1, 0, 1] -1 if the channel is at lower than the other, 1 if the channel is at highter than the other.
35
+ #
36
+ def <=>(other)
37
+ return nil unless other.respond_to?(:position)
38
+
39
+ @position <=> other.position
40
+ end
41
+
42
+ #
43
+ # Checks if the channel is same as another.
44
+ #
45
+ # @param [Discorb::GuildChannel] other The channel to check.
46
+ #
47
+ # @return [Boolean] `true` if the channel is same as another.
48
+ #
49
+ def ==(other)
50
+ return false unless other.respond_to?(:id)
51
+
52
+ @id == other.id
53
+ end
54
+
55
+ #
56
+ # Stringifies the channel.
57
+ #
58
+ # @return [String] The name of the channel with `#`.
59
+ #
60
+ def to_s
61
+ "##{@name}"
62
+ end
63
+
64
+ def mention
65
+ "<##{@id}>"
66
+ end
67
+
68
+ def parent
69
+ return nil unless @parent_id
70
+
71
+ @client.channels[@parent_id]
72
+ end
73
+
74
+ alias category parent
75
+
76
+ def guild
77
+ @client.guilds[@guild_id]
78
+ end
79
+
80
+ def inspect
81
+ "#<#{self.class} \"##{@name}\" id=#{@id}>"
82
+ end
83
+
84
+ #
85
+ # Deletes the channel.
86
+ # @async
87
+ #
88
+ # @param [String] reason The reason of deleting the channel.
89
+ #
90
+ # @return [Async::Task<self>] The deleted channel.
91
+ #
92
+ def delete(reason: nil)
93
+ Async do
94
+ @client
95
+ .http
96
+ .request(
97
+ Route.new(
98
+ base_url.wait.to_s,
99
+ "//webhooks/:webhook_id/:token",
100
+ :delete
101
+ ),
102
+ {},
103
+ audit_log_reason: reason
104
+ )
105
+ .wait
106
+ @deleted = true
107
+ self
108
+ end
109
+ end
110
+
111
+ alias close delete
112
+ alias destroy delete
113
+
114
+ #
115
+ # Moves the channel to another position.
116
+ # @async
117
+ #
118
+ # @param [Integer] position The position to move the channel.
119
+ # @param [Boolean] lock_permissions Whether to lock the permissions of the channel.
120
+ # @param [Discorb::CategoryChannel] parent The parent of channel.
121
+ # @param [String] reason The reason of moving the channel.
122
+ #
123
+ # @return [Async::Task<self>] The moved channel.
124
+ #
125
+ def move(
126
+ position,
127
+ lock_permissions: false,
128
+ parent: Discorb::Unset,
129
+ reason: nil
130
+ )
131
+ Async do
132
+ # @type var payload: Hash[Symbol, untyped]
133
+ payload = { position: position }
134
+ payload[:lock_permissions] = lock_permissions
135
+ payload[:parent_id] = parent&.id if parent != Discorb::Unset
136
+ @client
137
+ .http
138
+ .request(
139
+ Route.new(
140
+ "/guilds/#{@guild_id}/channels",
141
+ "//guilds/:guild_id/channels",
142
+ :patch
143
+ ),
144
+ payload,
145
+ audit_log_reason: reason
146
+ )
147
+ .wait
148
+ end
149
+ end
150
+
151
+ #
152
+ # Set the channel's permission overwrite.
153
+ # @async
154
+ #
155
+ # @param [Discorb::Role, Discorb::Member] target The target of the overwrite.
156
+ # @param [String] reason The reason of setting the overwrite.
157
+ # @param [{Symbol => Boolean}] perms The permission overwrites to replace.
158
+ #
159
+ # @return [Async::Task<void>] The task.
160
+ #
161
+ def set_permissions(target, reason: nil, **perms)
162
+ Async do
163
+ allow_value = @permission_overwrites[target]&.allow_value.to_i
164
+ deny_value = @permission_overwrites[target]&.deny_value.to_i
165
+ perms.each do |perm, value|
166
+ allow_value[Discorb::Permission.bits[perm]] = 1 if value == true
167
+ deny_value[Discorb::Permission.bits[perm]] = 1 if value == false
168
+ end
169
+ payload = {
170
+ allow: allow_value,
171
+ deny: deny_value,
172
+ type: target.is_a?(Member) ? 1 : 0
173
+ }
174
+ @client
175
+ .http
176
+ .request(
177
+ Route.new(
178
+ "/channels/#{@id}/permissions/#{target.id}",
179
+ "//channels/:channel_id/permissions/:target_id",
180
+ :put
181
+ ),
182
+ payload,
183
+ audit_log_reason: reason
184
+ )
185
+ .wait
186
+ end
187
+ end
188
+
189
+ alias modify_permissions set_permissions
190
+ alias modify_permisssion set_permissions
191
+ alias edit_permissions set_permissions
192
+ alias edit_permission set_permissions
193
+
194
+ #
195
+ # Delete the channel's permission overwrite.
196
+ # @async
197
+ #
198
+ # @param [Discorb::Role, Discorb::Member] target The target of the overwrite.
199
+ # @param [String] reason The reason of deleting the overwrite.
200
+ #
201
+ # @return [Async::Task<void>] The task.
202
+ #
203
+ def delete_permissions(target, reason: nil)
204
+ Async do
205
+ @client
206
+ .http
207
+ .request(
208
+ Route.new(
209
+ "/channels/#{@id}/permissions/#{target.id}",
210
+ "//channels/:channel_id/permissions/:target_id",
211
+ :delete
212
+ ),
213
+ {},
214
+ audit_log_reason: reason
215
+ )
216
+ .wait
217
+ end
218
+ end
219
+
220
+ alias delete_permission delete_permissions
221
+ alias destroy_permissions delete_permissions
222
+ alias destroy_permission delete_permissions
223
+
224
+ #
225
+ # Fetch the channel's invites.
226
+ # @async
227
+ #
228
+ # @return [Async::Task<Array<Discorb::Invite>>] The invites in the channel.
229
+ #
230
+ def fetch_invites
231
+ Async do
232
+ _resp, data =
233
+ @client
234
+ .http
235
+ .request(
236
+ Route.new(
237
+ "/channels/#{@id}/invites",
238
+ "//channels/:channel_id/invites",
239
+ :get
240
+ )
241
+ )
242
+ .wait
243
+ data.map { |invite| Invite.new(@client, invite, false) }
244
+ end
245
+ end
246
+
247
+ #
248
+ # Create an invite in the channel.
249
+ # @async
250
+ #
251
+ # @param [Integer] max_age The max age of the invite.
252
+ # @param [Integer] max_uses The max uses of the invite.
253
+ # @param [Boolean] temporary Whether the invite is temporary.
254
+ # @param [Boolean] unique Whether the invite is unique.
255
+ # @note if it's `false` it may return existing invite.
256
+ # @param [String] reason The reason of creating the invite.
257
+ #
258
+ # @return [Async::Task<Invite>] The created invite.
259
+ #
260
+ def create_invite(
261
+ max_age: nil,
262
+ max_uses: nil,
263
+ temporary: false,
264
+ unique: false,
265
+ reason: nil
266
+ )
267
+ Async do
268
+ _resp, data =
269
+ @client
270
+ .http
271
+ .request(
272
+ Route.new(
273
+ "/channels/#{@id}/invites",
274
+ "//channels/:channel_id/invites",
275
+ :post
276
+ ),
277
+ {
278
+ max_age: max_age,
279
+ max_uses: max_uses,
280
+ temporary: temporary,
281
+ unique: unique
282
+ },
283
+ audit_log_reason: reason
284
+ )
285
+ .wait
286
+ Invite.new(@client, data, false)
287
+ end
288
+ end
289
+
290
+ private
291
+
292
+ def _set_data(data)
293
+ @guild_id = data[:guild_id]
294
+ @position = data[:position]
295
+ @permission_overwrites =
296
+ if data[:permission_overwrites]
297
+ data[:permission_overwrites].to_h do |ow|
298
+ [
299
+ (ow[:type] == 1 ? guild.roles : guild.members)[ow[:id]],
300
+ PermissionOverwrite.new(ow[:allow].to_i, ow[:deny].to_i)
301
+ ]
302
+ end
303
+ else
304
+ {}
305
+ end
306
+ @parent_id = data[:parent_id]
307
+
308
+ super
309
+ end
310
+ end
311
+ end