discord.py如何使机器人不踢消息作者

时间:2020-08-07 11:17:14

标签: python discord discord.py

我有脚踢命令

    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, member : discord.Member, *, reason=None):
        await member.kick(reason=reason)
        
        await ctx.trigger_typing()
            
        embed = discord.Embed(title=f"{gem} **User kicked!**", color=colgreen)
        embed.add_field(name="**{} has been succesfully kicked from the server!**".format(member), value="**Reason: {}.**".format(reason), inline=False)
        embed.set_footer(text="{0}\nID: {1}\n{2}".format(ctx.message.author.name, ctx.message.author.id, datetime.datetime.now().strftime("%A, %B %d %Y at %H:%M:%S %p")), icon_url=ctx.message.author.avatar_url)
        await ctx.send(embed=embed)

但是,如果拥有踢踢成员权限的用户尝试踢自己,则机器人会踢踢他们。

我该如何使漫游器忽略用户并发送错误消息,导致您无法自拔?

ive尝试了if和else陈述,但是它不起作用... 我会得到一个错误,或者什么都没有...

1 个答案:

答案 0 :(得分:0)

您可以添加一条if语句来检查用户是否等于作者,例如:

    @commands.command()
    @commands.has_permissions(kick_members=True)
    async def kick(self, ctx, member : discord.Member, *, reason=None):
        if member == ctx.author: # Check if the user is the same as the author
            return

        await member.kick(reason=reason)
        
        await ctx.trigger_typing()
            
        embed = discord.Embed(title=f"{gem} **User kicked!**", color=colgreen)
        embed.add_field(name="**{} has been succesfully kicked from the server!**".format(member), value="**Reason: {}.**".format(reason), inline=False)
        embed.set_footer(text="{0}\nID: {1}\n{2}".format(ctx.message.author.name, ctx.message.author.id, datetime.datetime.now().strftime("%A, %B %d %Y at %H:%M:%S %p")), icon_url=ctx.message.author.avatar_url)
        await ctx.send(embed=embed)