【问题标题】:Background Task in random channels Discord.py随机通道中的后台任务 Discord.py
【发布时间】:2019-08-24 11:40:28
【问题描述】:

我正在尝试让我的后台任务使用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 = dollar
        emojimsg = await client.send_message(channel, emojigrab)
        await client.add_reaction(emojimsg, hand)
        pay = await client.wait_for_reaction(emoji=hand, message=emojimsg, timeout=1800,
                                             check=lambda reaction, user: user != client.user)
        if pay:
            await client.delete_message(emojimsg)
            await client.send_message(channel, "{} collects {:,} dollars".format(pay.user.mention, monies))
            add_dollars(pay.user, monies)
            await asyncio.sleep(int(time))

【问题讨论】:

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


    【解决方案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))
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-03
      • 2018-10-28
      • 2019-02-19
      • 1970-01-01
      相关资源
      最近更新 更多