javascript - 如何让机器人在 Discord 上编辑自己的消息

标签 javascript discord discord.js message edit

我的 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()asyncawait 来完成。从个人喜好来看,我经常使用第二个选项,尽管我已经为您列出了这两个选项。

最终代码

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/

相关文章:

javascript - 在 Cordova/ionic 中写入文件不起作用

重新实现 Array.prototype.slice 的 JavaScript 难题未按预期工作

javascript - 如何让我的不和谐机器人响应另一条消息嵌入字段中的关键字

javascript - 使用 Discord.JS 进行本地化和临时回复?

javascript - guildMemberAdd 和 guildMemberRemove 嵌入不再发送 (Discord.js)

javascript - Firefox 中 Google 的 downloadDataURI

javascript - 在网页上加载 Flash 横幅后添加 cookie

javascript - 我想在 discord.js v13 中使某人静音,但我在创建 VoiceState 对象时遇到问题

javascript - 获取/监听 channel 中的新消息

node.js - 根据服务器上的 X 时间将用户移动到不同的角色