Class: OnyxCord::Interactions::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/onyxcord/interactions/registry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#botObject (readonly)

Returns the value of attribute bot.



6
7
8
# File 'lib/onyxcord/interactions/registry.rb', line 6

def bot
  @bot
end

#commandsObject (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 message(name, **attributes, &block)
  register(Command.message(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.options.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.

Parameters:

  • delay (Numeric) (defaults to: 2)

    Seconds to wait before syncing.

  • server_id (Integer, String, nil) (defaults to: nil)

    Guild ID, or nil for global.

Returns:

  • (Thread, nil)

    The timer thread, or nil if cancelled.



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.

Parameters:

  • server_id (Integer, String, nil) (defaults to: nil)

    Guild ID for guild commands, or nil for global.

  • delete_unknown (Boolean) (defaults to: false)

    Whether to remove commands not in the registry.



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_commandsvoid

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.

Parameters:



57
58
59
# File 'lib/onyxcord/interactions/registry.rb', line 57

def sync_server_commands(server_id)
  sync!(server_id: server_id)
end

#user(name, **attributes, &block) ⇒ Object



19
20
21
# File 'lib/onyxcord/interactions/registry.rb', line 19

def user(name, **attributes, &block)
  register(Command.user(name, **attributes, &block))
end