Sha256: fc24a6150b7d3f864e80faf87749487da9b4c0723f6574b85fa290ee6e7610b5

Contents?: true

Size: 2 KB

Versions: 5

Compression:

Stored size: 2 KB

Contents

# frozen_string_literal: true

module Discorb
  #
  # Module to make extension.
  # extend this module to make your own extension.
  # @see file:docs/extension.md
  # @abstract
  #
  module Extension
    include Discorb::Command::Handler
    undef setup_commands

    @events = {}
    @client = nil

    #
    # Define a new event.
    #
    # @param [Symbol] event_name The name of the event.
    # @param [Symbol] id The id of the event. Used to delete the event.
    # @param [Hash] discriminator Other discriminators.
    # @param [Proc] block The block to execute when the event is triggered.
    #
    # @return [Discorb::Event] The event.
    #
    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[event_name] ||= []
      discriminator[:extension] = self.name
      @events[event_name] << Discorb::Event.new(block, id, discriminator)
    end

    #
    # Define a new once event.
    #
    # @param [Symbol] event_name The name of the event.
    # @param [Symbol] id The id of the event. Used to delete the event.
    # @param [Hash] discriminator Other discriminators.
    # @param [Proc] block The block to execute when the event is triggered.
    #
    # @return [Discorb::Event] The event.
    #
    def once_event(event_name, id: nil, **discriminator, &block)
      event(event_name, id: id, once: true, **discriminator, &block)
    end

    # @return [Hash{Symbol => Array<Discorb::Event>}] The events of the extension.
    attr_reader :events
    # @return [Array<Discorb::Command::Command] The commands of the extension.
    attr_reader :commands
    # @private
    attr_reader :bottom_commands

    # @!visibility private
    attr_accessor :client

    def self.extended(obj)
      obj.instance_variable_set(:@events, {})
      obj.instance_variable_set(:@commands, [])
      obj.instance_variable_set(:@bottom_commands, [])
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
discorb-0.5.6 lib/discorb/extension.rb
discorb-0.5.5 lib/discorb/extension.rb
discorb-0.5.4 lib/discorb/extension.rb
discorb-0.5.3 lib/discorb/extension.rb
discorb-0.5.2 lib/discorb/extension.rb