Trying to create a game lobby of sorts

Hi, I'm trying to create a simple game with a discord bot and I want to have it create a sort of "game lobby" when the start game command is run. I have gotten to the point where when I call the start command I create an embed which shows a list of joined players, and then there are two buttons, one to join the game and one to start the game. When join is clicked, it updates the embed and stores the joined player in a set. My difficulty is when I want to send a message to all the joined players when anyone clicks the start button. I want to send an ephemeral message to each player but I am not sure how to do this. I also want to pass the set of joined players onto the slash command handler to continue the game logic. Thanks!
4 Replies
d.js toolkit
d.js toolkit17mo 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!
xenoderf
xenoderfOP17mo ago
node v16.14.0, discord.js v14.12.1
const { ButtonInteraction } = require('discord.js')

const joinedPlayers = new Set();

module.exports = {
name: "interactionCreate",
/**
*
* @param {ButtonInteraction} interaction
*/
async execute(interaction) {
if (!interaction.isButton()) return;

const args = interaction.customId.split('_');
if (args[0] !== "begin") return;

const name = interaction.member.nickname;

switch (args[1]) {
case "join": {
//Add player to set of joined players, check if they are already joined
if (joinedPlayers.has(`${interaction.user.id}-${interaction.message.id}`)) {
return interaction.reply({content: "You already joined", ephemeral: true});
}
joinedPlayers.add(`${interaction.user.id}-${interaction.message.id}`);

console.log(joinedPlayers);

const beginEmbed = interaction.message.embeds[0];
if (!beginEmbed) {
return interaction.reply({content: "Unable to find poll embed", ephemeral: true});
}

const beginField = beginEmbed.fields[0];
const joinReply = "Successfully joined the game"

if (beginField.value == 'None') {
beginField.value = name;
} else {
beginField.value = beginField.value.concat(", ", name);
}

interaction.reply({content: joinReply, ephemeral: true});
interaction.message.edit({embeds: [beginEmbed]});
}
break;
case "start": {
//Start the game with players in set

}
break;
}


}
}
const { ButtonInteraction } = require('discord.js')

const joinedPlayers = new Set();

module.exports = {
name: "interactionCreate",
/**
*
* @param {ButtonInteraction} interaction
*/
async execute(interaction) {
if (!interaction.isButton()) return;

const args = interaction.customId.split('_');
if (args[0] !== "begin") return;

const name = interaction.member.nickname;

switch (args[1]) {
case "join": {
//Add player to set of joined players, check if they are already joined
if (joinedPlayers.has(`${interaction.user.id}-${interaction.message.id}`)) {
return interaction.reply({content: "You already joined", ephemeral: true});
}
joinedPlayers.add(`${interaction.user.id}-${interaction.message.id}`);

console.log(joinedPlayers);

const beginEmbed = interaction.message.embeds[0];
if (!beginEmbed) {
return interaction.reply({content: "Unable to find poll embed", ephemeral: true});
}

const beginField = beginEmbed.fields[0];
const joinReply = "Successfully joined the game"

if (beginField.value == 'None') {
beginField.value = name;
} else {
beginField.value = beginField.value.concat(", ", name);
}

interaction.reply({content: joinReply, ephemeral: true});
interaction.message.edit({embeds: [beginEmbed]});
}
break;
case "start": {
//Start the game with players in set

}
break;
}


}
}
grass
grass17mo ago
you cannot send an ephemeral message to someone without an interaction
xenoderf
xenoderfOP17mo ago
But there is an interaction right? because the players would all have hit join so is it possible to somehow send a follow up? Is it possible to follow up an earlier interaction when another unrelated interaction happens?
Want results from more Discord servers?
Add your server