0

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

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

4

1 回答 1

0

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

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()方法。

有关 Promise 的更多信息,请参阅此 MDN 文档
有关该TextChannel.send()方法的更多信息,请参阅Discord.js 文档

于 2019-05-30T23:59:40.267 回答