Class: Botbckt::Bot

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::BufferedLogger::Severity, Singleton
Defined in:
lib/botbckt/bot.rb

Overview

Create a new IRC bot. See Bot.start to get started.

Constant Summary collapse

AFFIRMATIVE =
["'Sea, mhuise.", "In Ordnung", "Ik begrijp", "Alles klar", "Ok.", "Roger.", "You don't have to tell me twice.", "Ack. Ack.", "C'est bon!"]
NEGATIVE =
["Titim gan éirí ort.", "Gabh mo leithscéal?", "No entiendo", "excusez-moi", "Excuse me?", "Huh?", "I don't understand.", "Pardon?", "It's greek to me."]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



14
15
16
# File 'lib/botbckt/bot.rb', line 14

def connection
  @connection
end

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/botbckt/bot.rb', line 12

def logger
  @logger
end

#storeObject

Returns the value of attribute store.



13
14
15
# File 'lib/botbckt/bot.rb', line 13

def store
  @store
end

Class Method Details

.befuddledObject

Returns a random “confused” message. Use as a kind of “method missing” on unknown user input.

– Inspired by Clojurebot: github.com/hiredman/clojurebot ++



156
157
158
# File 'lib/botbckt/bot.rb', line 156

def self.befuddled
  NEGATIVE[rand(NEGATIVE.size)]
end

.okObject

Returns a random “affirmative” message. Use to acknowledge user input.

– Inspired by Clojurebot: github.com/hiredman/clojurebot ++



146
147
148
# File 'lib/botbckt/bot.rb', line 146

def self.ok
  AFFIRMATIVE[rand(AFFIRMATIVE.size)]
end

.start(options) ⇒ Object

Parameters

options<Hash=> String,Integer>

Options (options)

:user<String>

The username this instance should use. Required.

:password<String>

A password to send to the Nickserv. Optional.

:server<String>

The FQDN of the IRC server. Required.

:port<~to_i>

The port number of the IRC server. Required.

:channels<Array>

An array of channels to join. Channel names should not include the ‘#’ prefix. Required.

:log<String>

The name of a log file. Defaults to ‘botbckt.log’.

:log_level<Integer>

The minimum severity level to log. Defaults to 1 (INFO).

:pid<String>

The name of a file to drop the PID. Defaults to ‘botbckt.pid’.

:daemonize<Boolean>

Fork and background the process. Defaults to true.

:backend_host<String>

The hostname of a Redis store. Optional.

:backend_port<Integer>

The port used by the Redis store. Optional.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/botbckt/bot.rb', line 32

def self.start(options)
  daemonize = options.delete(:daemonize)
  pid = options.delete(:pid) || 'botbckt.pid'

  if daemonize || daemonize.nil?
    EventMachine::fork_reactor do
      start!
      
      if pid
        File.open(pid, 'w') { |file| file.write("#{Process.pid}") }
        at_exit { File.delete(pid) if File.exist?(pid) }
      end
      
    end
  else
    EventMachine::run { start! }
  end
end

Instance Method Details

#commandsObject

Returns currently registered commands.



64
65
66
# File 'lib/botbckt/bot.rb', line 64

def commands
  @commands
end

#get(key, &block) ⇒ Object

Retrieves the value stored at key. Returns nil if the key does not exist.

Parameters

key<String>

The identifier to retrieve. Required.

&block

A callback to execute after the value is retrieved. The block should take a single parameter: the value retrieved. Required.

– TODO: Forwardable? ++



93
94
95
# File 'lib/botbckt/bot.rb', line 93

def get(key, &block)
  self.store && self.store.get(key, &block)
end

#increment!(key, &block) ⇒ Object

Increments the value stored at key by 1, creating the key and initializing it to 0 if necessary.

Parameters

key<String>

The identifier whose value should be incremented. Required.

&block

A callback to execute after the value is stored. The block should take a single parameter: the value stored. Optional.

– TODO: Forwardable? ++



108
109
110
# File 'lib/botbckt/bot.rb', line 108

def increment!(key, &block)
  self.store && self.store.increment!(key, &block)
end

#log(msg, level = INFO) ⇒ Object

Parameters

msg<String>

A message to log. Required.

level<Integer>

The minimum log level at which to log this message. Defaults to INFO.



172
173
174
# File 'lib/botbckt/bot.rb', line 172

def log(msg, level = INFO)
  self.logger.add(level, msg)
end

#register(command, callable) ⇒ Object

Registers a new command.

Parameters

command<~to_sym>

Trigger to register. Required.

callable<~call>

Callback or class with #call to execute. Required.



57
58
59
60
# File 'lib/botbckt/bot.rb', line 57

def register(command, callable)
  @commands ||= { }
  @commands[command.to_sym] = callable
end

#run(command, sender, channel, *args) ⇒ Object

Parameters

command<Symbol>

The name of a registered command to run. Required.

sender<String>

The sender (incl. hostmask) of the trigger. Required.

channel<String>

The channel on which the command was triggered. Required.

*args

Arguments to be passed to the command. Optional.

– TODO: Before/after callbacks? ++



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/botbckt/bot.rb', line 121

def run(command, sender, channel, *args)
  callable = commands[command.to_sym] || raise("Unregistered command called. (#{command})")
  
  if callable.is_a?(Class)
    # Callables are Singletons; we use #create! as a convention to give
    # possible setup code a place to live. 
    callable = callable.create!(sender, channel, *args)
  end
  
  if callable.respond_to?(:call)
    callable.call(sender, channel, *args)
  else
    raise("Non-callable used as command. (#{command})")
  end

rescue StandardError => error
  log "ERROR:\n#{error.inspect}\n#{error.backtrace}", DEBUG
  say Bot.befuddled, channel
end

#say(msg, channel) ⇒ Object

Parameters

msg<String>

A message to send to the channel. Required.

channel<String>

The channel to send the message. Required.



164
165
166
# File 'lib/botbckt/bot.rb', line 164

def say(msg, channel)
  self.connection.say msg, channel
end

#set(key, value, &block) ⇒ Object

Sets the key to the given value, creating the key if necessary.

Parameters

key<String>

The identifier for this value. Required.

value<Object>

The value to store at the key. Required.

&block

A callback to execute after the value is stored. The block should take a single parameter: the value stored. Optional.

– TODO: Forwardable? ++



79
80
81
# File 'lib/botbckt/bot.rb', line 79

def set(key, value, &block)
  self.store && self.store.set(key, value, &block)
end