The bot’s working directory contains a commands subdirectory and a tasks subdirectory. Custom IRC commands can be placed in the former, whereas custom wiki bot tasks go into the latter. Developing custom modules is explained in detail in this documentation.
Note that custom commands will override built-in commands and tasks with the same name.
earwigbot.bot.Bot is EarwigBot’s main class. You don’t have to instantiate this yourself, but it’s good to be familiar with its attributes and methods, because it is the main way to communicate with other parts of the bot. A Bot object is accessible as an attribute of commands and tasks (i.e., self.bot).
The most useful attributes are:
earwigbot.config.BotConfig stores configuration information for the bot. Its docstrings explains what each attribute is used for, but essentially each “node” (one of config.components, wiki, irc, commands, tasks, or metadata) maps to a section of the bot’s config.yml file. For example, if config.yml includes something like:
irc:
frontend:
nick: MyAwesomeBot
channels:
- "##earwigbot"
- "#channel"
- "#other-channel"
...then config.irc["frontend"]["nick"] will be "MyAwesomeBot" and config.irc["frontend"]["channels"] will be ["##earwigbot", "#channel", "#other-channel"].
Custom commands are subclasses of earwigbot.commands.Command that override Command‘s process() (and optionally check(), setup(), or unload()) methods.
Command‘s docstrings should explain what each attribute and method is for and what they should be overridden with, but these are the basics:
Class attribute name is the name of the command. This must be specified.
Class attribute commands is a list of names that will trigger this command. It defaults to the command’s name, but you can override it with multiple names to serve as aliases. This is handled by the default check() implementation (see below), so if check() is overridden, this is ignored by everything except the help command (so !help alias will trigger help for the actual command).
Class attribute hooks is a list of the “IRC events” that this command might respond to. It defaults to ["msg"], but options include "msg_private" (for private messages only), "msg_public" (for channel messages only), "join" (for when a user joins a channel), "part" (for when a user parts a channel), and "rc" (for recent change messages from the IRC watcher). See the afc_status plugin for a command that responds to other hook types.
Method setup() is called once with no arguments immediately after the command is first loaded. Does nothing by default; treat it like an __init__() if you want (__init__() does things by default and a dedicated setup method is often easier than overriding __init__() and using super).
Method check() is passed a Data object, and should return True if you want to respond to this message, or False otherwise. The default behavior is to return True only if data.is_command is True and data.command == name (or data.command is in commands if that list is overriden; see above), which is suitable for most cases. A possible reason for overriding is if you want to do something in response to events from a specific channel only. Note that by returning True, you prevent any other commands from responding to this message.
Method process() is passed the same Data object as check(), but only if check() returned True. This is where the bulk of your command goes. To respond to IRC messages, there are a number of methods of Command at your disposal. See the test command for a simple example, or look in Command‘s __init__() method for the full list.
The most common ones are say(chan_or_user, msg), reply(data, msg) (convenience function; sends a reply to the issuer of the command in the channel it was received), action(chan_or_user, msg), notice(chan_or_user, msg), join(chan), and part(chan).
Method unload() is called once with no arguments immediately before the command is unloaded, such as when someone uses !reload. Does nothing by default.
Commands have access to config.commands[command_name] for config information, which is a node in config.yml like every other attribute of bot.config. This can be used to store, for example, API keys or SQL connection info, so that these can be easily changed without modifying the command itself.
The command class doesn’t need a specific name, but it should logically follow the command’s name. The filename doesn’t matter, but it is recommended to match the command name for readability. Multiple command classes are allowed in one file.
The bot has a wide selection of built-in commands and plugins to act as sample code and/or to give ideas. Start with test, and then check out chanops and afc_status for some more complicated scripts.
Custom tasks are subclasses of earwigbot.tasks.Task that override Task‘s run() (and optionally setup() or unload()) methods.
Task‘s docstrings should explain what each attribute and method is for and what they should be overridden with, but these are the basics:
Class attribute name is the name of the task. This must be specified.
Class attribute number can be used to store an optional “task number”, possibly for use in edit summaries (to be generated with make_summary()). For example, EarwigBot’s config.wiki["summary"] is "([[WP:BOT|Bot]]; [[User:EarwigBot#Task $1|Task $1]]): $2", which the task class’s make_summary(comment) method will take and replace $1 with the task number and $2 with the details of the edit.
Additionally, shutoff_enabled() (which checks whether the bot has been told to stop on-wiki by checking the content of a particular page) can check a different page for each task using similar variables. EarwigBot’s config.wiki["shutoff"]["page"] is "User:$1/Shutoff/Task $2"; $1 is substituted with the bot’s username, and $2 is substituted with the task number, so, e.g., task #14 checks the page [[User:EarwigBot/Shutoff/Task 14]]. If the page’s content does not match config.wiki["shutoff"]["disabled"] ("run" by default), then shutoff is considered to be enabled and shutoff_enabled() will return True, indicating the task should not run. If you don’t intend to use either of these methods, feel free to leave this attribute blank.
Method setup() is called once with no arguments immediately after the task is first loaded. Does nothing by default; treat it like an __init__() if you want (__init__() does things by default and a dedicated setup method is often easier than overriding __init__() and using super).
Method run() is called with any number of keyword arguments every time the task is executed (by tasks.start(task_name, **kwargs), usually). This is where the bulk of the task’s code goes. For interfacing with MediaWiki sites, read up on the Wiki Toolset.
Method unload() is called once with no arguments immediately before the task is unloaded. Does nothing by default.
Tasks have access to config.tasks[task_name] for config information, which is a node in config.yml like every other attribute of bot.config. This can be used to store, for example, edit summaries or templates to append to user talk pages, so that these can be easily changed without modifying the task itself.
The task class doesn’t need a specific name, but it should logically follow the task’s name. The filename doesn’t matter, but it is recommended to match the task name for readability. Multiple tasks classes are allowed in one file.
See the built-in wikiproject_tagger task for a relatively straightforward task, or the afc_statistics plugin for a more complicated one.