discorb 0.5.5 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,9 +4,9 @@ module Discorb
4
4
  #
5
5
  # Handles application commands.
6
6
  #
7
- module Command
7
+ module ApplicationCommand
8
8
  #
9
- # Module to handle commands.
9
+ # Module to handle application commands.
10
10
  #
11
11
  module Handler
12
12
  #
@@ -24,15 +24,16 @@ module Discorb
24
24
  # | `:type` | `Object` | Type of the option. |
25
25
  # | `:choice` | `Hash{String => String, Integer, Float}` | Type of the option. |
26
26
  #
27
- # @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to.
27
+ # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to set the command to. `false` to global command, `nil` to use default.
28
28
  # @param [Proc] block Command block.
29
29
  #
30
- # @return [Discorb::Command::Command::SlashCommand]
30
+ # @return [Discorb::ApplicationCommand::Command::SlashCommand] Command object.
31
31
  #
32
32
  # @see file:docs/application_command.md#register-slash-command
33
+ # @see file:docs/cli/setup.md
33
34
  #
34
- def slash(command_name, description, options = {}, guild_ids: [], &block)
35
- command = Discorb::Command::Command::SlashCommand.new(command_name, description, options, guild_ids, block, 1, "")
35
+ def slash(command_name, description, options = {}, guild_ids: nil, &block)
36
+ command = Discorb::ApplicationCommand::Command::SlashCommand.new(command_name, description, options, guild_ids, block, 1, "")
36
37
  @commands << command
37
38
  @bottom_commands << command
38
39
  command
@@ -43,18 +44,19 @@ module Discorb
43
44
  #
44
45
  # @param [String] command_name Command name.
45
46
  # @param [String] description Command description.
46
- # @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to.
47
+ # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to set the command to. `false` to global command, `nil` to use default.
47
48
  #
48
- # @yield Block to execute as the command. It can be used to define sub-commands.
49
- # @yieldself [Discorb::Command::Command::GroupCommand] Group command.
49
+ # @yield Block to yield with the command.
50
+ # @yieldparam [Discorb::ApplicationCommand::Command::GroupCommand] group Group command.
50
51
  #
51
- # @return [Discorb::Command::Command::GroupCommand] Command object.
52
+ # @return [Discorb::ApplicationCommand::Command::GroupCommand] Command object.
52
53
  #
53
54
  # @see file:docs/slash_command.md
55
+ # @see file:docs/cli/setup.md
54
56
  #
55
- def slash_group(command_name, description, guild_ids: [], &block)
56
- command = Discorb::Command::Command::GroupCommand.new(command_name, description, guild_ids, nil, self)
57
- command.instance_eval(&block) if block_given?
57
+ def slash_group(command_name, description, guild_ids: nil, &block)
58
+ command = Discorb::ApplicationCommand::Command::GroupCommand.new(command_name, description, guild_ids, nil, self)
59
+ command.yield_self(&block) if block_given?
58
60
  @commands << command
59
61
  command
60
62
  end
@@ -63,16 +65,16 @@ module Discorb
63
65
  # Add message context menu command.
64
66
  #
65
67
  # @param [String] command_name Command name.
66
- # @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to.
68
+ # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to set the command to. `false` to global command, `nil` to use default.
67
69
  # @param [Proc] block Command block.
68
70
  # @yield [interaction, message] Block to execute.
69
- # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] Interaction object.
70
- # @yieldparam [Discorb::Message] user Message object.
71
+ # @yieldparam [Discorb::ApplicationCommandInteraction::UserMenuCommand] interaction Interaction object.
72
+ # @yieldparam [Discorb::Message] message Message object.
71
73
  #
72
- # @return [Discorb::Command::Command] Command object.
74
+ # @return [Discorb::ApplicationCommand::Command] Command object.
73
75
  #
74
- def message_command(command_name, guild_ids: [], &block)
75
- command = Discorb::Command::Command.new(command_name, guild_ids, block, 3)
76
+ def message_command(command_name, guild_ids: nil, &block)
77
+ command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 3)
76
78
  @commands << command
77
79
  command
78
80
  end
