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
data/lib/discorb/event.rb CHANGED
@@ -1,314 +1,360 @@
1
- # frozen_string_literal: true
2
-
3
- module Discorb
4
- #
5
- # Represents an event in guild.
6
- #
7
- class ScheduledEvent < DiscordModel
8
- # @private
9
- # @return [{Integer => Symbol}] The mapping of privacy level.
10
- PRIVACY_LEVEL = {
11
- 2 => :guild_only,
12
- }.freeze
13
- # @private
14
- # @return [{Integer => Symbol}] The mapping of status.
15
- STATUS = {
16
- 1 => :scheduled,
17
- 2 => :active,
18
- 3 => :completed,
19
- 4 => :canceled,
20
- }.freeze
21
- # @private
22
- # @return [{Integer => Symbol}] The mapping of entity_type.
23
- ENTITY_TYPE = {
24
- 1 => :stage_instance,
25
- 2 => :voice,
26
- 3 => :external,
27
- }.freeze
28
-
29
- # @!visibility private
30
- def initialize(client, data)
31
- @client = client
32
- @data = data
33
- _set_data(data)
34
- end
35
-
36
- #
37
- # Represents the metadata of the event.
38
- #
39
- class Metadata
40
- # @return [String, nil] The location of the event. Only present if the event is a external event.
41
- attr_reader :location
42
-
43
- # @!visibility private
44
- def initialize(data)
45
- @location = data[:location]
46
- end
47
-
48
- def inspect
49
- "#<#{self.class.name} #{@name}>"
50
- end
51
- end
52
-
53
- # @return [Discorb::Snowflake] The ID of the event.
54
- attr_reader :id
55
- # @return [String] The name of the event.
56
- attr_reader :name
57
- # @return [String] The description of the event.
58
- attr_reader :description
59
-
60
- # @return [Time] The time the event starts.
61
- attr_reader :scheduled_start_time
62
- alias start_time scheduled_start_time
63
- alias start_at scheduled_start_time
64
- # @return [Time] The time the event ends.
65
- attr_reader :scheduled_end_time
66
- alias end_time scheduled_end_time
67
- alias end_at scheduled_end_time
68
- # @return [:guild_only] The privacy level of the event.
69
- attr_reader :privacy_level
70
- # @return [:scheduled, :active, :completed, :canceled] The status of the event.
71
- attr_reader :status
72
- # @return [:stage_instance, :voice, :external] The type of the event.
73
- attr_reader :entity_type
74
- # @return [Discorb::Snowflake] The ID of the entity the event is for.
75
- attr_reader :entity_id
76
- # @return [Discorb::ScheduledEvent::Metadata] The metadata of the event.
77
- attr_reader :metadata
78
- # @return [Integer] The user count of the event.
79
- attr_reader :user_count
80
-
81
- # @!attribute [r] guild
82
- # @return [Discorb::Guild, nil] The guild of the event.
83
- # @!attribute [r] channel
84
- # @return [Discorb::Channel, nil] The channel of the event.
85
- # Only present if the event will do in stage instance or voice channel.
86
- # @!attribute [r] creator
87
- # @return [Discorb::User] The user who created the event.#
88
- # @!attribute [r] time
89
- # @return [Range<Time>] The time range of the event.
90
-
91
- def guild
92
- @client.guilds[@guild_id]
93
- end
94
-
95
- def channel
96
- @client.channels[@channel_id]
97
- end
98
-
99
- def creator
100
- @creator || @client.users[@creator_id]
101
- end
102
-
103
- def time
104
- @scheduled_start_time..@scheduled_end_time
105
- end
106
-
107
- #
108
- # Create a scheduled event for the guild.
109
- # @async
110
- #
111
- # @param [:stage_instance, :voice, :external] type The type of event to create.
112
- # @param [String] name The name of the event.
113
- # @param [String] description The description of the event.
114
- # @param [Time] start_time The start time of the event.
115
- # @param [Time, nil] end_time The end time of the event. Defaults to `nil`.
116
- # @param [Discorb::Channel, Discorb::Snowflake, nil] channel The channel to run the event in.
117
- # @param [String, nil] location The location of the event. Defaults to `nil`.
118
- # @param [:guild_only] privacy_level The privacy level of the event. This must be `:guild_only`.
119
- # @param [:active, :completed, :canceled] status The status of the event.
120
- #
121
- # @return [Async::Task<Discorb::ScheduledEvent>] The event that was created.
122
- #
123
- # @see Event#start
124
- # @see Event#cancel
125
- # @see Event#complete
126
- #
127
- def edit(
128
- type: Discorb::Unset,
129
- name: Discorb::Unset,
130
- description: Discorb::Unset,
131
- start_time: Discorb::Unset,
132
- end_time: Discorb::Unset,
133
- privacy_level: Discorb::Unset,
134
- location: Discorb::Unset,
135
- channel: Discorb::Unset,
136
- status: Discorb::Unset
137
- )
138
- Async do
139
- payload = case type == Discorb::Unset ? @entity_type : type
140
- when :stage_instance
141
- raise ArgumentError, "channel must be provided for stage_instance events" unless channel
142
-
143
- {
144
- name: name,
145
- description: description,
146
- scheduled_start_time: start_time.iso8601,
147
- scheduled_end_time: end_time&.iso8601,
148
- privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
149
- channel_id: channel&.id,
150
- entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:stage_instance),
151
- status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
152
- }.reject { |_, v| v == Discorb::Unset }
153
- when :voice
154
- raise ArgumentError, "channel must be provided for voice events" unless channel
155
-
156
- {
157
- name: name,
158
- description: description,
159
- scheduled_start_time: start_time.iso8601,
160
- scheduled_end_time: end_time&.iso8601,
161
- privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
162
- channel_id: channel&.id,
163
- entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:voice),
164
- status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
165
- }.reject { |_, v| v == Discorb::Unset }
166
- when :external
167
- raise ArgumentError, "location must be provided for external events" unless location
168
- raise ArgumentError, "end_time must be provided for external events" unless end_time
169
-
170
- {
171
- name: name,
172
- description: description,
173
- channel_id: nil,
174
- scheduled_start_time: start_time.iso8601,
175
- scheduled_end_time: end_time.iso8601,
176
- privacy_level: Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) || Discorb::Unset,
177
- entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:external),
178
- entity_metadata: {
179
- location: location,
180
- },
181
- status: Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset,
182
- }.reject { |_, v| v == Discorb::Unset }
183
- else
184
- raise ArgumentError, "Invalid scheduled event type: #{type}"
185
- end
186
- @client.http.request(
187
- Route.new(
188
- "/guilds/#{@guild_id}/scheduled-events/#{@id}",
189
- "//guilds/:guild_id/scheduled-events/:scheduled_event_id",
190
- :patch
191
- ), payload
192
- ).wait
193
- end
194
- end
195
-
196
- alias modify edit
197
-
198
- #
199
- # Starts the event. Shortcut for `edit(status: :active)`.
200
- #
201
- def start
202
- edit(status: :active)
203
- end
204
-
205
- #
206
- # Completes the event. Shortcut for `edit(status: :completed)`.
207
- #
208
- def complete
209
- edit(status: :completed)
210
- end
211
-
212
- alias finish complete
213
-
214
- #
215
- # Cancels the event. Shortcut for `edit(status: :canceled)`.
216
- #
217
- def cancel
218
- edit(status: :canceled)
219
- end
220
-
221
- #
222
- # Deletes the event.
223
- # @async
224
- #
225
- # @return [Async::Task<void>] The task.
226
- #
227
- def delete
228
- Async do
229
- @client.http.request(Route.new("/guilds/#{@guild_id}/scheduled-events/#{@id}",
230
- "//guilds/:guild_id/scheduled-events/:scheduled_event_id", :delete)).wait
231
- end
232
- end
233
-
234
- alias destroy delete
235
-
236
- #
237
- # Fetches the event users.
238
- # @async
239
- #
240
- # @note You can fetch all of members by not specifying a parameter.
241
- #
242
- # @param [Integer] limit The maximum number of users to fetch. Defaults to `100`.
243
- # @param [#to_s] after The ID of the user to start fetching from. Defaults to `nil`.
244
- # @param [#to_s] before The ID of the user to stop fetching at. Defaults to `nil`.
245
- # @param [Boolean] with_member Whether to include the member object of the event. Defaults to `false`.
246
- # This should be used for manual fetching of members.
247
- #
248
- # @return [Async::Task<Array<Discorb::Member>>] The event users.
249
- #
250
- def fetch_users(limit = nil, before: nil, after: nil, with_member: true)
251
- Async do
252
- if limit.nil?
253
- after = 0
254
- res = []
255
- loop do
256
- _resp, users = @client.http.request(
257
- Route.new(
258
- "/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true",
259
- "//guilds/:guild_id/scheduled-events/:scheduled_event_id/users",
260
- :get
261
- )
262
- ).wait
263
- break if users.empty?
264
-
265
- res += users.map { |u| Member.new(@client, @guild_id, u[:user], u[:member]) }
266
- after = users.last[:user][:id]
267
- end
268
- res
269
- else
270
- params = {
271
- limit: limit,
272
- before: Discorb::Utils.try(before, :id),
273
- after: Discorb::Utils.try(after, :id),
274
- with_member: with_member,
275
- }.filter { |_k, v| !v.nil? }.to_h
276
- _resp, messages = @client.http.request(
277
- Route.new(
278
- "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}",
279
- "//channels/:channel_id/messages",
280
- :get
281
- )
282
- ).wait
283
- messages.map { |m| Message.new(@client, m.merge({ guild_id: @guild_id.to_s })) }
284
- end
285
- end
286
- end
287
-
288
- alias fetch_members fetch_users
289
-
290
- private
291
-
292
- def _set_data(data)
293
- @id = Snowflake.new(data[:id])
294
- @guild_id = Snowflake.new(data[:guild_id])
295
- @channel_id = data[:channel_id] && Snowflake.new(data[:channel_id])
296
- @creator_id = data[:creator_id] && Snowflake.new(data[:creator_id])
297
- @name = data[:name]
298
- @description = data[:description]
299
- @scheduled_start_time = Time.iso8601(data[:scheduled_start_time])
300
- @scheduled_end_time = data[:scheduled_end_time] && Time.iso8601(data[:scheduled_end_time])
301
- @privacy_level = :guild_only # data[:privacy_level]
302
- @status = STATUS[data[:status]]
303
- @entity_type = ENTITY_TYPE[data[:entity_type]]
304
- @entity_id = data[:entity_id] && Snowflake.new(data[:entity_id])
305
- @entity_metadata = data[:entity_metadata] && Metadata.new(data[:entity_metadata])
306
- @creator = @client.users[@creator_id] || (data[:creator] && User.new(@client, data[:creator]))
307
- @user_count = data[:user_count]
308
- end
309
-
310
- class << self
311
- attr_reader :status, :entity_type, :privacy_level
312
- end
313
- end
314
- end
1
+ # frozen_string_literal: true
2
+
3
+ module Discorb
4
+ #
5
+ # Represents an event in guild.
6
+ #
7
+ class ScheduledEvent < DiscordModel
8
+ # @private
9
+ # @return [{Integer => Symbol}] The mapping of privacy level.
10
+ PRIVACY_LEVEL = { 2 => :guild_only }.freeze
11
+ # @private
12
+ # @return [{Integer => Symbol}] The mapping of status.
13
+ STATUS = {
14
+ 1 => :scheduled,
15
+ 2 => :active,
16
+ 3 => :completed,
17
+ 4 => :canceled
18
+ }.freeze
19
+ # @private
20
+ # @return [{Integer => Symbol}] The mapping of entity_type.
21
+ ENTITY_TYPE = { 1 => :stage_instance, 2 => :voice, 3 => :external }.freeze
22
+
23
+ # @!visibility private
24
+ def initialize(client, data)
25
+ @client = client
26
+ @data = data
27
+ _set_data(data)
28
+ end
29
+
30
+ #
31
+ # Represents the metadata of the event.
32
+ #
33
+ class Metadata
34
+ # @return [String, nil] The location of the event. Only present if the event is a external event.
35
+ attr_reader :location
36
+
37
+ # @!visibility private
38
+ def initialize(data)
39
+ @location = data[:location]
40
+ end
41
+
42
+ def inspect
43
+ "#<#{self.class.name} #{@name}>"
44
+ end
45
+ end
46
+
47
+ # @return [Discorb::Snowflake] The ID of the event.
48
+ attr_reader :id
49
+ # @return [String] The name of the event.
50
+ attr_reader :name
51
+ # @return [String] The description of the event.
52
+ attr_reader :description
53
+
54
+ # @return [Time] The time the event starts.
55
+ attr_reader :scheduled_start_time
56
+ alias start_time scheduled_start_time
57
+ alias start_at scheduled_start_time
58
+ # @return [Time] The time the event ends.
59
+ attr_reader :scheduled_end_time
60
+ alias end_time scheduled_end_time
61
+ alias end_at scheduled_end_time
62
+ # @return [:guild_only] The privacy level of the event.
63
+ attr_reader :privacy_level
64
+ # @return [:scheduled, :active, :completed, :canceled] The status of the event.
65
+ attr_reader :status
66
+ # @return [:stage_instance, :voice, :external] The type of the event.
67
+ attr_reader :entity_type
68
+ # @return [Discorb::Snowflake] The ID of the entity the event is for.
69
+ attr_reader :entity_id
70
+ # @return [Discorb::ScheduledEvent::Metadata] The metadata of the event.
71
+ attr_reader :metadata
72
+ # @return [Integer] The user count of the event.
73
+ attr_reader :user_count
74
+
75
+ # @!attribute [r] guild
76
+ # @return [Discorb::Guild, nil] The guild of the event.
77
+ # @!attribute [r] channel
78
+ # @return [Discorb::Channel, nil] The channel of the event.
79
+ # Only present if the event will do in stage instance or voice channel.
80
+ # @!attribute [r] creator
81
+ # @return [Discorb::User] The user who created the event.#
82
+ # @!attribute [r] time
83
+ # @return [Range<Time>] The time range of the event.
84
+
85
+ def guild
86
+ @client.guilds[@guild_id]
87
+ end
88
+
89
+ def channel
90
+ @client.channels[@channel_id]
91
+ end
92
+
93
+ def creator
94
+ @creator || @client.users[@creator_id]
95
+ end
96
+
97
+ def time
98
+ @scheduled_start_time..@scheduled_end_time
99
+ end
100
+
101
+ #
102
+ # Create a scheduled event for the guild.
103
+ # @async
104
+ #
105
+ # @param [:stage_instance, :voice, :external] type The type of event to create.
106
+ # @param [String] name The name of the event.
107
+ # @param [String] description The description of the event.
108
+ # @param [Time] start_time The start time of the event.
109
+ # @param [Time, nil] end_time The end time of the event. Defaults to `nil`.
110
+ # @param [Discorb::Channel, Discorb::Snowflake, nil] channel The channel to run the event in.
111
+ # @param [String, nil] location The location of the event. Defaults to `nil`.
112
+ # @param [:guild_only] privacy_level The privacy level of the event. This must be `:guild_only`.
113
+ # @param [:active, :completed, :canceled] status The status of the event.
114
+ #
115
+ # @return [Async::Task<Discorb::ScheduledEvent>] The event that was created.
116
+ #
117
+ # @see Event#start
118
+ # @see Event#cancel
119
+ # @see Event#complete
120
+ #
121
+ def edit(
122
+ type: Discorb::Unset,
123
+ name: Discorb::Unset,
124
+ description: Discorb::Unset,
125
+ start_time: Discorb::Unset,
126
+ end_time: Discorb::Unset,
127
+ privacy_level: Discorb::Unset,
128
+ location: Discorb::Unset,
129
+ channel: Discorb::Unset,
130
+ status: Discorb::Unset
131
+ )
132
+ Async do
133
+ payload =
134
+ case type == Discorb::Unset ? @entity_type : type
135
+ when :stage_instance
136
+ unless channel
137
+ raise ArgumentError,
138
+ "channel must be provided for stage_instance events"
139
+ end
140
+
141
+ {
142
+ name: name,
143
+ description: description,
144
+ scheduled_start_time: start_time.iso8601,
145
+ scheduled_end_time: end_time&.iso8601,
146
+ privacy_level:
147
+ Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) ||
148
+ Discorb::Unset,
149
+ channel_id: channel&.id,
150
+ entity_type:
151
+ Discorb::ScheduledEvent::ENTITY_TYPE.key(:stage_instance),
152
+ status:
153
+ Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset
154
+ }.reject { |_, v| v == Discorb::Unset }
155
+ when :voice
156
+ unless channel
157
+ raise ArgumentError, "channel must be provided for voice events"
158
+ end
159
+
160
+ {
161
+ name: name,
162
+ description: description,
163
+ scheduled_start_time: start_time.iso8601,
164
+ scheduled_end_time: end_time&.iso8601,
165
+ privacy_level:
166
+ Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) ||
167
+ Discorb::Unset,
168
+ channel_id: channel&.id,
169
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:voice),
170
+ status:
171
+ Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset
172
+ }.reject { |_, v| v == Discorb::Unset }
173
+ when :external
174
+ unless location
175
+ raise ArgumentError,
176
+ "location must be provided for external events"
177
+ end
178
+ unless end_time
179
+ raise ArgumentError,
180
+ "end_time must be provided for external events"
181
+ end
182
+
183
+ {
184
+ name: name,
185
+ description: description,
186
+ channel_id: nil,
187
+ scheduled_start_time: start_time.iso8601,
188
+ scheduled_end_time: end_time.iso8601,
189
+ privacy_level:
190
+ Discorb::ScheduledEvent::PRIVACY_LEVEL.key(privacy_level) ||
191
+ Discorb::Unset,
192
+ entity_type: Discorb::ScheduledEvent::ENTITY_TYPE.key(:external),
193
+ entity_metadata: {
194
+ location: location
195
+ },
196
+ status:
197
+ Discorb::ScheduledEvent::STATUS.key(status) || Discorb::Unset
198
+ }.reject { |_, v| v == Discorb::Unset }
199
+ else
200
+ raise ArgumentError, "Invalid scheduled event type: #{type}"
201
+ end
202
+ @client
203
+ .http
204
+ .request(
205
+ Route.new(
206
+ "/guilds/#{@guild_id}/scheduled-events/#{@id}",
207
+ "//guilds/:guild_id/scheduled-events/:scheduled_event_id",
208
+ :patch
209
+ ),
210
+ payload
211
+ )
212
+ .wait
213
+ end
214
+ end
215
+
216
+ alias modify edit
217
+
218
+ #
219
+ # Starts the event. Shortcut for `edit(status: :active)`.
220
+ #
221
+ def start
222
+ edit(status: :active)
223
+ end
224
+
225
+ #
226
+ # Completes the event. Shortcut for `edit(status: :completed)`.
227
+ #
228
+ def complete
229
+ edit(status: :completed)
230
+ end
231
+
232
+ alias finish complete
233
+
234
+ #
235
+ # Cancels the event. Shortcut for `edit(status: :canceled)`.
236
+ #
237
+ def cancel
238
+ edit(status: :canceled)
239
+ end
240
+
241
+ #
242
+ # Deletes the event.
243
+ # @async
244
+ #
245
+ # @return [Async::Task<void>] The task.
246
+ #
247
+ def delete
248
+ Async do
249
+ @client
250
+ .http
251
+ .request(
252
+ Route.new(
253
+ "/guilds/#{@guild_id}/scheduled-events/#{@id}",
254
+ "//guilds/:guild_id/scheduled-events/:scheduled_event_id",
255
+ :delete
256
+ )
257
+ )
258
+ .wait
259
+ end
260
+ end
261
+
262
+ alias destroy delete
263
+
264
+ #
265
+ # Fetches the event users.
266
+ # @async
267
+ #
268
+ # @note You can fetch all of members by not specifying a parameter.
269
+ #
270
+ # @param [Integer] limit The maximum number of users to fetch. Defaults to `100`.
271
+ # @param [#to_s] after The ID of the user to start fetching from. Defaults to `nil`.
272
+ # @param [#to_s] before The ID of the user to stop fetching at. Defaults to `nil`.
273
+ # @param [Boolean] with_member Whether to include the member object of the event. Defaults to `false`.
274
+ # This should be used for manual fetching of members.
275
+ #
276
+ # @return [Async::Task<Array<Discorb::Member>>] The event users.
277
+ #
278
+ def fetch_users(limit = nil, before: nil, after: nil, with_member: true)
279
+ Async do
280
+ if limit.nil?
281
+ after = 0
282
+ res = []
283
+ loop do
284
+ _resp, users =
285
+ @client
286
+ .http
287
+ .request(
288
+ Route.new(
289
+ "/guilds/#{@guild_id}/scheduled-events/#{@id}/users?limit=100&after=#{after}&with_member=true",
290
+ "//guilds/:guild_id/scheduled-events/:scheduled_event_id/users",
291
+ :get
292
+ )
293
+ )
294
+ .wait
295
+ break if users.empty?
296
+
297
+ res +=
298
+ users.map do |u|
299
+ Member.new(@client, @guild_id, u[:user], u[:member])
300
+ end
301
+ after = users.last[:user][:id]
302
+ end
303
+ res
304
+ else
305
+ params =
306
+ {
307
+ limit: limit,
308
+ before: Discorb::Utils.try(before, :id),
309
+ after: Discorb::Utils.try(after, :id),
310
+ with_member: with_member
311
+ }.filter { |_k, v| !v.nil? }.to_h
312
+ _resp, messages =
313
+ @client
314
+ .http
315
+ .request(
316
+ Route.new(
317
+ "/channels/#{channel_id.wait}/messages?#{URI.encode_www_form(params)}",
318
+ "//channels/:channel_id/messages",
319
+ :get
320
+ )
321
+ )
322
+ .wait
323
+ messages.map do |m|
324
+ Message.new(@client, m.merge({ guild_id: @guild_id.to_s }))
325
+ end
326
+ end
327
+ end
328
+ end
329
+
330
+ alias fetch_members fetch_users
331
+
332
+ private
333
+
334
+ def _set_data(data)
335
+ @id = Snowflake.new(data[:id])
336
+ @guild_id = Snowflake.new(data[:guild_id])
337
+ @channel_id = data[:channel_id] && Snowflake.new(data[:channel_id])
338
+ @creator_id = data[:creator_id] && Snowflake.new(data[:creator_id])
339
+ @name = data[:name]
340
+ @description = data[:description]
341
+ @scheduled_start_time = Time.iso8601(data[:scheduled_start_time])
342
+ @scheduled_end_time =
343
+ data[:scheduled_end_time] && Time.iso8601(data[:scheduled_end_time])
344
+ @privacy_level = :guild_only # data[:privacy_level]
345
+ @status = STATUS[data[:status]]
346
+ @entity_type = ENTITY_TYPE[data[:entity_type]]
347
+ @entity_id = data[:entity_id] && Snowflake.new(data[:entity_id])
348
+ @entity_metadata =
349
+ data[:entity_metadata] && Metadata.new(data[:entity_metadata])
350
+ @creator =
351
+ @client.users[@creator_id] ||
352
+ (data[:creator] && User.new(@client, data[:creator]))
353
+ @user_count = data[:user_count]
354
+ end
355
+
356
+ class << self
357
+ attr_reader :status, :entity_type, :privacy_level
358
+ end
359
+ end
360
+ end