Skip to content

Making modules

layz edited this page Mar 11, 2025 · 1 revision

example module with easy explanation in code

from .. import loader, utils

from pyrogram.types import Message
from aiogram.types import Message as AioMessage

import logging
import asyncio


# to define module class you just need to inherit loader.Module
class TestMod(loader.Module):
    def __init__(self):
        self.has_used_test_command = False

    @loader.command()
    async def test(self, message: Message):
        message = await utils.answer(message, "hello world")
        await asyncio.sleep(1)

        await utils.answer(message, "bye world")

        self.has_used_test_command = True

    @loader.watcher()
    async def message_watcher(self, message: Message):
        if message.from_user.id == self.client.me.id:
            logging.info("omg handled my message by myself")

    # there are many types of handlers and you can filter them like this
    # @loader.chosen_inline_handler(lambda self, chosen_inline_result: ...)
    # @loader.callback_handler(lambda self, callback_query: ...)
    # @loader.inline_handler(lambda self, inline_query: ...)
    # @loader.message_handler(lambda self, message: ...)

    # easy to use, also gives you permission to get module's attributes or use module's methods

    @loader.message_handler(
        lambda self, message: self.has_used_test_command is True
        and message.from_user.id == self.client.me.id
    )
    async def hello_message_handler(self, message: AioMessage):
        await message.reply("wow bot replied you because you used test command!!!")
Clone this wiki locally