How sudis/discord-email-verifier works
Project Type and Purpose
• This is a backend Node.js “Discord bot” service whose sole responsibility is to verify users’ email addresses via SMTP (e-mail) and store/track verification states in a MySQL database.
• It uses the Discord slash-command framework GCommands V9, Nodemailer for SMTP, and a MySQL backend.
File-Structure Analysis
• index.js – the entry point. Initializes the Discord client (with GCommands), loads configuration, connects to MySQL, registers slash commands and event listeners, and starts the bot.
• config/
– functions.js – common utility functions (e.g., database helpers, timeouts).
– mysql.js – establishes and exports a MySQL connection pool.
– settings.json – environment-specific settings (SMTP credentials, DB credentials, bot token).
– table.sql – DDL for creating the verification table.
• commands/verify.js – slash-command handler for /verify send <email> and any other verification subcommands.
• listeners/ready.js – event listener for the “ready” event (bot startup).
• examples/ – sample code showing how to programmatically issue commands or listen for events.
Key Architectural Patterns
• Event-Driven / Command Pattern – Discord events are fed into GCommands, which dispatches to command handlers or listeners.
• Layered Approach –
– Presentation layer: Discord API Gateway (slash commands and events).
– Application layer: command handlers (business logic), listener handlers.
– Infrastructure layer: MySQL database, external SMTP service.
• Asynchronous I/O – All I/O operations (Discord API, MySQL, SMTP) are handled asynchronously via Promises or callbacks.
Main Components
a. Discord API Gateway
– Emits raw websocket events (ready, interactionCreate).
– Receives outgoing messages & slash‐command registrations.
b. Bot Core (index.js + GCommands)
– Event Dispatcher: routes incoming interactions to the right command or listener.
– Configuration Loader: reads settings.json and initializes SMTP & DB modules.
c. Command Handler (commands/verify.js)
– Implements /verify send <email> logic:
• Insert or update verification record in MySQL with status=“PENDING” and a TTL.
• Generate and send verification code via Nodemailer to the provided email.
d. Listener Handler (listeners/ready.js)
– On “ready” event: logs startup info, registers slash commands with Discord.
e. MySQL Database (config/mysql.js + table.sql)
– Verification Table: fields like userId, email, status, timestamp.
– TTL cleanup logic (could be a scheduled job in functions.js or handled in command logic).
f. SMTP Service (Nodemailer + external Gmail SMTP)
– E-mail transport for sending verification messages.
g. Utilities (config/functions.js)
– Timeout/deletion logic for pending records.
– Other helper methods (email formatting, database wrappers).
Relationships and Data Flows
1. User issues /verify send <email> in Discord
↓
2. Discord Gateway → Bot Core (interactionCreate)
↓
3. Bot Core dispatch → Command Handler
↓
4. Command Handler
• Calls DB module → INSERT/UPDATE PENDING record
• Calls Email module → Nodemailer → Gmail SMTP
↓
5. Email is sent → User inbox
↓
6. (Optionally) User clicks link or issues another bot command → Bot Core → verify status updated to VERIFIED in DB
↓
7. Post-verification actions (e.g., assign Discord role) can be added here.
Essential Elements to Capture
• Bot Initialization (config load, DB connect, GCommands register)
• Runtime Event Loop (Discord events → command/listener dispatch)
• Data Store interactions (PENDING → VERIFIED → DELETE)
• External E-mail dispatch via Nodemailer