Bot

Provide a Bot class that react to IRC’s message and events.

Classes

class irc_api.bot.Bot(irc_params: tuple, *commands_modules, auth: tuple = (), channels: list = ['#general'], prefix: str = '', limit: int = 100)

Watch the IRC server and handle commands.

Attributes

prefixstr, public

The bot’s prefix for named command.

ircirc_api.irc.IRC, public

IRC wrapper which handle communication with IRC server.

historyirc_api.history.History, public

The messages history.

channelslist, public

The channels the bot will listen.

authtuple, public

This contains the username and the password for a SASL auth.

callbacksdict, public

The callbacks of the bot. This dictionnary is like {name: command} where name is the name of the command and command a BotCommand’s instance.

commands_helpdict, public

Same that callbacks but only with the documented commands.

threadslist, public

A list of threads for the commands with @api.every.

Methods

__init__(irc_params: tuple, *commands_modules, auth: tuple = (), channels: list = ['#general'], prefix: str = '', limit: int = 100)

Initialize the Bot instance.

Parameters

irc_paramstuple

A tuple like: (host, port) to connect to the IRC server.

authtuple, optionnal

Contains the IRC server informations (host, port).

channelslist, optionnal

Contains the names of the channels on which the bot will connect.

prefixstr

The bot’s prefix for named commands.

limitint

The message history of the bot. By default, the bot will remind 100 messages.

*commands_moduleoptionnal

Modules of commands that you can give to the bot at it’s creation.

add_command(command, add_to_help: bool = False)

Add a single command to the bot.

Parameters

commandBotCommand

The command to add to the bot.

add_to_helpbool, optionnal

If the command should be added to the documented functions.

add_commands(*commands)

Add a list of commands to the bot.

Parameters

*commands

The commands’ instances.

send(target: str, message: str)

Send a message to the specified target (channel or user).

Parameters

targetstr

The target of the message. It can be a channel or user (private message).

messagestr

The content of the message to send.

start(nick: str)

Start the bot and connect it to IRC. Handle the messages and callbacks too.

Parameters

nickstr

The nickname of the bot.

Examples

Assuming the module was imported as follow: from irc_api import api You can create a bot:

my_bot = api.Bot(
        irc_params=(irc.exemple.com, 6697),
        channels=["#general", "#bot-test"],
        prefix="!",
        cmnd_pack1, cmnd_pack2
    )
class irc_api.bot.BotCommand(name: str, func, events: list, desc: str, cmnd_type: int)

Implement a bot command.

Attributes

namestr, public

The name of the command.

funcfunction, public

The function to execute when the BotCommand is called.

eventslist, public

The list of the conditions on which the BotCommand will be called.

descstr, public

The description of the BotCommand. By default, the function’s docstring is used.

cmnd_typeint, public

The type of the command. * if cmnd_type = 0, the command is triggered on an event. * if cmnd_type = 1, the command is a named command. * if cmnd_type = 2, the command is a routine automatically triggered.

botirc_api.bot.Bot, public

The bot the command belongs to.

Functions

irc_api.bot.parse(message)

Parse the given message to detect the command and the arguments. If a command’s name is ‘cmnd’ and the bot receive the message cmnd arg1 arg2 this function will returns [arg1, arg2]. It allows to have a powerfull commands with custom arguments.

Parameters

messageirc_api.irc.Message

The message to parse.

Returns

args_to_returnlist

The list of the given arguments in the message.

irc_api.bot.convert(data, new_type: type, default=None)

Transtype a given variable into a given type. Returns a default value in case of failure.

Parameters

data

The given data to transtype.

new_type : type

irc_api.bot.check_args(func, *input_args)

Check if the given args fit to the function in terms of number and type.

Parameters

funcfunction

The function the user wants to run.

*input_args

The arguments given by the user.

Returns

converted_argslist

The list of the arguments with the right type. The surplus arguments are ignored.