20-questions but with buttons

This commit is contained in:
Dragon Fire
2024-03-31 12:00:39 -04:00
parent 1d5b4e1edb
commit 6ea155dd84
+49 -13
View File
@@ -1,4 +1,5 @@
const Command = require('../../framework/Command'); const Command = require('../../framework/Command');
const { MessageButton, MessageActionRow } = require('discord.js');
const request = require('node-superfetch'); const request = require('node-superfetch');
const cheerio = require('cheerio'); const cheerio = require('cheerio');
const { stripIndents } = require('common-tags'); const { stripIndents } = require('common-tags');
@@ -26,6 +27,7 @@ module.exports = class TwentyQuestionsCommand extends Command {
key: 'game', key: 'game',
prompt: `What game do you want to play? Either ${list(Object.keys(games), 'or')}.`, prompt: `What game do you want to play? Either ${list(Object.keys(games), 'or')}.`,
type: 'string', type: 'string',
default: '',
oneOf: Object.keys(games), oneOf: Object.keys(games),
parse: game => games[game.toLowerCase()] parse: game => games[game.toLowerCase()]
} }
@@ -37,25 +39,50 @@ module.exports = class TwentyQuestionsCommand extends Command {
const startURL = await this.initialize(game); const startURL = await this.initialize(game);
let question = await this.startGame(game, startURL); let question = await this.startGame(game, startURL);
let win = null; let win = null;
while (win === null) { const initialRow = new MessageActionRow().addComponents(
const answers = question.answers.map(answer => answer.text.toLowerCase()); new MessageButton().setCustomId('true').setLabel('Ready!').setStyle('PRIMARY'),
answers.push('end'); new MessageButton().setCustomId('false').setLabel('Nevermind').setStyle('SECONDARY')
await msg.say(stripIndents` );
**${question.question}** const gameMsg = await msg.reply({
${question.answers.map(answer => answer.text).join(' | ')} | End content: 'Welcome to 20 Questions! Think of something, and I will try to guess it.',
`); components: [initialRow]
const filter = res => res.author.id === msg.author.id && answers.includes(res.content.toLowerCase()); });
const msgs = await msg.channel.awaitMessages({ let buttonPress;
filter, try {
buttonPress = await gameMsg.awaitMessageComponent({
filter: res => res.user.id === msg.author.id,
max: 1, max: 1,
time: 30000 time: 30000
}); });
if (!msgs.size) { if (buttonPress.customId === 'false') return buttonPress.update({ content: 'Too bad...', components: [] });
await msg.say('Sorry, time is up!'); } catch {
return gameMsg.edit({ content: 'Guess you didn\'t want to play after all...', components: [] });
}
await this.sendLoadingMessage(buttonPress, [initialRow]);
while (win === null) {
const answers = question.answers.map(answer => answer.text.toLowerCase());
const row = new MessageActionRow();
for (const answer of answers) {
row.addComponents(new MessageButton().setCustomId(answer).setStyle('PRIMARY').setLabel(answer));
}
const sRow = new MessageActionRow();
sRow.addComponents(new MessageButton().setCustomId('end').setStyle('DANGER').setLabel('End'));
await buttonPress.editReply({
content: question.question,
components: [row, sRow]
});
try {
buttonPress = await gameMsg.awaitMessageComponent({
filter: res => res.user.id === msg.author.id,
max: 1,
time: 30000
});
} catch {
win = 'time'; win = 'time';
break; break;
} }
const choice = msgs.first().content.toLowerCase(); await this.sendLoadingMessage(buttonPress, [row, sRow]);
const choice = buttonPress.customId;
if (choice === 'end') { if (choice === 'end') {
win = 'time'; win = 'time';
break; break;
@@ -154,4 +181,13 @@ module.exports = class TwentyQuestionsCommand extends Command {
url url
}; };
} }
sendLoadingMessage(buttonPress, rows) {
for (const row of rows) {
for (const button of row.components) {
button.setDisabled(true);
}
}
return buttonPress.update({ content: 'Loading...', components: rows });
}
}; };