報錯
也就是所謂的error,我們有多種方式做處理
第一種
使用廣泛類型
1 2 3 4 5 6 7 8 9 10 11
|
@commands.Cog.listener() async def on_command_error(ctx, error): if isinstance(error, commands.CommandNotFound): await ctx.send(commands.CommandNotFound) if isinstance(error, commands.UserInputError): await ctx.send(commands.UserInputError) if isinstance(error, commands.MissingRequiredArgument): await ctx.send(commands.MissingRequiredArgument)
|
可以寫在前排,看起來方便,添加起來也方便
第二種
使用單個command,比如…….
1 2 3 4 5 6
| @commands.command() async def slots(self, ctx, amount, mang): await open_account(ctx.author)
|
我們將這些拿掉後,在這底下增加
1 2 3 4
| @slots.error async def slots_error(ctx, error): if isinstance(error, commands.MissingRequiredArgument): await ctx.send(commands.MissingRequiredArgument)
|
這樣一來,就會有東西出來了,來看看示意圖
第三種
使用 try……except……
1 2 3 4 5 6
| @commands.command() async def slots(self, ctx, amount, mang): try: await ctx.send("pass") except Exception as e : await ctx.send(e)
|
典型的偵錯
預防性措施
像我們先前有使用
1
| async def withdraw(ctx, amount = None):
|
這代表amount在默認的情況下是None,所以不會遇到錯誤,畢竟自己都在下方寫了return
總結
看個人的需求做error的處置
也可以將值預設,出問題就return回去