我正在尝试编辑我的机器人发送的消息,但出现错误
@client.command()
async def edit(ctx):
message = await ctx.send('testing')
time.sleep(0.3)
message.edit(content='v2')
错误:
RuntimeWarning: coroutine 'Message.edit' was never awaited
message.edit(content='v2')
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
顺便问一下,有没有什么方法可以只通过消息 ID 来编辑消息?
最佳答案
time.sleep()
是一个阻塞调用,这意味着它几乎搞砸了您的脚本。您将要使用的是 await asyncio.sleep()
。
另外,edit()
是协程,需要等待。您的命令应该如下所示:
import asyncio # if you haven't already
@client.command()
async def edit(ctx):
message = await ctx.send('testing')
await asyncio.sleep(0.3)
await message.edit(content='v2')
要通过 ID 编辑消息,您需要它来自的 channel :
@client.command()
async def edit(ctx, msg_id: int = None, channel: discord.TextChannel = None):
if not msg_id:
channel = client.get_channel(112233445566778899) # the message's channel
msg_id = 998877665544332211 # the message's id
elif not channel:
channel = ctx.channel
msg = await channel.fetch_message(msg_id)
await msg.edit(content="Some content!")
这个命令的用法是 !edit 112233445566778899 #message-channel-origin
假设前缀是 !
,并且如果消息在您执行命令的 channel 中。
引用资料:
关于python-3.x - 使用 discord.py 编辑消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62149285/