如何在特定频道中发送错误消息

时间:2021-03-28 07:51:59

标签: node.js discord.js

我希望我的机器人向我的私人服务器发送错误和警告,但我不知道该怎么做。也可以在嵌入中发送它吗?哪里和谁触发了错误?谢谢!

这是我试过的:

client.on("error", (e) => {
    client.channels.cache.get(`825634835572719658`).send(e)
})

1 个答案:

答案 0 :(得分:1)

您可以通过其名称找到频道,如下所示:

const channel = client.channels.cache.find(channel => channel.name === channelName)
channel.send(message)

创建嵌入消息如下

let embed = new MessageEmbed().setTitle("This is an error").setColor(0x7dd9e8)

您应该能够将两者结合起来向频道发送嵌入消息!

或者,使用较新的 discord.js 版本,您还可以执行以下操作:

// The channel that you want to send the messages to
const channel = client.channels.cache.get('channel id')

client.on('message',message => {
  // Ignore bots
  if (message.author.bot) return
  // Send the embed
  const embed = new Discord.MessageEmbed()
    .setDescription(message.content)
    .setAuthor(message.author.tag, message.author.displayAvatarURL())
  channel.send(embed).catch(console.error)
})