【问题标题】:Discord.py check if argument is is a number between certain valuesDiscord.py 检查参数是否是某些值之间的数字
【发布时间】:2021-04-17 09:39:48
【问题描述】:

我们有一个非常简单的 discord 脚本,用户可以在其中输入 !dino 1234

脚本会返回一个http://www.url.com/images/1234.png

作为 python 和 discord.py 的新手,我不知道如何检查输入的参数是否在 1-100(图片数量)之间,如果是,则只返回 url。

所以如果 arg > 0 &

bot = commands.Bot(command_prefix='!')
@bot.command(name='dino')
async def dino(ctx,arg :int):
 
   await ctx.send('http://www.url.com/images/{}.png'.format(arg))  
   await ctx.send('Picture #{}'.format(arg))

谢谢!

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    你可以试试这个

    bot = commands.Bot(command_prefix='!')
    @bot.command(name='dino')
    async def dino(ctx, arg):
       try:
          if int(arg) > 0 and int(arg) < 101:
             await ctx.send('http://www.url.com/images/{}.png'.format(arg))  
             await ctx.send('Picture #{}'.format(arg))
       except:
          await ctx.send('Please enter a valid number between 1 and 100')
    

    【讨论】:

    • 这仍然允许机器人发布任何号码。不过谢谢
    • @LegionOfBrad 对不起,我误解了你的意思。我已经更新了答案,看看它是否有效。
    【解决方案2】:
    bot = commands.Bot(command_prefix='!')
    @bot.command(name='dino')
    async def dino(ctx, arg:int):
        if 0 <= arg <= 100: #This will check the number is between 0 and 100.
            await ctx.send('http://www.url.com/images/{}.png'.format(arg))  
            await ctx.send('Picture #{}'.format(arg))
    

    【讨论】:

      猜你喜欢
      • 2017-02-05
      • 2023-02-02
      • 2020-11-03
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 2015-12-07
      • 2012-11-27
      • 1970-01-01
      相关资源
      最近更新 更多