Python Discord Bot Development

Victorium Z

Backend Engineer
Game Developer
AI Developer
Discord
GitHub
Visual Studio Code
With time, I have been perfecting my technique when developing Discord bots in a clear, organized and effective way. Here I leave you the most basic and fundamental parts of the bots, and some examples of commands.
# main.py
from asyncio import run

from discord import Intents
from discord.ext.commands import Bot

from utils.extensions import load_extensions
from utils.logs import log
from utils import env


def main():
    bot = Bot(command_prefix="!", intents=Intents.all(), help_command=None)
    run(load_extensions(bot))
    bot.run(env.get("TOKEN"), log_handler=None)

if __name__ == "__main__":
    try:
        main()
    except Exception as error:
        log("error", error=error)
# events/ready.py
from discord import Activity
from discord.ext.commands import Bot, Cog

from utils.logs import log
from utils.settings import setting
from utils.extensions import sync_commands


STATUS: str = setting("bot", "status")

ACTIVITY_NAME: str = setting("bot", "activity", "name")
ACTIVITY_TYPE: str = setting("bot", "activity", "type")


class OnReadyEvent(Cog):
    def __init__(self, bot: Bot) -> None:
        self.bot: Bot = bot

    @Cog.listener()
    async def on_ready(self) -> None:
        log("login", user=self.bot.user, user_id=self.bot.user.id)


        await self.bot.change_presence(
            status=STATUS,
            activity=Activity(
            name=ACTIVITY_NAME,
        type=ACTIVITY_TYPE
)
        )


        await sync_commands(self.bot)


async def setup(bot: Bot) -> None:
    await bot.add_cog(OnReadyEvent(bot))
# commands/ping.py
from discord import Interaction, User
from discord.ext.commands import Bot, Cog
from discord.app_commands import command, describe


from utils.messages import message
from utils.logs import log




NAME: str = message("commands", "ping", "name")
DESCRIPTION: str = message("commands", "ping", "description")


USER: str = message("commands", "ping", "user")




class PingCommand(Cog):
    def __init__(self, bot: Bot) -> None:
        self.bot: Bot = bot


    @command(name=NAME, description=DESCRIPTION)
    @describe(user=USER)
    async def ping(self, interaction: Interaction, user: User = None) -> None:
        log("command", command=NAME, user_name=interaction.user.display_name)
        try:
            await self.command(interaction, user)
        except Exception as error:
            log("error", error=error)


    async def command(self, interaction: Interaction, user: User) -> None:
        if user is None:
            user = interaction.user
        response: str = message("commands", "ping", "response", user_id=user.id)
        await interaction.response.send_message(response, ephemeral=True)


async def setup(bot: Bot) -> None:
    await bot.add_cog(PingCommand(bot))

Thanks for your interest in my way of working, and don't hesitate to contact me if you are interested in getting your own customized Discord bot. 😀 –

Partner With Victorium
View Services

More Projects by Victorium