我的 friend 为我编写了这段令人惊叹的代码,但它似乎不起作用。它的目的是根据命令发送消息,然后一遍又一遍地编辑该消息。但是当我运行代码时我的终端显示
DiscordAPIError: Cannot edit a message authored by another user method: 'patch', path: '/channels/808300406073065483/messages/811398346853318668', code: 50005, httpStatus: 403
有办法解决这个问题吗?
client.on('message', userMessage =>
{
if (userMessage.content === 'hi')
{
botMessage = userMessage.channel.send('hi there')
botMessage.edit("hello");
botMessage.edit("what up");
botMessage.edit("sup");
botMessage.react(":clap:")
}
});
最佳答案
Channel#send()
方法返回一个 promise ,这意味着您必须等待操作完成才能定义它。这可以使用 .then()
或 async
和 await
来完成。从个人喜好来看,我经常使用第二个选项,尽管我已经为您列出了这两个选项。
最终代码
client.on('message', async userMessage => {
if (userMessage.content === 'hi')
{
/*
botMessage = await userMessage.channel.send('hi there')
*/
userMessage.channel.send('hi there').then(botMessage => {
await botMessage.edit("hello");
await botMessage.edit("what up");
botMessage.edit("sup");
botMessage.react(":clap:")
})
}
});
关于javascript - 如何让机器人在 Discord 上编辑自己的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66311229/