Toastr - The simple pluggable IRC bot
#!/usr/bin/env perl
use Mojo::Base -strict;
use Toastr;
my $toastr = Toastr->new(
channels => ['#toastr'],
nick => 'toastr',
user => 'toastr bot',
server => 'irc.perl.org:6667',
);
$toastr->plugin('KarmaHandler');
$toastr->plugin('Toast');
$toastr->plugin('Hailo');
$toastr->start;
Toastr is a subclass of Mojo::IRC implementing a simple pluggable IRC bot. The base class attaches additional events which are emitted when the bot sees and/or receives a message. The events receive an instance of Toastr::Message containing lots of useful information about the message. Toastr's built-in events are prefixed with toastr_
and Mojo::IRC's events are prefixed with irc_
.
Plugins are subclasses of Toastr::Plugin. A plugin will most likely subscribe to one or more event, then either take some action on that event, or even emit events of its own. Plugins namespace must be of the form Toastr::Plugin::<PluginName>
. When loaded, the plugin instance will be stored in a the plugins
attribute named, keyed on the plugin name. The convenience method "plugin" may be used to access the plugin and load it if necessary.
Toastr inherits all of the events from Mojo::IRC and emits the following new ones:
$toastr->on( toastr_message => sub {
my ($toastr, $message) = @_;
...
});
Emitted for every message that the bot sees.
$toastr->on( toastr_direct_message => sub {
my ($toastr, $message) = @_;
...
});
Emitted for every private message (pm) or message prefixed with the bot's nick (the nick and an optional punctuation mark will be stripped from the message).
Toastr inherits all of the attributes from Mojo::IRC and implements the following new ones:
An array reference of channels to connect to.
A qr
regex which is used to extract valid nicks. This probably does not need to be changed from its default.
Holds instances of loaded plugins, keyed by plugin name. This attribute should not be changed manually, use the methods "plugin" and /load_plugin
.
Toastr inherits all of the methods from Mojo::IRC and implements the following new ones.
$toastr->attach;
This method must be called to subscribe to the necessary events and connect to the server. Note that the convenience method "start" calls this method for you.
$toastr->load_plugin('PluginName' => { attr => $val, attr => $val, ... });
Loads plugin PluginName
and creates an instance. An optional hash reference is used to set attributes on the plugin. The instance is stored in the /plugins
hash.
Note that this may be used to replace the instance if desired.
$toastr->msg( $chan => $text, $text, ... );
Send a privmsg
to the server and channel $chan
. The text of the message is all trailing arguments (joined by $"
).
$toastr->plugin('PluginName' => { attr => $val, attr => $val, ... });
Returns either the cached instance of the plugin or constructs one if necessary using "load_plugin". If the plugin is already loaded, the constructor hash is ignored.
http://github.com/jberger/Toastr
Joel Berger, <joel.a.berger@gmail.com>
Copyright (C) 2014 by Joel Berger
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.