From a122250e0ce2ca86eac73a2eaffd2e16f4dd3e43 Mon Sep 17 00:00:00 2001 From: Dragon Fire Date: Mon, 7 Jun 2021 18:03:00 -0400 Subject: [PATCH] Add buttons to true/false --- commands/games-sp/true-or-false.js | 28 +++++++++++++--------------- framework/Command.js | 7 +++++++ 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/commands/games-sp/true-or-false.js b/commands/games-sp/true-or-false.js index 15b040d4..d66664ad 100644 --- a/commands/games-sp/true-or-false.js +++ b/commands/games-sp/true-or-false.js @@ -1,10 +1,9 @@ const Command = require('../../framework/Command'); +const { MessageActionRow, MessageButton } = require('discord.js'); const { stripIndents } = require('common-tags'); const request = require('node-superfetch'); const { list } = require('../../util/Util'); const difficulties = ['easy', 'medium', 'hard']; -const trueAns = ['true', 't', 'tru', 'yes', 'y']; -const falseAns = ['false', 'f', 'no', 'n']; module.exports = class TrueOrFalseCommand extends Command { constructor(client) { @@ -49,26 +48,25 @@ module.exports = class TrueOrFalseCommand extends Command { if (!body.results) return msg.reply('Oh no, a question could not be fetched. Try again later!'); const correct = decodeURIComponent(body.results[0].correct_answer.toLowerCase()); const correctBool = correct === 'true'; + const row = new MessageActionRow().addComponents( + new MessageButton().setCustomID('true').setStyle('SUCCESS').setLabel('True'), + new MessageButton().setCustomID('false').setStyle('DANGER').setLabel('False') + ); await msg.reply(stripIndents` **You have 15 seconds to answer this question.** ${decodeURIComponent(body.results[0].question)} - **[T]rue or [F]alse?** - `); - const filter = res => { - if (res.author.id !== msg.author.id) return false; - return trueAns.includes(res.content.toLowerCase()) || falseAns.includes(res.content.toLowerCase()); - }; - const msgs = await msg.channel.awaitMessages(filter, { + `, { components: [row] }); + const filter = res => res.author.id !== msg.author.id; + const interactions = await msg.awaitMessageComponentInteractions(filter, { max: 1, time: 15000 }); - if (!msgs.size) return msg.reply(`Sorry, time is up! It was ${correctBool}.`); - const ans = msgs.first().content.toLowerCase(); - const ansBool = trueAns.includes(ans); - if (correctBool !== ansBool) return msg.reply(`Nope, sorry, it's ${correctBool}.`); - return msg.reply('Nice job! 10/10! You deserve some cake!'); + if (!interactions.size) return msg.edit(`Sorry, time is up! It was ${correctBool}.`, { components: [] }); + const ans = interactions.first().customID === 'true'; + if (correctBool !== ans) return msg.edit(`Nope, sorry, it's ${correctBool}.`, { components: [] }); + return msg.edit('Nice job! 10/10! You deserve some cake!', { components: [] }); } catch (err) { - return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); + return msg.edit(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); } } }; diff --git a/framework/Command.js b/framework/Command.js index 5e0f5286..09c54992 100644 --- a/framework/Command.js +++ b/framework/Command.js @@ -51,4 +51,11 @@ module.exports = class Command { enable() { this._enabled = true; } + + reload() { + delete require.cache[require.resolve(`../commands/${this.groupID}/${this.memberName}.js`)]; + const NewCmd = require(`../commands/${this.groupID}/${this.memberName}.js`); + this.client.registry.commands.delete(this.name); + this.client.registry.registerCommand(this.name, new NewCmd(this.client)); + } };