The main bot class, where all the magic happens.
This class handles the socket and all incoming and outgoing data. This classes methods should be used for sending data.
It keeps an index of loaded plugins and helps with the management of requirements.
To interact witht he bot it supplies a hook function that can be used to register callbacks with the bot.
Please check out Hook and hook() to find out how hooking your plugins in works.
Please note that this bot does absolutely nothing by itself.
It won’t even answer pings or identify. But there are some system plugins to do that. Check the plugins folder.
Holds the bot configuration.
Registered hooks
Registered modules
Will check through all instantiated plugins and call the ones that match the given event.
Collects the incoming data and adds it to the buffer.
Parameters: | data – The received data from the server. |
---|
Please do not use this function manually! It is only to be used by asnchat!
Depending on the configuration the log level and eventual handlers for the logging have to be configured, which is what this function does.
As IRC is a line based protocol which means every command is in its own line, this function is called as soon as a line and thus a command has been completely received.
The line is read from the buffer and the buffer cleared.
The function only does very basic syntax correction, to clear up input that does not match the usual format.
I.e. a PING command does not follow the usual syntax.
Thusly the function will get the relevant parameter and fill it into the usual structure.
This function will call the func:call_hooks function with the extracted data.
This is mainly a helper for inter dependency between plugins. If one plugin requires another one, it can get to it with this function. If the plugin has not been loaded yet, it will try to load it. If it fails or does not exist, the requiring plugin will also fail to load.
As soon as the socket is connected, the (made up) event SOCK_CONNECTED will be called! Use it to identify yourself. This bot does NOTHING by itself.
This method will register a hook with the bot. It is supposed to be used as a decorator, but can also be used as a normal function. Please see the Hook class documentation for an overview what a hook should look like.
This method will save the Hook class with the Alebot class until the __init__() function instantiates the hooks.
Will open the local config file config.json and load it as json. Will only accept a json object.
There are no required values. The file itself it optional. The bot will supply default values, if there are none specified.
Alebot itself makes use of the following options:
Any additional option can be configured. Plugin developers are encouraged to specifiy plugin objects with own configuration, as long as they make sure to use a specific name to avoid conflicts.
Load a specific plugin. This will try to find a specific plugin, load it and save it to the Alebot class, so that it can be retrieved later on.
It will also make sure, that plugins are only loaded once.
Will load all the plugins from the plugin folder. It will only load them though! The plugins still have to register themselves with the hook() function, if they want to interact with the bot.
This function does not do anything yet. Plugins have to be in the the same file as the bot itself.
The Event class is used to store data about the event and provide some helper methods that should save you some string manipulation.
The name attribute should always be given and will be a string, either as defined in the IRC RFC (event number or command) or one of the following custom events:
SOCK_CONNECTED: Sent as soon as the socket is connected.
Depending on the type of the event there might be one or me of the following attributes not empty (not None):
The user that caused the event. In case it is a channel or private message, it is the sender of the message, in case of a join, the joining person, in case of a kick, the kicking person and so on. If this is an actual user, and not a server, nick, ident and :attr`host` should be available, too.
If this is a user, it will be in the format of:
nick!ident@host
If it is a server, it will be in the form of:
subdomain.domain.zone
If the event has a message, reason or a similar thing, you will find it in body.
The target of the action, a channel if it is a channel message, the bot’s nick if it is a private one or anything else.
This is just a possible implementation for a hook. Every hook that registers itself with the bot has to implement two functions:
__init__() match() call()
Read these functions documentation to see what they should do.
Every hook that actually is supposed to be used, has to be prefixed by the @Bot.hook decorator. Doing so automatically adds the hook to the bot. The hook will be instantiated once on startup and then kept in memory until the bot dies.
This means you can do some magic processing like reading config files and stuff in the beginning. As soon as your bot will be initialized there will be a bot instance present although you won’t be able to send data in the beginning.
You can check out the default alebot plugins for examples on how to write plugins.
If you want to log data, it is recommended to access the bot’s logger using self.bot.logger. It supports python’s usual logging infrastructure and thus functions like debug, info, warn and error.
In case that your match() function returned True this function will be called. It will again receive:
param event: an instance of the Event class
Now you are free to send data or do whatever you have to do.
This function is used to evaluate whether the hook wants to react to the event that is passed on. It has to accept one parameter:
param event: an instance of the Event class
Although theoretically possible I recommend to separate the evaluation of a match and the actual reaction for the code’s clearnesses sake.
This way it is very easy to determine what triggers this hook.
returns: Either True or False, depending on whether a match is given or not.
As this is a callback you have very high flexibility regarding your matching. You can match combinations of nick, ident, host, events, target and body.
Check out the Event class to see what event data is available.
You can also use a regex or vary matches based on the time or the weather. Whatever you want.
This class can be used to do stuff in the background. It can be used for everything that might take some time and should not block the bot in the meantime.
It expects the following parameters:
param hook: the current hook instance (the active plugin) param event: the current event
You can basically overwrite the init event any pass less data, the example data her is just for convenience.
You will have to overwrite do() though. See the functions documentation for more information.
The task can be started using the start() function.
This is just a helpful mixin to provide a few basic irc command wrappers to different classes.
Send a message to a target.
param target: either a channel (with prefix) or a nick param text: the contents of the message
This is a hook that can be subclassed in case you want to react to a message on a channel or in private. It will react to the bot’s current nickname followed by a colon and the command specified in the command attribute.
In case you want your command to take parameters, too.
This is a hook that can be subclassed in case you want to react on a irc connection that is ready for commands. It waits for the end of the motd, or the message that there is no motd.
The match() function was implemented to listen to the correct events. You will just have to overwrite the :func`call` to actually do something.
Just a shortcut for simple commands that require admin permissions.
Just a shortcut for simple commands that require admin permissions.
Uses requests to rshorten the url with google. As soon as the answer is received, it sends the result to the channel.
Shorten links that are too long.
Tries to find all http links and spawns a background task that generates a goo.gl shortlink with the help of the google api.
Use the config setting “shortlink”: {“length”: <int>}. If not specified links from 50 chars up will be converted, if specified from the given number of chars up.