Discord py 权限系统(不能踢自己,不能踢模组)

时间:2021-07-09 18:15:04

标签: python discord bots

我正在写一个 kick 命令,结果成功了。今天我发现我的一半管理员为了好玩而禁止自己,所以对于这个问题,我想要一两行,如果版主尝试踢/禁止自己或有人说 manage_guild 权限,机器人会返回类似的东西“那个用户是工作人员,我不能踢他们!” 如果我有其他机器人的东西,上面写着“你想踢的成员是机器人!”,我会加分。也许通过“机器人”角色。

这是代码-

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
        await user.kick(reason=reason)
        kick = discord.Embed(title=f"User {user.name}#{user.discriminator} has been **kicked** from the server. <a:m_verifyblack:850825891780100096>", color=0xFF8300, description=f"Reason: {reason}\nBy: {ctx.author.mention}")
        await ctx.message.delete()
        await ctx.channel.send(embed=kick)
@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("**:no_entry_sign: You cant do that!**")
@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        kick = discord.Embed(title=f"Please pass a user.", description="`.kick {user}`\nExample:\n`.kick @user`,   `.kick {user.id}`")
        await ctx.channel.send(embed=kick)

2 个答案:

答案 0 :(得分:0)

您可以尝试检查上述人员的权限。例如,如果我不想让机器人踢一个拥有 ban_members 权限的人:

if user.guild_permissions.ban_members:
    await ctx.channel.send("Cannot kick this person.")

另外,为了防止踢自己,你可以试试:

if ctx.message.author == user:
    await ctx.channel.send("You can't kick yourself.")

答案 1 :(得分:0)

您可以简单地检查目标 user 是否具有 kick_members 权限并做出相应的反应。 您还可以检查 user 实际上是否是 bot

这是您的代码的修改版本,可以解决问题:

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, user: discord.Member, *, reason="No reason provided"):
        if not user.permissions_in(ctx.channel).kick_members or user.bot:
                return await ctx.send("You cannot kick staff members or bots")

        await user.kick(reason=reason)
        kick = discord.Embed(title=f"User {user.name}#{user.discriminator} has been **kicked** from the server. <a:m_verifyblack:850825891780100096>", color=0xFF8300, description=f"Reason: {reason}\nBy: {ctx.author.mention}")
        await ctx.message.delete()
        await ctx.channel.send(embed=kick)
@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("**:no_entry_sign: You cant do that!**")
@kick.error
async def kick_error(ctx, error):
    if isinstance(error, commands.MissingRequiredArgument):
        kick = discord.Embed(title=f"Please pass a user.", description="`.kick {user}`\nExample:\n`.kick @user`,   `.kick {user.id}`")
        await ctx.channel.send(embed=kick)