Skip to content

Making modules

layz edited this page Oct 7, 2023 · 3 revisions

To create a module, you need the following elements:

License (optional)
Imports
Class
Commands

Methods

Called on load

async def on_load(self):
    pass

Called on unload

async def on_unload(self):
    pass

Command handler, you can create it using a decorator or append "cmd" at the end

@loader.command()
async def command(self, message: Message, args=None):
    pass

async def commandcmd(self, message: Message, args=None):
    pass

Message handler

async def commandcmd(self, message: Message):
    pass

Database

# Test module
@loader.module('test')
class TestMod(loader.Module):
    async def on_load(self):
        # Getting and setting value
        self.test_value = self.db.get('testmod', 'testvalue', 'no value')
        self.db.set('testmod', 'loaded', True)

    async def on_unload(self):
        # Setting value
        self.db.set('testmod', 'testvalue', self.test_value)
        self.db.set('testmod', 'loaded', False)

Utils

async def answer(
    message: Union[Message, List[Message]],
    response: Union[str, Any],
    photo: bool = False,
    document: bool = False,
    topic: bool = False,
    caption: str = '',
    parse_mode: str = 'html',
    **kwargs
) -> List[Message]:
    """
    Sends a response to a message, with optional photo or document attachment.

    Parameters:
        message (Union[Message, List[Message]]): The original message or a list of messages to reply to.
        response (Union[str, Any]): The response to send. It can be a text message, a path to a photo/document, or a file-like object.
        photo (bool, optional): If True, a photo will be sent along with the response. Default is False.
        document (bool, optional): If True, a document will be sent along with the response. Default is False.
        caption (str, optional): Caption for the sent photo or document, if applicable.
        parse_mode (str, optional): Parse mode for formatting text. Default is 'html'.
        **kwargs: Additional keyword arguments for sending messages or files.

    Returns:
        List[Message]: A list of sent messages.

    Example:
        response_text = "Thank you for your message!"
        await utils.answer(message, response_text)
        
        response_image_path = "image.jpg"
        await utils.answer(message, response_image_path, photo=True, caption="Here's an image for you.")
    """

other methods in utils.py

Clone this wiki locally