How to delete a Discord channel using Python?

13,171

Solution 1

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.

Solution 2

@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')
Share:
13,171
Brinj4L
Author by

Brinj4L

Updated on June 14, 2022

Comments

  • Brinj4L
    Brinj4L about 2 years
    @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'

    • BrainDead
      BrainDead over 4 years
      Aside from previous errors, you need to call delete on a TextChannel object discordpy.readthedocs.io/en/latest/…
    • Harmon758
      Harmon758 over 4 years
      @AlbertoPoljak Any subclass of GuildChannel has its corresponding delete method. This includes VoiceChannel and CategoryChannel.
    • BrainDead
      BrainDead over 4 years
      @Harmon758 and? delete_text_channel and discord.TextChannel were enough of a hint for me to tell me that he's dealing with TextChannel. But I get your point altho I'm not sure comments are the best place to get into details. It was more of a hint from me.