Module: Discorb::Extension Abstract

Defined in:
lib/discorb/extension.rb

Overview

This module is abstract.

Module to make extension. extend this module to make your own extension.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#eventsHash{Symbol => Array<Discorb::Event>} (readonly)

Returns The events of the extension.

Returns:

  • (Hash{Symbol => Array<Discorb::Event>})

    The events of the extension.



49
50
51
# File 'lib/discorb/extension.rb', line 49

def events
  @events
end

Instance Method Details

#event(event_name, id: nil, **discriminator, &block) ⇒ Discorb::Event

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.

  • discriminator (Hash)

    Other discriminators.

  • block (Proc)

    The block to execute when the event is triggered.

Returns:

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
# File 'lib/discorb/extension.rb', line 24

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

  @events = {} if @events.nil?
  @events[event_name] ||= []
  discriminator[:extension] = Extension
  @events[event_name] << Discorb::Event.new(block, id, discriminator)
end

#once_event(event_name, id: nil, **discriminator, &block) ⇒ Discorb::Event

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.

  • discriminator (Hash)

    Other discriminators.

  • block (Proc)

    The block to execute when the event is triggered.

Returns:



44
45
46
# File 'lib/discorb/extension.rb', line 44

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