例外固定ban命令discord.py

发布于 2025-01-23 17:39:47 字数 1750 浏览 0 评论 0原文

我最近为机器人制作了禁令命令,但它不起作用,有人可以帮忙吗?每当我这样做!禁令时,它只会给出例外和成员。kick被涂黑了,我也因为调试目的而被禁止使用。 谢谢!

代码:

    @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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2025-01-30 17:39:47

代码有两个问题:

  1. 您的原因参数从来都不是没有,因为默认情况下是在上设置的“未提供的原因” ,因此您永远不会使用If If If If语句

  2. 您创建了变量 senddm ,然后在从未定义的新变量上使用嵌入函数(称为 em> embed

     @commands.command()
    异步def ban(self,ctx,成员:nextcord.member,*,原因= none):
    
        如果原因==无:
            embed = nextCord.embed(title =“ n ement unded!”,description = f“ {member.mention}已从'{ctx.guild}'\ n原因中禁止:'没有提供``提供``
            embed.timestamp = dateTime.dateTime.utcnow()
            embed.set_thumbnail(url =“ https://cdn.discordapp.com/attachments/942086556980764722/96772016819426069/ban_command.gif”)
            等待ctx.reply(嵌入=嵌入)
            等待成员。ban(原因=原因)
        别的:
            作者= ctx.message.author.name
            sendconfirnation = nextCord.embed(title =“ banned!”,description = f“禁止:{member.mention} \ n调节器:** {auter} ** \ n原因:** {quasence} ** **)
            senddm = nextcord.embed(title =“您已被禁止”)
            senddm.add_field(name =“服务器名称”,value = f“ {ctx.guild.name}”,inline = true)
            senddm.add_field(name =“ quasen”,value = f“ {quach}”,inline = true)
            senddm.timestamp = dateTime.dateTime.utcnow()
            等待成员。
            等待ctx.reply(embed = send confinfircation)
            等待成员。ban(原因=原因)
     

该代码应起作用,我在计算机上对其进行了测试,我取出嵌入颜色,尝试和块除外,但您可以将它们添加回。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文