Python随机选择导致discord.py

时间:2018-08-26 07:41:59

标签: python-3.x discord.py

如何使以下代码在不和谐频道中工作。使用命令时,输出应在不和谐通道中。

colours = {'red', 'blue', 'green', 'yellow', 'black', 'purple',
           'Brown', 'Orange', 'violet', 'gray'}
for n in [5]:
    cs = random.sample(colours, k=n)
    colours -= set(cs)
    print(cs)

2 个答案:

答案 0 :(得分:1)

这是工作代码

@bot.command(pass_context=True)  
async def pick(ctx):
  colours_copy = colours.copy()
  for n in [1, 2, 3]:
      cs = random.sample(colours_copy , k=n)
      colours_copy -= set(cs)
      await bot.send_message(ctx.message.channel, "{}\n".format(", ".join(cs)))

如果您每次有人跑!pick时都要从颜色中减去集合,那么您很快就会用完颜色

相反,您可以在函数内复制集合,以便即使在执行过程中从集合中减去,原始集合也始终会作为参考

“使用命令!choose时,输出应位于不协调通道中”
您可以更改命令名称(当前为pick

答案 1 :(得分:0)

@client.event
    async def on_message(message):
        if message.content.upper() == ".CHOOSE":

            # Your code here

            await client.send_message(message.channel, "> {}\n".format(", ".join(cs)))

机器人应将输出在控制台中的外观发送到命令所使用的相应通道。