Vote Button Update Logic

Hey, I'm working on a bot that implements mafia game mechanics, and I'm facing a challenge with updating the dynamic count emojis on voting buttons during the mafia vote phase. In the mafiaVote function, I dynamically create buttons for each non-mafia player, where the emoji on each button represents the current vote count. The logic works well for individual votes, but I'm struggling with updating the interaction for all mafia players after each mafia vote. Currently when a mafia player votes for a certain player, the vote counter changes only for the player who voted, but does not change for the rest of the players, except when the other player's votes The code:
async function mafiaVote(interactionCollector, interaction) {
const mafiaPlayers = players.filter((p) => roles[p] === ROLE_TYPES.MAFIA);
let voteCounts = {};

await interaction.channel.send("Mafia are selecting a player to eliminate.");

for (const player of players) {
voteCounts[player] = 0;
}

async function createButton(player) {
const voteCount = voteCounts[player];
const emoji = emojiMap[voteCount] || "šŸ”Ÿ";
return new ButtonBuilder()
.setCustomId(player)
.setLabel(await getUsernameById(interaction, player))
.setStyle(ButtonStyle.Primary)
.setDisabled(false)
.setEmoji(emoji);
}

let row = new ActionRowBuilder();

for (const player of players) {
if (!mafiaPlayers.includes(player)) {
const button = await createButton(player);
row.addComponents(button);
}
}

for (const [id, interaction] of interactionCollector) {
if (interaction && interaction.user) {
const playerRole = roles[interaction.user.id];
if (playerRole === ROLE_TYPES.MAFIA) {
await interaction.followUp({
content: `Mafia, who do you want to eliminate?`,
components: [row],
ephemeral: true,
});
}
}
}

const voteCollector = interaction.channel.createMessageComponentCollector({
filter: (i) => mafiaPlayers.includes(i.user.id),
time: 10000,
});

voteCollector.on("collect", async (i) => {
const targetId = i.customId;
voteCounts[targetId] += 1;

let updatedRow = new ActionRowBuilder();
for (const player of players) {
if (!mafiaPlayers.includes(player)) {
const button = await createButton(player);
updatedRow.addComponents(button);
}
}

// Problem: Update the interaction with the new button state to all the mafia players using for-of loop
try {
await i.update({ components: [updatedRow] });
} catch (error) {
console.error("Failed to edit message:", error);
}
});
}
async function mafiaVote(interactionCollector, interaction) {
const mafiaPlayers = players.filter((p) => roles[p] === ROLE_TYPES.MAFIA);
let voteCounts = {};

await interaction.channel.send("Mafia are selecting a player to eliminate.");

for (const player of players) {
voteCounts[player] = 0;
}

async function createButton(player) {
const voteCount = voteCounts[player];
const emoji = emojiMap[voteCount] || "šŸ”Ÿ";
return new ButtonBuilder()
.setCustomId(player)
.setLabel(await getUsernameById(interaction, player))
.setStyle(ButtonStyle.Primary)
.setDisabled(false)
.setEmoji(emoji);
}

let row = new ActionRowBuilder();

for (const player of players) {
if (!mafiaPlayers.includes(player)) {
const button = await createButton(player);
row.addComponents(button);
}
}

for (const [id, interaction] of interactionCollector) {
if (interaction && interaction.user) {
const playerRole = roles[interaction.user.id];
if (playerRole === ROLE_TYPES.MAFIA) {
await interaction.followUp({
content: `Mafia, who do you want to eliminate?`,
components: [row],
ephemeral: true,
});
}
}
}

const voteCollector = interaction.channel.createMessageComponentCollector({
filter: (i) => mafiaPlayers.includes(i.user.id),
time: 10000,
});

voteCollector.on("collect", async (i) => {
const targetId = i.customId;
voteCounts[targetId] += 1;

let updatedRow = new ActionRowBuilder();
for (const player of players) {
if (!mafiaPlayers.includes(player)) {
const button = await createButton(player);
updatedRow.addComponents(button);
}
}

// Problem: Update the interaction with the new button state to all the mafia players using for-of loop
try {
await i.update({ components: [updatedRow] });
} catch (error) {
console.error("Failed to edit message:", error);
}
});
}
4 Replies
d.js toolkit
d.js toolkitā€¢4mo 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!
pat
patā€¢4mo ago
get the id from the followUp and store it then try (if it exists) edit it with editReply({message: id, content, buttons }) should work
怎 šŸ‡©šŸ‡æ | š• - Kš•€š•ƒŁ š•ƒš”¼R 怏
i have been thinking about this idea, i will give it a try.
Want results from more Discord servers?
Add your server