Python 3.x 如何在synchronus函数中调用异步函数?

Python 3.x 如何在synchronus函数中调用异步函数?,python-3.x,discord,discord.py,Python 3.x,Discord,Discord.py,我正试图制造一个不和谐的机器人,它发送嘿!!在每天特定时间的特定频道中。 但这给了我一个错误 import discord import schedule bot = commands.Bot(command_prefix = "^") @bot.event async def on_ready(): schedule.every().day.at("18:00").do(job) while 1:

我正试图制造一个不和谐的机器人,它发送嘿!!在每天特定时间的特定频道中。 但这给了我一个错误

import discord
import schedule
    
bot = commands.Bot(command_prefix = "^")
    
@bot.event
async def on_ready():
   schedule.every().day.at("18:00").do(job)
        
   while 1:
       schedule.run_pending()
       time.sleep(1)

async def job():
        channel = bot.get_channel(72246xxxxxxxxx)
        await channel.send("Hey!!")
    
运行时警告:从未等待协同路由“作业”

@bot.event
async def on_ready():
    while True:
        await asyncio.sleep(1)
        x=datetime.today() #identify the time right now
        y=x.replace(hour=18, minute=0, second=0) #here you can modify time
        delta_t=y-x
        secs=delta_t.seconds
        print(secs) #you can delete this line if you would like to disable visual countdown
        if secs == 0: #when countdown have 0 seconds left it sends the message
            channel = bot.get_channel(1234567890) #your channel id
            await channel.send("Hey!!") #your message

我知道我对你的代码做了很多修改,但在Discord.py中,bot需要打开_ready或其他。您可以将作业作为脚本运行,但您的bot无法启动,因此建议使用on_ready或其他方式。您可以尝试使用asyncio.run(job()),但我不能保证它会起作用。您可以了解有关协同路由和任务的更多信息。

感谢您的回复,日程安排在on_ready事件中,但问题是它没有在同步函数中使用异步函数。我尝试使用async.create\u task()和async.run(),但没有任何效果。编辑代码您的代码工作正常,谢谢。我也尝试过nest_async,但它出现了错误。我很高兴听到我的答案有帮助D