Reputation: 3
#deleted unused registered channels
@bot.command(pass_context=True)
#so that only an admin can run this command
@commands.has_role('Admin')
async def unregister(ctx, catID):
cat = discord.utils.get(ctx.guild.categories, id = catID)
#await ctx.channel.send(cat)
for channel in cat.voice_channels:
await channel.delete()
for channel in cat.text_channels:
await channel.delete()
cat.delete()
This is the code I have currently. I have also tried it with client.get_channel(). I seem to get the error that category doesn't have access to .voice_channels and .text_channels, or that cat is a "NoneType". I am new to python and coding a discord bot so any help with this command would be great. Thank you!
The command is meant to take the categoryID you can get from right clicking and copying a category on discord. Then delete every voice and text channel under it. Then delete itself.
Upvotes: 0
Views: 1882
Reputation: 3602
Your code is nearly correct in itself, but can be simplified in many places.
cat
is not necessary at all, so it does not need to be defined, because we can simply say the following:
category: discord.CategoryChannel
Now we just have to enter the ID and the rest will take care of itself. Based on the definition we can now build the code further:
@bot.command(pass_context=True)
@commands.has_role('Admin')
async def unregister(ctx, category: discord.CategoryChannel):
delcategory = category # delcategory is our ID (category)
channels = delcategory.channels # Get all channels of the category
for channel in channels: # We search for all channels in a loop
try:
await channel.delete() # Delete all channels
except AttributeError: # If the category does not exist/channels are gone
pass
await delcategory.delete() # At the end we delete the category, if the loop is over
Upvotes: 2