【问题标题】:How can I embed messages using a Discord bot?如何使用 Discord 机器人嵌入消息?
【发布时间】:2021-01-12 07:22:52
【问题描述】:

我想编写一个机器人,将用户发送的消息嵌入特定频道。如果您对 GTA RP 服务器有所了解,那就像 Twitter 或 Instagram 机器人。

这是一个例子:

我认为这与console.log 和作者姓名有关,但我不确定这就是我在这里的原因。如何嵌入用户的消息,例如 这个?

【问题讨论】:

标签: javascript discord discord.js bots embed


【解决方案1】:

你可以使用MessageEmbed,就像programmerRaj所说的那样,或者使用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)
})

请注意,上面的代码将为 每个不是由机器人发送的消息发送嵌入,因此您可能需要对其进行修改,使其仅在您需要时发送。 p>

我建议阅读Discord.js' guide on embeds (archive) 或上面链接的文档,以获取有关如何使用嵌入的更多信息。

【讨论】:

  • 我这样做了,它与消息完美结合,但是当我尝试使用此代码“.setImage(message.attachments)”添加图像时,它不起作用。难道我做错了什么?我也试过 .setImage(message.attachments.first) 所以现在我没有收到错误,但图像在不同的消息中(不嵌入)
  • @EvaHatz first 是一个方法,setImage 接受一个 URL,所以你需要 .setImage(message.attachments.first().url)。
【解决方案2】:

我认为你需要的是:

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 Image 的链接。

P.S 此代码仅在您上传带有短信的图片时有效!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 2019-11-11
    • 2019-09-16
    • 2021-07-20
    • 2019-02-20
    • 2020-08-24
    • 2020-07-04
    • 2021-04-21
    相关资源
    最近更新 更多