我该如何使用Discord机器人嵌入信息?

5
我想编写一个机器人,能够在特定频道嵌入用户发送的信息。如果你了解GTA RP服务器,这有点类似于Twitter或Instagram的机器人。
以下是一个示例: screenshot of example embeds 我认为涉及到使用`console.log`和作者名称,但我不确定,所以才会来这里问。如何像这样嵌入用户的消息?

您可以创建一个 MessageEmbed。https://discord.js.org/#/docs/main/stable/class/MessageEmbed - programmerRaj
3个回答

8

你可以像programmerRaj所说的那样使用一个MessageEmbed,或者使用MessageOptions中的embed属性:

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的嵌入式指南存档)或上述链接中的文档,以获取有关如何使用嵌入式消息的更多信息。

我用消息测试了这个代码,它可以完美地工作。但是当我尝试使用".setImage(message.attachments)"添加图像时,它就无法正常工作了。我做错了什么吗?我还尝试过".setImage(message.attachments.first)",现在我不会再收到错误信息了,但是图像在另一条消息中(而不是嵌入式消息)。 - Eva Hatz
@EvaHatz first 是一个方法,setImage 接受一个 URL,所以你需要使用 .setImage(message.attachments.first().url) - Lauren Yim

0
我认为你需要的是:

你所需要的是:

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。然后我在我的代码中使用Discord图像的链接。

附言:此代码仅在您上传带有文本消息的图像时才有效!


0

链接无法使用。此外,建议在您的答案中包含一个描述,说明链接所指向的内容。您能改进您的答案吗? - ruud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接