【问题标题】:Embed message discord.js嵌入消息 discord.js
【发布时间】:2021-08-04 23:20:07
【问题描述】:

我见过很多这样的不和谐嵌入代码:

(这是一个老问题,我对编码很陌生......)

const { MessageEmbed } = require('discord.js');


const exampleEmbed = new MessageEmbed()
    .setColor('#0099ff')
    .setTitle('Some title')
    .setURL('https://discord.js.org/')
    .setAuthor('Some name', 'https://i.imgur.com/AfFp7pu.png', 'https://discord.js.org')
    .setDescription('Some description here')
    .setThumbnail('https://i.imgur.com/AfFp7pu.png')
    .addFields(
        { name: 'Regular field title', value: 'Some value here' },
        { name: '\u200B', value: '\u200B' },
        { name: 'Inline field title', value: 'Some value here', inline: true },
        { name: 'Inline field title', value: 'Some value here', inline: true },
    )
    .addField('Inline field title', 'Some value here', true)
    .setImage('https://i.imgur.com/AfFp7pu.png')
    .setTimestamp()
    .setFooter('Some footer text here', 'https://i.imgur.com/AfFp7pu.png');

channel.send({ embeds: [exampleEmbed] });

那么,我不明白的是什么是触发器?就像您应该为 pong 输入 .ping 一样,对吗?那么我输入什么来让我的机器人类型嵌入这个?

【问题讨论】:

  • 在开始创建 Discord 机器人之前,请先查看一些指南和教程。 This 是一个很棒的 discord.js 初学者指南,YouTube 上有大量的视频教程。如果您通过这些教程学习如何正确使用 djs,它将为您节省大量时间,因此您不必陷入创建不和谐机器人的每一步并在这里提出一个又一个问题。问这个问题很像试图跳过一个你一无所知的复杂游戏的教程。当你不能轻易做到时,你会迷失和困惑。

标签: discord.js


【解决方案1】:

您发送嵌入的方式(afaik)特定于 discord.js 版本 13,您很可能尚未更新 discord.js:

channel.send({ embeds: [exampleEmbed] });

我目前使用的版本是12.5.3
package.json 中检查您的discord.js 版本,如果版本不是13 或更高版本,请更新或者如果您想坚持使用12.5.3 版本,请尝试以下一些方法:

channel.send( exampleEmbed );

或者像这样:

channel.send({ embed: exampleEmbed });

像这样发送嵌入:“{ embeds: [exampleEmbed] }”使不和谐认为您发送的是空消息,因此它不会发送任何内容。

【讨论】:

  • 这实际上并不完全正确。在 discord.js V13 中,您必须将嵌入用作数组。
  • @ChristophBlüm 谢谢。我编辑了我的答案以考虑您的评论。
  • @code_monkey023 我的版本其实和你一样
  • @haojie 好的,如果您想坚持使用该版本,请尝试channel.send( exampleEmbed );channel.send({ embed: exampleEmbed });。如果您想使用答案中的代码,则应更新到 discord.js 的第 13 版。
  • @haojie 你的问题解决了吗?如果是这样,请将帮助您的答案标记为正确。
【解决方案2】:

此代码应位于事件处理程序中(例如 on.message)。如果该代码已经在一个事件处理程序中(除了这个const { MessageEmbed } = require('discord.js');,它应该在顶部),并且如果它仍然没有发送,那么你应该改变这一行:

channel.send({ embeds: [exampleEmbed] });

message.channel.send(exampleEmbed)

【讨论】:

    【解决方案3】:

    您提供的代码示例中没有触发器。您需要有一个消息事件或interactionCreate 事件的侦听器来侦听传统的基于消息的命令和斜线命令。您可以将处理程序传入这些侦听器并响应命令用法。

    因为看起来你是reading the discord.js guide,所以我建议reading from the start,首先会向你介绍如何处理消息。

    【讨论】:

      【解决方案4】:

      要发送它,只需使用消息事件。像这样:

      const { MessageEmbed, Client } = require('discord.js');
      const client = new Client();
      
      client.on('message', msg => {
      const { channel, content } = msg;
      if(msg.content === 'someCommand') {
      const exampleEmbed = new MessageEmbed()
      //setProperties...
      
      channel.send({ embeds: [exampleEmbed] });
      }
      })
      client.login('[TOKEN_HERE'])
      

      这里发生的情况是,当有人发送消息 (msg) 时,客户端正在接收消息对象。然后我们使用 channel 和 content 属性来评估命令,如果命令正确则发送嵌入。我拿出了属性设置以节省空间。您可以将它们添加回来。

      【讨论】:

      • 那么您在问题中发布的代码不是完整代码吗?好吧,只需删除前 2 行。我只是这样做,以便任何看到它的人都可以自己运行它。
      猜你喜欢
      • 2021-08-25
      • 2018-08-26
      • 2021-08-22
      • 2021-10-09
      • 2019-02-26
      • 2020-09-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多