From c2e1e97c4caaa61eedf2ca1af650e3290e8585ca Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 2 Aug 2021 18:04:56 -0400 Subject: [PATCH] Button-based join game --- commands/games-sp/akinator.js | 2 +- package.json | 40 ++++++++++++++-------------- util/Util.js | 50 ++++++++++++++++++++++------------- 3 files changed, 53 insertions(+), 39 deletions(-) diff --git a/commands/games-sp/akinator.js b/commands/games-sp/akinator.js index 52cbc284..c88f1d99 100644 --- a/commands/games-sp/akinator.js +++ b/commands/games-sp/akinator.js @@ -34,7 +34,7 @@ module.exports = class AkinatorCommand extends Command { } async run(msg, { region }) { - const aki = new Aki(region, !msg.channel.nsfw); + const aki = new Aki({ region, childMode: !msg.channel.nsfw }); let ans = null; let win = false; let timesGuessed = 0; diff --git a/package.json b/package.json index 50745978..492f877e 100644 --- a/package.json +++ b/package.json @@ -35,62 +35,62 @@ "#util": "./util/Util.js" }, "dependencies": { - "@discordjs/collection": "^0.1.6", - "@discordjs/opus": "^0.5.0", - "@tensorflow/tfjs-node": "^3.6.1", + "@discordjs/collection": "^0.2.1", + "@discordjs/opus": "^0.5.3", + "@tensorflow/tfjs-node": "^3.8.0", "@vitalets/google-translate-api": "^7.0.0", - "aki-api": "^5.2.1", + "aki-api": "^6.0.5", "bombsweeper.js": "^1.0.1", "canvas": "^2.8.0", - "cheerio": "^1.0.0-rc.9", + "cheerio": "^1.0.0-rc.10", "city-timezones": "^1.2.0", "cloc": "^2.8.0", "common-tags": "^1.8.0", "connect4-ai": "^0.1.3", "custom-translate": "^2.2.8", - "didyoumean2": "^4.2.0", + "didyoumean2": "^5.0.0", "discord.js": "github:discordjs/discord.js", "discord.js-docs": "github:TeeSeal/discord.js-docs", - "dotenv": "^9.0.2", + "dotenv": "^10.0.0", "emoji-regex": "^9.2.2", - "eslint": "^7.26.0", + "eslint": "^7.32.0", "fen-validator": "^1.4.1", "font-finder": "^1.1.0", "gifencoder": "^2.0.1", "gm": "^1.23.1", "html-entities": "^2.3.2", "image-to-ascii": "^3.0.13", - "ioredis": "^4.27.2", - "js-beautify": "^1.13.13", + "ioredis": "^4.27.7", + "js-beautify": "^1.14.0", "js-chess-engine": "^0.11.3", - "jszip": "^3.6.0", - "kuroshiro": "^1.1.2", + "jszip": "^3.7.0", + "kuroshiro": "^1.2.0", "kuroshiro-analyzer-kuromoji": "^1.1.0", - "mathjs": "^9.4.0", + "mathjs": "^9.4.4", "minimist": "^1.2.5", "moment": "^2.29.1", "moment-duration-format": "^2.3.2", "moment-timezone": "^0.5.33", "node-superfetch": "^0.2.3", - "nsfwjs": "^2.4.0", + "nsfwjs": "^2.4.1", "ntcjs": "^1.1.2", "parse-domain": "^3.0.3", "pokersolver": "^2.1.4", "random-js": "^2.1.0", "rss-parser": "^3.12.0", "semver": "^7.3.5", - "sherlockjs": "^1.4.0", + "sherlockjs": "^1.4.2", "stackblur-canvas": "^2.5.0", - "tesseract.js": "^2.1.4", + "tesseract.js": "^2.1.5", "text-diff": "^1.0.1", "tictactoe-minimax-ai": "^1.2.1", - "twemoji-parser": "^13.0.0", - "user-agents": "^1.0.658", + "twemoji-parser": "^13.1.0", + "user-agents": "^1.0.731", "valid-url": "^1.0.9", "wavefile": "^11.0.0", "winston": "^3.3.3", - "wuzzy": "^0.1.7", - "ytdl-core": "^4.8.0", + "wuzzy": "^0.1.8", + "ytdl-core": "^4.9.0", "zip-to-timezone": "^1.1.5" }, "optionalDependencies": { diff --git a/util/Util.js b/util/Util.js index 9ee8a3be..6fbb2c4c 100644 --- a/util/Util.js +++ b/util/Util.js @@ -1,3 +1,4 @@ +const { MessageActionRow, MessageButton } = require('discord.js'); const crypto = require('crypto'); const request = require('node-superfetch'); const fs = require('fs'); @@ -333,34 +334,47 @@ module.exports = class Util { static async awaitPlayers(msg, max, min, blacklist) { if (max === 1) return [msg.author.id]; const addS = min - 1 === 1 ? '' : 's'; - await msg.say(stripIndents` - You will need at least ${min - 1} more player${addS} (at max ${max - 1}). To join, type \`join game\`. - As the host, ${msg.author}, you can type \`start game\` to start the game early. - `); + const row = new MessageActionRow().addComponents( + new MessageButton().setCustomID('join').setLabel('Join Game').setStyle('PRIMARY'), + new MessageButton().setCustomID('start').setLabel('Start Game').setStyle('SUCCESS') + ); + let text = stripIndents` + You will need at least ${min - 1} more player${addS} (at max ${max - 1}). + As the host, ${msg.author}, you can start the game early. + `; + text += '\n'; + await msg.say(text, { components: [row] }); const joined = []; joined.push(msg.author.id); - const filter = res => { - if (res.author.bot) return false; - if (blacklist.includes(res.author.id)) return false; - if (res.author.id === msg.author.id && res.content.toLowerCase() === 'start game') return true; - if (joined.includes(res.author.id)) return false; - if (res.content.toLowerCase() !== 'join game') return false; + const filter = interaction => { + if (interaction.user.bot) return false; + if (blacklist.includes(interaction.user.id)) return false; + if (interaction.user.id !== msg.author.id && interaction.customID === 'start') return false; + if (joined.includes(interaction.user.id)) return false; return true; }; - const collector = msg.channel.createMessageCollector(filter, { max: max - 1, time: 120000 }); - collector.on('collect', res => { - if (res.content.toLowerCase() === 'start game') { - Util.reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); + const collector = msg.channel.createMessageComponentCollector(filter, { + componentType: 'BUTTON', + max: max - 1, + time: 120000 + }); + collector.on('collect', interaction => { + if (interaction.customID === 'start') { + interaction.deferUpdate(); collector.stop(); return; } joined.push(res.author.id); - Util.reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅'); + text += '✅'; + interaction.update(text); }); return new Promise(res => { - collector.once('end', verify => { - verify.set(msg.id, msg); - if (joined.length < min) return res(false); + collector.once('end', () => { + if (joined.length < min) { + interaction.update('Failed to start the game.', { components: [] }); + return res(false); + } + interaction.update('Let the game begin!', { components: [] }); return res(joined); }); });