Reduce code dupe

This commit is contained in:
Dragon Fire
2020-06-09 10:16:37 -04:00
parent 2e1a30f62d
commit 362e55a370
6 changed files with 30 additions and 88 deletions
+3 -22
View File
@@ -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)) {
+2 -21
View File
@@ -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;
+2 -23
View File
@@ -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;
+2 -21
View File
@@ -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');
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiao",
"version": "116.9.5",
"version": "116.9.6",
"description": "Your personal server companion.",
"main": "Xiao.js",
"scripts": {
+20
View File
@@ -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, '');