Bot::Maverick - Mojo::IRC Bot framework
use Bot::Maverick;
my $bot = Bot::Maverick->new(
name => 'MyIRCBot',
networks => {
libera => {
class => 'Libera',
irc => { server => 'irc.libera.chat' },
},
},
);
$bot->start;
Bot::Maverick is an IRC bot framework built on Mojo::IRC that supports connecting to multiple networks and is designed to be configurable and customizable via plugins. A core set of plugins is included from Bot::Maverick::Plugin::Core which includes basic administrative and configuration commands, and additional plugins can be registered that may implement any functionality from adding more commands to hooking into message events and more.
Bot::Maverick is configured by INI files: one global configuration file and a network configuration file for each network it connects to. The global configuration file is used as a default configuration for each network. Any configuration specified in the constructor or when adding a network will be written to the configuration files, overriding existing configuration.
Configuration INI files are organized into sections, and all parameters must be within a section.
IRC networks are represented by Bot::Maverick::Network (or subclassed) objects that handle all communication with that network. Networks can be specified in the bot's constructor, or added later with the "network" method.
Plugins are Moo objects that compose the role Bot::Maverick::Plugin. They are registered by calling the required method register
and may add commands, hooks, or anything else to the bot instance. A plugin may also register a method of its own as a "helper method" which then can be called on the bot instance from elsewhere. The plugin object is passed as the invocant of the registered helper method.
Commands are represented by Bot::Maverick::Command objects which define the properties of the command and a callback "on_run" that is called when the command is invoked by a IRC user.
Hooks are subroutines which are run whenever a specific action occurs. They are a powerful way to add global functionality.
$bot->on(start => sub { my ($bot) = @_; ... });
Emitted when the bot is started.
$bot->on(stop => sub { my ($bot) = @_; ... });
Emitted when the bot is stopped.
$bot->on(reload => sub { my ($bot) = @_; ... });
Emitted when the bot is reloaded.
$bot->on(privmsg => sub { my ($bot, $m) = @_; ... });
The privmsg
hook is called whenever the bot receives a channel or private message ("privmsg") from an IRC user. The callback will receive the Bot::Maverick object and the Bot::Maverick::Message object containing the message details.
$bot->on(before_command => sub { my ($bot, $m) = @_; ... });
The before_command
hook is called before a recognized command is executed. The callback will receive the Bot::Maverick object and the Bot::Maverick::Message object containing the message details.
$bot->on(after_command => sub { my ($bot, $m) = @_; ... });
The after_command
hook is called after a command has been executed. The callback will receive the Bot::Maverick object and the Bot::Maverick::Message object containing the message details.
Constructs a new Bot::Maverick object instance. Accepts values for the following attributes:
- name
-
name => 'Botzilla',
Name for the bot, defaults to
Maverick
. The bot's name is used as a default nick for networks where it is not configured, as well as lowercased for the default filenames for configuration and other files. For example, a bot namedFred
will (by default) use the configuration filefred.conf
, network configuration filesfred-<network>.conf
, and database filefred.db
. - networks
-
networks => { libera => { class => 'Libera', config => { debug => 1 } } },
Hash reference that must contain at least one network to connect to. Keys are names which will be used to identify the network as well as lowercased to define the default network configuration filename. Values are hash references containing an optional object
class
(defaults to Bot::Maverick::Network) and configuration for the network. Theclass
will be appended toBot::Maverick::Network::
if it does not contain::
. - plugins
-
plugins => [qw/DNS GeoIP/], plugins => { Core => 0, DNS => { native => 0 }, GeoIP => 1 },
Plugins to register with the bot, specified as plugin class names, which are appended to
Bot::Maverick::Plugin::
if they do not contain::
. May be specified as an array reference to simply include the list of plugins, or as a hash reference to configure the plugins. If the hash value is false, the plugin will be not be registered. If the value is a hash reference, it will be passed to the plugin's constructor. See Bot::Maverick::Plugin for documentation on Maverick plugins. - config
-
config => { debug => 1, irc => { nick => 'OtherGuy' } },
Global configuration that will override any defaults or existing global configuration file. To override configuration for a specific network, see the
networks
attribute. - config_dir
-
config_dir => "$HOME/.mybot",
Directory to store configuration and other files. Defaults to current directory.
- config_file
-
config_file => 'myconfig.conf',
Filename for main configuration file. Defaults to bot's name, lowercased and appended with
.conf
. Network configuration filenames default to bot's name, lowercased and appended with lowercased-<network>.conf
. - storage_file
-
storage_file => 'mystuff.db',
Filename for database storage file. Defaults to bot's name, lowercased and appended with
.db
. - logger
-
logger => Mojo::Log->new('/var/log/botstuff.log')->level('warn'),
Logger object that will be used for all debug, informational and warning output. Can be any type of logger that has
debug
,info
,warn
,error
, andfatal
methods, such as Mojo::Log. Defaults to logging to the file specified by the configurationlogfile
, or STDERR otherwise.
Start the bot and connect to configured networks. Blocks until the bot is told to stop.
Disconnect from all networks and stop the bot.
Reloads configuration and reopens log handles.
$bot = $bot->add_network(libera => { class => 'Libera' });
Adds a network for the bot to connect to. See "NETWORKS".
$bot = $bot->add_plugin(DNS => { native => 0 });
Registers a plugin with optional hashref of parameters to pass to the plugin's register
method. See "PLUGINS".
$bot = $bot->add_helper(DNS => 'dns_resolve');
Adds a helper method to the bot, so that it may be called on the bot via "AUTOLOAD".
my $bool = $bot->has_helper('dns_resolve');
Returns a boolean value that is true if the specified helper method is available.
$bot = $bot->add_command(Bot::Maverick::Command->new(...));
$bot = $bot->add_command(...);
Adds a Bot::Maverick::Command to the bot, or passes the arguments to construct a new Bot::Maverick::Command and add it to the bot. See "COMMANDS".
my $command = $bot->get_command('say');
Returns the Bot::Maverick::Command object representing the command, or undef
if the command does not exist.
my $commands = $bot->get_commands_by_prefix('wo');
Returns an array reference of command objects matching a prefix, if any.
$bot = $bot->remove_command('locate');
Removes a command from the bot by name if it exists.
my $loop = $bot->loop;
Returns the Mojo::IOLoop singleton used for the bot's operation.
my $future = $bot->new_future;
Returns a Future::Mojo initialized with the Mojo::IOLoop singleton.
my $future = $bot->timer_future(1);
Returns a Future::Mojo initialized with the Mojo::IOLoop singleton, which will be set to done after the specified delay in seconds.
$future = $bot->adopt_future($future);
Stores a reference to the passed Future object which will be cleared once the future is ready. Stored futures will be cancelled if the bot is stopped.
my $future = $bot->ua_request($url);
my $future = $bot->ua_request(post => $url, {Authorization => 'open sesame'}, json => [1,2,3]);
Runs a non-blocking Mojo::UserAgent request and returns a Future::Mojo that will be set to failed on connection or HTTP error, and otherwise will be set to done with the Mojo::Message::Response. The first argument can optionally be a request method (defaults to get
), and the remaining arguments are passed to Mojo::UserAgent.
my $future = $bot->fork_call(sub { return 'foo' });
Runs a code ref in a forked process using Mojo::IOLoop::Subprocess::Sereal and returns a Future::Mojo that will be set to failed if the code throws an exception, and otherwise will be set to done with the returned values.
Report any issues on the public bugtracker.
Dan Book, dbook@cpan.org
Copyright 2015, Dan Book.
This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.