简体   繁体   中英

discord.py how to make the bot not to kick the message author

i have a kick command

    @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)

but if a user who has kick members permissions, and tries to kick himself, the bot will kick them.

how can i make the bot ignore the user and send a error that you cannot kick yourself??

ive tried if and else statetements, but its not working... i will either get a error, or nothing...

You could add a if statement to check if the user is equal to the author, like this for example:

    @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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM