【问题标题】:How to make a embed message to be edited later如何使嵌入消息稍后编辑
【发布时间】:2019-10-15 16:28:15
【问题描述】:

我正在尝试开发一些代码,在机器人上线时显示它,并让嵌入消息的颜色每 2 秒更改一次。 (2000 毫秒)但我不知道怎么做,我收到一个错误,上面写着 channel.send.edit is not a function 或类似的东西。

我所做的是... 创建超时。 编辑消息,但它会为正常运行时间部分显示不同的消息/输出。 删除并发送消息。

var myInfo = new discord.RichEmbed()
.setAuthor(`${bot.user.username} is now ONLINE`,`${bot.user.avatarURL}`)
.setDescription(`The bot is now up, please check the following below to see when the bot was online.`)
.addField(`Ready At`,`${bot.readyAt}`,true)
.addField(`Timestamp`,`${bot.readyTimestamp}`,true)
.addField(`Uptime`,`${bot.uptime}`,true)
.setColor(0x008704)
.setFooter('Bot is now online.')
.setThumbnail(`${bot.user.avatarURL}`)

var myInfo2 = new discord.RichEmbed()
.setAuthor(`${bot.user.username} is now ONLINE`,`${bot.user.avatarURL}`)
.setDescription(`The bot is now up, please check the following below to see when the bot was online.`)
.addField(`Ready At`,`${bot.readyAt}`,true)
.addField(`Timestamp`,`${bot.readyTimestamp}`,true)
.addField(`Uptime`,`${bot.uptime}`,true)
.setColor(0x00c13a)
.setFooter('Bot is now online.')
.setThumbnail(`${bot.user.avatarURL}`)

bot.channels.get("523649838693482507").send(myInfo).edit(myInfo2);

我希望机器人上线后会发送嵌入消息,然后 2 秒后机器人会编辑颜色,依此类推。

输出是一个给出错误的机器人,或者根本不工作。

【问题讨论】:

    标签: discord.js commando


    【解决方案1】:

    您可以通过将then() 方法附加到返回的promise 来使用message.channel.send(...) 的结果,如下所示...

    message.channel.send(myInfo)
      .then(m => m.edit(myInfo2)) // Note that this will edit the message immediately.
      .catch(console.error);
    

    你会注意到我添加了一个 catch() 方法来捕获在返回错误的事件中被拒绝的承诺。

    或者,您可以将变量声明为已履行的承诺。但是,await 关键字只能在异步函数中。这是一个例子......

    client.on('message', async message => {
      try {
        const m = await message.channel.send('Hello.');
        await m.edit('Hi again.');
      } catch(err) {
        console.error(err);
      }
    });
    

    在这种情况下,我们可以使用try...catch 语句代替单独的catch() 方法。

    有关承诺的更多信息,请参阅this MDN documentation
    有关TextChannel.send() 方法的更多信息,请参阅the Discord.js documentation

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 2021-05-14
      • 2021-11-01
      • 2012-01-29
      • 2020-09-08
      • 2021-08-31
      • 2021-05-09
      • 2021-10-30
      • 1970-01-01
      相关资源
      最近更新 更多