简体   繁体   English

Discord Bot,每 24 小时发送一次消息

[英]Discord Bot, Sending A Message Every 24 Hours

I would like to ask anyone who knows Python.想请教知道Python的人。 I plan to make my discord bot send a certain message at a certain time.我计划让我的 discord 机器人在特定时间发送特定消息。 I was planning on making it remind me and other people for a certain occasion.我打算让它在某个场合提醒我和其他人。 On my end, I would like the bot to send that message every 24 hours.就我而言,我希望机器人每 24 小时发送一次该消息。 My code works when looping the message however, it only works when I use minutes or seconds.我的代码在循环消息时有效,但它仅在我使用分钟或秒时有效。 If I try to input days or hours, it won't work.如果我尝试输入天数或小时数,它将不起作用。 I also tried to input the number of minutes/seconds for 24 hours and it wouldn't work as well.我还尝试输入 24 小时的分钟/秒数,但效果不佳。 Below this text would be my code.在此文本下方将是我的代码。 Does anyone here know how to solve this, or at least find an alternate solution?这里有谁知道如何解决这个问题,或者至少找到一个替代解决方案? I'm quite unsure of how to work with tasks and loops.我很不确定如何处理任务和循环。 I give you my thanks in advance.我提前向你表示感谢。

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

You can use Advanced Python Scheduler for doing it in a better way.您可以使用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

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

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