A Discord bot that uses machine learning to detect and moderate hate speech in server messages. The bot monitors messages in real-time, identifies potentially harmful content, and takes configurable actions such as flagging, deleting messages, or temporarily muting users.

- Real-time hate speech detection using a pre-trained machine learning model
- Configurable actions: flag, delete, or mute
- Server-specific settings with channel and role whitelisting
- Moderator notification system
- Detection statistics and analysis tools
- Admin commands for configuration and moderation
- Python 3.9 or higher
- A Discord account and a registered application/bot
- pip (Python package manager)
-
Clone this repository:
git clone https://github.com/yourusername/discord-hate-speech-bot.git cd discord-hate-speech-bot
-
Create a virtual environment:
python -m venv venv
-
Activate the virtual environment:
- On Windows:
venv\Scripts\activate
- On macOS/Linux:
source venv/bin/activate
- On Windows:
-
Install required packages:
pip install -r requirements.txt
-
Create a
.env
file in the project root directory with your Discord bot token:DISCORD_TOKEN=your_discord_bot_token
- Go to the Discord Developer Portal
- Click "New Application" and give it a name
- Navigate to the "Bot" tab and click "Add Bot"
- Under "Privileged Gateway Intents", enable "Message Content Intent"
- Copy the bot token and add it to your
.env
file - Generate an invite link from the OAuth2 URL Generator:
- Select "bot" scope
- Select permissions: "Read Messages/View Channels", "Send Messages", "Manage Messages", "Moderate Members"
- Use the generated URL to invite the bot to your server
-
Make sure your virtual environment is activated
-
Run the bot:
python bot.py
-
The bot will connect to Discord and start monitoring messages in servers where it has been added
!analyze <text>
- Analyze text for hate speech (Moderator only)!stats
- Show detection statistics (Moderator only)!reset_stats
- Reset detection statistics (Admin only)!config
- View current server configuration (Admin only)!config <setting> <value>
- Update server configuration (Admin only)
enabled
- Enable/disable the bot (true/false)threshold
- Detection sensitivity (0.0-1.0)action
- Action to take when hate speech is detected (flag/delete/mute)mod_channel
- Channel ID for moderator notificationswhitelist_channels
- Channels to ignorewhitelist_roles
- Roles that bypass detection
The project includes a comprehensive test suite to ensure everything works correctly:
-
Make sure your virtual environment is activated
-
Install test dependencies:
pip install pytest pytest-asyncio
-
Run all tests:
pytest -xvs test_bot.py
-
Run specific test categories:
# Test just the detector pytest -xvs tests/test_bot.py::TestHateSpeechDetector # Test server configuration pytest -xvs tests/test_bot.py::TestServerConfig # Test bot events pytest -xvs tests/test_bot.py::TestBotEvents # Test bot commands pytest -xvs tests/test_bot.py::TestBotCommands
-
Run a specific test:
pytest -xvs test_bot.py::TestHateSpeechDetector::test_predict_hate_speech
The default bot uses a pre-trained model, but you can fine-tune or replace it:
- Collect training data (labeled examples of hate speech and non-hate speech)
- Fine-tune the model using the Hugging Face Transformers library
- Replace the model path in the
HateSpeechDetector
class
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Hugging Face Transformers for the pre-trained models
- discord.py for the Discord API wrapper