简体   繁体   English

如何使用 python 让 Discord 机器人每天在特定时间运行 function?

[英]How to make Discord bot run a function every day at a specific time using python?

I've been trying to get my Discord bot to run a function at a specific time every day.我一直试图让我的 Discord 机器人在每天的特定时间运行 function。 Currently, the bot can do something every 24 hours, so all I need to do to is get it to start at a specific time.目前,机器人可以每 24 小时执行一次操作,所以我需要做的就是让它在特定时间启动。 However, I have not been able to figure out why I can't get it to work.但是,我无法弄清楚为什么我不能让它工作。 I have tried multiple solutions, using schedule, aioscheduler, etc. I've tried the solutions from the other times this question was asked but I could not get any of them to work.我尝试了多种解决方案,使用 schedule、aioscheduler 等。我已经尝试过其他时候提出这个问题的解决方案,但我无法让它们中的任何一个工作。

Currently, the bot runs, and it doesn't throw any errors, but the function roletask() never seems to be called.目前,机器人运行,它不会抛出任何错误,但 function roletask() 似乎从未被调用。 ((Roletask is set to run every 5 seconds for testing purposes)) ((出于测试目的,Roletask 设置为每 5 秒运行一次))

EDIT: I was able to get it fixed, by changing "datetime.hour" and "Datetime.minute" to "now.hour" and "now.minute".编辑:通过将“datetime.hour”和“Datetime.minute”更改为“now.hour”和“now.minute”,我能够修复它。 Furthermore, because of the way I imported things, "Datetime.datetime(...)" needed to be changed to just "datetime(...)"此外,由于我导入东西的方式,“Datetime.datetime(...)”需要更改为“datetime(...)”

Hopefully that helps anyone else running into this problem into the future!希望这可以帮助其他任何人在未来遇到这个问题!

import discord
import random
import asyncio
import schedule
import threading
import time
from datetime import datetime, timedelta
from discord.ext import commands, tasks
from discord.utils import get

bot = commands.Bot(command_prefix='[]')
bot.remove_command("help")
guild = bot.get_guild(607452358544195588)
role_id = 738129548805275710
ROLE_NAME1 = "q-and-a"
ROLE_NAME2 = "tunes"


@tasks.loop(seconds=5)
async def roletask():
    print("ur bad")
    channel = bot.get_channel(681179611253571688)
    await channel.send('<@&738129548805275710> You are part of the test role!')


@roletask.before_loop
async def before_my_task():
    hour = 23
    minute = 23
    await bot.wait_until_ready()
    now = datetime.now()
    future = datetime.datetime(now.year, now.month, now.day, hour, minute)
    if datetime.hour >= hour and datetime.minute > minute:
        future += timedelta(days=1)
    await asyncio.sleep((future-now).seconds)

roletask.start()

@bot.event
async def on_ready():
    await bot.change_presence(status=discord.Status.online, activity=discord.Game('[]help'))
    print('We have logged in as {0.user}'.format(bot))

I am not familiar with discordbots but you can try apscheduler it is used to schedule jobs for specific time.我不熟悉 discordbots,但您可以尝试使用 apscheduler 来安排特定时间的作业。 You'll need to install apscheduler using this:您需要使用以下命令安装 apscheduler:

pip install APScheduler

Here is an example code:这是一个示例代码:

from datetime import datetime
from apscheduler.scheduler import Scheduler

# Create the scheduler and start it
sched = Scheduler()
sched.start()

# Define the function that is to be executed
def job(text):
    print(text)

# The job will be executed on August 5th, 2020 at 16:30:05
exec_date =  datetime(2020, 5, 5, 16, 30, 5)

# Store the job in a variable in case we want to cancel it
job = sched.add_date_job(job, exec_date, ['my_text'])

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

相关问题 Discord.py Bot 每天特定时间运行功能 - Discord.py Bot run function at specific time every day 用 Python 编写 Discord 机器人 - 如何让它每天在特定时间发送消息? - Programming a Discord bot in Python- How do I make it send a message every day at a certain time? 如何每天在特定时间运行python函数 - How to run a python function every day at specific time 如何在python每天特定时间运行一个function? - How to run a function every day at a specific time in python? 如何让机器人每天在特定时间在 discord.py 中运行定义的函数? - How to make the bot run a defined function at a specific time everyday in discord.py? Python:如何自动化脚本在特定时间每天运行? - Python: How to automate script to run every day at specific time? 如何让我的 Discord 机器人在每个星期日的 0:00 运行一个函数? - How do I make my Discord bot run a function every sunday at 0:00? 如何让我的 python 电报机器人每天在特定时间发送消息? - how to make my python telegram bot to send message at certain time every day? 如何让 python discord 机器人每分钟使用一个命令 - How to make python discord bot use a command every minute 在 Heroku 上部署时,每天在特定时间运行函数的 Python 脚本不起作用 - Python script to run a function at a specific time every day does not work when deployed on Heroku
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM