Discord.py-Rewrite当存在未知命令或其他错误时发送错误消息 [英] Discord.py-Rewrite Sending an Error message when there is an unknown command or other error
问题描述
我希望能够发送类似 await ctx.send(错误:未知命令.请-help接受可接受的命令."
之类的消息,或类似的内容(仅供参考,我不问如何删除 Help已经是一个已定义的函数
或类似的东西.);但是我不知道如何为不是命令的东西发出错误消息,例如命令出现错误,例如他们未放置任何参数,然后我为该 @ command.error
犯了一个错误,然后我对其进行了定义.但是我不确定如何如果可能,是否有办法检查代码底部的错误是什么?如是否需要管理员,但他们丢失了该错误,然后 if isinstance(error,commands.MissingPermissions):
还是如果他们需要允许服务器成员向他们发送消息,那么也许还有其他事情需要检查吗?
I want to be able to send a message like await ctx.send("Error: Unknown command. Do -help for acceptable commands."
or something like that (FYI, I'm not asking how to remove the Help is already a defined function
or something like that.); But I don't know how to make an error message for something that isn't a command, Like if there is an error with a command like they didn't put any parameters then I'm able to make an error for that @command.error
then i define it. But I'm not sure how to start this. And if possible, would there be a way where at the bottom of the code, is checked what the error is? like if they need admin but they are missing it then if isinstance(error, commands.MissingPermissions):
or if they need to allow server members to message them, maybe it would have a different thing to check?
TLDR;我希望能够在我的代码的底部有一个东西来检查是否存在错误,如果存在,是否是由命令所引起的错误(然后它用 ctx.send
声明某些内容)),但如果是由于缺少管理员或角色而引起的,或者可能是由于他们需要允许服务器成员在隐私设置中为dm造成的,则它还会说些什么.我不想为每个命令都定义一个错误.
TLDR; I want to be able to have at the bottom of my code a thing that checks if there was an error and if so, if its caused by the command not existing (then it says something with ctx.send
) but if its caused by missing admin or missing role, or possibly them needing to allow server members to dm them in privacy settings, then it also says something. I don't want to have to define an error for every command.
我还有一条命令要求数字起作用,并且我想要一段代码来检查其是否为整数,如果不是,则给出错误信息,提示它必须为数字.然后检查其是否为正数,如果不是,则给出错误.
I also have a command that requires numbers to work, and i want a block of code that checks if its an integer, and if not, gives an error saying that it needs to be a number. Then checks if its a positive number, and if not, gives an error.
推荐答案
Discord.py has a on_command_error
event, which takes the error as argument. You can use it this way:
@bot.event
async def on_command_error(ctx, error):
await ctx.send(f"An error occured: {str(error)}")
以下是每个不和谐的证据.
如果您希望为每个错误提供自定义消息,则可以通过以下方式做到这一点:
Here's a list of every discord exeptions.
If you want a custom message for each error, you can do it this way:
@bot.event
async def on_message_error(ctx, error):
if isinstance(error, discord.ext.commands.errors.CommandNotFound):
await ctx.send("Unknown command")
这篇关于Discord.py-Rewrite当存在未知命令或其他错误时发送错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!