首页 > 解决方案 > 如何让我的 Discord 机器人在每周日 0:00 运行一个功能?

问题描述

我有一个使用 discord.py 的 Python discord 机器人我想每周日 0:00 做一些事情。我怎样才能做到这一点?我使用 Python 3.7 和 discord.py 1.3.2

标签: pythonpython-3.xdiscord.pydiscord.py-rewrite

解决方案


对于提供的间隔,这不是一种更有效的方法,而是一种方法

您可以使用 python 的schedule模块。

import schedule

def job():
    "Write your job here "


if __name__=="__main__":

    schedule.every().sunday.at("00:00").do(job)      
    while True:
        schedule.run_pending()

更新:上面的编程是阻塞的。

对于非阻塞,您可以按以下方式结合使用线程和调度。

import threading

import schedule

def job():
    """Your job here"""

def threaded(func):
    job_thread = threading.Thread(target=func)
    job_thread.start()

if __name__=="__main__":

    schedule.every().sunday.at("00:00").do(threaded,job)      
    while True:
        schedule.run_pending()
        """you can write your other tasks here"""

该程序为您的计划作业创建另一个线程。


推荐阅读