如何制作嵌入消息以供以后编辑

时间:2019-05-30 02:57:43

标签: discord.js commando

我正在尝试开发一些代码,以显示该bot何时在线,并让嵌入的消息颜色每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秒钟后机器人会编辑颜色,依此类推。

输出是发出错误或完全不起作用的机器人。

2 个答案:

答案 0 :(得分:0)

您可以通过将then()方法附加到返回的Prom来使用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

答案 1 :(得分:-1)

  

编辑:

尝试这个

client.on('message', message => {
if (message.channel.id === "523649838693482507") {
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}`)

    message.channel.send(myInfo)
        .then(() => message.edit(myInfo2));
}
});
相关问题