130 lines
4.3 KiB
JavaScript
130 lines
4.3 KiB
JavaScript
const {
|
|
Client,
|
|
Events,
|
|
GatewayIntentBits,
|
|
ModalBuilder,
|
|
TextInputBuilder,
|
|
ActionRowBuilder,
|
|
EmbedBuilder,
|
|
Colors, ButtonBuilder
|
|
} = require('discord.js');
|
|
const {DATABASE_CLIENT, insertPollIntoDB} = require('./database');
|
|
const {TOKEN} = require('../config.json');
|
|
const {v4: uuidv4} = require('uuid');
|
|
const {POLL_COMMAND_NAME} = require("./constants");
|
|
const {ButtonStyle} = require("discord-api-types/v10");
|
|
|
|
const client = new Client({intents: [GatewayIntentBits.Guilds]});
|
|
|
|
client.once(Events.ClientReady, async readyClient => {
|
|
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
|
await DATABASE_CLIENT.connect();
|
|
});
|
|
|
|
client.on(Events.InteractionCreate, async interaction => {
|
|
if (interaction.isCommand()) {
|
|
const {commandName} = interaction;
|
|
|
|
if (commandName === POLL_COMMAND_NAME) {
|
|
await interaction.showModal(buildPollModal());
|
|
}
|
|
} else if (interaction.isModalSubmit()) {
|
|
const pollToCreate = interaction.components;
|
|
const sender = interaction.user;
|
|
const guildFromWhereUserSendedInteraction = interaction.member.guild;
|
|
const pollQuestionObj = pollToCreate[0].components[0];
|
|
let voteOptions = [
|
|
pollToCreate[1].components[0].value,
|
|
pollToCreate[2].components[0].value,
|
|
pollToCreate[3].components[0].value,
|
|
pollToCreate[4].components[0].value
|
|
];
|
|
|
|
voteOptions = [...new Set(voteOptions)].filter(voteOption => voteOption !== '');
|
|
const introEmbed = new EmbedBuilder()
|
|
.setTitle("Les paris sont ouverts !")
|
|
.setDescription("Vous pouvez parier vos points, ils ne passeront pas en dessous de 50")
|
|
.setColor(Colors.Aqua);
|
|
const buttons = new ActionRowBuilder();
|
|
|
|
let pollDesc = '';
|
|
voteOptions.forEach((voteOption, index) => {
|
|
buttons.addComponents(new ButtonBuilder()
|
|
.setCustomId(`${interaction.customId}_${voteOption}_${index + 1}`)
|
|
.setLabel(voteOption)
|
|
.setStyle(ButtonStyle.Primary));
|
|
pollDesc += `Option ${index + 1}: ${voteOption}\n`
|
|
});
|
|
|
|
const pollEmbed = new EmbedBuilder()
|
|
.setTitle(`Sondage: ${pollQuestionObj.value}`)
|
|
.setDescription(pollDesc)
|
|
.setColor(Colors.Green);
|
|
|
|
insertPollIntoDB(sender.id, interaction.customId, guildFromWhereUserSendedInteraction.id, pollQuestionObj.value);
|
|
|
|
await interaction.reply({embeds: [introEmbed, pollEmbed], components: [buttons]});
|
|
}
|
|
|
|
});
|
|
|
|
function buildPollModal() {
|
|
const pollId = uuidv4();
|
|
|
|
const pollModal = new ModalBuilder()
|
|
.setTitle("Créer un vote")
|
|
.setCustomId(pollId)
|
|
|
|
const questionRow = new ActionRowBuilder().addComponents(
|
|
new TextInputBuilder()
|
|
.setCustomId("label")
|
|
.setLabel("Question du vote")
|
|
.setPlaceholder("Shy aime-t-il les robots?")
|
|
.setStyle(1)
|
|
.setMinLength(1)
|
|
.setRequired(true)
|
|
);
|
|
|
|
const firstOption = new ActionRowBuilder().addComponents(
|
|
new TextInputBuilder()
|
|
.setCustomId("vote1")
|
|
.setLabel("Première option")
|
|
.setPlaceholder("Oui")
|
|
.setStyle(1)
|
|
.setMinLength(1)
|
|
.setRequired(true)
|
|
);
|
|
|
|
const secondOption = new ActionRowBuilder().addComponents(
|
|
new TextInputBuilder()
|
|
.setCustomId("vote2")
|
|
.setLabel("Deuxième option")
|
|
.setPlaceholder("Non")
|
|
.setStyle(1)
|
|
.setMinLength(1)
|
|
.setRequired(false)
|
|
);
|
|
const thirdOption = new ActionRowBuilder().addComponents(
|
|
new TextInputBuilder()
|
|
.setCustomId("vote3")
|
|
.setLabel("Troisième option")
|
|
.setPlaceholder("Peut-être")
|
|
.setStyle(1)
|
|
.setMinLength(1)
|
|
.setRequired(false)
|
|
);
|
|
const fourthOption = new ActionRowBuilder().addComponents(
|
|
new TextInputBuilder()
|
|
.setCustomId("vote4")
|
|
.setLabel("Quatrième option")
|
|
.setPlaceholder("Pas du tout")
|
|
.setStyle(1)
|
|
.setMinLength(1)
|
|
.setRequired(false)
|
|
);
|
|
|
|
return pollModal.addComponents(questionRow, firstOption, secondOption, thirdOption, fourthOption);
|
|
}
|
|
|
|
client.login(TOKEN);
|