我花了几个小时阅读类似的问题并寻找解决此问题的其他方法。我正在尝试创建一个 Discord 机器人,每隔 x 时间向 channel 发送一条消息,但我不断收到此错误:
'运行时警告:从未等待协程'function1' self._run_job(job)'
import discord,random,asyncio,os
from discord.ext import commands, tasks
from datetime import datetime
import schedule
import time
token = 'xxxx'
bot = discord.Client
bot = commands.Bot(command_prefix='.')
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name)
print(bot.user.id)
print('-'*80)
async def function1():
channel = bot.get_channel(656805505804533761)
await channel.send('test')
print('test')
await asyncio.sleep(1)
schedule.every(1).second.do(function1)
while True:
schedule.run_pending()
bot.run(token)
最佳答案
这是因为您使用的调度库不支持协程。
相反,您可以使用 create_task
使用事件循环来创建后台任务。
有关discord.py 的示例,请参阅 https://github.com/Rapptz/discord.py/blob/master/examples/background_task.py .
或者,您也可以使用 discord.py's tasks extension ,您似乎已经在导入它。
关于python - 我的 Python Discord 机器人中出现 'RuntimeWarning: coroutine ' 函数 1' was never awaited' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59440851/