discord.js embedMsg 没有定义?

Posted

技术标签:

【中文标题】discord.js embedMsg 没有定义?【英文标题】:discord.js embedMsg not defined? 【发布时间】:2020-04-29 16:28:42 【问题描述】:

您好,我正在尝试制作一个不和谐的机器人,一旦您单击添加到其嵌入消息中的反应,它就会根据您单击的消息响应一条消息,一个表示是,一个表示否。

一切看起来都很好,但是当我运行我的命令时,我得到了 const collector = embedMsg.message embedMsg 未定义。 我已经尝试了一切,所以我很感谢我能得到的任何帮助

这是我的代码:

const Client, RichEmbed = require('discord.js');
const client = new Client();
client.once('ready', () => 
   console.log('dance time');

client.on('message', message => 
if(message.author.bot)

if(message.embeds)

   const embedMsg= message.embed.find(msg => msg.title === 'Boogie Time!?');
   if(embedMsg)
   
       embedMsg.message.react('✅')
       .then(reaction => reaction.message.react('❌'))

       .catch(error => console.error);
   

return;


   // Do this after you've added reactions

// This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
const filter = (reaction, user) => (reaction.emoji.name === '✅' || reaction.emoji.name === '❌') && user.id === message.author.id;

// Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter
const collector = embedMsg.message.createReactionCollector(filter, time: 30000);

// This event is emitted when a reaction passes through the filter
collector.on('collect', r => r.name === '✅' ? console.log('Reacted Yes') : console.log('Reacted No'));

if(message.content.toLowCase() === 'boogie')

   const embed = new RichEmbed();
   embed.setTitle("Boogie Time!?")
   embed.setColor("GREEN")
   embed.setDescription("Are you sure?")
   message.channel.send(embed);

)
);

【问题讨论】:

【参考方案1】:

您的代码中的第一个问题是 client.on('ready' 块,您在 client.on('message' 块之后结束他。正确的做法是:

client.on('ready', () => 
   console.log('dance time');
)

message.embeds它的集合数组,所以需要用到

const embedMsg = message.embeds.find(msg => msg.title === 'Boogie Time!?');

为了代码的最佳可读性,尽量不要使用if(somethink) then do somethink 更好的方法是if(!somethink) return;

这将使您摆脱大量的括号。在您的代码中,您尝试等待反应,无论此消息是否嵌入。并且在添加return 的反应后停止您的功能,因此收集器将永远无法工作。

当你 const 一个收集器时,你不需要你 embedMSG.message 因为 find 方法已经返回了一条消息。

所以你的代码一定是这样的

    const Client, RichEmbed = require('discord.js');
    const client = new Client();
    client.once('ready', () => 
       console.log('dance time');
    )
    client.on('message', message => 

      if(!message.author.bot || !message.embeds) return
      const embedMsg = message.embeds.find(embed => embed.title === 'Boogie Time!?');
      if(!embedMsg) return

      embedMsg.react('?')
            .then(() => message.react('?'))
            .catch(() => console.error('One of the emojis failed to react.'));

    // This is filter, this specified which reactions it should capture, you can use filter to make sure you're only catching specific reactions by specific user
    const filter = (reaction, user) => (reaction.emoji.name === '?' || reaction.emoji.name === '?') && user.id === message.author.id;

    // Here, we're defining a collector that will be active for 30 seconds and collect reactions that pass the above filter

    // This event is emitted when a reaction passes through the filter
    collector.on('collect', r => r.name === '?' ? console.log('Reacted Yes') : console.log('Reacted No'));

      if(message.content.toLowCase() === 'boogie')
         const embed = new RichEmbed();
         embed.setTitle("Boogie Time!?")
         embed.setColor("GREEN")
         embed.setDescription("Are you sure?")
         message.channel.send(embed);
      
  )

【讨论】:

以上是关于discord.js embedMsg 没有定义?的主要内容,如果未能解决你的问题,请参考以下文章

Discord.js - .then 即使在函数中也没有定义?

Discord.js 频道未定义欢迎消息

Discord.js 变量在定义时不存在?

我的 discord.js 机器人的 sql 没有/不能从我的 sqlite3 db 定义一个表

使用 Node.js、Mongoose 和 Discord.js 的未定义错误 [无法读取未定义的属性]

message.channel.send 不工作 discord.js