如何使用 Discord 机器人嵌入消息?
How can I embed messages using a Discord bot?
我想编写一个机器人程序,将用户发送的消息嵌入特定频道。如果您对 GTA RP 服务器有所了解,它就像是 Twitter 或 Instagram 机器人。
这是一个例子:
我认为这与 console.log
和作者姓名有关,但我不确定,所以这就是我来这里的原因。我如何嵌入用户的消息,例如
这个?
您可以使用 MessageEmbed
, like programmerRaj said, or use the embed
property in MessageOptions
:
const {MessageEmbed} = require('discord.js')
const embed = new MessageEmbed()
.setTitle('some title')
.setDescription('some description')
.setImage('image url')
// Discord.js v13
// These two are the same thing
channel.send({embeds: [embed]})
channel.send({
embeds: [{
title: 'some title',
description: 'some description',
image: {url: 'image url'}
}]
})
// Discord.js v12
// These two are the same thing
channel.send(embed)
channel.send({
embed: {
title: 'some title',
description: 'some description',
image: {url: 'image url'}
}
})
要在特定频道中发送嵌入的用户消息,您可以这样做,其中 client
是您的 Discord.js Client
:
// 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 MessageEmbed()
.setDescription(message.content)
.setAuthor(message.author.tag, message.author.displayAvatarURL())
channel.send({embeds: [embed]}).catch(console.error)
// Discord.js v12:
// channel.send(embed).catch(console.error)
})
请注意,上面的代码将为 每条 不是由机器人发送的消息发送嵌入,因此您可能需要修改它,以便它仅在您需要时发送
我建议阅读 Discord.js' guide on embeds (archive) 或上面链接的文档以获取有关如何使用嵌入的更多信息。
我认为你需要的是:
const Discord = require('discord.js');
const client = new Discord.Client()
client.on("message", async message =>{
if(message.author.bot) return;
if(message.channel.id === 'channelID'){
message.delete();
const newEmbed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setDescription(`${message.content}`)
.setColor('#d32256')
.setImage(message.attachments.first().proxyURL)
.setTimestamp()
.setFooter('Instagram', 'ImageOfInsta');
message.channel.send(newEmbed);
},
});
其中 channelID 表示:您希望机器人重新发布的频道和 ImageOfInsta:instagram 徽标的图像!我通常先下载图像或徽标,然后在 Discord.Then 中重新上传我在我的代码中使用 Discord Image 的 link。
P.S 只有当您上传带有短信的图片时,此代码才有效!
我想编写一个机器人程序,将用户发送的消息嵌入特定频道。如果您对 GTA RP 服务器有所了解,它就像是 Twitter 或 Instagram 机器人。
这是一个例子:
我认为这与 console.log
和作者姓名有关,但我不确定,所以这就是我来这里的原因。我如何嵌入用户的消息,例如
这个?
您可以使用 MessageEmbed
, like programmerRaj said, or use the embed
property in MessageOptions
:
const {MessageEmbed} = require('discord.js')
const embed = new MessageEmbed()
.setTitle('some title')
.setDescription('some description')
.setImage('image url')
// Discord.js v13
// These two are the same thing
channel.send({embeds: [embed]})
channel.send({
embeds: [{
title: 'some title',
description: 'some description',
image: {url: 'image url'}
}]
})
// Discord.js v12
// These two are the same thing
channel.send(embed)
channel.send({
embed: {
title: 'some title',
description: 'some description',
image: {url: 'image url'}
}
})
要在特定频道中发送嵌入的用户消息,您可以这样做,其中 client
是您的 Discord.js Client
:
// 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 MessageEmbed()
.setDescription(message.content)
.setAuthor(message.author.tag, message.author.displayAvatarURL())
channel.send({embeds: [embed]}).catch(console.error)
// Discord.js v12:
// channel.send(embed).catch(console.error)
})
请注意,上面的代码将为 每条 不是由机器人发送的消息发送嵌入,因此您可能需要修改它,以便它仅在您需要时发送
我建议阅读 Discord.js' guide on embeds (archive) 或上面链接的文档以获取有关如何使用嵌入的更多信息。
我认为你需要的是:
const Discord = require('discord.js');
const client = new Discord.Client()
client.on("message", async message =>{
if(message.author.bot) return;
if(message.channel.id === 'channelID'){
message.delete();
const newEmbed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setDescription(`${message.content}`)
.setColor('#d32256')
.setImage(message.attachments.first().proxyURL)
.setTimestamp()
.setFooter('Instagram', 'ImageOfInsta');
message.channel.send(newEmbed);
},
});
其中 channelID 表示:您希望机器人重新发布的频道和 ImageOfInsta:instagram 徽标的图像!我通常先下载图像或徽标,然后在 Discord.Then 中重新上传我在我的代码中使用 Discord Image 的 link。
P.S 只有当您上传带有短信的图片时,此代码才有效!