python - 我的 Python Discord 机器人中出现 'RuntimeWarning: coroutine ' 函数 1' was never awaited' 错误

标签 python discord discord.py

我花了几个小时阅读类似的问题并寻找解决此问题的其他方法。我正在尝试创建一个 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/

相关文章:

python - Discord.py 重写自定义错误

python - Pandas:使用索引中包含的列名时出现 KeyError

Python作为PDF编辑和处理框架

python - k 组合的迭代器

javascript - Bot 将字符串返回到我没有指定的数字

python - 在 f 字符串内循环作为嵌入的值

python - python中的继承和覆盖__init__

node.js - Discord.js client.on

javascript - 如何在discord bot上删除所有角色并添加一个角色,然后删除添加的角色并恢复以前的角色

discord.py - 我怎样才能让机器人改变它在其名字下显示的不和谐的存在(听、看、玩等...)?