UNPKG

discord-mini-games-es.js

Version:

Un paquete para implementar minijuegos usando discord.js-v14 en español

117 lines (114 loc) 6.73 kB
const discord = require('discord.js'); const pokemons = require('./assets/pokemon.json').pokemons; const {EmbedBuilder,ButtonBuilder,ButtonStyle,ActionRowBuilder,ComponentType} = require('discord.js') class GuessThePokemon{ /** * Initialises a new instance of Guess The Pokemon Game. * @param {`Message/Interaction`} message The Message Object. * @param {`GameOptions-Object`} gameOptions The game Options Object. * @returns {GuessThePokemon} Game instance. */ constructor(message,gameOptions) { if(!message) throw new Error("no se proporciona el mensaje."); this.message = message; if(gameOptions && typeof gameOptions !== 'object') throw new TypeError("gameOptions debe ser un objeto."); this.isSlash = gameOptions?.isSlash ?? false; if(this.isSlash == true){ if(!(this.message instanceof discord.CommandInteraction)){ throw new TypeError("El mensaje debe ser una instancia de Interacción de Comando.") } } else { if(!(this.message instanceof discord.Message)) { throw new TypeError("El mensaje debe ser una instancia de Mensaje de Discord.") } } this.time = gameOptions?.time ?? 45000; this.replied = false; this.randomN = (min,max) => {return Math.floor(Math.random()*max)+min;} this.edit = async (messageOptions,replyMessage) => { messageOptions.fetchReply = true; if(this.replied == false) { this.replied=true; if(this.isSlash == true) return await replyMessage.editReply(messageOptions) return await this.message.reply(messageOptions);} else return await replyMessage.edit(messageOptions) } this.options = gameOptions; this.onWin = gameOptions?.onWin ?? null; this.onTie = gameOptions?.onTie ?? null; this.onTimeUp = gameOptions?.onTimeUp ?? null; this.board = []; if(this.opponent && this.player.id == this.opponent.id) throw new Error('El jugador y el oponente no pueden ser el mismo.'); if(this.onWin && typeof this.onWin !== 'function') throw new TypeError('onWin debe ser una función.'); if(this.options?.emoji1 && typeof this.options?.emoji1 !== 'string') throw new TypeError('emoji1 Debe ser un String.'); if(this.options?.emoji2 && typeof this.options?.emoji2 !== 'string') throw new TypeError('emoji2 Debe ser un String.'); if(this.options?.emptyEmoji && typeof this.options?.emptyEmoji !== 'string') throw new TypeError('emptyEmoji Debe ser un String.'); if(this.onTie && typeof this.onTie !== 'function') throw new TypeError('onTie Debe de ser una función.'); if(this.onTimeUp && typeof this.onTimeUp!== 'function') throw new TypeError('onTimeUp Debe de ser una función.'); if(typeof this.isSlash !== 'boolean') throw new TypeError('isSlash Debe ser un valor Bolean.'); if(typeof this.time !== 'number') throw new TypeError('time debe de ser un número.'); if(this.options?.resTime && typeof this.options?.resTime !== 'number') throw new TypeError('resTime debe de ser un número'); if(this.time < 5000) throw new RangeError('time Debe ser mayor que 5000'); if(this.options?.title && typeof this.options?.title !== 'string') throw new TypeError('title debe de ser un String.'); if(this.options?.winDes && typeof this.options?.winDes !== 'string') throw new TypeError('winDes debe de ser un String.'); if(this.options?.tieDes && typeof this.options?.tieDes !== 'string') throw new TypeError('tieDes debe de ser un String.'); if(this.options?.nextDes && typeof this.options?.nextDes !== 'string') throw new TypeError('nextDes debe de ser un String.'); if(this.options?.timeUpDes && typeof this.options?.timeUpDes !== 'string') throw new TypeError('timeUpDes debe de ser un String.'); if(this.options?.confirmDes && typeof this.options?.confirmDes !== 'string') throw new TypeError('confirmDes debe de ser un String.'); if(this.options?.declineDes && typeof this.options?.declineDes !== 'string') throw new TypeError('declineDes debe de ser un String.'); if(this.options?.noResDes && typeof this.options?.noResDes !== 'string') throw new TypeError('noResDes debe de ser un String.'); } /** * Starts The Game. */ async run() { if(this.isSlash == true) { await this.message.deferReply().catch(() => {}); } const game = this; function embedGen(text, color) { const embed = new discord.EmbedBuilder() .setTitle(game.options?.title ??"Adivina el Pokémon") .setDescription(text) .setColor(color) .setTimestamp() .setImage(`https://tanvishgg.github.io/assets/pokemons/${game.pokemon.name}.png`) .setFooter({text:`Pedido por ${game.player.username}`}) .setThumbnail(game.player.avatarURL()); return embed; } const pokemon = this.pokemon.name.replace(/-/g," "); const hint = `\`${pokemon.replace(/-/g," ").replace(/[^0-9\s]/g, '_')}\``; let tries = this.options?.tries ?? 2; const msg = await this.edit({embeds:[embedGen(this.options?.startDes?.replace(/{hint}/g,hint)?.replace(/{tries}/g,tries) ?? `Adivina el siguiente Pokémon en ${tries} intentos. \n Pista: ${hint}`,"Blue")]},this.message) const collectorFilter = m => m && m.author.id == this.player.id; const collector = msg.channel.createMessageCollector({ filter: collectorFilter, idle: this.time, max:tries}); let played = false; collector.on('collect', async m => { if(m.content.toLowerCase() == pokemon.toLowerCase()) { m.delete(); played = true; collector.stop(); this.edit({embeds:[embedGen(this.options?.winDes?.replace(/{pokemon}/g,`\`${pokemon}\``) ?? `¡Adivinaste bien!, el Pokémon es \`${pokemon}\``,'Green')]},msg) if(this.onWin) await this.onWin(); } else{ tries--; m.delete() if(tries == 0) { played = true; this.edit({embeds:[embedGen(this.options?.loseDes?.replace(/{user_option}/g,`\`${m.content}\``)?.replace(/{pokemon}/g,`\`${pokemon}\``) ?? `Perdiste. Adivinaste \`${m.content}\`, pero el Pokemon es \`${pokemon}\``,'Red')]},msg) } else { this.edit({embeds:[embedGen(this.options?.retryDes?.replace(/{user_option}/g,`\`${m.content}\``)?.replace(/{tries}/g,tries)?.replace(/{hint}/g,hint) ?? `Adivinaste \`${m.content}\` lo cual es incorrecto, te quedan ${intentos} intentos restantes.\nPista: ${hint}`,'Red')]},msg) if(this.onLose) await this.onLose(); }} }); collector.on('end', async () => { if(played == false) { await this.edit({embeds:[embedGen(this.options?.timeUpDes?.replace(/{pokemon}/g,`\`${pokemon}\``) ?? `Juego terminado: Se acabó el tiempo, el Pokémon era \`${pokemon}\``,'Red')]},msg) if(this.onTimeUp) await this.onTimeUp(); } }) } } module.exports = GuessThePokemon;