Creating a Discord Bot to Delete Channels with Python

·

3 min read

Creating a Discord Bot to Delete Channels with Python

Table of contents

No heading

No headings in the article.

Contributor: Ghost Ops

A while ago, my friends and I created a Discord server to discuss games, anime, and rant about classmates. Inspired by the "Group chat got leaked" meme, we wanted to protect our messages. Since bots can only delete messages within 14 days, I developed a bot to delete a temporary rant channel.

First, we start by importing the necessary libraries:

pip install discord.py

We also initialize the required intents to enable access to message content, members, and guilds.

Next, we create an instance of the Bot class from discord.ext.commands, and set the command prefix and intents. This will be the foundation of our Discord bot.

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.guilds = True

bot = commands.Bot(command_prefix='!', intents=intents)

CHANNEL_ID = None

We define a global variable called CHANNEL_ID and set it to None. This variable will store the ID of the channel that we want to delete.

Within the bot's event handlers, we have the on_ready event that will print a message when the bot is logged in and ready to be used. We also have the on_message event that allows the bot to process commands in messages.

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user.name}')

This event is triggered when the bot successfully connects to the Discord server. It prints a message indicating the successful login.

@bot.event
async def on_message(message):
    await bot.process_commands(message)

This event is triggered whenever a new message is received. It ensures that the bot processes any valid commands in the message. This command, delete_channel, is used to delete a specific channel. It requires the user to have manage_channels permission. It checks if a valid CHANNEL_ID is assigned and deletes the corresponding channel. If the CHANNEL_ID is not assigned or the channel is invalid, appropriate messages are sent.

@bot.command()
@commands.has_permissions(manage_channels=True)
async def delete_channel(ctx):
    global CHANNEL_ID

    if not CHANNEL_ID:
        await ctx.send("No channel ID assigned.")
        return

    channel = bot.get_channel(int(CHANNEL_ID))

    if channel is None:
        await ctx.send("Invalid channel ID.")
        return

    await channel.delete()
    print(f'Deleted channel: {channel.name}')

To make sure the program working properly we add an error handler for the delete_channel function. If a user lacks the required permissions, it sends a message indicating the lack of permission.

@delete_channel.error
async def delete_channel_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You do not have permission to delete channels.")

Now let us create a function assign to assign the current channel's ID to the CHANNEL_ID variable. It allows the bot to know which channel to delete.

@bot.command()
async def assign(ctx):
    global CHANNEL_ID
    CHANNEL_ID = ctx.channel.id
    await ctx.send(f"Channel ID assigned: {CHANNEL_ID}")

Using bot.run() function we can activate the bot with provided bot token from Discord Developer Portal. Replace 'YOUR_BOT_TOKEN' with the actual token.

bot.run('YOUR_BOT_TOKEN')

Now you can invite your bot using URL_generator in the Discord Developer portal.

You can assign the channel ID by using the !assign command in the channel you want to delete. Using the !delete_channel command anywhere in the server, you can delete the assigned channel.