Buttons

I made a team for voting, it works. I made the code to make the buttons work - they don't work Command
const { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('poll')
.setDescription('Создать голосование')
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addStringOption(options => options
.setName("question")
.setDescription("Укажите вопрос")
.setRequired(true)
),
/**
*
* @param {ChatInputCommandInteraction} interaction
*/
async execute(interaction) {
const pollQuestion = interaction.options.getString("question");

const pollEmbed = new EmbedBuilder()
.setTitle("Fury Голосование 📢")
.setDescription("**Вопрос:**\n" + pollQuestion)
.setImage("https://i.postimg.cc/jdZpyQ90/Line-1123.png")
.addFields([
{name: "Да", value: "0", inline: true},
{name: "Нет", value: "0", inline: true}
])
.setColor('Orange');

const replyObject = await interaction.reply({embeds: [pollEmbed], fetchReply: true});

const pollButtons = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setLabel('Yes')
.setCustomId(`Poll-Yes-${replyObject.id}`)
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setLabel('No')
.setCustomId(`Poll-No-${replyObject.id}`)
.setStyle(ButtonStyle.Danger)
)

interaction.editReply({components: [pollButtons]});
}
}
const { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('poll')
.setDescription('Создать голосование')
.setDMPermission(false)
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.addStringOption(options => options
.setName("question")
.setDescription("Укажите вопрос")
.setRequired(true)
),
/**
*
* @param {ChatInputCommandInteraction} interaction
*/
async execute(interaction) {
const pollQuestion = interaction.options.getString("question");

const pollEmbed = new EmbedBuilder()
.setTitle("Fury Голосование 📢")
.setDescription("**Вопрос:**\n" + pollQuestion)
.setImage("https://i.postimg.cc/jdZpyQ90/Line-1123.png")
.addFields([
{name: "Да", value: "0", inline: true},
{name: "Нет", value: "0", inline: true}
])
.setColor('Orange');

const replyObject = await interaction.reply({embeds: [pollEmbed], fetchReply: true});

const pollButtons = new ActionRowBuilder()
.addComponents(
new ButtonBuilder()
.setLabel('Yes')
.setCustomId(`Poll-Yes-${replyObject.id}`)
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setLabel('No')
.setCustomId(`Poll-No-${replyObject.id}`)
.setStyle(ButtonStyle.Danger)
)

interaction.editReply({components: [pollButtons]});
}
}
2 Replies
d.js toolkit
d.js toolkit16mo 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!
Обкуренный
const { ButtonInteraction } = require("discord.js");

const votedMembers = new Set();

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

const splittedArray = interaction.customId.split('-');
if(splittedArray[0] !== "Poll") return;

if(votedMembers.has(`${interaction.user.id}-${interaction.message.id}`))
return interaction.reply({content: "Вы уже проголосовали!", ephemeral: true});

votedMembers.add(`${interaction.user.id}-${interaction.message.id}`);

const pollEmbed = interaction.message.embeds[0];
if(!pollEmbed) return interaction.reply({
content: "",
ephemeral: true
});

const yesField = pollEmbed.fields[0];
const noField = pollEmbed.fields[1];

const VoteCountedReply = "Ваш голос был успешно учтён!";

switch(splittedArray[1]) {
case "Yes" : {
const newYesCount = parseInt(yesField.value) + 1;
yesField.value = newYesCount;

interaction.reply({content: VoteCountedReply, ephemeral: true});
interaction.message.edit({embeds: [pollEmbed]});
}
break;
case "No": {
const newNoCount = parseInt(noField.value) + 1;
noField.value = newNoCount;

interaction.reply({content: VoteCountedReply, ephemeral: true});
interaction.message.edit({embeds: [pollEmbed]});
}
break;
}
}
}
const { ButtonInteraction } = require("discord.js");

const votedMembers = new Set();

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

const splittedArray = interaction.customId.split('-');
if(splittedArray[0] !== "Poll") return;

if(votedMembers.has(`${interaction.user.id}-${interaction.message.id}`))
return interaction.reply({content: "Вы уже проголосовали!", ephemeral: true});

votedMembers.add(`${interaction.user.id}-${interaction.message.id}`);

const pollEmbed = interaction.message.embeds[0];
if(!pollEmbed) return interaction.reply({
content: "",
ephemeral: true
});

const yesField = pollEmbed.fields[0];
const noField = pollEmbed.fields[1];

const VoteCountedReply = "Ваш голос был успешно учтён!";

switch(splittedArray[1]) {
case "Yes" : {
const newYesCount = parseInt(yesField.value) + 1;
yesField.value = newYesCount;

interaction.reply({content: VoteCountedReply, ephemeral: true});
interaction.message.edit({embeds: [pollEmbed]});
}
break;
case "No": {
const newNoCount = parseInt(noField.value) + 1;
noField.value = newNoCount;

interaction.reply({content: VoteCountedReply, ephemeral: true});
interaction.message.edit({embeds: [pollEmbed]});
}
break;
}
}
}
There are no errors in the console
Want results from more Discord servers?
Add your server