这是一个Flask服务器,还可以控制Discord机器人。 On_ready()是程序的入口点,然后将其带到my_background_task()。 此功能添加了每天在特定时间运行的cron作业。
我想运行check_Test(),它将基于cron作业是运行fn_1还是fn_2在不和谐通道中发送适当的消息。
我面临的问题是,计划程序中添加的作业完成后,我不知道如何调用check_Test()。
这是代码:
import discord
import asyncio
from discord.ext import commands, tasks
import os
import flask
from apscheduler.schedulers.background import BackgroundScheduler
import datetime
server = flask.Flask(__name__)
test = ""
client = discord.Client()
client = commands.Bot(command_prefix = ".")
channel_id = ****
channel = client.get_channel(channel_id)
async def my_background_task():
await client.wait_until_ready()
channel_id = ****
channel = client.get_channel(channel_id)
scheduler = BackgroundScheduler(daemon=True)
scheduler.add_job(fn_1, "cron", day_of_week="mon-sat", hour=00, minute=59, second=20, replace_existing=True, id="fn_1")
scheduler.start()
@client.event
async def on_ready():
channel_id = ****
channel = client.get_channel(channel_id)
print("bot ready")
await channel.send('This Bot is now Ready')
client.loop.create_task(my_background_task())
def fn_1():
global test
print("running some code for fn_1")
test = "success1"
def fn_2():
global test
print("running some code for fn_2")
test = "success2"
async def check_Test():
if(test == "success1"):
channel_id = ******
channel = client.get_channel(channel_id)
await channel.send('Test Has value of : Success 1')
elif(test == "success2"):
channel_id = ****
channel = client.get_channel(channel_id)
await channel.send('Test Has value of : Success 2')
client.run("****")
答案 0 :(得分:0)
为此,我认为您应该使check_Test()成为任务循环(https://discordpy.readthedocs.io/en/latest/ext/tasks/index.html?highlight=tasks%20loop#discord.ext.tasks.loop)
@tasks.loop(hours=1)
async def check_Test():
或者您应该将fn_1()和fn_2()设置为任务循环,然后在方法的最后调用check_Test()。