Class: OnyxCord::Interactions::Registry
- Inherits:
-
Object
- Object
- OnyxCord::Interactions::Registry
- Defined in:
- lib/onyxcord/interactions/registry.rb
Instance Attribute Summary collapse
-
#bot ⇒ Object
readonly
Returns the value of attribute bot.
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
Instance Method Summary collapse
-
#initialize(bot) ⇒ Registry
constructor
A new instance of Registry.
- #message(name, **attributes, &block) ⇒ Object
- #register(command) ⇒ Object
-
#schedule_delayed_command_sync(delay: 2, server_id: nil) ⇒ Thread?
Schedule a debounced sync.
- #slash(name, description:, **attributes, &block) ⇒ Object
-
#sync!(server_id: nil, delete_unknown: false) ⇒ void
Immediately sync all commands to Discord.
-
#sync_global_commands ⇒ void
Sync global application commands.
-
#sync_server_commands(server_id) ⇒ void
Sync guild application commands.
- #user(name, **attributes, &block) ⇒ Object
Constructor Details
#initialize(bot) ⇒ Registry
Returns a new instance of Registry.
8 9 10 11 12 13 |
# File 'lib/onyxcord/interactions/registry.rb', line 8 def initialize(bot) @bot = bot @commands = {} @pending_sync = nil @sync_mutex = Mutex.new end |
Instance Attribute Details
#bot ⇒ Object (readonly)
Returns the value of attribute bot.
6 7 8 |
# File 'lib/onyxcord/interactions/registry.rb', line 6 def bot @bot end |
#commands ⇒ Object (readonly)
Returns the value of attribute commands.
6 7 8 |
# File 'lib/onyxcord/interactions/registry.rb', line 6 def commands @commands end |
Instance Method Details
#message(name, **attributes, &block) ⇒ Object
23 24 25 |
# File 'lib/onyxcord/interactions/registry.rb', line 23 def (name, **attributes, &block) register(Command.(name, **attributes, &block)) end |
#register(command) ⇒ Object
27 28 29 30 31 32 |
# File 'lib/onyxcord/interactions/registry.rb', line 27 def register(command) command.parse(&command.block) if command.block && command..empty? @commands[command.name] = command wire_handler(command) command end |
#schedule_delayed_command_sync(delay: 2, server_id: nil) ⇒ Thread?
Schedule a debounced sync. Multiple calls within the delay window are collapsed into a single sync. Default delay is 2 seconds.
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/onyxcord/interactions/registry.rb', line 66 def schedule_delayed_command_sync(delay: 2, server_id: nil) @sync_mutex.synchronize do @pending_sync&.kill if @pending_sync.is_a?(Thread) @pending_sync = Thread.new do sleep(delay) @sync_mutex.synchronize { @pending_sync = nil } sync!(server_id: server_id) end end end |
#slash(name, description:, **attributes, &block) ⇒ Object
15 16 17 |
# File 'lib/onyxcord/interactions/registry.rb', line 15 def slash(name, description:, **attributes, &block) register(Command.chat_input(name, description: description, **attributes, &block)) end |
#sync!(server_id: nil, delete_unknown: false) ⇒ void
This method returns an undefined value.
Immediately sync all commands to Discord.
38 39 40 41 42 43 44 45 46 |
# File 'lib/onyxcord/interactions/registry.rb', line 38 def sync!(server_id: nil, delete_unknown: false) # rubocop:disable Lint/UnusedMethodArgument payload = @commands.values.map(&:to_h) if server_id @bot.bulk_overwrite_guild_application_commands(server_id, payload) else @bot.bulk_overwrite_global_application_commands(payload) end end |
#sync_global_commands ⇒ void
This method returns an undefined value.
Sync global application commands.
50 51 52 |
# File 'lib/onyxcord/interactions/registry.rb', line 50 def sync_global_commands sync!(server_id: nil) end |
#sync_server_commands(server_id) ⇒ void
This method returns an undefined value.
Sync guild application commands.
57 58 59 |
# File 'lib/onyxcord/interactions/registry.rb', line 57 def sync_server_commands(server_id) sync!(server_id: server_id) end |