Module: Discorb::Extension::ClassMethods

Includes:
ApplicationCommand::Handler
Defined in:
lib/discorb/extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationCommand::Handler

#message_command, #setup_commands, #slash, #slash_group, #user_command

Instance Attribute Details

#commands -> Array<Discorb::ApplicationCommand::Command> (readonly)

Returns The commands of the extension.

Returns:



69
70
71
# File 'lib/discorb/extension.rb', line 69

def commands
  @commands
end

#events -> Hash{Symbol => Array<Discorb::EventHandler>} (readonly)

Returns The events of the extension.

Returns:



67
68
69
# File 'lib/discorb/extension.rb', line 67

def events
  @events
end

Instance Method Details

#event(event_name, id: nil, **metadata, &block) -> Discorb::EventHandler

Define a new event.

Parameters:

  • event_name (Symbol)

    The name of the event.

  • id (Symbol) (defaults to: nil)

    The id of the event. Used to delete the event.

  • metadata (Hash)

    Other metadata.

Returns:

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
# File 'lib/discorb/extension.rb', line 43

def event(event_name, id: nil, **, &block)
  raise ArgumentError, "Event name must be a symbol" unless event_name.is_a?(Symbol)
  raise ArgumentError, "block must be given" unless block_given?

  @events[event_name] ||= []
  [:extension] = self.name
  @events[event_name] << [id, , block]
end

#once_event(event_name, id: nil, **metadata, &block) -> Discorb::EventHandler

Define a new once event.

Parameters:

  • event_name (Symbol)

    The name of the event.

  • id (Symbol) (defaults to: nil)

    The id of the event. Used to delete the event.

  • metadata (Hash)

    Other metadata.

  • block (Proc)

    The block to execute when the event is triggered.

Returns:



62
63
64
# File 'lib/discorb/extension.rb', line 62

def once_event(event_name, id: nil, **, &block)
  event(event_name, id: id, once: true, **, &block)
end