Reputation:
I want to get an argument like in the image below:
My code:
class Admin(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
async def purge(self, ctx, amount: int):
await ctx.channel.purge(limit=amount+1)
@purge.error
async def purge_error(self, ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send('Please enter a number.')
elif isinstance(error, commands.BadArgument):
await ctx.send('This is not a number.')
How can I do it?
Upvotes: 0
Views: 2525
Reputation: 5157
commands.BadArgument
is not passed the inspect.Parameter
that other exceptions like commands.MissingRequiredArgument
have as a param
attribute.
If you're trying to get the parameter name for MissingRequiredArgument
, you can just use that attribute and Parameter.name
.
Otherwise, BadArgument
is usually passed a message. The most common case for this is a string that contains the Parameter.name
in the form 'Converting to "{}" failed for parameter "{}".'.format(name, param.name)
where name
is the converter name and param
is the Parameter
, and parsing that is likely how the bot in your image is getting it. In this case, you can get this message by casting the BadArgument
to a string or as the first and only argument in the tuple returned by the exception's args
attribute. See Python's documentation on handling exceptions for more detail.
Note though, that BadArgument
can be raised for reasons other than an integer conversion and with different or no messages, e.g. for boolean converters, Discord converters, custom converters, etc.
Upvotes: 2