如何使用 Discord 机器人嵌入消息? [英] How can I embed messages using a Discord bot?

查看:42
本文介绍了如何使用 Discord 机器人嵌入消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是一个例子:

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

解决方案

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

这篇关于如何使用 Discord 机器人嵌入消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