@@ -81,16 +83,16 @@ module Discorb
81
83
  # Add user context menu command.
82
84
  #
83
85
  # @param [String] command_name Command name.
84
- # @param [Array<#to_s>] guild_ids Guild IDs to restrict the command to.
86
+ # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to set the command to. `false` to global command, `nil` to use default.
85
87
  # @param [Proc] block Command block.
86
88
  # @yield [interaction, user] Block to execute.
87
- # @yieldparam [Discorb::CommandInteraction::UserMenuCommand] Interaction object.
89
+ # @yieldparam [Discorb::ApplicationCommandInteraction::UserMenuCommand] interaction Interaction object.
88
90
  # @yieldparam [Discorb::User] user User object.
89
91
  #
90
- # @return [Discorb::Command::Command] Command object.
92
+ # @return [Discorb::ApplicationCommand::Command] Command object.
91
93
  #
92
- def user_command(command_name, guild_ids: [], &block)
93
- command = Discorb::Command::Command.new(command_name, guild_ids, block, 2)
94
+ def user_command(command_name, guild_ids: nil, &block)
95
+ command = Discorb::ApplicationCommand::Command.new(command_name, guild_ids, block, 2)
94
96
  @commands << command
95
97
  command
96
98
  end
@@ -100,20 +102,32 @@ module Discorb
100
102
  # @see Client#initialize
101
103
  #
102
104
  # @param [String] token Bot token.
105
+ # @param [Array<#to_s>, false, nil] guild_ids Guild IDs to use as default. If `false` is given, it will be global command.
106
+ #
103
107
  # @note `token` parameter only required if you don't run client.
104
108
  #
105
- def setup_commands(token = nil)
109
+ def setup_commands(token = nil, guild_ids: nil)
106
110
  Async do
107
111
  @token ||= token
108
112
  @http = HTTP.new(self)
109
- global_commands = @commands.select { |c| c.guild_ids.empty? }
110
- guild_ids = Set[*@commands.map(&:guild_ids).flatten]
113
+ global_commands = @commands.select { |c| c.guild_ids == false or c.guild_ids == [] }
114
+ local_commands = @commands.select { |c| c.guild_ids.is_a?(Array) and c.guild_ids.any? }
115
+ default_commands = @commands.select { |c| c.guild_ids.nil? }
116
+ if guild_ids.is_a?(Array)
117
+ default_commands.each do |command|
118
+ command.instance_variable_set(:@guild_ids, guild_ids)
119
+ end
120
+ local_commands += default_commands
121
+ else
122
+ global_commands += default_commands
123
+ end
124
+ final_guild_ids = local_commands.map(&:guild_ids).flatten.map(&:to_s).uniq
111
125
  app_info = fetch_application.wait
112
126
  http.put("/applications/#{app_info.id}/commands", global_commands.map(&:to_hash)).wait unless global_commands.empty?
113
- guild_ids.each do |guild_id|
114
- commands = @commands.select { |c| c.guild_ids.include?(guild_id) }
127
+ final_guild_ids.each do |guild_id|
128
+ commands = local_commands.select { |c| c.guild_ids.include?(guild_id) }
115
129
  http.put("/applications/#{app_info.id}/guilds/#{guild_id}/commands", commands.map(&:to_hash)).wait
116
- end unless guild_ids.empty?
130
+ end unless final_guild_ids.empty?
117
131
  @log.info "Successfully setup commands"
118
132
  end
119
133
  end
@@ -146,10 +160,10 @@ module Discorb
146
160
  # @!visibility private
147
161
  def initialize(name, guild_ids, block, type)
148
162
  @name = name
149
- @guild_ids = guild_ids.map(&:to_s)
163
+ @guild_ids = guild_ids&.map(&:to_s)
150
164
  @block = block
151
165
  @raw_type = type
152
- @type = Discorb::Command::Command.types[type]
166
+ @type = Discorb::ApplicationCommand::Command.types[type]
153
167
  @type_raw = type
154
168
  @id_map = Discorb::Dictionary.new
155
169
  end
@@ -174,12 +188,8 @@ module Discorb
174
188
 
