The behavior you're noticing is due to the Commands extension's Converters. Specifically, the problem is with how you've declared your command's parameters:
@commands.command()
async def slowmode(self, ctx, seconds=5):
seconds
has an int
default argument, so the Converter is casting it to an int
. Since you also accept string values for this argument, it's going to raise BadArgument
when it attempts to convert 'on'
to an int
. There is a special converter for typing.Union
that you can use to annotate your seconds
variable to correctly accept str
and int
inputs:
from typing import Union
@commands.command()
async def slowmode(self, ctx, seconds: Union[int, str] = 5):
In addition, you should check the type of seconds
before attempting to check for string values:
@commands.command()
async def slowmode(self, ctx, seconds: Union[int, str] = 5):
if isinstance(seconds, str):
if seconds == 'on':
seconds = 5
elif seconds == 'off':
seconds = 0
# no longer needed
# seconds = int(seconds)
This way, you also no longer need to explicitly cast seconds
to int
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…