Creating a Simple “Hello, World!” Bot

This page assumes you’ve completed Getting Started, and have a project folder ready. If not, check the previous pages.

Before editing the generated Hello World module, let’s talk about how Monium loads modules.

How Monium Loads Modules

When you start the bot, Monium will first check the config.py’s MODULE_PATH for all the directories it should load modules from. Then, it will go through each directory checking for directories that contain a module.py file. When it finds a module.py file, it will import it, and get a list of all classes extending Module. After every module is loaded and right before the connection to Discord happens, all classes are initialized and put on a list for future use.

Why did we talk about this? Because knowing how the module loader works will help us understand why some things are the way they are. For example, we know that every module must have a module.py file, or a single module folder can contain multiple module classes, if they can be seen from module.py (by importing them, for example)

After that long explanation, let’s actually start writing our own module.

Module Basics

Now, open the module file we created in the last tutorial, and remove everything inside it. We’re going to write it from scratch.

Before doing anything, we need a module class, which is a class extending Module. If you go to the documentation of the Module class (by clicking that link above), you will see some attributes. The first 4 (id, name, version and authors) need to be provided by the module creator. The rest comes from Monium’s API.

Here is an example module class:

from monium.module import Module

class HelloWorldModule(Module):
    id = "helloworld"
    name = "Hello World Module"
    version = (1, 0, 0)
    authors = ["Admicos"]

Now, this is technically a module. running:

$ monium info

will show that it’s recognized by the module loader. But it won’t do anything if we ran the bot.

For the module to be functional, we need to add a couple of event handlers.

Events

To handle an event, we just need to implement that event’s method. The event we’re going to implement is on_ready. Since Monium uses discord.py underneath everything, all discord.py events are supported. You can see a list of them here. Also, events are coroutines and regular method definition syntax on its own won’t work. To create an event, you need to create an async method, like so:

async def on_ready():
    # ... event stuff ...

Note

Tip: If you’re using an IDE like PyCharm, after writing async def it can autocomplete the event name and arguments (with type hints).

Now, our module is going to send Hello, World to all channels upon startup, and this is the part where discord.py knowledge is helpful. (Most of Monium can be considered wrappers to the discord.py events and functions, so that knowledge will get really helpful. Learning it is neccessary to write good bots)

Here is how we’re going to do it:

import discord

# ...

async def on_ready():
    # self.client is an instance of MoniumClient, which is a subclass of
    # discord.Client. This will be what you'll use as the client in discord.py
    for channel in self.client.get_all_channels():
        if channel.type == discord.ChannelType.text:
            await self.client.send_message(channel, "Hello, world!")

And with that, our full module class becomes:

import discord

from monium.module import Module

class HelloWorldModule(Module):
    id = "helloworld"
    name = "Hello World Module"
    version = (1, 0, 0)
    authors = ["Admicos"]

    async def on_ready():
        for channel in self.client.get_all_channels():
            if channel.type == discord.ChannelType.text:
                await self.client.send_message(channel, "Hello, world!")

The same module (with some comments) can be found on our examples repository.

In the next tutorial, we’re going to launch the bot and test our module.