如何使消息被DM设置为机器人,该消息显示在服务器不和谐的频道中.js



我需要帮助制作一个不和谐的机器人程序,这样如果消息被称为机器人程序,消息就会显示在服务器不和谐.js 的通道中

一个非常基本的方法是查看消息通道类型是否为DM(if (message.channel.type === 'dm')(,如果是,我们将把消息内容发送到某个通道(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"的变量,并用dmID替换client.channels.cache.get中的'channel_id',这样您就可以更改该变量,它将在任何地方更改。