【问题标题】:discord.py rewrite | Making errors for my commandsdiscord.py 重写 |为我的命令出错
【发布时间】:2019-07-10 16:31:55
【问题描述】:

现在我完成了我的审核命令 [大部分],我正在尝试添加错误。我已经犯了“请指定一个成员”错误,但我无法让机器人说“这个成员不存在”输入无效名称时。

@client.command(name='kick',
            brief='Kicks user',
            aliases=['Kick'],
            pass_context=True)
async def kick(context, member:discord.Member=None):
# Errors
if not member:
    await context.send('Please specify a member.')
    return
# Actual Kicking
if context.author.guild_permissions.kick_members == True:
    await member.kick()
    await context.send(f"{member.mention} was kicked ")
else:
    await context.send(context.message.author.mention + ", you don't have permission")

这是我的命令之一,一切正常。如果该成员显然不存在,我想要一个显示“找不到用户”的错误。例如,k!kick ijhguiserb 会让机器人说“未找到成员”,而不是在 shell 中给我一个错误。

我们将不胜感激,谢谢!

【问题讨论】:

    标签: python python-3.x discord.py discord.py-rewrite


    【解决方案1】:

    您必须定义一个error handler 来处理ConversionError

    from discord.ext.commands import ConversionError
    
    @kick.error
    async def kick_error(ctx, error):
        if isinstance(error, (ConversionError, BadArgument)):
            await ctx.send("Member not found")
        else:
            raise error
    

    【讨论】:

    • 我把它放到了我的代码中,但是没有任何改变。你知道为什么吗?
    • 更新,经过一番思考,我想出了:elif isinstance(error, BadArgument): await ctx.send('Member not found'),这让机器人告诉我找不到该成员。感谢您的链接,它将帮助我修复更多这些东西! :)
    猜你喜欢
    • 2020-10-19
    • 2021-07-15
    • 2020-05-03
    • 2020-11-06
    • 2021-03-30
    • 2020-09-11
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    相关资源
    最近更新 更多