我是否缺少基于random.sample的命令的某些内容

时间:2019-01-12 03:28:40

标签: python python-3.x discord.py

我正试图让机器人从1到x进行抽奖命令,然后选择y个中奖号码

@bot.command(name='raffle',
         description="Command usage: !raffle x y",
         brief="Picks a number between 0 and the input x y times",
         pass_context=True)        
async def raffle(ctx, x, y):
    message = await bot.say(str('Generating random numbers...'))
    await sleep(1)
    await bot.edit_message(message, new_content="Assessing the tallies...", embed=None)
    await sleep(1)
    await bot.edit_message(message, new_content="Numbercrunching...", embed=None)
    await sleep(1)
    await bot.edit_message(message, new_content="The number is.. *drum roll*", embed=None)
    await sleep(1)
    pool = list(range(1, (x)))
    z = random.sample(pool, k=(y))
    await bot.say(z)

1 个答案:

答案 0 :(得分:0)

discord将参数保存为字符串的方式

@bot.command(pass_context=True)
async def example(ctx,x):
  print(type(x))

将打印

<class 'str'>

并且由于所有range参数和k的{​​{1}}参数都采用整数,因此必须分别将random.samplex转换为int

话虽如此,改变

y

pool = list(range(1, (x)))
z = random.sample(pool, k=(y))
await bot.say(z)

以及每个pool = list(range(1, int(x))) z = random.sample(pool, k=int(y)) await bot.say(z) await sleep(1)

应该可以解决问题