Skip to content

Module structure

Modules it is separated files with bot functionality. In mxbt you can use special class Module for implementing module structure.

bot.py structure

from mxbt import Bot, Context, Creds, Listener
import asyncio
import os

bot = Bot(
    prefix="!",                 # Standart command prefix, commands can setup it own prefix
    creds=Creds.from_json_file(
        "credits.json"          # You can also make Credits object via providing Dict with same structure 
    )
)
lr = Listener(bot)

@lr.on_command(prefix="?", aliases=["echo", "e"])   # Every command can setup it own prefix, it is optionaly
async def ctx_echo(ctx: Context) -> None:           # Context object contains main info about event
    await ctx.reply(ctx.body)                       # Reply message to event room

# Mount all modules in modules dir
current_dir = os.path.dirname(__file__) + "/"
for filename in os.listdir(current_dir + "modules"):
    # find *.py files in modules dir and mount it
    if filename.endswith(".py"):
        lr.mount_module(f"modules.{filename[:-3]}")

# Or you can mount all modules in module dir:
# lr.mount_modules("modules")

if __name__ == "__main__":
    asyncio.run(lr.start_polling()) # Run bot event loop

modules/help.py structure

from mxbt.module import *

class Help(Module): # Instantiate from Module parent class

    # bot.on_command replaced with command decorator from mxbt.module
    @command(prefix="?", aliases=['help-moduled'])
    # Because it is class method, it also need to provide self attribute
    async def help_moduled(self, ctx: Context) -> None:
        await ctx.reply(
            f"Powered by: https://codeberg.org/warlock",
        )