polish-gamecord
Version:
Discord Gamecord is a powerful npm package with a collection of minigames for your discord bot
128 lines (108 loc) • 7.42 kB
JavaScript
const { MessageEmbed, MessageButton, MessageActionRow } = require('discord.js');
const { disableButtons, shuffleArray, formatMessage } = require('../utils/utils');
const events = require('events');
module.exports = class FindEmoji extends events {
constructor(options = {}) {
if (!options.isSlashGame) options.isSlashGame = false;
if (!options.message) throw new TypeError('BRAK_WIADOMOŚCI: Nie podano opcji wiadomości.');
if (typeof options.message !== 'object') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja wiadomości musi być obiektem.');
if (typeof options.isSlashGame !== 'boolean') throw new TypeError('NIEPRAWIDŁOWY_TYP_KOMENDY: Opcja isSlashGame musi być typem boolean.');
if (!options.embed) options.embed = {};
if (!options.embed.title) options.embed.title = 'Znajdź Emoji';
if (!options.embed.color) options.embed.color = '#5865F2';
if (!options.embed.description) options.embed.description = 'Zapamiętaj emoji z planszy poniżej.';
if (!options.embed.findDescription) options.embed.findDescription = 'Znajdź emoji {emoji} zanim czas minie.';
if (!options.timeoutTime) options.timeoutTime = 60000;
if (!options.hideEmojiTime) options.hideEmojiTime = 5000;
if (!options.buttonStyle) options.buttonStyle = 'PRIMARY';
if (!options.emojis) options.emojis = ['🍉', '🍇', '🍊', '🍋', '🥭', '🍎', '🍏', '🥝', '🥥', '🍓', '🍒'];
if (!options.winMessage) options.winMessage = 'Wygrałeś! Wybrałeś poprawne emoji. {emoji}';
if (!options.loseMessage) options.loseMessage = 'Przegrałeś! Wybrałeś niepoprawne emoji. {emoji}';
if (!options.timeoutMessage) options.timeoutMessage = 'Przegrałeś! Czas minął. Emoji to {emoji}';
if (typeof options.embed !== 'object') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Opcja embed musi być obiektem.');
if (typeof options.embed.title !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Tytuł embed musi być ciągiem znaków.');
if (typeof options.embed.color !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Kolor embed musi być ciągiem znaków.');
if (typeof options.embed.description !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Opis embed musi być ciągiem znaków.');
if (typeof options.embed.findDescription !== 'string') throw new TypeError('NIEPRAWIDŁOWY_EMBED: Opis findDescription musi być ciągiem znaków.');
if (typeof options.timeoutTime !== 'number') throw new TypeError('NIEPRAWIDŁOWY_CZAS: Opcja czasu przekroczenia musi być liczbą.');
if (typeof options.hideEmojiTime !== 'number') throw new TypeError('NIEPRAWIDŁOWY_CZAS: Opcja czasu ukrycia emoji musi być liczbą.');
if (typeof options.buttonStyle !== 'string') throw new TypeError('NIEPRAWIDŁOWY_STYL_PRZYCISKU: Styl przycisku musi być ciągiem znaków.');
if (!Array.isArray(options.emojis)) throw new TypeError('NIEPRAWIDŁOWE_EMOJIS: Opcja emojis musi być tablicą.');
if (typeof options.winMessage !== 'string') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja wiadomości o wygranej musi być ciągiem znaków.');
if (typeof options.loseMessage !== 'string') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja wiadomości o przegranej musi być ciągiem znaków.');
if (typeof options.timeoutMessage !== 'string') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja wiadomości o czasie przekroczenia musi być ciągiem znaków.');
if (options.playerOnlyMessage !== false) {
if (!options.playerOnlyMessage) options.playerOnlyMessage = 'Tylko {player} może używać tych przycisków.';
if (typeof options.playerOnlyMessage !== 'string') throw new TypeError('NIEPRAWIDŁOWA_WIADOMOŚĆ: Opcja playerOnlyMessage musi być ciągiem znaków.');
}
super();
this.options = options;
this.message = options.message;
this.emojis = options.emojis;
this.selected = null;
this.emoji = null;
}
async sendMessage(content) {
if (this.options.isSlashGame) return await this.message.editReply(content).catch(e => {});
else return await this.message.channel.send(content).catch(e => {});
}
async startGame() {
if (this.options.isSlashGame || !this.message.author) {
if (!this.message.deferred) await this.message.deferReply().catch(e => {});
this.message.author = this.message.user;
this.options.isSlashGame = true;
}
this.emojis = shuffleArray(this.emojis).slice(0, 8);
this.emoji = this.emojis[Math.floor(Math.random() * this.emojis.length)];
const embed = new MessageEmbed()
.setColor(this.options.embed.color)
.setTitle(this.options.embed.title)
.setDescription(this.options.embed.description)
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) });
const msg = await this.sendMessage({ embeds: [embed], components: this.getComponents(true) });
setTimeout(async () => {
embed.setDescription(this.options.embed.findDescription.replace('{emoji}', this.emoji));
await msg.edit({ embeds: [embed], components: this.getComponents(false) });
const collector = msg.createMessageComponentCollector({ idle: this.options.timeoutTime });
collector.on('collect', async (btn) => {
await btn.deferUpdate().catch(e => {});
if (btn.user.id !== this.message.author.id) {
if (this.options.playerOnlyMessage) btn.followUp({ content: formatMessage(this.options, 'playerOnlyMessage'), ephemeral: true });
return;
}
this.selected = this.emojis[parseInt(btn.customId.split('_')[1])];
return collector.stop();
})
collector.on('end', async (_, reason) => {
if (reason === 'idle' || reason === 'user') return this.gameOver(msg, (reason === 'user'));
})
}, this.options.hideEmojiTime);
}
gameOver(msg, result) {
const FindEmojiGame = { player: this.message.author, selectedEmoji: this.selected, correctEmoji: this.emoji };
const resultMessage = result ? ((this.selected === this.emoji) ? 'win' : 'lose') : 'timeout';
this.emit('gameOver', { result: resultMessage, ...FindEmojiGame });
if (!result) this.selected = this.emoji;
const embed = new MessageEmbed()
.setColor(this.options.embed.color)
.setTitle(this.options.embed.title)
.setDescription(this.options[resultMessage+'Message'].replace('{emoji}', this.emoji))
.setFooter({ text: this.message.author.tag, iconURL: this.message.author.displayAvatarURL({ dynamic: true }) })
return msg.edit({ embeds: [embed], components: disableButtons(this.getComponents(true)) });
}
getComponents(showEmoji) {
const components = [];
for (let x = 0; x < 2; x++) {
const row = new MessageActionRow();
for (let y = 0; y < 4; y++) {
const buttonEmoji = this.emojis[x * 4 + y];
const btn = new MessageButton().setEmoji(showEmoji ? buttonEmoji : null).setCustomId('findEmoji_' + (x*4 + y))
.setStyle(buttonEmoji === this.selected ? (this.selected === this.emoji ? 'SUCCESS' : 'DANGER') : this.options.buttonStyle);
if (!showEmoji) btn.setLabel('\u200b');
row.addComponents(btn);
}
components.push(row);
}
return components;
}
}