【问题标题】:how to detect an embed and then resend it (discord.js)如何检测嵌入然后重新发送它(discord.js)
【发布时间】:2020-11-24 20:23:44
【问题描述】:

所以我要做的是检测机器人何时在频道中发送嵌入,当它检测到这一点时,获取该嵌入并重新发送它,就像它发送时一样。

例如,如果机器人检测到在一个频道中发送了嵌入,它将在另一个频道中发送完全相同的嵌入。但这样做的原因是因为我想从多个机器人中获取嵌入。

discordjs.guide 中说要使用此代码:

const receivedEmbed = message.embeds[0];
const exampleEmbed = new Discord.MessageEmbed(receivedEmbed).setTitle('New title');

channel.send(exampleEmbed);

但这对我不起作用

【问题讨论】:

  • 什么不起作用?有错误吗?
  • 你的问题是矛盾的。如果嵌入是由机器人发送的,您是否只想重新发送?您要在同一频道或不同频道重新发送嵌入内容吗?
  • @DaemonBeast 将嵌入从一个频道中的机器人重新发送到另一个频道

标签: javascript node.js discord discord.js


【解决方案1】:

您需要将channel.send(exampleEmbed); 行中的channel 替换为对频道的实际引用。由于您将使用消息事件处理程序,因此您可以使用message.channel 获取发送消息的通道。

我还添加了一项检查,以确保消息是由机器人发送并包含嵌入内容。

client.on('message', message => {
  // check to ensure message was sent by bot and contains embed
  if (!message.author.bot || !message.embeds[0]) return;

  const receivedEmbed = message.embeds[0];
  const exampleEmbed = new Discord.MessageEmbed(receivedEmbed).setTitle('New title');

  // send in same channel
  message.channel.send(exampleEmbed);

  // send in different channel
  client.channels.fetch(/* Channel ID */).then(channel => {
    channel.send(exampleEmbed);
  });
  // alternatively, you can use this (but the function must be asynchronous)
  const channel = await client.channels.fetch(/* Channel ID */);
  channel.send(exampleEmbed);
});

有关有效属性和方法的更多信息,请阅读Discord.js docs

【讨论】:

  • 为此
【解决方案2】:

以下代码将检查消息是否有任何嵌入,并重新发送第一个。

if (message.embeds) {
    message.channel.send({ embed: message.embeds.first() || message.embeds[0] })
};

【讨论】:

    【解决方案3】:
    const embed = message.embeds[0];
    const editedEmbed = embed
    .setTitle('Edited!')
    .addField('Test Field!', 'This is a test', true);
    message.channel.send(editedEmbed);
    

    这对我来说非常好。 问题是您没有选择 TextChannel。 (message.channel)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 2020-10-17
      • 2020-10-09
      • 2021-08-22
      • 1970-01-01
      • 2020-07-17
      • 2020-07-04
      相关资源
      最近更新 更多