python - 如何让我的 Discord 机器人在每周日 0:00 运行一个功能?
问题描述
我有一个使用 discord.py 的 Python discord 机器人我想每周日 0:00 做一些事情。我怎样才能做到这一点?我使用 Python 3.7 和 discord.py 1.3.2
解决方案
对于提供的间隔,这不是一种更有效的方法,而是一种方法
您可以使用 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"""
该程序为您的计划作业创建另一个线程。
推荐阅读
- windows-runtime - 低功耗蓝牙:使用 WinRT API 的问题
- python - 如何控制seaborn条形图中条形之间的空白?
- r-markdown - R降价摘要输出
- reactjs - 流程无法决定选择哪种情况并显示不相关的代码
- oracle - 需要通过触发器在表中插入机器名和 DML 命令
- c++ - SDL2_image 渲染 - 只有在打印源 SDL_Rect 的属性时才能正确渲染图像
- visual-studio-2017 - 解决方案资源管理器不同的标记
- c++ - 将 uint64_t 转换为 struct 时出现编译器错误
- c# - 如何将 C# DLL 导入 MQL5 脚本
- python - 为什么 pandas.xs 不能从一个级别中选择多个键?