首页 > 解决方案 > 为什么我的交互收集器不收集?(discord.js)

问题描述

我是 javascript 尤其是 discord.js 的初学者编码器 在将我的头绕在 discord.js 指南上之后,我仍然无法让收集器工作。

client.on('interactionCreate', interaction => {
  if (!interaction.isButton()) return;
  const filter = i => i.customId === '1' || i.customId === "2";
  const collectchannel = interaction.channel;
  const collector = collectchannel.createMessageComponentCollector( filter, { time: 10000 });

  collector.on('collect', i => {
    if (i.customId === '1') {
      client.commands.get('help').execute(interaction, 1, Discord);
    } else if (i.customId === '2') {
      client.commands.get('help').execute(interaction, 2, Discord);
    }
  });

  collector.on('end', collected => {
    console.log(`Collected ${collected.size} interactions.`);
  });
});

当我按下按钮时,收集功能什么也不做。我尝试放置一个console.log("test"),但它不会触发。然而,collector.on('end', collected => {确实火了。这可能是因为我不是一个好的程序员。如果可以,请帮忙!

标签: javascriptnode.jsdiscorddiscord.js

解决方案


您有一个MessageComponentCollector内部事件处理程序interactionCreate,在这种情况下,这可能不是您想要的。

这是正在发生的事情:

  1. 你点击按钮

  2. 您的eventCreate处理程序触发,检查它是否是被单击的按钮并启动 MessageComponentCollector

  3. MessageComponentCollector等待按钮单击,并且尚未触发,因为已经发生了一个(首先触发您的处理interactionCreate程序的那个)

  4. 每当您再次单击该按钮时,收集器就会触发,但interactionCreate处理程序也会触发,然后您返回#2,启动另一个收集器


您可能想要的是在没有收集器的情况下处理您的按钮点击:

client.on('interactionCreate', interaction => {
    if (!interaction.isButton()) return;
    if (i.customId === '1') {
        client.commands.get('help').execute(interaction, 1, Discord);
    } else if (i.customId === '2') {
        client.commands.get('help').execute(interaction, 2, Discord);
    }
});

推荐阅读