UNPKG

discord-ftd

Version:

Discord - FTD (Fun Time Discord) is an NPM module packed with exciting Discord games for bots.

175 lines (133 loc) 6.93 kB
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder } = require('discord.js'); const { performChecks } = require('../functions/check'); class WouldYouRather { static gameRunning = false; constructor(){ this.votes = new Map(); this.i = null; } // Function to create a progress bar based on vote counts createProgressBar(count1, count2, total) { const percentage1 = total === 0 ? 0 : (count1 / total) * 100; const percentage2 = total === 0 ? 0 : (count2 / total) * 100; const progressLength = 20; const progressChars1 = '■'.repeat(Math.round((percentage1 / 100) * progressLength)); const progressChars2 = '■'.repeat(Math.round((percentage2 / 100) * progressLength)); const emptyChars1 = '□'.repeat(progressLength - progressChars1.length); const emptyChars2 = '□'.repeat(progressLength - progressChars2.length); const progressBar1 = `${progressChars1}${emptyChars1} **${percentage1.toFixed(2)}%**\n`; const progressBar2 = `${progressChars2}${emptyChars2} **${percentage2.toFixed(2)}%**\n`; return { option1: progressBar1, option2: progressBar2 }; } // Function to perform checks on command options async startGame(options) { const { interaction, question, option1, option2, time, embedColor, embedTitle, buttonStyle, sendChannel } = options; // Check if a game is already running if (this.gameRunning) { if (interaction.deferred) { return interaction.followUp({ content: 'A game is already running!', ephemeral: true }); } else { return interaction.reply({ content: 'A game is already running!', ephemeral: true }); } } try { // Perform checks on command options performChecks(options); } catch (error) { return interaction.reply({ content: error.message, ephemeral: true }); } // Start the game await interaction.reply({ content: 'Game started!', ephemeral: true }); const timeInMs = time * 60000; const progressBar = this.createProgressBar(0, 0, 0); // Customize the appearance of the embed const embed = new EmbedBuilder() .setTitle(embedTitle || 'Would You Rather?') .setDescription(`${question}\nHosted By: <@${interaction.user.id}>\n`) .setColor(embedColor || '#80b918') .addFields({ name: 'Option 1', value: progressBar.option1, inline: false }, { name: 'Option 2', value: progressBar.option2, inline: false }); // Customize the appearance of the buttons const button1 = new ButtonBuilder() .setLabel(option1) .setCustomId('wyr_option_1') .setStyle(buttonStyle || '3') .setDisabled(false); const button2 = new ButtonBuilder() .setLabel(option2) .setCustomId('wyr_option_2') .setStyle(buttonStyle || '3') .setDisabled(false); // Set up the ActionRow with customized buttons const buttons = new ActionRowBuilder().addComponents(button1, button2); const channel = sendChannel ? interaction.guild.channels.cache.get(sendChannel) : interaction.channel; // Send the embed and buttons to the channel this.i = await channel.send({ embeds: [embed], components: [buttons] }); this.gameRunning = true; // Set up a collector to handle button interactions const collector = interaction.channel.createMessageComponentCollector({ filter: (btnInteraction) => btnInteraction.isButton() && btnInteraction.customId.startsWith('wyr_option'), time: timeInMs, }); collector.on('collect', async (btnInteraction) => { const userId = btnInteraction.user.id; const selectedOption = btnInteraction.customId.split('_')[2]; this.votes.set(userId, selectedOption); const option1Count = Array.from(this.votes.values()).filter(choice => choice === '1').length; const option2Count = Array.from(this.votes.values()).filter(choice => choice === '2').length; const totalVotes = this.votes.size; const progressBar = this.createProgressBar(option1Count, option2Count, totalVotes); const embed = new EmbedBuilder() .setTitle(embedTitle || 'Would You Rather?') .setDescription(`${options.question}\nHosted By: <@${interaction.user.id}>\n`) .setColor(embedColor || '#80b918') .addFields({ name: 'Option 1', value: progressBar.option1, inline: false }, { name: 'Option 2', value: progressBar.option2, inline: false }); await btnInteraction.update({ embeds: [embed], components: [buttons] }); }); collector.on('end', async () => { const totalVotes = this.votes.size; const option1Votes = Array.from(this.votes.values()).filter(vote => vote === '1').length; const option2Votes = totalVotes - option1Votes; const winner = option1Votes > option2Votes ? options.option1 : options.option2; const bar = this.createProgressBar(option1Votes, option2Votes, totalVotes); const resultsEmbed = new EmbedBuilder() .setTitle(`**Results!**`) .setColor('#dbb42c') .setDescription(`The winner is: **${winner}**\n\n**Votes:**\n${options.option1}: ${bar.option1}\n${options.option2}: ${bar.option2}`); await interaction.editReply({ embeds: [resultsEmbed], components: [] }); }); } async endGame(interaction) { // Check if a game is running if (!this.gameRunning) { return interaction.reply({ content: 'No game is currently running!', ephemeral: true }); } // Set gameRunning flag to false this.gameRunning = false; // Get the total number of votes const totalVotes = this.votes.size; // Get the number of votes for each option const option1Votes = Array.from(this.votes.values()).filter(vote => vote === '1').length; const option2Votes = totalVotes - option1Votes; // Determine the winner based on vote counts var winner = option1Votes > option2Votes ? 'Option 1' : 'Option 2'; if (option1Votes === option2Votes) { winner = 'Tie'; } else if (totalVotes === 0) { winner = 'No votes'; } // Create progress bars for each option const progressBar = this.createProgressBar(option1Votes, option2Votes, totalVotes); // Create an embed to display the results const embed = new EmbedBuilder() .setTitle('Game Ended') .setDescription(`The game has ended!\n\n**Winner:** ${winner}\n\n**Votes:**\nOption 1: ${progressBar.option1}\nOption 2: ${progressBar.option2}`) .setColor('#42f56c'); // Edit the initial message with the results await this.i.edit({ embeds: [embed], components: [] }); await interaction.reply({ content: 'Game ended!', ephemeral: true }); } } module.exports = { WouldYouRather };