Class: Botbckt::Command

Inherits:
Object
  • Object
show all
Includes:
Utilities, Singleton
Defined in:
lib/botbckt/command.rb

Overview

This acts as a kind of abstract class for Botbckt commands. Subclass this class to define new bot commands.

Command subclasses must (re-)define call. If any setup is needed, override create! and return self.instance.

Direct Known Subclasses

Gooble, Google, Meme, Ping, Remind, Snack, StarJar, Ticker, Weather

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utilities

#freenode_split

Class Method Details

.create!(*args) ⇒ Object

By default, returns the singleton instance. Override in a subclass if a different behavior is expected.

Parameters (args)

sender<String>

The user and host of the triggering user. Example: botbckt!n=botbckt@unaffiliated/botbckt

channel<String>

The channel on which the command was triggered. Example: #ruby-lang

*args

Any string following the trigger in the message



21
22
23
# File 'lib/botbckt/command.rb', line 21

def self.create!(*args)
  self.instance
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.



51
52
53
# File 'lib/botbckt/command.rb', line 51

def self.say(msg, channel)
  Botbckt::Bot.instance.say(msg, channel)
end

.trigger(command, &block) ⇒ Object

Registers a new command with the bot.

Parameters

command<Symbol>

In-channel trigger for the command. Required.

&block

An optional block to execute, in lieu of call.



43
44
45
# File 'lib/botbckt/command.rb', line 43

def self.trigger(command, &block)
  Botbckt::Bot.instance.register(command, block_given? ? block : self)
end

Instance Method Details

#call(*args) ⇒ Object

This method is executed by the Bot when a command is triggered. Override it in a subclass to get the behavior you want.

Parameters (args)

sender<String>

The user and host of the triggering user. Example: botbckt!n=botbckt@unaffiliated/botbckt

channel<String>

The channel on which the command was triggered. Example: #ruby-lang

*args

Any string following the trigger in the message

Raises:

  • (NoMethodError)


33
34
35
# File 'lib/botbckt/command.rb', line 33

def call(*args)
  raise NoMethodError, "Implement #call in a subclass."
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.



80
81
82
# File 'lib/botbckt/command.rb', line 80

def get(key, &block)
  Botbckt::Bot.instance.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.



92
93
94
# File 'lib/botbckt/command.rb', line 92

def increment!(key, &block)
  Botbckt::Bot.instance.increment!(key, &block)
end

#say(msg, channel) ⇒ Object

Proxy for Command.say



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

def say(msg, channel)
  self.class.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.



69
70
71
# File 'lib/botbckt/command.rb', line 69

def set(key, value, &block)
  Botbckt::Bot.instance.set(key, value, &block)
end