MehlBot is a Discord bot that responds to commands by executing (callback) functions.
It's built around the discord.Client
class.
This allows an easy integration to existing bots and creation of new bots.
Documentation: docs
See examples/hellow_bot.py for full source code.
# imports ...
class HelloBot(discord.Client): # discord.Client class
def __init__(self, intents: Intents, **options) -> None:
super().__init__(intents=intents, **options)
async def on_ready(self) -> None: # Gets called when bot is ready/started
print("Bot started.")
async def on_message(self, message: Message):
if message.author == self.user: # skip if message's author is the bot
return
# necessary callback command
# bot_commands are in mehlbot.command
command_found = await command_callback.process_command(self, bot_commands, message)
# prefix with command if message is command
log_msg = "" if not command_found else "command: "
log_msg += f"{message.author.nick} ({message.author.name}): '{message.content}'"
print(log_msg)
def main():
hello_bot = HelloBot(discord.Intents.all()) # discord intents
with Path("token.txt").open() as file: # load token form file (.gitignore) or use env
token = file.read()
hello_bot.run(token)
And this shows how to add commands
from mehlbot.command import add_command
async def _add(message: discord.Message, client: discord.Client, args: List):
url = args[0]
...
async def _export_playlist(message: discord.Message, client: discord.Client, args: List):
playlist = args[0]
...
add_command("add", _add,
allowed_num_args=[["url"]], # requires exactly 1 argument
description="Adds the given YouTube url to the current playlist.")
add_command("export playlist", _export_playlist,
allowed_num_args=[[], ["playlist_name"]], # either 0 or 1 arg
description="Exports urls of current playlist or of given playlist.")