This commit is contained in:
Dragon Fire
2024-03-31 01:50:34 -04:00
parent a11a7a232e
commit 1d5b4e1edb
+37 -45
View File
@@ -13,6 +13,7 @@ module.exports = class TwentyQuestionsCommand extends Command {
group: 'games-sp', group: 'games-sp',
memberName: '20-questions', memberName: '20-questions',
description: 'Think of something and 20Q will read your mind by asking a few simple questions.', description: 'Think of something and 20Q will read your mind by asking a few simple questions.',
game: true,
credit: [ credit: [
{ {
name: '20Q.net', name: '20Q.net',
@@ -33,53 +34,44 @@ module.exports = class TwentyQuestionsCommand extends Command {
} }
async run(msg, { game }) { async run(msg, { game }) {
const current = this.client.games.get(msg.channel.id); const startURL = await this.initialize(game);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`); let question = await this.startGame(game, startURL);
try { let win = null;
const startURL = await this.initialize(game); while (win === null) {
let question = await this.startGame(game, startURL); const answers = question.answers.map(answer => answer.text.toLowerCase());
let win = null; answers.push('end');
this.client.games.set(msg.channel.id, { name: this.name }); await msg.say(stripIndents`
while (win === null) { **${question.question}**
const answers = question.answers.map(answer => answer.text.toLowerCase()); ${question.answers.map(answer => answer.text).join(' | ')} | End
answers.push('end');
await msg.say(stripIndents`
**${question.question}**
${question.answers.map(answer => answer.text).join(' | ')} | End
`);
const filter = res => res.author.id === msg.author.id && answers.includes(res.content.toLowerCase());
const msgs = await msg.channel.awaitMessages({
filter,
max: 1,
time: 30000
});
if (!msgs.size) {
await msg.say('Sorry, time is up!');
win = 'time';
break;
}
const choice = msgs.first().content.toLowerCase();
if (choice === 'end') {
win = 'time';
break;
}
const answer = question.answers[answers.indexOf(choice)];
question = await this.nextQuestion(game, answer.href, question.url);
if (typeof question.win !== 'undefined') {
win = question.win;
break;
}
}
this.client.games.delete(msg.channel.id);
if (win === 'time') return msg.say('Game ended due to forfeit.');
return msg.say(stripIndents`
**${question.winText}**
${question.result}
`); `);
} catch (err) { const filter = res => res.author.id === msg.author.id && answers.includes(res.content.toLowerCase());
this.client.games.delete(msg.channel.id); const msgs = await msg.channel.awaitMessages({
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`); filter,
max: 1,
time: 30000
});
if (!msgs.size) {
await msg.say('Sorry, time is up!');
win = 'time';
break;
}
const choice = msgs.first().content.toLowerCase();
if (choice === 'end') {
win = 'time';
break;
}
const answer = question.answers[answers.indexOf(choice)];
question = await this.nextQuestion(game, answer.href, question.url);
if (typeof question.win !== 'undefined') {
win = question.win;
break;
}
} }
if (win === 'time') return msg.say('Game ended due to forfeit.');
return msg.say(stripIndents`
**${question.winText}**
${question.result}
`);
} }
makeBaseURI(game) { makeBaseURI(game) {