Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a Discord channel using Python?

@Bot.command(pass_context= True)
async def complete(ctx):
    guild = ctx.message.guild
    id_admin = discord.Role.id=658306078928273408
    overwrites = {
        id_admin: discord.PermissionOverwrite(send_messages=True),
        guild.me: discord.PermissionOverwrite(read_messages=True),

    }
    await guild.delete_text_channel(discord.TextChannel.name)

I didn't find the right attribute in Discord API.
What attribute should I use to delete the channel in which I wrote the command?
Error: AttributeError: 'Guild' object has no attribute 'delete_text_channel'

like image 756
Brinj4L Avatar asked Jan 27 '26 19:01

Brinj4L


2 Answers

@Harmon758 gives a very good idea of how the delete command should be called but for anyone not familiar with discord API, here is how I handle delete channel request:

@bot.command(name='delete-channel', help='delete a channel with the specified name')
async def delete_channel(ctx, channel_name):
   # check if the channel exists
   existing_channel = discord.utils.get(guild.channels, name=channel_name)
   
   # if the channel exists
   if existing_channel is not None:
      await existing_channel.delete()
   # if the channel does not exist, inform the user
   else:
      await ctx.send(f'No channel named, "{channel_name}", was found')
like image 127
Pranav S Avatar answered Jan 29 '26 07:01

Pranav S


You can use the GuildChannel.delete method, with any subclass of GuildChannel.
You can retrieve the TextChannel the message was sent in using Context.channel.

You should not be modifying the attributes of discord.py classes, and you should be referencing the attributes of specific objects/instances of those classes.

like image 22
Harmon758 Avatar answered Jan 29 '26 09:01

Harmon758