Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cyberbullying Detection Discord Bot

A Discord bot that automatically detects and moderates cyberbullying messages in real-time using Machine Learning (SVM classifier with TF-IDF vectorization) and a curated suspicious words list.

Features

  • Real-time message analysis — Every message is scanned for cyberbullying content
  • Dual detection system — Combines a suspicious words list with a trained SVM model for accurate detection
  • Auto-moderation — Deletes offensive messages automatically
  • User tracking — Tracks offenders using SQLite, flags users after 10+ violations, and auto-kicks after 20+
  • Logging channel — Dedicated channel for admins to see all violations with detailed embeds
  • Offense history — Full history of each user's violations stored in database
  • Comprehensive admin commands — Check users, view history, reset data, list flagged users, and more

How It Works

  1. A message is sent in the server
  2. The bot preprocesses the text (removes URLs, mentions, normalizes whitespace)
  3. Checks the message against a list of known suspicious/offensive words
  4. If no match, the message is vectorized using TF-IDF and classified by the SVM model
  5. If cyberbullying is detected:
    • The message is deleted
    • The user is warned
    • The incident is logged to the database and log channel
    • The user's offense count is incremented
  6. After exceeding thresholds, users are flagged (10+) or kicked (20+) automatically

Project Structure

├── bot.py                  # Main bot script
├── bot.log                 # Bot activity log (auto-generated)
├── discord_bully.db        # SQLite database (auto-generated)
├── server_config.json      # Server configurations (auto-generated)
├── data/
│   ├── cyberbullying.csv   # Training dataset
│   ├── CyberTrollIEEE.csv  # Training dataset (IEEE)
│   └── sus_words.csv       # Curated list of suspicious/offensive words
├── ipynb/
│   └── main.ipynb          # Jupyter notebook for model training & analysis
├── models/                 # Pre-trained models (not included in repo)
│   ├── svc_model.pkl       # Trained SVM classifier
│   └── tfidf_vectorizer.pkl # Fitted TF-IDF vectorizer
├── requirements.txt        # Python dependencies
├── .env                    # Environment variables (not included in repo)
└── LICENSE

Setup

1. Clone the repository

git clone https://github.com/<your-username>/<repo-name>.git
cd <repo-name>

2. Download the pre-trained models

The models/ folder is not included in this repository. Download it from Google Drive:

Download Models

Place the downloaded files (svc_model.pkl and tfidf_vectorizer.pkl) inside a models/ directory at the project root.

3. Install dependencies

pip install -r requirements.txt

4. Configure environment variables

Create a .env file in the project root:

DISCORD_BOT_TOKEN=your_discord_bot_token_here

5. Run the bot

python bot.py

Requirements

  • Python 3.8+
  • A Discord Bot Token (from the Discord Developer Portal)
  • CUDA-compatible GPU (optional, for model training)

Bot Commands

All moderation commands require Administrator permission.

Command Description
!help_mod Show all moderation commands
!check_user <username> Check a user's offense record
!user_history <username> [limit] View a user's recent offenses (default: 5)
!reset_user <username> Reset a user's offense data
!flagged_users List all flagged users
!top_offenders [limit] Show top offenders (default: 10)
!set_log_channel [#channel] Set the logging channel for violations
!bot_stats Show bot statistics

Logging Channel

Set up a dedicated logging channel to receive detailed reports of all detected violations:

  1. Create a private channel for moderators (e.g., #mod-logs)
  2. Run !set_log_channel #mod-logs in that channel
  3. The bot will now send detailed embeds for every:
    • Cyberbullying detection (with message content, detection method, user info)
    • User kick
    • User reset

Configuration

The following values can be adjusted in bot.py:

FLAG_THRESHOLD = 10   # Offenses before user is flagged
KICK_THRESHOLD = 20   # Offenses before user is auto-kicked

Database Schema

user_data

Column Type Description
username TEXT Primary key, Discord username
user_id INTEGER Discord user ID
count INTEGER Total offense count
flag INTEGER 1 if flagged, 0 otherwise
kicked INTEGER 1 if kicked, 0 otherwise
last_offense TEXT ISO timestamp of last offense

offense_log

Column Type Description
id INTEGER Auto-increment primary key
user_id INTEGER Discord user ID
username TEXT Discord username
guild_id INTEGER Server ID
channel_id INTEGER Channel ID
message_content TEXT The offensive message (truncated to 500 chars)
detection_method TEXT "word_list" or "ml_model"
timestamp TEXT ISO timestamp

Datasets

The model was trained on the following datasets:

  • CyberTrollIEEE.csv — IEEE cybertroll dataset with content and annotation columns (1 = cyberbullying, 0 = safe)
  • cyberbullying.csv — Additional cyberbullying dataset

Tech Stack

  • Discord APIdiscord.py
  • ML Model — SVM (Support Vector Machine) via scikit-learn
  • Feature Extraction — TF-IDF Vectorizer
  • Database — SQLite
  • Training — PyTorch, Transformers, CUDA (GPU-accelerated)

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

Discord bot that detects cyberbullying using ML

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages