Sha256: 2e38eae9112a43a3021726e48f9a7ca0b75d274215f7d6d323aa1282331be533

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 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
    @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 = {} if @events.nil?
      @events[event_name] ||= []
      discriminator[:extension] = Extension
      @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

    # @!visibility private
    attr_accessor :client
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
discorb-0.0.8 lib/discorb/extension.rb
discorb-0.0.7 lib/discorb/extension.rb
discorb-0.0.6 lib/discorb/extension.rb
discorb-0.0.5 lib/discorb/extension.rb
discorb-0.0.4 lib/discorb/extension.rb
discorb-0.0.3 lib/discorb/extension.rb
discorb-0.0.2 lib/discorb/extension.rb
discorb-0.0.1 lib/discorb/extension.rb