【问题标题】:Discord Bot, Sending A Message Every 24 HoursDiscord Bot,每 24 小时发送一次消息
【发布时间】:2021-07-26 21:01:13
【问题描述】:

我想问任何了解 Python 的人。我计划让我的不和谐机器人在特定时间发送特定消息。我打算让它在某个场合提醒我和其他人。就我而言,我希望机器人每 24 小时发送一次该消息。我的代码在循环消息时有效,但它仅在我使用分钟或秒时有效。如果我尝试输入天数或小时数,它将不起作用。我还尝试输入 24 小时的分钟/秒数,但效果不佳。在此文本下方将是我的代码。这里有谁知道如何解决这个问题,或者至少找到一个替代解决方案?我很不确定如何处理任务和循环。在此先向您表示感谢。

@tasks.loop(hours=24)
async def e():
    await client.get_channel(channel id here).send("@everyone It's A New Day!")


@e.before_loop
async def before_e():
    await client.wait_until_ready()

e.start()

【问题讨论】:

  • 您最好在 AWS EC2 实例上将其作为 CRON 作业运行。
  • 你开始循环了吗?
  • @yudhiesh 我明白了。谢谢你的回答。我很难弄清楚该怎么做。
  • @12944qwerty 是的,我做到了。我做了一些测试运行,它可以完美运行几秒钟和几分钟。不幸的是,当我使用小时数时,它不起作用。
  • 不起作用是什么意思?只要你启动循环它就应该工作

标签: python discord discord.py


【解决方案1】:

您可以使用Advanced Python Scheduler 以更好的方式进行操作。

from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger

async def sendNewDayMessage():
    await client.get_channel(channel_id).send("@everyone It's A New Day!")

sched = AsyncIOScheduler()
sched.start()
sched.add_job(sendNewDayMessage, CronTrigger(hour=0, minute=0, second=0)) #on 00:00

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 2020-12-01
    • 2021-03-02
    • 2021-01-12
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多