Discordjs "createMessageComponentCollector"从其他消息中收集交互



我在Discordjs中有2个斜杠命令。在这两个命令中,我有一个按钮和一个收集器,像这样:

const message = await interaction.reply({ components: [row] });
const collector = message.createMessageComponentCollector({ComponentType: ComponentType.Button});
collector.on('collect', async (i) => {}

如果我理解正确的话,它应该只收集消息中的交互。但在我的例子中,如果user1键入/command1, user2键入/command2,然后user2单击/command1的按钮,则可以执行/command2按钮的收集器的代码

,

/command1 :
async execute(interaction: any) {
const button = new ButtonBuilder().setLabel('Command 1').setStyle(ButtonStyle.Primary).setCustomId(`command1`);
const row = new ActionRowBuilder().addComponents(button);
const message = await interaction.reply({ components: [row] });
const collector = message.createMessageComponentCollector({ComponentType: ComponentType.Button});
collector.on('collect', async (i) => {i.reply('collector 1')}
}
/command2 :
async execute(interaction: any) {
const button = new ButtonBuilder().setLabel('Command 2').setStyle(ButtonStyle.Primary).setCustomId(`command2`);
const row = new ActionRowBuilder().addComponents(button);
const message = await interaction.reply({ components: [row] });
const collector = message.createMessageComponentCollector({ComponentType: ComponentType.Button});
collector.on('collect', async (i) => {i.reply('collector 2')}
}

在我之前写的场景中,如果user2点击user1的/command1按钮,bot将回复

collector 2

它实际上有时会回复collector 1,所以我认为两个收集器都被触发了,我得到错误DiscordAPIError[10062]:来自第二个收集器的未知交互,试图同时回复。

createMessageComponentCollector不应该只从一个特定的消息收集交互吗?为什么两个收集器都被触发了?

我可以在收集器中写if (i.customId != 'command1') return;之类的东西,但我觉得不应该需要它?它也没有修复相同/命令的收集器,这些收集器也将被触发,并且必须为我制作的每个收集器添加一堆过滤器,这是不实用的。

谢谢

您可以使用过滤器来忽略不需要的交互。

下面是我从文档中修改的一个例子(它未经测试,请预先警告):

// make sure the interaction's message is the correct message
const filter = (interaction) => interaction.message.id === message.id;
const collector = message.createMessageComponentCollector({ filter, time: 15_000 });
collector.on('collect', i => console.log(`Collected ${i.customId}`));
collector.on('end', collected => console.log(`Collected ${collected.size} items`));

找到解决方案。这是错误的:

const message = await interaction.reply({ components: [row] });
const collector = message.createMessageComponentCollector({ComponentType: ComponentType.Button});

await interaction.reply({ components: [row] });
const message = interaction.fetchReply();
const collector = message.createMessageComponentCollector({ComponentType: ComponentType.Button});

运行正常

相关内容

  • 没有找到相关文章

最新更新