Discord.py-Rewrite Sending an Error message when there is an unknown command or other error

試著忘記壹切 提交于 2020-12-15 01:55:26

问题


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; 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.

Extra

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.


回答1:


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")


来源:https://stackoverflow.com/questions/62771154/discord-py-rewrite-sending-an-error-message-when-there-is-an-unknown-command-or

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!