Search code examples
pythondiscorddiscord.py

Nuke every channel in a server discord.py


so this worked before but it suddenly doesn't work. The point of this code is that it goes through every text channel in a server and nukes it (basically reclones and deletes the old channel while keeping the recloned channel there). However, it only works for few channels now and then stops by saying:

 raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: NotFound: 404 Not Found (error code: 10003): Unknown Channel

Now, I'm new to discord.py. So if anyone could show me the fix code for this I'd really be grateful, along with a small explanation so I know what I did wrong.I tried fixing it myself by adding the recloned channel in a list by appending it to that list and skipping it if it saw that channel, but the code didn't work. Below is my code, I'd be really grateful is someone showed the fix code for it and how it works:

@client.command()
async def reclone(ctx):
  for channel in ctx.guild.text_channels:
      time.sleep(0.2)
      print(f"Cloning {channel.name}")
      newchannel = await channel.clone(reason="Channel has been nuked")
      print(f"Cloned {channel.name}")
      await channel.delete() 
      print(f"Nuked {channel.name}")
      time.sleep(0.2)
  # Editing the position here
      await newchannel.edit(position=channel.position)
      print(f"Repositioned {newchannel.name}")
      await newchannel.send("**[Soon]** :copyright:")
      print(f"Sent message to {newchannel.name}")

I've done ctx.guild.text_channels as I only want it to delete all text channels in a server and reclone, nothing else.


Solution

  • It looks like your code is correct, you just need to make sure that the bot has the correct permissions: try giving it administrator permissions.

    Here is the code that I used:

    import discord 
    from discord.ext import commands
    from discord.ext.commands import has_permissions
    import time
    
    intents = discord.Intents.default()
    intents.message_content = True
    bot = commands.Bot(command_prefix = ".", intents=intents)
    
    @bot.command()
    async def reclone(ctx):
      for channel in ctx.guild.text_channels:
          time.sleep(0.2)
          print(f"Cloning {channel.name}")
          newchannel = await channel.clone(reason="Channel has been nuked")
          print(f"Cloned {channel.name}")
          await channel.delete() 
          print(f"Nuked {channel.name}")
          time.sleep(0.2)
      # Editing the position here
          await newchannel.edit(position=channel.position)
          print(f"Repositioned {newchannel.name}")
          await newchannel.send("**[Soon]** :copyright:")
          print(f"Sent message to {newchannel.name}")
    
    bot.run("BOT TOKEN HERE")
    

    (I just added some stuff to make the bot run)