175
189
  # @!visibility private
176
190
  def initialize(name, description, options, guild_ids, block, type, parent)
191
+ super(name, guild_ids, block, type)
177
192
  @description = description
178
- @name = name
179
- @guild_ids = guild_ids.map(&:to_s)
180
- @block = block
181
- @type = Discorb::Command::Command.types[type]
182
- @type_raw = 1
183
193
  @options = options
184
194
  @id = nil
185
195
  @parent = parent
@@ -241,7 +251,7 @@ module Discorb
241
251
  # Represents the command with subcommands.
242
252
  #
243
253
  class GroupCommand < Command
244
- # @return [Array<Discorb::Command::Command::SlashCommand, Discorb::Command::Command::SubcommandGroup>] The subcommands of the command.
254
+ # @return [Array<Discorb::ApplicationCommand::Command::SlashCommand, Discorb::ApplicationCommand::Command::SubcommandGroup>] The subcommands of the command.
245
255
  attr_reader :commands
246
256
  # @return [String] The description of the command.
247
257
  attr_reader :description
@@ -258,11 +268,11 @@ module Discorb
258
268
  #
259
269
  # Add new subcommand.
260
270
  #
261
- # @param (see Discorb::Command::Handler#slash)
262
- # @return [Discorb::Command::Command::SlashCommand] The added subcommand.
271
+ # @param (see Discorb::ApplicationCommand::Handler#slash)
272
+ # @return [Discorb::ApplicationCommand::Command::SlashCommand] The added subcommand.
263
273
  #
264
274
  def slash(command_name, description, options = {}, &block)
265
- command = Discorb::Command::Command::SlashCommand.new(command_name, description, options, [], block, 1, @name)
275
+ command = Discorb::ApplicationCommand::Command::SlashCommand.new(command_name, description, options, [], block, 1, @name)
266
276
  @client.bottom_commands << command
267
277
  @commands << command
268
278
  command
@@ -274,16 +284,16 @@ module Discorb
274
284
  # @param [String] command_name Group name.
275
285
  # @param [String] description Group description.
276
286
  #
277
- # @yield Block to execute as the command. It can be used to define sub-commands.
278
- # @yieldself [Discorb::Command::Command::SubcommandGroup] Group command.
287
+ # @yield Block to yield with the command.
288
+ # @yieldparam [Discorb::ApplicationCommand::Command::SubcommandGroup] group Group command.
279
289
  #
280
- # @return [Discorb::Command::Command::SubcommandGroup] Command object.
290
+ # @return [Discorb::ApplicationCommand::Command::SubcommandGroup] Command object.
281
291
  #
282
292
  # @see file:docs/slash_command.md
283
293
  #
284
294
  def group(command_name, description, &block)
285
- command = Discorb::Command::Command::SubcommandGroup.new(command_name, description, @name, @client)
286
- command.instance_eval(&block) if block_given?
295
+ command = Discorb::ApplicationCommand::Command::SubcommandGroup.new(command_name, description, @name, @client)
296
+ command.yield_self(&block) if block_given?
287
297
  @commands << command
288
298
  command
289
299
  end
@@ -332,7 +342,7 @@ module Discorb
332
342
  # Represents the subcommand group.
333
343
  #
334
344
  class SubcommandGroup < GroupCommand
335
- # @return [Array<Discorb::Command::Command::SlashCommand>] The subcommands of the command.
345
+ # @return [Array<Discorb::ApplicationCommand::Command::SlashCommand>] The subcommands of the command.
336
346
  attr_reader :commands
337
347
 
338
348
  # @!visibility private
@@ -349,11 +359,11 @@ module Discorb
349
359
 
350
360
  #
351
361
  # Add new subcommand.
352
- # @param (see Discorb::Command::Handler#slash)
353
- # @return [Discorb::Command::Command::SlashCommand] The added subcommand.
362
+ # @param (see Discorb::ApplicationCommand::Handler#slash)
363
+ # @return [Discorb::ApplicationCommand::Command::SlashCommand] The added subcommand.
354
364
  #
355
365
  def slash(command_name, description, options = {}, &block)
