Ferretsroq
Ferretsroq
DIAdiscord.js - Imagine an app
Created by Ferretsroq on 2/7/2024 in #djs-questions
Avoiding Overwriting Variables in Collectors (14.14.1)
When using collectors, what is the best way to preserve a variable determined in one interaction for use in a future interaction on the same message? For example, I'm making an rpg bot that first asks which stat you're rolling with, and then which modifiers apply. If I assign the stat value to a variable, that variable gets overwritten if a second person uses the command before the first person finishes, and they both wind up rolling whichever stat was selected second. What is the best way to tie the stat to the specific message being interacted with, such that no overwriting occurs when multiple people are using the same command at the same time? Code snippet:
const reply = await interaction.reply({content: 'Choose a stat to roll with!', components: [buttonRow0]});

const filter = (i) => i.user.id === interaction.member.id;

const collector = reply.createMessageComponentCollector({
componentType: ComponentType.Button,
filter
});

stat = ''; // Unsure if I should initialize stat here or elsewhere, right now it's being overwritten
collector.on('collect', async (interaction) => {
if(interaction.customId.startsWith('rollStat')) {
stat = await this.CheckTalents(interaction); // Method that edits message to check for modifiers
return;
}
else if(interaction.customId.startsWith('rollTalent'))
{
await this.Roll(interaction, stat); // Final rolling with stat and modifiers
return;
}
});
const reply = await interaction.reply({content: 'Choose a stat to roll with!', components: [buttonRow0]});

const filter = (i) => i.user.id === interaction.member.id;

const collector = reply.createMessageComponentCollector({
componentType: ComponentType.Button,
filter
});

stat = ''; // Unsure if I should initialize stat here or elsewhere, right now it's being overwritten
collector.on('collect', async (interaction) => {
if(interaction.customId.startsWith('rollStat')) {
stat = await this.CheckTalents(interaction); // Method that edits message to check for modifiers
return;
}
else if(interaction.customId.startsWith('rollTalent'))
{
await this.Roll(interaction, stat); // Final rolling with stat and modifiers
return;
}
});
4 replies