【问题标题】:Using discord.py to send a message everyday at a specified time使用 discord.py 每天在指定时间发送消息
【发布时间】:2022-01-25 17:19:15
【问题描述】:

我正在尝试制作一个 Discord 机器人,它会在给定时间在频道发送消息,我找到了以前的代码,但它们似乎对我不起作用,因为它们会给出缩进错误。

我不是经验丰富的程序员,任何答案都会很有帮助。

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    我建议您从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方法。

    【讨论】:

    • 谢谢,但我对类还不熟悉。虽然我现在确实设法让循环工作,但这次我想在不同的线程中运行它。
    猜你喜欢
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    • 2019-06-02
    • 2021-04-27
    相关资源
    最近更新 更多