356
- command = Discorb::Command::Command::SlashCommand.new(command_name, description, options, [], block, 1, @parent + " " + @name)
366
+ command = Discorb::ApplicationCommand::Command::SlashCommand.new(command_name, description, options, [], block, 1, @parent + " " + @name)
357
367
  @commands << command
358
368
  @client.bottom_commands << command
359
369
  command
@@ -171,7 +171,7 @@ module Discorb
171
171
  #
172
172
  # @param [String] reason The reason of deleting the channel.
173
173
  #
174
- # @return [self] The deleted channel.
174
+ # @return [Async::Task<self>] The deleted channel.
175
175
  #
176
176
  def delete!(reason: nil)
177
177
  Async do
@@ -194,7 +194,7 @@ module Discorb
194
194
  # @param [Discorb::CategoryChannel] parent The parent of channel.
195
195
  # @param [String] reason The reason of moving the channel.
196
196
  #
197
- # @return [self] The moved channel.
197
+ # @return [Async::Task<self>] The moved channel.
198
198
  #
199
199
  def move(position, lock_permissions: false, parent: :unset, reason: nil)
200
200
  Async do
@@ -273,7 +273,7 @@ module Discorb
273
273
  # @param [Integer] archive_in Alias of `default_auto_archive_duration`.
274
274
  # @param [String] reason The reason of editing the channel.
275
275
  #
276
- # @return [self] The edited channel.
276
+ # @return [Async::Task<self>] The edited channel.
277
277
  #
