Ryzer
Ryzer

Reputation: 46

Exception Fixing ban command discord.py

I recently made a ban command for my bot but its not working, can anyone help? whenever i do !ban it just gives the exception and member.kick is blacked out, also i user kick insted of ban because of debugging purposes Thanks!

Code:

    @commands.command()
    async def ban(self, ctx, member:nextcord.Member,*, reason="No Reason Provided"):
        try:
            if reason == None:
                embed = nextcord.Embed(title="Member Banned!", description=f"{member.mention} has been banned from '{ctx.guild}'\n Reason: `No Reason Provided`", colour=BLUE_COLOUR)
                embed.timestamp = datetime.datetime.utcnow()
                embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/942086556980764722/967720168199426069/Ban_Command.gif")
                await ctx.reply(embed=embed)
                await member.kick(reason=reason)
            else:
                author = ctx.message.author.name
                SendConfirmation = nextcord.Embed(title="Member Banned! ", description=f"Banned: {member.mention}\n Moderator: **{author}** \n Reason: **{reason}**", color=GOLD_COLOUR)
                SendDM = nextcord.Embed(title="You Have been Banned", colour=BLUE_COLOUR)
                embed.add_field(name="Server Name", value=f"{ctx.guild.name}", inline=True)
                embed.add_field(name="Reason", value=f"{reason}", inline=True)
                embed.timestamp = datetime.datetime.utcnow()
                await member.send(embed=SendDM)
                await ctx.reply(embed=SendConfirmation)
        except Exception:
            ErrorEmbed = nextcord.Embed(title="Something Went Wrong", description="There was an error trying to perform this command.", colour=ERROR_COLOUR)
            ErrorEmbed.timestamp = datetime.datetime.utcnow()
            await ctx.reply(embed=ErrorEmbed)
            return

Upvotes: 1

Views: 172

Answers (1)

Flavio Lugli
Flavio Lugli

Reputation: 36

There were 2 issues with the code:

  1. Your reason argument was never none because it was set by default on "No Reason Provided" so you would never use the if statement

  2. you created the variable SendDM and then used the embed function on a new variable that was never defined (called embed)

    @commands.command()
    async def ban(self,ctx, member:nextcord.Member,*, reason=None):
    
        if reason == None:
            embed = nextcord.Embed(title="Member Banned!", description=f"{member.mention} has been banned from '{ctx.guild}'\n Reason: `No Reason Provided`")
            embed.timestamp = datetime.datetime.utcnow()
            embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/942086556980764722/967720168199426069/Ban_Command.gif")
            await ctx.reply(embed=embed)
            await member.ban(reason=reason)
        else:
            author = ctx.message.author.name
            SendConfirmation = nextcord.Embed(title="Member Banned! ", description=f"Banned: {member.mention}\n Moderator: **{author}** \n Reason: **{reason}**")
            SendDM = nextcord.Embed(title="You Have been Banned")
            SendDM.add_field(name="Server Name", value=f"{ctx.guild.name}", inline=True)
            SendDM.add_field(name="Reason", value=f"{reason}", inline=True)
            SendDM.timestamp = datetime.datetime.utcnow()
            await member.send(embed=SendDM)
            await ctx.reply(embed=SendConfirmation)
            await member.ban(reason=reason)
    

This code should work, I tested it on my machine, I took away embed colors and the try and except block but you can add them back.

One last thing, I suggest to remove the try and except block and handle errors with actual error handlers. You can find how to use them in the documentation of the nextcord library.

Upvotes: 1

Related Questions