mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-15 15:57:47 +02:00
Reduce code dupe
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const Collection = require('@discordjs/collection');
|
const Collection = require('@discordjs/collection');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const { SUCCESS_EMOJI_ID } = process.env;
|
const { awaitPlayers } = require('../../util/Util');
|
||||||
const nums = require('../../assets/json/bingo');
|
const nums = require('../../assets/json/bingo');
|
||||||
const rows = Object.keys(nums);
|
const rows = Object.keys(nums);
|
||||||
const callNums = Array.from({ length: 75 }, (v, i) => i + 1);
|
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.`);
|
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 });
|
this.client.games.set(msg.channel.id, { name: this.name });
|
||||||
try {
|
try {
|
||||||
const awaitedPlayers = await this.awaitPlayers(msg, playersCount);
|
const awaitedPlayers = await awaitPlayers(msg, playersCount);
|
||||||
if (!awaitedPlayers) {
|
if (!awaitedPlayers) {
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
return msg.say('Game could not be started...');
|
return msg.say('Game could not be started...');
|
||||||
@@ -65,7 +65,7 @@ module.exports = class BingoCommand extends Command {
|
|||||||
**${this.findRowValue(picked)} ${picked}**!
|
**${this.findRowValue(picked)} ${picked}**!
|
||||||
|
|
||||||
Check your DMs for your board. If you have bingo, type \`bingo\`!
|
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 => {
|
const filter = res => {
|
||||||
if (!players.has(res.author.id)) return false;
|
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() {
|
generateBoard() {
|
||||||
const result = [];
|
const result = [];
|
||||||
for (const [rowID, values] of Object.entries(nums)) {
|
for (const [rowID, values] of Object.entries(nums)) {
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
const Command = require('../../structures/Command');
|
const Command = require('../../structures/Command');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const Collection = require('@discordjs/collection');
|
const Collection = require('@discordjs/collection');
|
||||||
const { delay } = require('../../util/Util');
|
const { delay, awaitPlayers } = require('../../util/Util');
|
||||||
const questions = require('../../assets/json/guesspionage');
|
const questions = require('../../assets/json/guesspionage');
|
||||||
const { SUCCESS_EMOJI_ID } = process.env;
|
|
||||||
const guesses = ['much higher', 'higher', 'lower', 'much lower'];
|
const guesses = ['much higher', 'higher', 'lower', 'much lower'];
|
||||||
const max = 8;
|
const max = 8;
|
||||||
const min = 2;
|
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.`);
|
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 });
|
this.client.games.set(msg.channel.id, { name: this.name });
|
||||||
try {
|
try {
|
||||||
const awaitedPlayers = await this.awaitPlayers(msg, players);
|
const awaitedPlayers = await awaitPlayers(msg, players, min);
|
||||||
if (!awaitedPlayers) {
|
if (!awaitedPlayers) {
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
return msg.say('Game could not be started...');
|
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) {
|
makeLeaderboard(pts) {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let previousPts = null;
|
let previousPts = null;
|
||||||
|
|||||||
@@ -2,8 +2,7 @@ const Command = require('../../structures/Command');
|
|||||||
const request = require('node-superfetch');
|
const request = require('node-superfetch');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const Collection = require('@discordjs/collection');
|
const Collection = require('@discordjs/collection');
|
||||||
const { delay } = require('../../util/Util');
|
const { delay, awaitPlayers } = require('../../util/Util');
|
||||||
const { SUCCESS_EMOJI_ID } = process.env;
|
|
||||||
const trueOptions = ['true', 'yes', 'the truth', 't', 'tru', 'tr', 'y', 'ye'];
|
const trueOptions = ['true', 'yes', 'the truth', 't', 'tru', 'tr', 'y', 'ye'];
|
||||||
const falseOptions = ['false', 'lie', 'no', 'a lie', 'f', 'fals', 'fal', 'fa', 'n', 'l'];
|
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.`);
|
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 });
|
this.client.games.set(msg.channel.id, { name: this.name });
|
||||||
try {
|
try {
|
||||||
const awaitedPlayers = await this.awaitPlayers(msg, players);
|
const awaitedPlayers = await awaitPlayers(msg, players);
|
||||||
let turn = 0;
|
let turn = 0;
|
||||||
const pts = new Collection();
|
const pts = new Collection();
|
||||||
for (const player of awaitedPlayers) {
|
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) {
|
makeLeaderboard(pts) {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let previousPts = null;
|
let previousPts = null;
|
||||||
|
|||||||
@@ -3,8 +3,7 @@ const Collection = require('@discordjs/collection');
|
|||||||
const { Hand } = require('pokersolver');
|
const { Hand } = require('pokersolver');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const Deck = require('../../structures/cards/Deck');
|
const Deck = require('../../structures/cards/Deck');
|
||||||
const { formatNumber, list, delay } = require('../../util/Util');
|
const { formatNumber, list, delay, awaitPlayers } = require('../../util/Util');
|
||||||
const { SUCCESS_EMOJI_ID } = process.env;
|
|
||||||
const max = 6;
|
const max = 6;
|
||||||
const min = 2;
|
const min = 2;
|
||||||
const bigBlindAmount = 100;
|
const bigBlindAmount = 100;
|
||||||
@@ -49,7 +48,7 @@ module.exports = class PokerCommand extends Command {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
const awaitedPlayers = await this.awaitPlayers(msg, playersCount);
|
const awaitedPlayers = await awaitPlayers(msg, playersCount, min);
|
||||||
if (!awaitedPlayers) {
|
if (!awaitedPlayers) {
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
return msg.say('Game could not be started...');
|
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) {
|
determineActions(turnPlayer, currentBet) {
|
||||||
const actions = [];
|
const actions = [];
|
||||||
if (turnPlayer.currentBet !== currentBet) actions.push('fold');
|
if (turnPlayer.currentBet !== currentBet) actions.push('fold');
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "xiao",
|
"name": "xiao",
|
||||||
"version": "116.9.5",
|
"version": "116.9.6",
|
||||||
"description": "Your personal server companion.",
|
"description": "Your personal server companion.",
|
||||||
"main": "Xiao.js",
|
"main": "Xiao.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const crypto = require('crypto');
|
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 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 no = ['no', 'n', 'nah', 'nope', 'nop', 'iie', 'いいえ', 'non', 'fuck off'];
|
||||||
const inviteRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(\.gg|(app)?\.com\/invite|\.me)\/([^ ]+)\/?/gi;
|
const inviteRegex = /(https?:\/\/)?(www\.|canary\.|ptb\.)?discord(\.gg|(app)?\.com\/invite|\.me)\/([^ ]+)\/?/gi;
|
||||||
@@ -179,6 +180,25 @@ module.exports = class Util {
|
|||||||
return false;
|
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) {
|
static cleanAnilistHTML(html, removeLineBreaks = true) {
|
||||||
let clean = html;
|
let clean = html;
|
||||||
if (removeLineBreaks) clean = clean.replace(/\r|\n|\f/g, '');
|
if (removeLineBreaks) clean = clean.replace(/\r|\n|\f/g, '');
|
||||||
|
|||||||
Reference in New Issue
Block a user