Module: Twitchbot::Plugin

Included in:
AuthPlugin, ChannelPlugin, DebugPlugin, PingPlugin
Defined in:
lib/twitchbot/plugin.rb

Overview

Base Plugin module that listens for IRC commands and calls the associated method of the class that includes it

Constant Summary collapse

COMMANDS =

The commands that are registered to this base module via classes that include it

Example state during runtime:

{ AuthPlugin: { '376': :request_caps }, ChannelPlugin: { 'CAP': :join_channel, 'JOIN': :process_join, 'PART': :process_part } }
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

Define a class method called register on each class that includes this module, which allows the user to add methods to the COMMANDS constant

def self.register(command:, method:)
  COMMANDS[base] = {} if COMMANDS[base].nil?
  COMMANDS[base][params[:command]] = params[:method]
end


46
47
48
49
50
51
52
53
# File 'lib/twitchbot/plugin.rb', line 46

def self.included(klass)
  klass.instance_eval do
    define_singleton_method 'register' do |params|
      COMMANDS[klass] = {} if COMMANDS[klass].nil?
      COMMANDS[klass][params[:command]] = params[:method]
    end
  end
end

Instance Method Details

#close(handler) ⇒ Object

Method that can be overriden to react to the eventmachine :close event



37
# File 'lib/twitchbot/plugin.rb', line 37

def close(handler) end

#error(handler) ⇒ Object

Method that can be overriden to react to the eventmachine :error event



34
# File 'lib/twitchbot/plugin.rb', line 34

def error(handler) end

#message(handler) ⇒ Object

Method that reacts to the eventmachine :message event and processes each message in the EventHandler, calling the appropriate method if available.

It is not recommended to override this method unless you plan on handling all logic for reacting to methods yourself.



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/twitchbot/plugin.rb', line 21

def message(handler)
  handler.messages.each do |message|
    command = message&.command
    # Grab all registered methods of the including class
    commands = COMMANDS[self.class]
    if !command.nil? && !commands[command].nil?
      # Call the including class method and pass the EventHandler to it
      send(commands[command], handler)
    end
  end
end

#open(handler) ⇒ Object

Method that can be overriden to react to the eventmachine :open event



14
# File 'lib/twitchbot/plugin.rb', line 14

def open(handler) end