异常修复禁令命令 discord.py

Exception Fixing ban command discord.py

我最近为我的机器人做了一个禁止命令,但它不起作用,有人可以帮忙吗?每当我执行 !ban 时,它只会给出异常并且 member.kick 被涂黑,而且由于调试目的,我的用户 kick insted of ban 谢谢!

代码:

    @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

代码有 2 个问题:

  1. 您的 原因 参数从未 none 因为它默认设置为 “未提供原因” 所以你永远不会使用 if 语句

  2. 您创建了变量 SendDM,然后在从未定义的新变量(称为 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)
    

此代码应该可以工作,我在我的机器上对其进行了测试,我删除了嵌入颜色和 try 和 except 块,但您可以将它们添加回去。

最后一件事,我建议删除 tryexcept 块并使用实际的错误处理程序处理错误。您可以在 nextcord 库的文档中找到如何使用它们。