TTastic
TTastic
DIAdiscord.js - Imagine an app
Created by TTastic on 9/11/2023 in #djs-questions
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.
30 replies
DIAdiscord.js - Imagine an app
Created by TTastic on 9/9/2023 in #djs-questions
Deferrals
I've had a few issues as I have started to try and learn deferring and avoiding timeout errors. Many scripts that worked before now only work half the time. Often I get the discord API error for 10062 which I understand means it usually took longer than 3 seconds to respond, then it times out. I also am self hosting this bot for testing purposes and have a less than optimal connection. However, all these commands worked fine previously so I want to make sure its not something in my amateur code. More than anything Im just wanting to verify whether or not Im approaching this correctly or if its a strange issues because of my subpar connection speeds. Ive been through the documentation and cant seem to find what I'm doing wrong, if anything at all. I've tried a few variations of things, sometimes with half sucess, but this is an example code thats having the problem right now and crashing for the api 10062 error:
20 replies