Brinj4L
Brinj4L

Reputation: 33

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'

Upvotes: 3

Views: 20677

Answers (2)

Pranav S
Pranav S

Reputation: 425

@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')

Upvotes: 2

Harmon758
Harmon758

Reputation: 5157

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.

Upvotes: 2

Related Questions