【问题标题】:Why is my interaction collector not collecting? (discord.js)为什么我的交互收集器不收集? (discord.js)
【发布时间】:2021-12-10 21:02:29
【问题描述】:

我是 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 => { 实际上会触发。 这可能是因为我不是一个好的程序员。如果可以,请帮忙!

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • 您确定交互 ID 正确吗?尝试在 collect 事件中的两个 if 之前放置一个 console.log(i.customId);
  • @Tenclea collect 事件不会触发代码中的任何内容,所以我还不能测试它。未打印日志。
  • 不带过滤器可以试试吗?

标签: javascript node.js discord discord.js


【解决方案1】:

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

这是发生了什么:

  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);
    }
});

【讨论】:

  • 谢谢!我不聪明,所以我只是使用了一个不必要的收集器:D
猜你喜欢
  • 2021-03-31
  • 2022-07-31
  • 1970-01-01
  • 1970-01-01
  • 2020-12-30
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 2015-07-30
相关资源
最近更新 更多