278
278
  def edit(name: :unset, position: :unset, category: :unset, parent: :unset,
279
279
  topic: :unset, nsfw: :unset, announce: :unset,
@@ -310,7 +310,7 @@ module Discorb
310
310
  # @param [String] name The name of the webhook.
311
311
  # @param [Discorb::Image] avatar The avatar of the webhook.
312
312
  #
313
- # @return [Discorb::Webhook::IncomingWebhook] The created webhook.
313
+ # @return [Async::Task<Discorb::Webhook::IncomingWebhook>] The created webhook.
314
314
  #
315
315
  def create_webhook(name, avatar: nil)
316
316
  Async do
@@ -327,7 +327,7 @@ module Discorb
327
327
  # @macro async
328
328
  # @macro http
329
329
  #
330
- # @return [Array<Discorb::Webhook>] The webhooks in the channel.
330
+ # @return [Async::Task<Array<Discorb::Webhook>>] The webhooks in the channel.
331
331
  #
332
332
  def fetch_webhooks
333
333
  Async do
@@ -419,7 +419,7 @@ module Discorb
419
419
  # @macro async
420
420
  # @macro http
421
421
  #
422
- # @return [Array<Discorb::Invite>] The invites in the channel.
422
+ # @return [Async::Task<Array<Discorb::Invite>>] The invites in the channel.
423
423
  #
424
424
  def fetch_invites
425
425
  Async do
@@ -440,7 +440,7 @@ module Discorb
440
440
  # @note if it's `false` it may return existing invite.
441
441
  # @param [String] reason The reason of creating the invite.
442
442
  #
443
- # @return [Invite] The created invite.
443
+ # @return [Async::Task<Invite>] The created invite.
444
444
  #
445
445
  def create_invite(max_age: nil, max_uses: nil, temporary: false, unique: false, reason: nil)
446
446
  Async do
@@ -487,7 +487,7 @@ module Discorb
487
487
  # @macro async
488
488
  # @macro http
489
489
  #
490
- # @return [Array<Discorb::Message>] The pinned messages in the channel.
490
+ # @return [Async::Task<Array<Discorb::Message>>] The pinned messages in the channel.
491
491
  #
492
492
  def fetch_pins
493
493
  Async do
@@ -535,7 +535,7 @@ module Discorb
535
535
  # @param [Boolean] public Whether the thread is public.
536
536
  # @param [String] reason The reason of starting the thread.
537
537
  #
538
- # @return [Discorb::ThreadChannel] The started thread.
538
+ # @return [Async::Task<Discorb::ThreadChannel>] The started thread.
539
539
  #
540
540
  def start_thread(name, message: nil, auto_archive_duration: 1440, public: true, reason: nil)
541
541
  Async do
@@ -560,7 +560,7 @@ module Discorb
560
560
  # @macro async
561
561
  # @macro http
562
562
  #
563
- # @return [Array<Discorb::ThreadChannel>] The archived threads in the channel.
563
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The archived threads in the channel.
564
564
  #
565
565
  def fetch_archived_public_threads
566
566
  Async do
@@ -574,7 +574,7 @@ module Discorb
574
574
  # @macro async
575
575
  # @macro http
576
576
  #
577
- # @return [Array<Discorb::ThreadChannel>] The archived private threads in the channel.
577
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The archived private threads in the channel.
578
578
  #
579
579
  def fetch_archived_private_threads
580
580
  Async do
@@ -591,7 +591,7 @@ module Discorb
591
591
  # @param [Integer] limit The limit of threads to fetch.
592
592
  # @param [Time] before <description>
593
593
  #
594
- # @return [Array<Discorb::ThreadChannel>] The joined archived private threads in the channel.
594
+ # @return [Async::Task<Array<Discorb::ThreadChannel>>] The joined archived private threads in the channel.
595
595
  #
596
596
  def fetch_joined_archived_private_threads(limit: nil, before: nil)
597
597
  Async do
@@ -660,7 +660,7 @@ module Discorb
660
660
  # @param [Symbol] rtc_region The region of the voice channel.
661
661
  # @param [String] reason The reason of editing the voice channel.
662
662
  #
663
- # @return [self] The edited voice channel.
663
+ # @return [Async::Task<self>] The edited voice channel.
664
664
  #
665
665
  def edit(name: :unset, position: :unset, bitrate: :unset, user_limit: :unset, rtc_region: :unset, reason: nil)
666
666
  Async do
@@ -728,7 +728,7 @@ module Discorb
728
728
  # @param [Symbol] rtc_region The region of the stage channel.
729
729
  # @param [String] reason The reason of editing the stage channel.
730
730
  #
731
- # @return [self] The edited stage channel.
731
+ # @return [Async::Task<self>] The edited stage channel.
732
732
  #
733
733
  def edit(name: :unset, position: :unset, bitrate: :unset, rtc_region: :unset, reason: nil)
734
734
  Async do
@@ -753,7 +753,7 @@ module Discorb
753
753
  # @param [Boolean] public Whether the stage instance is public or not.
754
754
  # @param [String] reason The reason of starting the stage instance.
755
755
  #
756
- # @return [Discorb::StageInstance] The started stage instance.
756
+ # @return [Async::Task<Discorb::StageInstance>] The started stage instance.
757
757
  #
758
758
  def start(topic, public: false, reason: nil)
759
759
  Async do
@@ -767,8 +767,8 @@ module Discorb
767
767
  # @macro async
768
768
  # @macro http
769
769
  #
770
- # @return [StageInstance] The current stage instance.
771
- # @return [nil] If there is no current stage instance.
770
+ # @return [Async::Task<StageInstance>] The current stage instance.
771
+ # @return [Async::Task<nil>] If there is no current stage instance.
772
772
  #
773
773
  def fetch_stage_instance
774
774
  Async do
@@ -853,7 +853,7 @@ module Discorb
853
853
  # @param [Boolean] locked Whether the thread is locked or not.
854
854
  # @param [String] reason The reason of editing the thread.
855
855
  #
856
- # @return [self] The edited thread.
856
+ # @return [Async::Task<self>] The edited thread.
857
857
  #
858
858
  # @see #archive
859
859
  # @see #lock
@@ -41,7 +41,7 @@ module Discorb
41
41
  attr_reader :messages
42
42
  # @return [Discorb::Logger] The logger.
43
43
  attr_reader :log
44
- # @return [Array<Discorb::Command::Command>] The commands that the client is using.
44
+ # @return [Array<Discorb::ApplicationCommand::Command>] The commands that the client is using.
45
45
  attr_reader :commands
46
46
  # @return [Float] The ping of the client.
47
47
  # @note This will be calculated from heartbeat and heartbeat_ack.
@@ -91,6 +91,7 @@ module Discorb
91
91
  @commands = []
92
92
  @bottom_commands = []
93
93
  @status = :initialized
94
+ set_default_events
94
95
  end
95
96
 
96
97
  #
@@ -99,14 +100,15 @@ module Discorb
99
100
  #
100
101
  # @param [Symbol] event_name The name of the event.
101
102
  # @param [Symbol] id Custom ID of the event.
102
- # @param [Hash] discriminator The discriminator of the event.
103
+ # @param [Hash] metadata The metadata of the event.
103
104
  # @param [Proc] block The block to execute when the event is triggered.
104
105
  #
105
106
  # @return [Discorb::Event] The event.
106
107
  #
107
- def on(event_name, id: nil, **discriminator, &block)
108
- ne = Event.new(block, id, discriminator)
108
+ def on(event_name, id: nil, **metadata, &block)
109
+ ne = Event.new(block, id, metadata)
109
110
  @events[event_name] ||= []
111
+ @events[event_name].delete_if { |e| e.metadata[:override] }
110
112
  @events[event_name] << ne
111
113
  ne
112
114
  end
@@ -118,12 +120,9 @@ module Discorb
118
120
  #
119
121
  # @return [Discorb::Event] The event.
120
122
  #
121
- def once(event_name, id: nil, **discriminator, &block)
122
- discriminator[:once] = true
123
- ne = Event.new(block, id, discriminator)
124
- @events[event_name] ||= []
125
- @events[event_name] << ne
126
- ne
123
+ def once(event_name, id: nil, **metadata, &block)
124
+ metadata[:once] = true
125
+ on(event_name, id: id, **metadata, &block)
127
126
  end
128
127
 
129
128
  #
@@ -174,23 +173,17 @@ module Discorb
174
173
  end
175
174
  @log.debug "Dispatching event #{event_name}"
176
175
  events.each do |block|
177
- lambda { |event_args|
176
+ Async do
178
177
  Async(annotation: "Discorb event: #{event_name}") do |task|
179
178
  if block.is_a?(Discorb::Event)
180
- @events[event_name].delete(block) if block.discriminator[:once]
179
+ @events[event_name].delete(block) if block.metadata[:once]
181
180
  end
182
- block.call(*event_args)
181
+ block.call(*args)
183
182
  @log.debug "Dispatched proc with ID #{block.id.inspect}"
184
183
  rescue StandardError, ScriptError => e
185
- message = "An error occurred while dispatching proc with ID #{block.id.inspect}\n#{e.full_message}"
186
- dispatch(:error, event_name, event_args, e)
187
- if @log.out
188
- @log.error message
189
- else
190
- warn message
191
- end
184
+ dispatch(:error, event_name, args, e)
192
185
  end
193
- }.call(args)
186
+ end
194
187
  end
195
188
  end
196
189
  end
@@ -202,7 +195,7 @@ module Discorb
202
195
  #
203
196
  # @param [#to_s] id <description>
204
197
  #
205
- # @return [Discorb::User] The user.
198
+ # @return [Async::Task<Discorb::User>] The user.
206
199
  #
207
200
  # @raise [Discorb::NotFoundError] If the user doesn't exist.
208
201
  #
@@ -220,7 +213,7 @@ module Discorb
220
213
  #
221
214
  # @param [#to_s] id The ID of the channel.
222
215
  #
223
- # @return [Discorb::Channel] The channel.
216
+ # @return [Async::Task<Discorb::Channel>] The channel.
224
217
  #
225
218
  # @raise [Discorb::NotFoundError] If the channel doesn't exist.
226
219
  #
@@ -238,7 +231,7 @@ module Discorb
238
231
  #
239
232
  # @param [#to_s] id <description>
240
233
  #
241
- # @return [Discorb::Guild] The guild.
234
+ # @return [Async::Task<Discorb::Guild>] The guild.
242
235
  #
243
236
  # @raise [Discorb::NotFoundError] If the guild doesn't exist.
244
237
  #
@@ -258,7 +251,7 @@ module Discorb
258
251
  # @param [Boolean] with_count Whether to include the count of the invite.
259
252
  # @param [Boolean] with_expiration Whether to include the expiration of the invite.
260
253
  #
261
- # @return [Discorb::Invite] The invite.
254
+ # @return [Async::Task<Discorb::Invite>] The invite.
262
255
  #
263
256
  def fetch_invite(code, with_count: false, with_expiration: false)
264
257
  Async do
@@ -275,7 +268,7 @@ module Discorb
275
268
  #
276
269
  # @param [Boolean] force Whether to force the fetch.
277
270
  #
278
- # @return [Discorb::Application] The application.
271
+ # @return [Async::Task<Discorb::Application>] The application.
279
272
  #
280
273
  def fetch_application(force: false)
281
274
  Async do
@@ -292,7 +285,7 @@ module Discorb
292
285
  # @macro async
293
286
  # @macro http
294
287
  #
295
- # @return [Array<Discorb::Sticker::Pack>] The packs.
288
+ # @return [Async::Task<Array<Discorb::Sticker::Pack>>] The packs.
296
289
  #
297
290
  def fetch_nitro_sticker_packs
298
291
  Async do
@@ -369,10 +362,11 @@ module Discorb
369
362
  def extend(mod)
370
363
  if mod.respond_to?(:events)
371
364
  @events.each_value do |event|
372
- event.delete_if { |c| c.discriminator[:extension] == mod.name }
365
+ event.delete_if { |c| c.metadata[:extension] == mod.name }
373
366
  end
374
367
  mod.events.each do |name, events|
375
- @events[name] = [] if @events[name].nil?
368
+ @events[name] ||= []
369
+ @events[name].delete_if { |c| c.metadata[:override] }
376
370
  events.each do |event|
377
371
  @events[name] << event
378
372
  end
@@ -391,16 +385,20 @@ module Discorb
391
385
  end
392
386
 
393
387
  include Discorb::Gateway::Handler
394
- include Discorb::Command::Handler
388
+ include Discorb::ApplicationCommand::Handler
395
389
 
396
390
  #
397
391
  # Starts the client.
398
392
  # @note This method behavior will change by CLI.
399
393
  # @see file:docs/cli.md
400
394
  #
401
- # @param [String] token The token to use.
395
+ # @param [String, nil] token The token to use.
402
396
  #
403
- def run(token)
397
+ # @note If the token is nil, you should use `discorb run` with the `-t` or `--token` option.
398
+ #
399
+ def run(token = nil)
400
+ token ||= ENV["DISCORB_CLI_TOKEN"]
401
+ raise ArgumentError, "Token is not specified, and -t/--token is not specified" if token.nil?
404
402
  case ENV["DISCORB_CLI_FLAG"]
405
403
  when nil
406
404
  start_client(token)
@@ -436,7 +434,14 @@ module Discorb
436
434
  end
437
435
  start_client(token)
438
436
  when "setup"
439
- setup_commands(token)
437
+ guild_ids = "global"
438
+ if guilds = ENV["DISCORB_SETUP_GUILDS"]
439
+ guild_ids = guilds.split(",")
440
+ end
441
+ if guild_ids == ["global"]
442
+ guild_ids = false
443
+ end
444
+ setup_commands(token, guild_ids: guild_ids).wait
440
445
  end
441
446
  end
442
447
 
@@ -460,7 +465,7 @@ module Discorb
460
465
  }
461
466
  @token = token.to_s
462
467
  @close_condition = Async::Condition.new
463
- main_task = Async do
468
+ @main_task = Async do
464
469
  @status = :running
465
470
  connect_gateway(true).wait
466
471
  rescue
@@ -469,7 +474,14 @@ module Discorb
469
474
  raise
470
475
  end
471
476
  @close_condition.wait
472
- main_task.stop
477
+ @main_task.stop
478
+ end
479
+ end
480
+
481
+ def set_default_events
482
+ on :error, override: true do |event_name, _args, e|
483
+ message = "An error occurred while dispatching #{event_name}:\n#{e.full_message}"
484
+ @log.error message, fallback: $stderr
473
485
  end
474
486
  end
475
487
  end