【发布时间】:2022-01-25 17:19:15
【问题描述】:
我正在尝试制作一个 Discord 机器人,它会在给定时间在频道发送消息,我找到了以前的代码,但它们似乎对我不起作用,因为它们会给出缩进错误。
我不是经验丰富的程序员,任何答案都会很有帮助。
【问题讨论】:
标签: python discord.py
我正在尝试制作一个 Discord 机器人,它会在给定时间在频道发送消息,我找到了以前的代码,但它们似乎对我不起作用,因为它们会给出缩进错误。
我不是经验丰富的程序员,任何答案都会很有帮助。
【问题讨论】:
标签: python discord.py
我建议您从apscheduler 模块中查找AsyncIOScheduler。如果您使用的是基于类的机器人,下面的代码应该可以工作:
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from discord.ext.commands import Bot as BotBase
class Bot(BotBase):
def __init__(self):
super().__init__(command_prefix=your_command)
self.scheduler = AsyncIOScheduler()
async def on_ready(self):
self.scheduler.add_job(self.birthday, CronTrigger(hour=0))
self.scheduler.start()
async def action(self):
channel = self.get_channel(channel_id)
await channel.send('Your message')
每当CronTrigger的小时计数器达到0时,调度程序就会执行action方法。
【讨论】: