如何使用 Discord 机器人嵌入消息?

2022-01-10 00:00:00 discord bots embed javascript discord.js

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

这是一个例子:

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

解决方案

你可以使用 MessageEmbed,就像programmerRaj说的那样,或者使用embed属性#/docs/main/stable/typedef/MessageOptions" rel="nofollow noreferrer">MessageOptions:

const {MessageEmbed} = require('discord.js')常量嵌入 = 新 MessageEmbed().setTitle('一些标题').setDescription('一些描述').setImage('图片地址')//Discord.js v13//这两个是同一个东西channel.send({embeds: [embed]})频道.发送({嵌入:[{标题:'一些标题',描述:'一些描述',图片:{url:'图片网址'}}]})//Discord.js v12//这两个是同一个东西频道.发送(嵌入)频道.发送({嵌入:{标题:'一些标题',描述:'一些描述',图片:{url:'图片网址'}}})

要在特定频道中发送嵌入的用户消息,您可以执行以下操作,其中 client 是您的 Discord.js Client:

//你想发送消息的频道const channel = client.channels.cache.get('channel id')client.on('消息',消息 => {//忽略机器人如果(message.author.bot)返回//发送嵌入常量嵌入 = 新 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 的嵌入指南(archive) 或链接的文档有关如何使用嵌入的更多信息.

I want to code a bot that will embed a user's sent message in a specific channel. If you know anything about GTA RP servers, it's like a Twitter or Instagram bot.

Here's an example:

I think it's something about the console.log and the author's name, but I'm not sure so that's why I'm here. How can I embed users' messages like this?

解决方案

You can use a 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'}
  }
})

To send an embed of users' message in a particular channel, you can do something like this, where client is your 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)
})

Note that the above code will send the embed for every message not sent by a bot, so you will probably want to modify it so that it only sends it when you want it to.

I recommend reading Discord.js' guide on embeds (archive) or the documentation linked above for more information on how to use embeds.

相关文章