随机通道中的后台任务Discord.py

时间:2019-04-03 03:46:16

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

我正在尝试让后台任务使用random.choice()通过不同的渠道发送。当我打开漫游器时,它将仅在一个随机通道中发送,并且仅在该通道中发送。是否有一种方法可以在每次循环时发送不同的频道?

async def test_loop():
    await client.wait_until_ready()
    channels = ['550528972226', '5149003563352', '514900351233', '5799132312340']
    channel = client.get_channel(random.choice(channels))
    while not client.is_closed:
        time = random.randint(1,5)+random.random()
        monies = random.randint(100,250)
        emojigrab = ''
        emojimsg = await client.send_message(channel, emojigrab)
        await client.add_reaction(emojimsg, "")
        pay = await client.wait_for_reaction(emoji="", message=emojimsg, timeout=1800,
                                             check=lambda reaction, user: user != client.user)
        if pay:
            await client.delete_message(emojimsg)
            await client.send_message(channel, "{} secures the bag for ${:,}".format(pay.user.mention, monies))
            add_dollars(pay.user, monies)
            await asyncio.sleep(int(time))

1 个答案:

答案 0 :(得分:1)

当前,channel = client.get_channel(random.choice(channels))在while循环之外,这意味着变量channel永远不会改变。每次发送新消息时,将其移至while循环内即可进行更改。

async def test_loop():
    await client.wait_until_ready()
    channels = ['550528972226', '5149003563352', '514900351233', '5799132312340']
    while not client.is_closed:
        channel = client.get_channel(random.choice(channels))
        time = random.randint(1,5)+random.random()
        monies = random.randint(100,250)
        emojigrab = ''
        emojimsg = await client.send_message(channel, emojigrab)
        await client.add_reaction(emojimsg, "")
        pay = await client.wait_for_reaction(emoji="", message=emojimsg, timeout=1800,
                                             check=lambda reaction, user: user != client.user)
        if pay:
            await client.delete_message(emojimsg)
            await client.send_message(channel, "{} secures the bag for ${:,}".format(pay.user.mention, monies))
            add_dollars(pay.user, monies)
            await asyncio.sleep(int(time))