简体   繁体   中英

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. 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. 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.

Currently, the bot runs, and it doesn't throw any errors, but the function roletask() never seems to be called. ((Roletask is set to run every 5 seconds for testing purposes))

EDIT: I was able to get it fixed, by changing "datetime.hour" and "Datetime.minute" to "now.hour" and "now.minute". Furthermore, because of the way I imported things, "Datetime.datetime(...)" needed to be changed to just "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. You'll need to install apscheduler using this:

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'])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM