简体   繁体   English

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

[英]Background Task in random channels Discord.py

I'm trying to get my background task to send in different channels using random.choice() . 我正在尝试让我的后台任务使用random.choice()在不同的通道中发送。 When I turn the bot on, it will send in only one random channel and that channel only. 当我打开漫游器时,它将仅在一个随机通道中发送,并且仅在该通道中发送。 Is there a way to send in a different channel each time it loops? 有没有一种方法可以在每次循环时发送不同的通道?

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

Currently, channel = client.get_channel(random.choice(channels)) is outside of your while loop, meaning that variable channel never changes. 当前, channel = client.get_channel(random.choice(channels))在您的while循环之外,这意味着可变channel永远不会改变。 Move it to inside your while loop to change it every time a new message is going to be sent. 每次发送新消息时,将其移至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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM