Zela
Zela
DIAdiscord.js - Imagine an app
Created by Zela on 3/16/2024 in #djs-questions
Looping through an array getting input each iteration
I'm trying to loop through an array to get input each iteration of the loop. The issue I'm running into right now is the loop goes right to the end. How can I delay this so that I can get the input at each step of the way rather than instantly jumping to the end? My code is:
await interaction.reply(`The following players rolled the same highest number and need to select a swing: ${toFix.join(", ")}`);
toFix.forEach(async player => {
buttons = [];
indexes = [];
battleState[player].rolls.forEach((roll, i) => {
if (roll === Math.max(...battleState[player].rolls)) {
indexes.push(i);
buttons.push(new ButtonBuilder().setLabel(battleState[player].colors[i]).setStyle(ButtonStyle.Primary).setCustomId(`tie-${i}`));
}
});
row = new ActionRowBuilder().addComponents(...buttons);
response = await interaction.editReply({content : `${player} rolled ${battleState[player].rolls.map((y, i) => `\`${battleState[player].colors[i]}: ${y}\``).join(", ")}\nSelect one of the following:`,
components: [row],
});

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

collector = response.createMessageComponentCollector({
componentType: ComponentType.Button,
filter,
time: 15_000,
});

collector.on('collect', async (i) => {
console.log(i.customId);
index = i.customId.split("-")[1];
battleState[player].swing = [battleState[player].colors[index], battleState[player].rolls[index]];
fs.writeFileSync(`./battles/${interaction.channel.id}.json`, JSON.stringify(battleState), { flag: 'w' }, function(err) {
if (err)
return console.error(err);
});
interaction.editReply({content:`${player} rolled ${battleState[player].rolls.map((y, i) => `\`${battleState[player].colors[i]}: ${y}\``).join(", ")} and was added to the battle with a swing of \`${battleState[player].swing[0]}: ${battleState[player].swing[1]}\``, components: []});
collector.stop();
});

collector.on('end', async (collected, reason) => {
if (reason === 'time') {
index = indexes[0];
battleState[player].swing = [battleState[player].colors[index], battleState[player].rolls[index]];
fs.writeFileSync(`./battles/${interaction.channel.id}.json`, JSON.stringify(battleState), { flag: 'w' }, function(err) {
if (err)
return console.error(err);
});
interaction.editReply({content:`Time is up, ${player} rolled ${battleState[player].rolls.map((y, i) => `\`${battleState[player].colors[i]}: ${y}\``).join(", ")} and has been added to the battle with their swing set to \`${battleState[player].swing[0]}: ${battleState[player].swing[1]}\``,components: []});
}
});
});
await interaction.reply(`The following players rolled the same highest number and need to select a swing: ${toFix.join(", ")}`);
toFix.forEach(async player => {
buttons = [];
indexes = [];
battleState[player].rolls.forEach((roll, i) => {
if (roll === Math.max(...battleState[player].rolls)) {
indexes.push(i);
buttons.push(new ButtonBuilder().setLabel(battleState[player].colors[i]).setStyle(ButtonStyle.Primary).setCustomId(`tie-${i}`));
}
});
row = new ActionRowBuilder().addComponents(...buttons);
response = await interaction.editReply({content : `${player} rolled ${battleState[player].rolls.map((y, i) => `\`${battleState[player].colors[i]}: ${y}\``).join(", ")}\nSelect one of the following:`,
components: [row],
});

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

collector = response.createMessageComponentCollector({
componentType: ComponentType.Button,
filter,
time: 15_000,
});

collector.on('collect', async (i) => {
console.log(i.customId);
index = i.customId.split("-")[1];
battleState[player].swing = [battleState[player].colors[index], battleState[player].rolls[index]];
fs.writeFileSync(`./battles/${interaction.channel.id}.json`, JSON.stringify(battleState), { flag: 'w' }, function(err) {
if (err)
return console.error(err);
});
interaction.editReply({content:`${player} rolled ${battleState[player].rolls.map((y, i) => `\`${battleState[player].colors[i]}: ${y}\``).join(", ")} and was added to the battle with a swing of \`${battleState[player].swing[0]}: ${battleState[player].swing[1]}\``, components: []});
collector.stop();
});

collector.on('end', async (collected, reason) => {
if (reason === 'time') {
index = indexes[0];
battleState[player].swing = [battleState[player].colors[index], battleState[player].rolls[index]];
fs.writeFileSync(`./battles/${interaction.channel.id}.json`, JSON.stringify(battleState), { flag: 'w' }, function(err) {
if (err)
return console.error(err);
});
interaction.editReply({content:`Time is up, ${player} rolled ${battleState[player].rolls.map((y, i) => `\`${battleState[player].colors[i]}: ${y}\``).join(", ")} and has been added to the battle with their swing set to \`${battleState[player].swing[0]}: ${battleState[player].swing[1]}\``,components: []});
}
});
});
As of now, when I input 2 players, foo and bar, in the array in order, the message automatically skips to bar and whichever button I click for bar is updated in by battleState for foo as well.
25 replies