首页 > 解决方案 > Discord Bot Kick 命令无法按预期工作

问题描述

我的 kick 命令需要帮助。我没有收到任何错误,但结果与我预期的不同,我的代码如下。

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(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{}'.format(member.mention))
    except Exception:
        await bot.say("Something went wrong")

@kick.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 

但是命令确实踢了该成员,但没有说踢了(会员名)。相反,它说“出了点问题”。还有两个命令适用于我的 kick 命令。

标签: python-3.xdiscord.py

解决方案


我真的不明白你为什么要尝试,除非exception不是这样。

我删除了*inasync def kick()函数,因为您只需要成员而不是多个参数,所以将它放在那里有点毫无意义,而且我还删除了tryexcept认为在这种情况下没用的东西。

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(ctx, target: discord.Member=None):

    if target.server_permissions.administrator:

        await bot.say("Target is an admin")
    else:
        await bot.kick(target)
        await bot.say('Kicked{}'.format(target.mention))

@kick.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

例如,如果您想检查主持人或使用该命令的人是否也将用户置于应该被踢的命令中,您可以使用一条if语句来检查该discord.Member功能是否仍然存在none,如果是,它将输出聊天中的消息。在这种情况下,我将消息“您忘记了用户”

@bot.command(pass_context=True, name="kick")

@has_permissions(kick_members=True)

async def kick(ctx, target: discord.Member=None):

    if target.server_permissions.administrator:

        await bot.say("Target is an admin")
    elif target = None:
        await bot.say("You forgot the user")
    else:
        await bot.kick(target)
        await bot.say('Kicked{}'.format(target.mention))

@kick.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

推荐阅读