Search code examples
pythondiscorddiscord.pypycord

Unbanning a member from all guilds the bot is in


I am trying to unban a member from all guilds that the bot is in. My issue is when i run the command with the banned users ID in it i get the following error.

Traceback (most recent call last):
  File "C:\Users\Shadow\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\bot.py", line 1114, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "C:\Users\Shadow\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 375, in invoke
    await injected(ctx)
  File "C:\Users\Shadow\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\commands\core.py", line 132, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: NotFound: 404 Not Found (error code: 10007): Unknown Member

These are the steps I have tried.

            async for guild in self.bot.fetch_guilds():
                userr = await guild.fetch_member(id)
                if userr in guild.bans:
                    await guild.unban(userr)
                    await ctx.send("Unbanned the user.")

I am expecting the bot to unban the specified ID from all the servers the bot is in.

FULL CODE:

    @commands.slash_command(name="unban", description="When you have specified the user name and hashtag the bot will unban that user from all servers.")
    async def unban(self, ctx, id: discord.SlashCommandOptionType.string):
        async for guild in self.bot.fetch_guilds():
            try:
                user = await guild.fetch_member(ctx.author.id)
            except discord.NotFound:
                continue
        
        head_admin = discord.utils.get(guild.roles, name="Head Admin")
        developer = discord.utils.get(guild.roles, name="Developer")
        owner = discord.utils.get(guild.roles, name="Owner")
        if head_admin or developer or owner in user.roles:
            async for guild in self.bot.fetch_guilds():
                userr = await guild.fetch_member(id)
                if userr in guild.bans:
                    await guild.unban(userr)
                    await ctx.send("Unbanned the user.")

Solution

  • After doing some troubleshooting messing around with different user converters I have found the solution to my problem.

    I had changed the user_id input to a user input which then you would put the user ID in. It then loops through the guilds and unbans the user from all guilds. If the user is not detected in a guilds ban list it will continue going through them.

        @commands.slash_command(name="unban", description="When you have specified the user name and hashtag the bot will unban that user from all servers.")
        async def unban(self, ctx, userr: discord.User):
            async for guild in self.bot.fetch_guilds():
                try:
                    user = await guild.fetch_member(ctx.author.id)
                except discord.NotFound:
                    continue
            
                head_admin = discord.utils.get(guild.roles, name="Head Admin")
                developer = discord.utils.get(guild.roles, name="Developer")
                owner = discord.utils.get(guild.roles, name="Owner")
                if head_admin or developer or owner in user.roles:
                    try:
                        async for guild in self.bot.fetch_guilds():
                            await guild.unban(userr)
                    except:
                        continue
                    await ctx.send("Unbanned the user.")
                else:
                    await ctx.respond("You don't have permission to run this command.")