discord-ftd
Version:
Discord - FTD (Fun Time Discord) is an NPM module packed with exciting Discord games for bots.
96 lines (75 loc) • 3.08 kB
JavaScript
const { ActionRowBuilder, ButtonBuilder, EmbedBuilder, ModalBuilder, TextInputBuilder, TextInputStyle, Collector } = require('discord.js');
const words = require('./wordleWords.js'); // Assuming you have a list of words in this file
class WordleGame {
constructor() {
this.rightWord = words[Math.floor(Math.random() * words.length)];
this.gameEnded = false;
this.guesses = [];
}
startGame(interaction, guess) {
this.interaction = interaction;
this.guess = guess;
const embed = this.createEmbed();
const button = this.createButton();
const row = new ActionRowBuilder().addComponents(button);
interaction.reply({ embeds: [embed], components: [row] });
const modal = this.createModal();
const filter = (interaction) => interaction.customId === 'guess' && interaction.user.id === this.interaction.user.id;
const collector = interaction.createMessageComponentCollector({ filter, time: 60000 });
collector.on('collect', async (interaction) => {
if (interaction.customId === 'guess') {
await interaction.deferUpdate();
await interaction.showModal(modal);
}
});
collector.on('end', async (collected) => {
if (collected.size === 0) {
this.gameEnded = true;
return interaction.reply(`You ran out of time! The word was ${this.rightWord}`);
}
});
interaction.awaitModalSubmit({ filter: (interaction) => interaction.customId === 'guess' && interaction.user.id === this.interaction.user.id, time: 60000 })
.then(async (interaction) => {
const guess = interaction.fields.getTextInputValue('guessInput');
if (guess.toLowerCase() === this.rightWord.toLowerCase()) {
this.gameEnded = true;
await interaction.reply(`You guessed the word! The word was ${this.rightWord}`);
} else {
this.guesses.push(guess);
await interaction.reply({ embeds: [this.createEmbed()], components: [this.createButton()] });
}
})
}
createEmbed() {
const color = this.gameEnded ? 'Green' : 'Red';
const title = this.gameEnded ? 'Wordle (Ended)' : 'Wordle (In Progress)';
const embed = new EmbedBuilder()
.setColor(color)
.setTitle(title)
.setDescription(`Guesses: ${this.guesses.join(', ')}`);
return embed;
}
createButton() {
const button = new ButtonBuilder()
.setCustomId('guess')
.setLabel('Guess')
.setStyle('3')
.setDisabled(this.gameEnded);
return button;
}
createModal() {
const modal = new ModalBuilder()
.setTitle('Guess the word')
.setCustomId('guess')
const input = new TextInputBuilder()
.setCustomId('guessInput')
.setPlaceholder('Enter your guess')
.setMinLength(1)
.setMaxLength(10)
.setStyle(TextInputStyle.Short)
.setRequired(true);
modal.addComponents(input);
return modal;
}
}
module.exports = {WordleGame};