Class: HWPing::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/hwping/bot.rb

Constant Summary collapse

MESSAGES =
{
  :unauthorized => "Sorry, you're unauthorized to use this bot!",
  :image        => 'Image saved at http://%s/%s',
  :firing       => 'Firing rocket in 5 seconds!',
  :notarget     => 'Unknown target!',
  :fire         => 'Rocket launch initiated!',
  :reset        => 'Launcher set back to default position!',
  :position     => 'Current position: %d %d',
  :target_list  => 'Available targets: %s',
  :target_get   => "Target's position: %d %d",
  :target_del   => 'Target successfully deleted!',
  :target_set   => "Target's position saved at: %d %d",
  :move         => 'Move operation finished!',
  :badcommand   => "Unknown command, type 'help' for further information!",
  :help         => "Hardware Pinger\n
    Usage in a channel: hwping <target>\n
    Available commands in private:\n
      help\n
      fire\n
      position\n
      up|down|left|right\n
      reset\n
      target list\n
      target set <nick> <right> <up>\n
      target get <nick>\n
      target del <nick>\n"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(handler, config = {}) ⇒ Bot

Initialize a bot and return with its Cinch instance



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hwping/bot.rb', line 34

def initialize(handler, config = {})
  @bot = Cinch::Bot.new do
    configure do |c|
      c.nick     = config.nick
      c.server   = config.server
      c.port     = config.port
      c.channels = config.channels.map { |m| "\##{m}" }
    end

    # For channel mesages, just reply with the matching message
    on :channel, /^hwping/ do |e|
      r = handler.channel(e)
      e.reply(MESSAGES[r])
    end

    # For private messages, build a reply message from the format strinc and the passed variables
    on :private do |e|
      (r, *f) = handler.private(e)
      e.reply(MESSAGES[r] % f)
    end
  end
end

Instance Method Details

#startObject

Start the bot



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

def start
  @bot.start
end

#stopObject

Stop the bot



63
64
65
# File 'lib/hwping/bot.rb', line 63

def stop
  @bot.quit
end