【问题标题】:Discord.py: Have one check have two different error outputsDiscord.py:有一个检查有两个不同的错误输出
【发布时间】:2022-01-16 04:33:58
【问题描述】:

所以,我已经完成并为我的不和谐机器人添加了不同的功能。我正在努力使一个函数如果使用不正确可能会根据用户权限产生两种不同的错误输出。

例如: 当我以服务器管理员的身份使用命令“!clear”时,我将它放在我会得到错误的地方, “请指定要删除的消息数量。”因为该命令应该像 "!clear (amount)" 一样使用。

但是当不是管理员的人运行此命令时,我希望填充不同的错误。 例如:普通用户尝试使用“!clear”,我希望错误输出说 “错误:您没有此命令的权限。”

但我无法弄清楚如何将两个不同的错误输出应用于一个函数/命令。

每当我进入普通帐户并执行命令“!clear”时,它都会填充“请指定要删除的消息数量。”,而我只想为管理员输出该错误。

我只想要输出“错误:您没有此命令的权限。”显示普通用户何时尝试使用管理机器人命令。

@client.command()
@commands.has_permissions(administrator=True) # Only allows administrators to use this command
async def clear(ctx, amount : int):
    await ctx.channel.purge(limit=amount)
    await ctx.send(f'**Messages have been cleared! :thumbsup:**')

@clear.error
async def clear_error(ctx, error):
    if isinstance(error, commands.MissingPermissions): # Error populates when someone with no permissions tries to run the command.
     await ctx.send(f'**Error: You do not have permissions for this command.**')
     

@clear.error
async def clear_error(ctx, error): # Error populates when the user does not specify the amount of messages to delete
    await ctx.send(f'**Please specify an amount of messages to delete. :memo:**')```


    

【问题讨论】:

    标签: python discord discord.py embed


    【解决方案1】:

    您可以只检查在同一 clear_error 函数中出现的错误,例如:

    @client.command()
    @commands.has_permissions(administrator=True) # Only allows administrators to use this command
    async def clear(ctx, amount : int):
        await ctx.channel.purge(limit=amount)
        await ctx.send(f'**Messages have been cleared! :thumbsup:**')
        
    @clear.error
    async def clear_error(ctx, error):
        if isinstance(error, commands.MissingPermissions): # Error populates when someone with no permissions tries to run the command.
            await ctx.send(f'**Error: You do not have permissions for this command.**')
    
        elif isinstance(error, commands.MissingRequiredArgument): # Error populates when the user does not specify the amount of messages to delete
            await ctx.send(f'**Please specify an amount of messages to delete. :memo:**')
    

    您甚至可以添加更多类型的错误,更好的方法是添加更一般的错误消息。

    为了避免将 @clear.error 装饰器放在每个函数之前,您可以覆盖 discord.py 中的默认错误处理程序:

    @commands.Cog.listener()
    async def on_command_error(self, ctx, error):
        if isinstance(error, commands.MissingPermissions): # Error populates when someone with no permissions tries to run the command.
            await ctx.send(f'**Error: You do not have permissions for this command.**')
    
        elif isinstance(error, commands.MissingRequiredArgument): # Error populates when the user does not specify the amount of messages to delete
            await ctx.send(f'**Please specify an amount of messages to delete. :memo:**')
    

    文档:

    【讨论】:

    • 请记住,每次任何命令引发 MissingRequiredArgument 错误时都会触发第二个选项,因此需要相应地编辑消息的措辞。
    • 这就像一个魅力,我感谢伟大的信息!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2021-07-16
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多