"... has never been awaited"错误(不和谐机器人,蟒蛇)


async def on_ready():
print('We have logged in as {0.user}'.format(client))
schedule.every(5).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)


async def job():
print("I'm working...")
channel = client.get_channel(651895929426673704)
await channel.send('hello')

这样我就得到了错误";共同出游的"工作"从来都不是等待自己的_运行作业(作业(";但无论我在哪里添加它,我总是得到";对象函数不能用在"await"表达式中;有人能告诉我我做错了什么吗?或者我该把它放在哪里?

据我所知"时间表";模块不支持异步任务。您可以尝试使用aiochule,它是从dbader的存储库中派生出来的。工作方式相同,仅适用于async函数。

另一个选项已经内置在discordneneneba API中,使用任务。

from discord.ext import tasks
...
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
job.start() # To start the loop when your bot starts
@task.loop(seconds=5)
async def job():
print("I'm working...")
channel = client.get_channel(651895929426673704)
await channel.send('hello')

# You can also add commands to start and stop the task loop
@client.command()
async def job_start(ctx):
job.start()
@client.command()
async def job_stop(ctx):
job.stop()

使用aioschedule在给定时间安排每天任务的示例

import aioschedule as schedule
...
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
schedule.every().day.at("20:00").do(job) # "job" is scheduled for 8pm every day
while True:
await schedule.run_pending()
await asyncio.sleep(1)
async def job():
print("I'm working...")
channel = client.get_channel(channel_id)
await channel.send('hello')

也许是这样的:

import asyncio
async def on_ready():
print('We have logged in as {0.user}'.format(client))
schedule.every(5).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)


async def job2():
print("I'm working...")
channel = client.get_channel(651895929426673704)
await channel.send('hello')
def job():
asyncio.gather(job)

最新更新