A Discord bot that automatically cleans messages in specified channels after a certain period of time.
- Automatically delete messages in specified channels after a set amount of time.
- Supports different cleaning intervals for different channels.
- Commands to enable the cleaner, set cleaning intervals, and manually test the cleaner.
- Permission checks to ensure only users with specified roles can execute commands.
- The scheduled cleanup runs every 15 minutes (Default).
- Limit the amount of inputs to prevent spam/DoS type scenarios.
- A server to run the bot on
- Python 3.6 or higher
- Linux system to run the bot (Raspberry Pi or similar)
- App setup in Discord Developer portal (Scroll down to bottom to see how)
-
Ensure Python and pip are installed:
sudo apt update sudo apt install python3 python3-pip
-
Clone this repository:
git clone https://github.com/hitem/CleanBot.git cd CleanBot
-
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activate
-
Install
pipenv
within the virtual environment:pip install pipenv
-
Install the required Python packages using
pipenv
:pipenv install
-
Create a
.env
file following the format of.env_example
. Add your DISCORD_BOT_TOKEN accordingly (make sure you have completed the Prerequisites) -
Run the bot:
pipenv run python3 CleanBotman.py
Here are some extra step to run your bot as a service on the server incase of reboot or similiar scenarios.
-
Create a systemd service file:
sudo nano /etc/systemd/system/discord-cleaner-bot.service
-
Add the following content to the file, make sure you change
/path/to/your/
to correct directory and username.[Unit] Description=Discord Cleaner Bot After=network.target [Service] Type=simple User=your_username WorkingDirectory=/path/to/your/CleanBot ExecStart=/bin/bash -c 'source /path/to/your/CleanBot/venv/bin/activate && pipenv run python3 /path/to/your/CleanBot/CleanBotman.py' Restart=on-failure StandardOutput=journal StandardError=journal [Install] WantedBy=multi-user.target
-
Reload systemd to recognize the new service:
sudo systemctl daemon-reload
-
Enable the service to start on boot:
sudo systemctl enable discord-cleaner-bot
-
Start the service:
sudo systemctl start discord-cleaner-bot
-
Check the service status:
sudo systemctl status discord-cleaner-bot
-
!enablecleaner CHANNEL_ID
Enable the cleaner for a specific channel. The default cleaning interval is 24 hours. -
!setcleaningtime HOURS
Set the cleaning interval for the current channel.HOURS
must be between 1 and 72. -
!testcleaner TIME
Manually test the cleaner in the current channel.TIME
can beall
to delete all messages or a number of hours. -
!checkpermissions
Check your permissions. This won't show the role name, but it will show your permission ID. -
!listchannels
List all channels + channel_id on the current server (discord calls it guild). -
!cleanersetting
Check if the cleaner is enabled and what the current timer setting is for the current channel. It returns "Cleaner is enabled and timer is set to xx hours" if enabled, otherwise it will state that the cleaner is not enabled for the channel. -
!cleanerhelp
Lists all the bot-commands available.
The bot uses systemd journal for logging. To view the logs, use:
sudo journalctl -u discord-cleaner-bot -f
- Go to the Discord Developer Portal.
- Click on "New Application".
- Enter a name for your bot and click "Create".
- Go to the "Bot" section and click "Add Bot".
- Click "Yes, do it!" to confirm.
- Under the "Token" section, click "Copy" to copy your bot token. This will be used as the
DISCORD_BOT_TOKEN
environment variable. - Under "Privileged Gateway Intents", enable "Message Content Intent".
- Save your changes.
- Go to the "OAuth2" section, then "URL Generator".
- Under "SCOPES", select "bot".
- Under "BOT PERMISSIONS", select the permissions your bot needs:
Manage Messages
Read Messages
Send Messages
View Channels
Read Message History
- Copy the generated URL and open it in your browser.
- Select the server you want to add the bot to and authorize it.