Issue understanding collector filters

Hi all, I was recently working on learning and integrating collector filters for my code so that if a user who didnt start the interaction tries to press a button reply, it wont work for them. I believe what I need is a collector filter. Based on the discord.js guide and documentation I tried to set one up like this:
//Define the collector for the button components
const collector = interaction.channel.createMessageComponentCollector({
componentType: ComponentType.BUTTON,
filter: (i) => i.user.id === interaction.user.id, // Filter so only person who used command can react
});

collector.on("collect", async (interaction) => {
const activityType = interaction.customId.replace(
"activity_",
""
);
const activityScript = activitiesData[activityType]?.buttonScript;

if (activityScript) {
try {
// Import and execute the corresponding activity script
const activityFunction = require(`../utils/activities/${activityScript}.js`);
await activityFunction(interaction);
} catch (error) {
console.error(
`Error executing activity script for ${activityType}: ${error}`
);
}
}
});
//Define the collector for the button components
const collector = interaction.channel.createMessageComponentCollector({
componentType: ComponentType.BUTTON,
filter: (i) => i.user.id === interaction.user.id, // Filter so only person who used command can react
});

collector.on("collect", async (interaction) => {
const activityType = interaction.customId.replace(
"activity_",
""
);
const activityScript = activitiesData[activityType]?.buttonScript;

if (activityScript) {
try {
// Import and execute the corresponding activity script
const activityFunction = require(`../utils/activities/${activityScript}.js`);
await activityFunction(interaction);
} catch (error) {
console.error(
`Error executing activity script for ${activityType}: ${error}`
);
}
}
});
The code runs as intended and I thought it was working fine, however when testing with a second person they were able to use the button responses. I can provide more code if needed but I think Im misunderstanding a fundamental part of filters.
20 Replies
d.js toolkit
d.js toolkit15mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
chewie
chewie15mo ago
that collector is 100% not running for other users
TTastic
TTasticOP15mo ago
The collector is on the main script for a slash command, I ran the command myself and the buttons appeared. I have someone next to me in real life click one of the buttons on my interaction on their discord account and it ran for them sucessfully even though it was my accounts interaction.
treble/luna
treble/luna15mo ago
that componenttype is incorrect
d.js docs
d.js docs15mo ago
RangeError [BitFieldInvalid]: Invalid bitfield flag or number: undefined - All SCREAMING_SNAKE_CASE enums have been changed to PascalCase - Intents: Intents.FLAGS.GUILD_MESSAGES -> GatewayIntentBits.GuildMessages - Permissions: Permissions.FLAGS.SEND_MESSAGES -> PermissionFlagsBits.SendMessages If you are waiting for button or select menu input from a specific message, don't create the collector on the channel. - Channel collectors return component interactions for any component within that channel.
- <Channel>.createMessageComponentCollector(…)
+ <Message>.createMessageComponentCollector(…)
- <Channel>.createMessageComponentCollector(…)
+ <Message>.createMessageComponentCollector(…)
TTastic
TTasticOP15mo ago
Are you saying I should replace to = interaction.message.createMessageComponentCollector ?
const collector = interaction.channel.createMessageComponentCollector
const collector = interaction.channel.createMessageComponentCollector
Or do you mean the componenttype where I have the button? Sorry, just trying to clarify what you mean. It seems I should have the collector be only for the interaction and not the channel, but if I change it to interaction.message I start getting typeerrors.
treble/luna
treble/luna15mo ago
attach it to the message your buttons are attached to
TTastic
TTasticOP15mo ago
Oh wait ok, sorry, I'm self taught and really bad with terminology. Based on what you said I assume that line means I'm attaching the collector to the interactions channel and not to the interaction message itself and that's why others can respond? And by setting the collector instead to the actual interaction message it'll achieve the desired effect? Sorry, just wanting to make sure I understand the logic so I can stop myself from making the mistake in the future.
treble/luna
treble/luna15mo ago
not sure why its responding but it might fix it also note the other thing i said, its not BUTTON, its Button
TTastic
TTasticOP15mo ago
I kept it as interaction.channel.createMessageComponentCollector and ONLY changed BUTTON to Button...that did the trick! I'm getting some other unrelated error now but I can solve that one. Thanks for the help 🙂 Actually did not solve, seemed to at first. But I will keep as 'Button' and attempt to attach the collector instead to the message as recommended. As far as I'm aware my code already should be attaching the collector directly to the interaction, so
const collector = interaction.channel.createMessageComponentCollector
const collector = interaction.channel.createMessageComponentCollector
If thats only attaching it to the channel the buttons are in, whats the correct way to do it? I'm not logging any errors when it runs as posted, but others are able to respond regardless of the filter.
treble/luna
treble/luna15mo ago
attach it to the message the buttons are attached to
TTastic
TTasticOP15mo ago
I understand you're saying to do that - I'm not sure how. From what I've read even after your reply, I'm still of the mind that my code should already be attaching it directly to the interaction message. Obviously that's not working, so I'm trying again to re-read the documentation.
treble/luna
treble/luna15mo ago
show your full code because now you're attaching to the channel, not the message
TTastic
TTasticOP15mo ago
This is the full code, relevant collector lines start at around line 85
TTastic
TTasticOP15mo ago
Should I store the editReply message in a variable and set collector to that?
treble/luna
treble/luna15mo ago
yep also that code still has BUTTON
TTastic
TTasticOP15mo ago
Sorry, ctrl+z'd a bit much. Was setting up to store message. One sec.
// Respond with the location info embed and buttons
await interaction.editReply({
embeds: [locationEmbed],
components: [rowActivities],
});


const locationMessage = await interaction.editReply({
embeds: [locationEmbed],
components: [rowActivities],
});


//Define the collector for the button components
const collector = locationMessage.createMessageComponentCollector({
componentType: ComponentType.Button,
filter: (i) => i.user.id === interaction.user.id, // Filter so only the person who used the command can react
});
// Respond with the location info embed and buttons
await interaction.editReply({
embeds: [locationEmbed],
components: [rowActivities],
});


const locationMessage = await interaction.editReply({
embeds: [locationEmbed],
components: [rowActivities],
});


//Define the collector for the button components
const collector = locationMessage.createMessageComponentCollector({
componentType: ComponentType.Button,
filter: (i) => i.user.id === interaction.user.id, // Filter so only the person who used the command can react
});
Does that look correct for setting it to the message as opposed to channel?
treble/luna
treble/luna15mo ago
that looks good
TTastic
TTasticOP15mo ago
I would assume, just looks too easy! lol. Thanks, gonna test it thouroughly here. Tested with 4 accounts this time. Seems to have worked nicely! Thank you again @wolvinny🌈 for being patient with me. I understand what I messed up well enough to hopefully avoid it in the future! 🙂
treble/luna
treble/luna15mo ago
awesome!
Want results from more Discord servers?
Add your server