From 362e55a3700cfc8a60baeac8a683ed6e525bd755 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Tue, 9 Jun 2020 10:16:37 -0400 Subject: [PATCH] Reduce code dupe --- commands/games-mp/bingo.js | 25 +++---------------------- commands/games-mp/guesspionage.js | 23 ++--------------------- commands/games-mp/lie-swatter.js | 25 ++----------------------- commands/games-mp/poker.js | 23 ++--------------------- package.json | 2 +- util/Util.js | 20 ++++++++++++++++++++ 6 files changed, 30 insertions(+), 88 deletions(-) diff --git a/commands/games-mp/bingo.js b/commands/games-mp/bingo.js index 589166bb..2897e868 100644 --- a/commands/games-mp/bingo.js +++ b/commands/games-mp/bingo.js @@ -1,7 +1,7 @@ const Command = require('../../structures/Command'); const Collection = require('@discordjs/collection'); const { stripIndents } = require('common-tags'); -const { SUCCESS_EMOJI_ID } = process.env; +const { awaitPlayers } = require('../../util/Util'); const nums = require('../../assets/json/bingo'); const rows = Object.keys(nums); const callNums = Array.from({ length: 75 }, (v, i) => i + 1); @@ -31,7 +31,7 @@ module.exports = class BingoCommand extends Command { if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); this.client.games.set(msg.channel.id, { name: this.name }); try { - const awaitedPlayers = await this.awaitPlayers(msg, playersCount); + const awaitedPlayers = await awaitPlayers(msg, playersCount); if (!awaitedPlayers) { this.client.games.delete(msg.channel.id); return msg.say('Game could not be started...'); @@ -65,7 +65,7 @@ module.exports = class BingoCommand extends Command { **${this.findRowValue(picked)} ${picked}**! Check your DMs for your board. If you have bingo, type \`bingo\`! - _Next number will be called in 20 seconds._ + _Next number will be called in 20 seconds. ${validNums.length - 1} numbers left._ `); const filter = res => { if (!players.has(res.author.id)) return false; @@ -89,25 +89,6 @@ module.exports = class BingoCommand extends Command { } } - async awaitPlayers(msg, players) { - if (players === 1) return [msg.author.id]; - await msg.say(`You will need at least 1 more player (at max ${players - 1}). To join, type \`join game\`.`); - const joined = []; - joined.push(msg.author.id); - const filter = res => { - if (res.author.bot) return false; - if (joined.includes(res.author.id)) return false; - if (res.content.toLowerCase() !== 'join game') return false; - joined.push(res.author.id); - res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); - return true; - }; - const verify = await msg.channel.awaitMessages(filter, { max: players - 1, time: 30000 }); - verify.set(msg.id, msg); - if (verify.size < 1) return false; - return verify.map(player => player.author.id); - } - generateBoard() { const result = []; for (const [rowID, values] of Object.entries(nums)) { diff --git a/commands/games-mp/guesspionage.js b/commands/games-mp/guesspionage.js index 26504a79..dc91b238 100644 --- a/commands/games-mp/guesspionage.js +++ b/commands/games-mp/guesspionage.js @@ -1,9 +1,8 @@ const Command = require('../../structures/Command'); const { stripIndents } = require('common-tags'); const Collection = require('@discordjs/collection'); -const { delay } = require('../../util/Util'); +const { delay, awaitPlayers } = require('../../util/Util'); const questions = require('../../assets/json/guesspionage'); -const { SUCCESS_EMOJI_ID } = process.env; const guesses = ['much higher', 'higher', 'lower', 'much lower']; const max = 8; const min = 2; @@ -54,7 +53,7 @@ module.exports = class GuesspionageCommand extends Command { if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); this.client.games.set(msg.channel.id, { name: this.name }); try { - const awaitedPlayers = await this.awaitPlayers(msg, players); + const awaitedPlayers = await awaitPlayers(msg, players, min); if (!awaitedPlayers) { this.client.games.delete(msg.channel.id); return msg.say('Game could not be started...'); @@ -164,24 +163,6 @@ module.exports = class GuesspionageCommand extends Command { } } - async awaitPlayers(msg, players) { - await msg.say(`You will need at least 1 more player (at max ${players - 1}). To join, type \`join game\`.`); - const joined = []; - joined.push(msg.author.id); - const filter = res => { - if (res.author.bot) return false; - if (joined.includes(res.author.id)) return false; - if (res.content.toLowerCase() !== 'join game') return false; - joined.push(res.author.id); - res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); - return true; - }; - const verify = await msg.channel.awaitMessages(filter, { max: players - 1, time: 30000 }); - verify.set(msg.id, msg); - if (verify.size < min) return false; - return verify.map(player => player.author.id); - } - makeLeaderboard(pts) { let i = 0; let previousPts = null; diff --git a/commands/games-mp/lie-swatter.js b/commands/games-mp/lie-swatter.js index 517ae348..f8c89a29 100644 --- a/commands/games-mp/lie-swatter.js +++ b/commands/games-mp/lie-swatter.js @@ -2,8 +2,7 @@ const Command = require('../../structures/Command'); const request = require('node-superfetch'); const { stripIndents } = require('common-tags'); const Collection = require('@discordjs/collection'); -const { delay } = require('../../util/Util'); -const { SUCCESS_EMOJI_ID } = process.env; +const { delay, awaitPlayers } = require('../../util/Util'); const trueOptions = ['true', 'yes', 'the truth', 't', 'tru', 'tr', 'y', 'ye']; const falseOptions = ['false', 'lie', 'no', 'a lie', 'f', 'fals', 'fal', 'fa', 'n', 'l']; @@ -45,7 +44,7 @@ module.exports = class LieSwatterCommand extends Command { if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); this.client.games.set(msg.channel.id, { name: this.name }); try { - const awaitedPlayers = await this.awaitPlayers(msg, players); + const awaitedPlayers = await awaitPlayers(msg, players); let turn = 0; const pts = new Collection(); for (const player of awaitedPlayers) { @@ -145,26 +144,6 @@ module.exports = class LieSwatterCommand extends Command { }); } - async awaitPlayers(msg, players) { - const min = 1; - if (players === 1) return [msg.author.id]; - await msg.say(`You can have at most ${players - 1} more players. To join, type \`join game\`.`); - const joined = []; - joined.push(msg.author.id); - const filter = res => { - if (res.author.bot) return false; - if (joined.includes(res.author.id)) return false; - if (res.content.toLowerCase() !== 'join game') return false; - joined.push(res.author.id); - res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); - return true; - }; - const verify = await msg.channel.awaitMessages(filter, { max: players - 1, time: 60000 }); - verify.set(msg.id, msg); - if (verify.size < min) return false; - return verify.map(player => player.author.id); - } - makeLeaderboard(pts) { let i = 0; let previousPts = null; diff --git a/commands/games-mp/poker.js b/commands/games-mp/poker.js index f59286d7..14ec1fb8 100644 --- a/commands/games-mp/poker.js +++ b/commands/games-mp/poker.js @@ -3,8 +3,7 @@ const Collection = require('@discordjs/collection'); const { Hand } = require('pokersolver'); const { stripIndents } = require('common-tags'); const Deck = require('../../structures/cards/Deck'); -const { formatNumber, list, delay } = require('../../util/Util'); -const { SUCCESS_EMOJI_ID } = process.env; +const { formatNumber, list, delay, awaitPlayers } = require('../../util/Util'); const max = 6; const min = 2; const bigBlindAmount = 100; @@ -49,7 +48,7 @@ module.exports = class PokerCommand extends Command { } }); try { - const awaitedPlayers = await this.awaitPlayers(msg, playersCount); + const awaitedPlayers = await awaitPlayers(msg, playersCount, min); if (!awaitedPlayers) { this.client.games.delete(msg.channel.id); return msg.say('Game could not be started...'); @@ -181,24 +180,6 @@ module.exports = class PokerCommand extends Command { } } - async awaitPlayers(msg, players) { - await msg.say(`You will need at least 1 more player (at max ${players - 1}). To join, type \`join game\`.`); - const joined = []; - joined.push(msg.author.id); - const filter = res => { - if (res.author.bot) return false; - if (joined.includes(res.author.id)) return false; - if (res.content.toLowerCase() !== 'join game') return false; - joined.push(res.author.id); - res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); - return true; - }; - const verify = await msg.channel.awaitMessages(filter, { max: players - 1, time: 30000 }); - verify.set(msg.id, msg); - if (verify.size < min) return false; - return verify.map(player => player.author.id); - } - determineActions(turnPlayer, currentBet) { const actions = []; if (turnPlayer.currentBet !== currentBet) actions.push('fold'); diff --git a/package.json b/package.json index d0630896..d02a0930 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "xiao", - "version": "116.9.5", + "version": "116.9.6", "description": "Your personal server companion.", "main": "Xiao.js", "scripts": { diff --git a/util/Util.js b/util/Util.js index b6dc6325..6b285856 100644 --- a/util/Util.js +++ b/util/Util.js @@ -1,4 +1,5 @@ const crypto = require('crypto'); +const { SUCCESS_EMOJI_ID } = process.env; const yes = ['yes', 'y', 'ye', 'yeah', 'yup', 'yea', 'ya', 'hai', 'si', 'sí', 'oui', 'はい', 'correct']; const no = ['no', 'n', 'nah', 'nope', 'nop', 'iie', 'いいえ', 'non', 'fuck off']; const inviteRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(\.gg|(app)?\.com\/invite|\.me)\/([^ ]+)\/?/gi; @@ -179,6 +180,25 @@ module.exports = class Util { return false; } + static async awaitPlayers(msg, max, min = 1) { + if (max === 1) return [msg.author.id]; + await msg.say(`You will need at least ${min} more player (at max ${max - 1}). To join, type \`join game\`.`); + const joined = []; + joined.push(msg.author.id); + const filter = res => { + if (res.author.bot) return false; + if (joined.includes(res.author.id)) return false; + if (res.content.toLowerCase() !== 'join game') return false; + joined.push(res.author.id); + res.react(SUCCESS_EMOJI_ID || '✅').catch(() => null); + return true; + }; + const verify = await msg.channel.awaitMessages(filter, { max: max - 1, time: 30000 }); + verify.set(msg.id, msg); + if (verify.size < min) return false; + return verify.map(player => player.author.id); + } + static cleanAnilistHTML(html, removeLineBreaks = true) { let clean = html; if (removeLineBreaks) clean = clean.replace(/\r|\n|\f/g, '');