Discord.py异步函数不提供任何输出,也不执行任何操作

2024-09-30 20:38:32 发布

您现在位置:Python中文网/ 问答频道 /正文

代码如下:

print('hmm1') #testing, this one prints
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='&')
client.run('my token', bot=False)

async def testFunction():
    print('hmm') #<- this one does not print.
    channel = await client.get_channel(708848617301082164)
    message_id=715307791379595275
    msg = await client.get_message(channel, message_id)
    await msg.edit(content="L")
    await msg.edit(content="W")
    print('edited message!')
testFunction()
# none of the above works. I only get "hmm1" printed in console.

我不知道发生了什么,因为控制台中没有任何错误或输出。有人知道这个问题吗


Tags: importclientidmessagegetchannelmsgawait
1条回答
网友
1楼 · 发布于 2024-09-30 20:38:32

如果您不熟悉异步函数,则需要对它们进行await处理。可以在msg.edit(...中看到协同路由的示例,因为edit()是一个协同路由,因此您需要像这样await处理:await testFunction()

另外,client.get_channel()client.get_message()不是协程,因此不需要等待它们

正如Eric提到的,您还需要将client.run('...向下移动到文件中的最后一行,否则它将阻止脚本的其余部分。下面是代码的结构:

# imports

# commands, events, functions

# last line
client.run('...

看起来您也在使用一些旧文档,因为d.py已经转移到rewrite(v1.x),并且看起来您使用的client.get_message()实际上来自v0.16.x

我想读一下these changes以熟悉重写。尽量避免过时的教程

作为一个小起点,您的await client.get_message(channel, message_id)应该变成await channel.fetch_message(message_id)


参考文献:

相关问题 更多 >