Discord.py检查参数是否为特定值之间的数字

2024-06-01 02:28:31 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

由于不熟悉python和discord.py,我无法确定如何检查输入的参数是否在1-100(图片数量)之间,如果是,则只返回url

如果arg>;0&&书信电报;101

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))

谢谢


Tags: 脚本comsendhttpurlpngwwwbot
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))

你可以试试这个

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')

相关问题 更多 >