-
Notifications
You must be signed in to change notification settings - Fork 0
Architecture Overview
This document explains the architecture and structure of the Ui-Py Discord bot, helping developers understand how the various components work together.
Ui-Py is built using the discord.py library and follows a modular design pattern. The bot is structured as follows:
Ui-Py/
├── main.py # Entry point and core bot class
├── functions/ # Command modules organized by category
│ ├── system/ # Core system commands
│ └── tool/ # Utility and feature-specific commands
├── media/ # Static media files
└── Wiki/ # Documentation
The UiPy class in main.py is the central component that:
- Initializes the bot with the necessary intents
- Sets up an HTTP session for web requests
- Dynamically loads command modules
- Handles events like connecting to Discord and shutting down
The bot uses commands.AutoShardedBot from discord.py, which automatically manages sharding for larger bot installations.
Commands are organized as extensions (cogs) in the functions/ directory, categorized by their purpose:
-
functions/system/- Core functionality and admin commands -
functions/tool/- User-facing features and utilities
Each extension follows a standard structure:
- A class that inherits from
commands.Cog - Command methods decorated with
@app_commands.command() - A
setupfunction that registers the cog with the bot
Example:
from discord.ext import commands
from discord import app_commands, Interaction
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from main import UiPy
class MyCog(commands.Cog):
def __init__(self, bot: "UiPy"):
self.bot = bot
@app_commands.command(
name="mycommand",
description="Description of my command"
)
async def my_command(self, interaction: Interaction):
await interaction.response.send_message("Hello, world!")
async def setup(bot: "UiPy"):
await bot.add_cog(MyCog(bot))Ui-Py uses Discord's slash commands system for all user interactions, which provides:
- Better user experience with command auto-completion
- Structured parameter input
- Integrated permission system
Commands are registered with Discord through the sync command, which can sync commands:
- Globally (takes up to an hour to propagate)
- To a specific guild (instant, good for testing)
The bot also uses the event system from discord.py to respond to various Discord events:
-
on_ready: Called when the bot connects to Discord -
on_message: Used byredirect.pyto process links in messages
Custom events can be added by implementing the appropriate event handler in a cog.
Dependencies are managed using Pipenv, with:
-
Pipfile: Defines required packages -
Pipfile.lock: Ensures consistent installations
Ui-Py is designed to be deployed in a Docker container, with:
-
Dockerfile: Multi-stage build for smaller images -
docker-compose.yml: Simple orchestration -
deploy-dc.sh: Deployment script
This containerized approach ensures consistent execution across different environments.
To add a new feature to Ui-Py:
- Create a new Python file in the appropriate subdirectory of
functions/ - Define a cog class with your commands
- Implement a
setupfunction - Test locally with the guild sync command
- When ready, sync globally
When extending Ui-Py, follow these guidelines:
- Keep commands organized by purpose and category
- Use type hints for better code quality
- Follow Discord rate limits for API calls
- Handle errors gracefully and provide user feedback
- Document your commands with clear descriptions
- Use async/await correctly to avoid blocking the bot
- Leverage Discord's features like embeds and buttons for rich interactions