Some checks failed
Auto Build and Push Docker Image / build (push) Failing after 15s
4.4 KiB
4.4 KiB
AI Agent Guide: Modular Discord Bot Architecture
This document is intended for AI agents to understand, recreate, or extend the pixelpoebel Discord bot. It describes the design patterns, conventions, and key considerations for this specific modular architecture.
🏗 Architecture Overview
The bot uses a Modular Command & Event Loading pattern with ESM (ECMAScript Modules) and TypeScript.
Key Design Patterns:
- Extended Client Pattern: We extend the
discord.jsClientclass to hold global state like thecommandsCollection. This avoids global variables and improves type safety. - Dynamic Discovery: The
src/index.tsentry point usesreaddirSyncand dynamicimport()to automatically register any command or event found in thesrc/commandsorsrc/eventsdirectories. - Interface-Driven Commands: All commands must implement the
Commandinterface defined insrc/structures/Command.ts. - Persistent SQLite Database: The bot uses
better-sqlite3for data persistence. It's initialized insrc/structures/Database.tsand stores data in thedata/directory. - Twitch Monitoring Service: The
TwitchManagerinsrc/structures/handles periodic API polling and notification logic. - Granular Permission Handling: Commands like
twitch.tsuse manual permission checks (interaction.memberPermissions.has(PermissionFlagsBits.Administrator)) to allow a mix of public and restricted subcommands within a single command.
💾 Database Interaction
To use the database in any command or event:
import { DB } from '../../structures/Database.js';
// Example: Get one row
const user = DB.get('SELECT * FROM mod_logs WHERE user_id = ?', userId);
// Example: Insert data
DB.run('INSERT INTO mod_logs (guild_id, user_id, action) VALUES (?, ?, ?)', guildId, userId, 'WARN');
Persistence: In Docker, the data/ folder is mapped to a volume to ensure data survives container restarts.
📡 Twitch API Implementation
The bot uses the Twitch Helix API with a client credentials flow. It requires:
TWITCH_CLIENT_IDTWITCH_CLIENT_SECRET
Workflow:
TwitchManager.getAccessToken(): Handles token acquisition and automatic renewal.TwitchManager.fetchStreamData(channelName): Helper for real-time status checks.TwitchManager.checkStreams(): Periodically checks the status of monitored streamers from the database.TwitchManager.sendNotification(): Constructs and sends an embed with live stream details to the designated Discord channel.
🚀 How to Extend
Adding a Command:
- Navigate to
src/commands/. - Create a subfolder for the category (e.g.,
utility,owner,moderation). - Create a file (e.g.,
ping.ts). - Implement the
Commandinterface andexport default. - Run
npm run deploy(or use/deployin Discord) to register it with Discord.
Adding an Event:
- Navigate to
src/events/. - Create a file (e.g.,
messageCreate.ts). export defaultan object withname,once(optional), andexecute.
⚠️ Critical AI Agent Constraints
- Environment Variables: Always use
process.env.DISCORD_TOKEN,CLIENT_ID,TWITCH_CLIENT_ID, andTWITCH_CLIENT_SECRET. Never hardcode credentials. - Type-Safe Casting: When using
interaction.options.getMember('target'), always cast toGuildMemberif you need guild-specific methods like.kick(). - Interaction States: Remember to check
interaction.repliedorinteraction.deferredbefore replying in error handlers to avoid "Interaction already replied" errors. - Deployment: Slash commands are NOT automatically registered on bot start. You MUST run the
deploy-commands.tsscript whenever the command data changes. - Owner-Checks: Use
client.application.ownerto verify permissions for owner-only commands.
📦 Replication Checklist for Agents
- Initialize NPM project with ESM.
- Set up
tsconfig.jsonfor NodeNext. - Create
ExtendedClient.tsto store commands. - Create
index.tswith dynamic folder loading logic. - Create
deploy-commands.tsfor REST registration. - Implement global error handling in
interactionCreate.ts. - Initialize SQLite database in
index.tsviaDB.init(). - Set up
TwitchManagerfor stream monitoring. - Enable
Message Content Intentin Discord Developer Portal if message tracking is needed.