Allow multiple aki games in same channel

This commit is contained in:
Dragon Fire
2021-06-07 22:14:12 -04:00
parent 3d3a25e62b
commit 43bbf79214
2 changed files with 136 additions and 154 deletions
+108 -122
View File
@@ -1,7 +1,6 @@
const Command = require('../../framework/Command');
const { MessageEmbed, MessageActionRow, MessageButton } = require('discord.js');
const { Aki, regions } = require('aki-api');
const { stripIndents } = require('common-tags');
const { list } = require('../../util/Util');
module.exports = class AkinatorCommand extends Command {
@@ -35,143 +34,130 @@ module.exports = class AkinatorCommand extends Command {
}
async run(msg, { region }) {
const current = this.client.games.get(msg.channel.id);
if (current) return msg.reply(`Please wait until the current game of \`${current.name}\` is finished.`);
try {
const aki = new Aki(region, !msg.channel.nsfw);
let ans = null;
let win = false;
let timesGuessed = 0;
let guessResetNum = 0;
let wentBack = false;
let forceGuess = false;
const initialRow = new MessageActionRow().addComponents(
new MessageButton().setCustomID('true').setLabel('Ready!').setStyle('PRIMARY'),
new MessageButton().setCustomID('false').setLabel('Nevermind').setStyle('SECONDARY')
const aki = new Aki(region, !msg.channel.nsfw);
let ans = null;
let win = false;
let timesGuessed = 0;
let guessResetNum = 0;
let wentBack = false;
let forceGuess = false;
const initialRow = new MessageActionRow().addComponents(
new MessageButton().setCustomID('true').setLabel('Ready!').setStyle('PRIMARY'),
new MessageButton().setCustomID('false').setLabel('Nevermind').setStyle('SECONDARY')
);
const gameMsg = await msg.reply('Welcome to Akinator! Think of a character, and I will try to guess it.', {
components: [initialRow]
});
const initialVerify = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
max: 1,
time: 30000
});
if (!initialVerify.size) {
return gameMsg.edit('Guess you didn\'t want to play after all...', { components: [] });
}
let buttonPress = initialVerify.first();
if (buttonPress.customID === 'false') return buttonPress.update('Too bad...', { components: [] });
await this.sendLoadingMessage(buttonPress, [initialRow]);
const guessBlacklist = [];
while (timesGuessed < 3) {
if (guessResetNum > 0) guessResetNum--;
if (ans === null) {
await aki.start();
} else if (wentBack) {
wentBack = false;
} else {
try {
await aki.step(ans);
} catch {
await aki.step(ans);
}
}
if (!aki.answers || aki.currentStep >= 79) forceGuess = true;
const row = new MessageActionRow();
for (const answer of aki.answers) {
row.addComponents(new MessageButton().setCustomID(answer).setStyle('PRIMARY').setLabel(answer));
}
const sRow = new MessageActionRow();
if (aki.currentStep > 0) {
sRow.addComponents(new MessageButton().setCustomID('back').setStyle('SECONDARY').setLabel('Back'));
}
sRow.addComponents(new MessageButton().setCustomID('end').setStyle('DANGER').setLabel('End'));
await buttonPress.editReply(
`**${aki.currentStep + 1}.** ${aki.question} (${Math.round(Number.parseInt(aki.progress, 10))}%)`,
{ components: [row, sRow] }
);
const gameMsg = await msg.reply('Welcome to Akinator! Think of a character, and I will try to guess it.', {
components: [initialRow]
});
const initialVerify = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
const interactions = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
max: 1,
time: 30000
});
if (!initialVerify.size) {
return gameMsg.edit('Guess you didn\'t want to play after all...', { components: [] });
if (!interactions.size) {
win = 'time';
break;
}
let buttonPress = initialVerify.first();
if (buttonPress.customID === 'false') return buttonPress.update('Too bad...', { components: [] });
await this.sendLoadingMessage(buttonPress, [initialRow]);
const guessBlacklist = [];
this.client.games.set(msg.channel.id, { name: this.name });
while (timesGuessed < 3) {
if (guessResetNum > 0) guessResetNum--;
if (ans === null) {
await aki.start();
} else if (wentBack) {
wentBack = false;
} else {
try {
await aki.step(ans);
} catch {
await aki.step(ans);
}
buttonPress = interactions.first();
await this.sendLoadingMessage(buttonPress, [row, sRow]);
const choice = interactions.first().customID;
if (choice === 'end') {
forceGuess = true;
} else if (choice === 'back') {
if (guessResetNum > 0) guessResetNum++;
wentBack = true;
await aki.back();
continue;
} else {
ans = aki.answers.indexOf(choice);
}
if ((aki.progress >= 90 && !guessResetNum) || forceGuess) {
timesGuessed++;
guessResetNum += 10;
await aki.win();
const guess = aki.answers.filter(g => !guessBlacklist.includes(g.id))[0];
if (!guess) {
win = true;
break;
}
if (!aki.answers || aki.currentStep >= 79) forceGuess = true;
const row = new MessageActionRow();
for (const answer of aki.answers) {
row.addComponents(new MessageButton().setCustomID(answer).setStyle('PRIMARY').setLabel(answer));
}
const sRow = new MessageActionRow();
if (aki.currentStep > 0) {
sRow.addComponents(new MessageButton().setCustomID('back').setStyle('SECONDARY').setLabel('Back'));
}
sRow.addComponents(new MessageButton().setCustomID('end').setStyle('DANGER').setLabel('End'));
await buttonPress.editReply(
`**${aki.currentStep + 1}.** ${aki.question} (${Math.round(Number.parseInt(aki.progress, 10))}%)`,
{ components: [row, sRow] }
guessBlacklist.push(guess.id);
const embed = new MessageEmbed()
.setColor(0xF78B26)
.setTitle(`I'm ${Math.round(guess.proba * 100)}% sure it's...`)
.setDescription(`${guess.name}${guess.description ? `\n_${guess.description}_` : ''}`)
.setThumbnail(guess.absolute_picture_path || null)
.setFooter(forceGuess ? 'Final Guess' : `Guess ${timesGuessed}`);
const guessRow = new MessageActionRow().addComponents(
new MessageButton().setCustomID('true').setLabel('Yes').setStyle('SUCCESS'),
new MessageButton().setCustomID('false').setLabel('No').setStyle('DANGER')
);
const interactions = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
await buttonPress.editReply('Is this your character?', { embeds: [embed], components: [guessRow] });
const verification = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
max: 1,
time: 30000
});
if (!interactions.size) {
if (!verification.size) {
win = 'time';
break;
}
buttonPress = interactions.first();
await this.sendLoadingMessage(buttonPress, [row, sRow]);
const choice = interactions.first().customID;
if (choice === 'end') {
forceGuess = true;
} else if (choice === 'back') {
if (guessResetNum > 0) guessResetNum++;
wentBack = true;
await aki.back();
continue;
} else {
ans = aki.answers.indexOf(choice);
}
if ((aki.progress >= 90 && !guessResetNum) || forceGuess) {
timesGuessed++;
guessResetNum += 10;
await aki.win();
const guess = aki.answers.filter(g => !guessBlacklist.includes(g.id))[0];
if (!guess) {
win = true;
break;
}
guessBlacklist.push(guess.id);
const embed = new MessageEmbed()
.setColor(0xF78B26)
.setTitle(`I'm ${Math.round(guess.proba * 100)}% sure it's...`)
.setDescription(stripIndents`
${guess.name}${guess.description ? `\n_${guess.description}_` : ''}
_**Type [y]es or [n]o to continue.**_
`)
.setThumbnail(guess.absolute_picture_path || null)
.setFooter(forceGuess ? 'Final Guess' : `Guess ${timesGuessed}`);
const guessRow = new MessageActionRow().addComponents(
new MessageButton().setCustomID('true').setLabel('Yes').setStyle('SUCCESS'),
new MessageButton().setCustomID('false').setLabel('No').setStyle('DANGER')
);
await buttonPress.editReply('Is this your character?', { embeds: [embed], components: [guessRow] });
const verification = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
max: 1,
time: 30000
});
if (!verification.size) {
win = 'time';
break;
}
buttonPress = verification.first();
await this.sendLoadingMessage(buttonPress, [guessRow]);
if (buttonPress.customID === 'true') {
win = false;
break;
} else if (timesGuessed >= 3 || forceGuess) {
win = true;
break;
}
buttonPress = verification.first();
await this.sendLoadingMessage(buttonPress, [guessRow]);
if (buttonPress.customID === 'true') {
win = false;
break;
} else if (timesGuessed >= 3 || forceGuess) {
win = true;
break;
}
}
this.client.games.delete(msg.channel.id);
const row = new MessageActionRow();
row.addComponents(
new MessageButton().setLabel('Akinator Website').setStyle('LINK').setURL('https://akinator.com/')
);
if (win === 'time') {
return buttonPress.editReply('I guess your silence means I have won.', { components: [row] });
}
if (win) {
return buttonPress.editReply('Bravo, you have defeated me.', { components: [row] });
}
return buttonPress.editReply('Guessed right one more time! I love playing with you!', { components: [row] });
} catch (err) {
this.client.games.delete(msg.channel.id);
throw err;
}
const row = new MessageActionRow();
row.addComponents(
new MessageButton().setLabel('Akinator Website').setStyle('LINK').setURL('https://akinator.com/')
);
if (win === 'time') {
return buttonPress.editReply('I guess your silence means I have won.', { components: [row] });
}
if (win) {
return buttonPress.editReply('Bravo, you have defeated me.', { components: [row] });
}
return buttonPress.editReply('Guessed right one more time! I love playing with you!', { components: [row] });
}
sendLoadingMessage(buttonPress, rows) {
+28 -32
View File
@@ -36,38 +36,34 @@ module.exports = class TrueOrFalseCommand extends Command {
}
async run(msg, { difficulty }) {
try {
const { body } = await request
.get('https://opentdb.com/api.php')
.query({
amount: 1,
type: 'boolean',
encode: 'url3986',
difficulty
});
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')
);
const questionMsg = await msg.reply(stripIndents`
**You have 15 seconds to answer this question.**
${decodeURIComponent(body.results[0].question)}
`, { components: [row] });
const filter = res => res.user.id === msg.author.id;
const interactions = await questionMsg.awaitMessageComponentInteractions(filter, {
max: 1,
time: 15000
const { body } = await request
.get('https://opentdb.com/api.php')
.query({
amount: 1,
type: 'boolean',
encode: 'url3986',
difficulty
});
if (!interactions.size) return questionMsg.edit(`Sorry, time is up! It was ${correctBool}.`, { components: [] });
const ans = interactions.first();
const ansBool = ans.customID === 'true';
if (correctBool !== ansBool) return ans.update(`Nope, sorry, it's ${correctBool}.`, { components: [] });
return ans.update('Nice job! 10/10! You deserve some cake!', { components: [] });
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
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')
);
const questionMsg = await msg.reply(stripIndents`
**You have 15 seconds to answer this question.**
${decodeURIComponent(body.results[0].question)}
`, { components: [row] });
const filter = res => res.user.id === msg.author.id;
const interactions = await questionMsg.awaitMessageComponentInteractions(filter, {
max: 1,
time: 15000
});
if (!interactions.size) return questionMsg.edit(`Sorry, time is up! It was ${correctBool}.`, { components: [] });
const ans = interactions.first();
const ansBool = ans.customID === 'true';
if (correctBool !== ansBool) return ans.update(`Nope, sorry, it's ${correctBool}.`, { components: [] });
return ans.update('Nice job! 10/10! You deserve some cake!', { components: [] });
}
};