'Discord button interaction lets everyone interact with it
I'm trying to code a tic tac toe command for my Discord bot, and I want that only the creator of the interaction and the tagged user can play, so that everyone else doesn't interrupt it, I tried by making a filter like so:
const taggedUser = interaction.options.get("user").id;
const filter = (ButtonInteraction) => {
if (ButtonInteraction.user.id === interaction.user.id ||
ButtonInteraction.user.id === taggedUser) return true;
else return interaction.followUp({content: `${ButtonInteraction.user} this is not your match! >:(`, ephemeral: true});
}
but when the command gets executed, and neither the interaction author or the tagged user tries to interact, the interaction is successful, and I recieve the message in ephemeral form, what can I do? (also suggestion for coding the tictactoe command are appreciated :3). Here's the full code:
const { SlashCommandBuilder } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()
.setName("tictactoe")
.setDescription("Play a game of tic tac toe with your friends!")
.addUserOption(option =>
option.setName("user")
.setDescription("The user you wanna play with.")
.setRequired(true)),
async execute(client, interaction, Discord) {
await interaction.deferReply();
const taggedUser = interaction.options.get("user").id;
let grid = [
new Discord.MessageActionRow().addComponents([
new Discord.MessageButton().setCustomId("1").setLabel(" ").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("2").setLabel(" ").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("3").setLabel(" ").setStyle("SECONDARY"),
]),
new Discord.MessageActionRow().addComponents([
new Discord.MessageButton().setCustomId("4").setLabel(" ").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("5").setLabel(" ").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("6").setLabel(" ").setStyle("SECONDARY"),
]),
new Discord.MessageActionRow().addComponents([
new Discord.MessageButton().setCustomId("7").setLabel(" ").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("8").setLabel(" ").setStyle("SECONDARY"),
new Discord.MessageButton().setCustomId("9").setLabel(" ").setStyle("SECONDARY"),
])
];
await interaction.editReply({content: `Your turn, ${interaction.user}! (X)`, components: grid});
const filter = (ButtonInteraction) => {
if (ButtonInteraction.user.id === interaction.user.id ||
ButtonInteraction.user.id === taggedUser) return true;
else return interaction.followUp({content: `${ButtonInteraction.user} this is not your match! >:(`, ephemeral: true});
}
let collector = interaction.channel.createMessageComponentCollector({ filter, max: 9 });
collector.on("collect", async interaction => {
switch (interaction.customId) {
case "1":
let row = grid[0].components[0];
if (interaction.user.id === taggedUser) row.label = "O";
else row.label = "X";
row.disabled = true
await interaction.update({components: grid})
break;
}
});
}
}
Thank you!
Solution 1:[1]
Try to to this:
const filter = ({ user }) => { [...]
and remove "ButtonInteraction" from the if statements inside the filter.
Let me know if it worked, or i will remove this answer!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vxrious |
