A Node.js bot for Discord that uses AI to automatically moderate messages and remove spam or scam content from your server.
- Automatically monitors Discord channels for spam and scam messages
- Uses OpenAI's GPT models to intelligently detect problematic content
- Considers previous channel messages for context when moderating
- Can be configured to monitor specific channels or the entire server
- Allows excluding users with specific roles from moderation
- Automatically times out users who post spam/scam content (configurable duration)
- Sends detailed notifications to a designated channel for moderation actions
- Provides detailed logging of moderation activities
- Runs as a systemd service on Ubuntu for 24/7 operation
- Node.js 16.x or higher
- Discord Bot Token (with appropriate permissions)
- OpenAI API Key
- Ubuntu server (for systemd service setup)
-
Clone this repository:
git clone https://github.com/EkuboProtocol/discord-mod-bot.git cd discord-mod-bot -
Install dependencies:
npm install
-
Create a
.envfile based on the example:cp .env.example .env nano .env # Edit with your actual credentials -
Configure your environment variables in the
.envfile:DISCORD_TOKEN=your_discord_bot_token DISCORD_SERVER_ID=your_server_id OPENAI_API_KEY=your_openai_api_key OPENAI_MODEL=gpt-3.5-turbo # Add more configuration as needed
Start the bot directly using Node.js:
node src/index.jsThe bot supports the following command line arguments:
Options:
-t, --token Discord bot token [string]
-s, --server Discord server ID [string]
-c, --channels Comma-separated list of moderated channel IDs
(default: all) [string]
-e, --excluded-roles Comma-separated list of role IDs to exclude from
moderation [string]
-x, --excluded-channels Comma-separated list of channel IDs to exclude from
moderation [string]
-w, --welcome-channel Channel ID of the welcome channel to exclude from
moderation [string]
-m, --openai-model OpenAI model to use [string]
-n, --notification-channel Channel ID to send notifications to [string]
--context-messages Number of previous messages for context [number]
-l, --log-level Log level (error, warn, info, debug)
[string] [default: "info"]
-k, --openai-api-key OpenAI API key [string]
-h, --help Show help [boolean]
Example:
node src/index.js --token=your_token --server=your_server_id --channels=channel1,channel2 --excluded-roles=role1,role2 --openai-model=gpt-4-
Copy the service file to systemd:
sudo cp discord-mod-bot.service /etc/systemd/system/
-
Edit the service file to match your environment:
sudo nano /etc/systemd/system/discord-mod-bot.service
-
Reload systemd, enable and start the service:
sudo systemctl daemon-reload sudo systemctl enable discord-mod-bot sudo systemctl start discord-mod-bot -
Check the status:
sudo systemctl status discord-mod-bot
The repository includes a ready-to-use App Platform spec at .do/app.yaml that deploys the bot as a worker service.
- Install and authenticate
doctl:doctl auth init
- Review
.do/app.yamland update the GitHub repo/branch or worker sizing if needed. - Create the app:
doctl apps create --spec .do/app.yaml
- After the app is created, add the required environment variables (Discord token, server ID, OpenAI key, etc.) under Settings → Environment Variables or by updating the spec and running:
doctl apps update <APP_ID> --spec .do/app.yaml
The spec lists the variables the worker expects:
DISCORD_TOKEN(secret) – Discord bot tokenDISCORD_SERVER_ID– ID of the Discord server to moderateOPENAI_API_KEY(secret) – OpenAI API keyOPENAI_MODEL– Model to use (defaults togpt-3.5-turbo)LOG_LEVEL– Logging verbosity (error,warn,info,debug)CONTEXT_MESSAGE_COUNT– Number of prior messages to send for contextTIMEOUT_DURATION– Timeout duration in minutes (set0to disable)MODERATED_CHANNELS,EXCLUDED_CHANNELS,EXCLUDED_ROLES,WELCOME_CHANNEL_ID,NOTIFICATION_CHANNEL_ID,IGNORED_PHRASES– Optional fine-tuning values; leave blank to use defaults
Remember to keep secret values (Discord/OpenAI tokens) stored as Secret scope variables in App Platform so they are not committed to the repository.
The bot can be configured using environment variables or command line arguments. Command line arguments take precedence over environment variables.
DISCORD_TOKEN: Your Discord bot tokenDISCORD_SERVER_ID: The ID of your Discord serverMODERATED_CHANNELS: Comma-separated list of channel IDs to moderate (leave empty to moderate all)EXCLUDED_CHANNELS: Comma-separated list of channel IDs to exclude from moderationWELCOME_CHANNEL_ID: Channel ID of the welcome channel to exclude from moderation (automated messages)EXCLUDED_ROLES: Comma-separated list of role IDs to exclude from moderationTIMEOUT_DURATION: Duration in minutes to timeout users when their message is deleted (default: 5, set to 0 to disable)OPENAI_API_KEY: Your OpenAI API keyOPENAI_MODEL: Model to use for AI moderation (default: gpt-3.5-turbo)IGNORED_PHRASES: Comma-separated list of phrases to ignore (won't be sent to OpenAI)NOTIFICATION_CHANNEL_ID: Channel ID to send detailed moderation notificationsCONTEXT_MESSAGE_COUNT: Number of previous messages to include for context (default: 5)LOG_LEVEL: Logging level (error, warn, info, debug)
Logs are stored in the logs directory:
logs/combined.log: Contains all log messageslogs/error.log: Contains only error messages
The bot monitors messages for:
- Messages containing suspicious links or asking users to open tickets
- Messages asking users to DM for support instead of using public channels
- Generic job-seeking messages that appear to be copy-pasted
- Messages asking who to contact for unspecified business/partnerships
- Messages promising rewards, giveaways, or airdrops that seem suspicious
- Impersonation of staff or team members
- Phishing attempts asking for personal information or wallet addresses
There are several ways to integrate this bot with Discord:
This is the most common method where you create a bot application in the Discord Developer Portal and add it to your server. This approach:
- Requires a separate bot user in your server
- Needs specific permissions to read/delete messages
- Is easy to set up through the Discord Developer Portal
For more advanced integrations with fewer permissions, you could modify this code to use:
- Discord Webhooks: For sending notifications without a full bot
- Discord Interactions: For slash commands (would require additional development)
While technically possible to use a user account token instead of a bot token, this approach:
- Violates Discord's Terms of Service
- Can result in account termination
- Is not supported by this code
If you already have a bot for your server, you could integrate this moderation code as a module in your existing bot framework to avoid adding another bot user to your server.
- Bot not starting: Check the logs for error messages
- Bot not detecting messages: Make sure the bot has the required permissions in Discord
- OpenAI API errors: Verify your API key and quota
- Bot not running after system restart: Make sure the systemd service is enabled
- Context not being considered: Ensure the bot has permissions to read message history
MIT