【问题标题】:Unable to fix 'RuntimeWarning: coroutine 'Command.__call__' was never awaited' error无法修复'RuntimeWarning:协程'Command.__call__'从未等待'错误
【发布时间】:2021-02-06 08:18:51
【问题描述】:

我一直在尝试创建一个 Discord 自我机器人,它根据另一个程序上的按钮输入在特定频道中发布消息。我不断收到此错误,并已尽一切努力修复它:'RuntimeWarning: coroutine 'Command.call' was never waiting'.任何帮助将不胜感激!

import tkinter as tk
from discord.ext import commands
import asyncio

bot = commands.Bot('', self_bot=True)


@bot.command()
async def on_command():
    channel = bot.get_channel('ID')
    await channel.send('Message')

root = tk.Tk()
root.title("Test")
root.geometry("1280x720")
root.configure(background="#161616")

pathBtn = tk.Button(root, height=1, width=10, text="Text", background="#282828", command=lambda: on_command()).pack()

spaceLabel = tk.Label(root, text="", height=1, background="#161616").pack()

root.mainloop()

bot.run("TOKEN", bot=False)

【问题讨论】:

    标签: python asynchronous url-rewriting discord.py


    【解决方案1】:

    on_command 不是函数,它是 coroutine,请参见此处docs

    你应该使用await expression:

    def run_command():
         loop = asyncio.get_event_loop()
         return loop.run_until_complete(on_command())
    
    .....
    
    
    command=lambda: run_command()
    

    希望能帮到你。

    【讨论】:

    • 感谢您的回复;但是,我的意思是现在收到一个语法错误:SyntaxError: 'await' outside async function。
    • @user14505443 请检查一下
    • 我很感激!我相信它解决了这个问题,但现在又出现了另一个问题:AttributeError: 'NoneType' object has no attribute 'send'。
    猜你喜欢
    • 2022-01-16
    • 2020-05-07
    • 2018-10-26
    • 2021-10-22
    • 2019-12-15
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多