繁体   English   中英

踢命令中的Discord Bot检查权限

[英]Discord Bot Check Permissions In Kick Command

这是我的踢命令。 但是我一直在尝试执行一个命令,该命令检查要被禁止的成员是否是管理员(管理员权限)。 我没有,下面的代码没有任何错误。 但是我找不到能做到这一点的代码。如果我犯了任何错误,请告诉我,以便我可以通过代码和发布问题来更正它。

@bot.command(pass_context=True)
async def kick(context, member: discord.Member):

    if context.message.author.server_permissions.kick_members:

        await bot.say('You have kick Perms so now starting the command')
        if member== context.message.server.owner:
            await bot.say('U cant ban a moderator')
        else:
            await bot.say('Almost done!')
            time.sleep(1)
            try:
                await bot.kick(member)
                await bot.send_message(bot.get_channel('553090886683197451'),'has been kicked')
            except Exception:
                    await bot.say('the member is a mod or the bot has crashed')
    else:
        await bot.say("U don't perms :sweat_smile:")

您可以使用sevrer_permissions属性,以与检查当前代码中的作者权限相同的方式检查目标权限:

from discord import Member
from discord.ext.commands import Bot, has_permissions, CheckFailure, BadArgument

bot = Bot("!")

@bot.command(pass_context=True, name="kick")
@has_permissions(kick_members=True)
async def kick_command(ctx, *, target: Member):
    if target.server_permissions.administrator:
        await bot.say("Target is an admin")
    else:
        try:
            await bot.kick(target)
            await bot.say("Kicked")
        except Exception:
            await bot.say("Something went wrong")

@kick_command.error
async def kick_error(error, ctx):
    if isinstance(error, CheckFailure):
         await bot.send_message(ctx.message.channel, "You do not have permissions")
    elif isinstance(error, BadArgument):
        await bot.send_message(ctx.message.channel, "Could not identify target")
    else:
        raise error

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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