Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
85 views
in Technique[技术] by (71.8m points)

python - If Statement Is Not Being Reached

I'm making a simple discord.py command that is, for some reason, not working. I'm trying to make a slowmode command, and there seems to be a fault in it. This is the code in the command:

  @commands.command()
  async def slowmode(self, ctx, seconds=5):
    if seconds == 'off':
      seconds = 0
    elif seconds == 'on':
      seconds = 5

    seconds = int(seconds)
    
    await ctx.channel.edit(slowmode_delay=seconds)

    embed = discord.Embed(
      title='Slowmode Changed',
      description=f'The slowmode for {ctx.message.channel.mention} has been changed to: `{seconds}` seconds.',
      color=0x15E700
    )
    await ctx.send(embed=embed)

The issue is, whenever I enter $slowmode off or $slowmode on ($ is the prefix), I get the following error: discord.ext.commands.errors.BadArgument: Converting to "int" failed for parameter "seconds".

I am clearly stating that, if seconds is either on or off, it will be turned to 5 or 0 respectively.

In addition, whenever I choose to enter an improper argument, as in a bunch of random letters, and I have a try and except block, the code immediately skips the try and except block and returns the exact error above. It is almost as if the code isn't there.

question from:https://stackoverflow.com/questions/65853960/if-statement-is-not-being-reached

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...