【问题标题】:Cannot send message to channel using discord.py无法使用 discord.py 向频道发送消息
【发布时间】:2019-08-07 08:26:38
【问题描述】:

我有下面的代码,正在尝试每 60 秒向不和谐频道发送消息。登录成功,但频道上没有任何反应。

async def my_background_task():
    await client.wait_until_ready()
    counter = 0
    channel = discord.Object(id='608412706310979613')
    while not client.is_closed:
        counter += 1
        await channel.send(counter)
    await client.send_message(channel, counter)
    await asyncio.sleep(60) # task runs every 60 seconds

我希望在指定通道上每 60 秒接收一次递增计数

【问题讨论】:

    标签: python-3.x discord.py


    【解决方案1】:

    如果您的discord.py 版本超过1.1.0,您可以使用discord.ext.tasks

    你的工作可以用它来写,如下所示。

    from discord.ext import tasks, commands
    
    bot = commands.Bot()
    counter = 0
    
    @tasks.loop(seconds=60)
    async def my_background_task():
        global counter
        channel = bot.get_channel('608412706310979613')
        if channel is not None:
            counter += 1
            await channel.send(counter)
    
    bot.run(token)
    my_background_task.start()
    

    相关文档:https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 2021-05-03
      • 2022-01-07
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      • 2020-12-03
      相关资源
      最近更新 更多