Discord Bot to Purge Server Channels
Creating a Discord bot to delete messages from different channels is feasible with Python and the discord.py
library. Here is a simple guide to set it up on Debian.
- Go to the Discord Developer Portal.
- Click on New Application, give it a name, and click on Create.
- Go to the Bot tab and click on Add Bot. Confirm.
- Under Token, click on Copy to save the bot token (keep it private).
- Go to the Discord Developer Portal.
- Select your application (the bot).
- In the left menu, click on Bot.
- Under Privileged Gateway Intents, enable the Message Content Intent option.
- Click on Save Changes.
- Under the OAuth2 > URL Generator tab, check:
bot
in Scopes.- The necessary permissions in Bot Permissions (e.g.,
Send Messages
).
- Copy the generated URL and open it in your browser.
- Invite the bot to your Discord server by following the instructions.
Enable Developer Mode in Discord (Settings > Advanced > Developer Mode). Right-click on the relevant channel and select Copy ID.
On your Debian, install Python and the necessary libraries:
sudo apt update
sudo apt install python3 python3-pip -y
pip3 install discord.py
Create a file bot.py
with the following content:
import discord
from discord.ext import commands
import asyncio
# Replace with your own token
TOKEN = "YOUR_TOKEN_HERE"
# Replace with the IDs of the channels where you want to delete messages
CHANNEL_IDS = [123456789012345678, 987654321098765432] # Example channel IDs
# Maximum number of messages to delete per execution
AMOUNT_TO_CLEAR = 100
# Initialize the bot with the necessary intents
intents = discord.Intents.default()
intents.messages = True # Allows the bot to interact with messages
intents.message_content = True # Necessary to access message content
bot = commands.Bot(command_prefix="!", intents=intents)
@bot.event
async def on_ready():
print(f"Bot connected as {bot.user}")
for channel_id in CHANNEL_IDS:
channel = bot.get_channel(channel_id)
if channel:
try:
# Purge messages in the channel
deleted = await channel.purge(limit=AMOUNT_TO_CLEAR)
print(f"{len(deleted)} messages deleted in channel {channel_id}.")
except discord.errors.Forbidden:
print(f"Access denied to delete messages in channel {channel_id}.")
except discord.errors.HTTPException as e:
print(f"HTTP error while attempting to purge in channel {channel_id}: {e}")
else:
print(f"Channel with ID {channel_id} not found.")
# Stop the bot after executing the purge
await bot.close()
# Run the bot
bot.run(TOKEN)
To run the bot in the background, you can use nohup
so it continues to run even if you close the terminal. Here is the command to execute to start the bot and log the output to a file:
nohup python3 /path/to/bot.py > /path/to/bot_output.log 2>&1 &
This command runs the script in the background and redirects the output (logs and errors) to the bot_output.log
file.
To automatically restart the bot every Monday at 5:00 AM, you need to set up a cron job. To do this, edit your crontab:
crontab -e
Add the following line to run the bot every Monday at 5:00 AM:
0 5 * * 1 nohup python3 /path/to/bot.py > path/to/bot_output.log 2>&1 &
nohup
is optional since the script is not being run from the terminal.
This will ensure that your bot is automatically executed every Monday at 5:00 AM.
With this guide, you have a Discord bot that purges messages every 7 days in the channels of your choice. You can adapt the script and settings according to your needs and use cron
to schedule the automatic execution of the bot every Monday at 5:00 AM.
- Keep your token secret: Never share your token. If you think it is compromised, regenerate it from the Discord portal.
- Bot permissions: Ensure the bot has the necessary permissions on each channel.
- Test locally: Make sure the script works correctly before automating it.