Jim Halpert
Jim Halpert

Reputation: 37

Python discord bot does not accept errors

I've coded a python discord BOT and I've had this problem for quite some time now. It doesn't except the errors even though I have mentioned it in the code. This is the snippet that has the issue ==>

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

It's basically a command that uses the spoiler feature in discord. I want the BOT to ask the user to type a text after the command "$spoiler" in case they haven't done it already. I tried typing just the command in my server and I got this error - "discord.ext.commands.errors.MissingRequiredArgumen". I did except that error in the code, but then python just ignores it like it's not even specified.

This is the error that I recieve ==>

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.

Does anyone know a fix for this?

Upvotes: 0

Views: 244

Answers (1)

Bagle
Bagle

Reputation: 2346

This may not be the way you were handling the error, but it is one of the ways in case all else fails.

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

Upvotes: 1

Related Questions