Python Discord机器人不接受错误

时间:2020-11-04 06:47:53

标签: python error-handling discord discord.py

我已经编写了python discord BOT,现在已经有一段时间了。即使我在代码中提到了错误,它也不会出现错误。这是存在==>

问题的代码段
@client.command()
async def spoiler(ctx, *,text):
author = ctx.author
author = author.mention
try:
    await ctx.channel.purge(limit=1)
    await ctx.send("Spoiler from " + author + "\n" "||" + text + "||")
except discord.errors.Forbidden:
    await ctx.send("Grant me the permission to purge channels and try again.")
except asyncio.TimeoutError:
    await ctx.send(author + " Are you typing an essay or what?")
except discord.ext.commands.errors.MissingRequiredArgument:
    await ctx.send(author + " You need to type the text you want to disclose!")

基本上,这是一个不和谐地使用扰流板功能的命令。我希望BOT要求用户在命令“ $ spoiler”后键入文本,以防用户尚未输入文本。我尝试仅在服务器中键入命令,但出现此错误-“ discord.ext.commands.errors.MissingRequiredArgumen”。除了代码中的错误外,我没有做过,但是随后python只是忽略了它,因为甚至没有指定它。

这是我收到的错误==>

Ignoring exception in command spoiler:
Traceback (most recent call last):
File "C:\Softwares\Python\lib\site-packages\discord\ext\commands\bot.py", 
line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Softwares\Python\lib\site-packages\discord\ext\commands\core.py", 
line 851, in invoke
await self.prepare(ctx)
File "C:\Softwares\Python\lib\site-packages\discord\ext\commands\core.py", 
line 786, in prepare
await self._parse_arguments(ctx)
File "C:\Softwares\Python\lib\site-packages\discord\ext\commands\core.py", 
line 706, in _parse_arguments
kwargs[name] = await self.transform(ctx, param)
File "C:\Softwares\Python\lib\site-packages\discord\ext\commands\core.py", 
line 542, in transform
raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: text is a required 
argument that is missing.

有人知道这个解决方法吗?

1 个答案:

答案 0 :(得分:0)

这可能不是您处理错误的方式,但这是万一其他所有方法失败的方式之一。

@client.command()
async def spoiler(ctx, *,text):
    author = ctx.author
    author = author.mention
    try:
        await ctx.channel.purge(limit=1)
        await ctx.send("Spoiler from " + author + "\n" "||" + text + "||")
    except asyncio.TimeoutError:#using try and except for handling timeout error
        await ctx.send("Are you typing an essay or what?")      

@spoiler.error
async def spoiler_error(ctx, error):
    if isinstance(error, discord.errors.Forbidden):#permission error, may need to change
        await ctx.send("Grant me the permission to purge channels and try again.")
    if isinstance(error, discord.ext.commands.errors.MissingRequiredArgument):#if text is a missing required argument
        await ctx.send("You need to type the text you want to disclose!")