【问题标题】:Discord Bot Kick Command Doesn't Work As ExpectedDiscord Bot Kick 命令无法按预期工作
【发布时间】:2019-07-31 03:49:20
【问题描述】:

我的 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.x discord.py


    【解决方案1】:

    如果不是这样,我真的不明白您为什么要尝试将 exception 除外。

    我删除了 async 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 语句来检查 @987654328 是否@函数仍然是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
    

    【讨论】:

    • 先生,非常感谢它帮助了我很多。
    猜你喜欢
    • 2018-03-04
    • 2021-03-01
    • 2021-01-05
    • 2020-08-07
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 2014-11-28
    • 2019-08-22
    相关资源
    最近更新 更多