如何做到这一点,如果一条消息被发送到机器人,该消息会显示在服务器 discord.js 的频道中

时间:2021-04-14 14:57:36

标签: discord discord.js

我需要帮助制作一个不和谐的机器人,这样如果一条消息被发送到一个机器人,消息就会显示在服务器 discord.js 的频道中

1 个答案:

答案 0 :(得分:0)

一个非常基本的方法就是查看消息通道类型是否为DM(if (message.channel.type === 'dm')),如果是,我们将消息内容发送到某个通道({{1} })。 放在一起,是这样的:

client.channels.cache.get('channel_id').send(message.content)

那只会发送消息内容。

如果您想查看发送消息的人,可以执行以下操作:

if (message.channel.type === 'dm') {
        client.channels.cache.get('channel_id').send(message.content)
}

这会向您发送类似 client.channels.cache.get('channel_id').send(`New message by ${message.author.tag}: ${message.content}`) 之类的信息。
(如果您想要一个简单的 New message by Username#0000: Hello world! 语句来获取发送者和消息内容,那么这是一个停止的好地方.)

然后我们可以继续将其创建为嵌入:

if

您始终可以创建一个像 var dmMessage = new Discord.MessageEmbed() // This creates an empty embed .setTitle("New DM!") // This sets the title of the embed to 'New DM!' .setDescription(`**New message from ${message.author.tag}**(${message.author.id})\n**Message content**: ${message.content}`) // This is the main text of the embed .setColor("GREEN") // This sets the color of the embed. It can be RED, PURPLE, etc. or it can be a hex code .setTimestamp() // This shows the time when the embed was sent. ( it would also be the time the message was sent ) .setFooter(client.user.tag, client.user.displayAvatarURL()) // This sets the bots tag and profile picture to the footer .setAuthor(message.author.tag, message.author.displayAvatarURL({dynamic: true})) // This sets the 'author' of the embed. It will show up at the top of the embed. The {dynamic: true} just makes it animate the profile photo if it is animated if (message.channel.type === 'dm') { client.channels.cache.get('channel_id').send(dmMessage) // Sends the embed to the specified channel ID } 这样的变量并将 var dmID = "your_channel_id" 中的 'channel_id' 替换为 client.channels.cache.get,这样您就可以更改该变量,并且它会随处更改。