mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-23 10:02:05 +02:00
Button Akinator
This commit is contained in:
@@ -1,8 +1,8 @@
|
|||||||
const Command = require('../../framework/Command');
|
const Command = require('../../framework/Command');
|
||||||
const { MessageEmbed } = require('discord.js');
|
const { MessageEmbed, MessageActionRow, MessageButton } = require('discord.js');
|
||||||
const { Aki, regions } = require('aki-api');
|
const { Aki, regions } = require('aki-api');
|
||||||
const { stripIndents } = require('common-tags');
|
const { stripIndents } = require('common-tags');
|
||||||
const { list, verify } = require('../../util/Util');
|
const { list } = require('../../util/Util');
|
||||||
|
|
||||||
module.exports = class AkinatorCommand extends Command {
|
module.exports = class AkinatorCommand extends Command {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
@@ -45,6 +45,22 @@ module.exports = class AkinatorCommand extends Command {
|
|||||||
let guessResetNum = 0;
|
let guessResetNum = 0;
|
||||||
let wentBack = false;
|
let wentBack = false;
|
||||||
let forceGuess = 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: [] });
|
||||||
const guessBlacklist = [];
|
const guessBlacklist = [];
|
||||||
this.client.games.set(msg.channel.id, { name: this.name });
|
this.client.games.set(msg.channel.id, { name: this.name });
|
||||||
while (timesGuessed < 3) {
|
while (timesGuessed < 3) {
|
||||||
@@ -61,27 +77,28 @@ module.exports = class AkinatorCommand extends Command {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!aki.answers || aki.currentStep >= 79) forceGuess = true;
|
if (!aki.answers || aki.currentStep >= 79) forceGuess = true;
|
||||||
const answers = aki.answers.map(answer => answer.toLowerCase());
|
const row = new MessageActionRow();
|
||||||
answers.push('end');
|
for (const answer of aki.answers) {
|
||||||
if (aki.currentStep > 0) answers.push('back');
|
row.addComponents(new MessageButton().setCustomID(answer).setStyle('PRIMARY').setLabel(answer));
|
||||||
await msg.say(stripIndents`
|
}
|
||||||
|
if (aki.currentStep > 0) {
|
||||||
|
row.addComponents(new MessageButton().setCustomID('back').setStyle('SECONDARY').setLabel('Back'));
|
||||||
|
}
|
||||||
|
row.addComponents(new MessageButton().setCustomID('end').setStyle('DANGER').setLabel('End'));
|
||||||
|
await buttonPress.update(stripIndents`
|
||||||
**${aki.currentStep + 1}.** ${aki.question} (${Math.round(Number.parseInt(aki.progress, 10))}%)
|
**${aki.currentStep + 1}.** ${aki.question} (${Math.round(Number.parseInt(aki.progress, 10))}%)
|
||||||
${aki.answers.join(' | ')}${aki.currentStep > 0 ? ` | Back` : ''} | End
|
${aki.answers.join(' | ')}${aki.currentStep > 0 ? ` | Back` : ''} | End
|
||||||
`);
|
`, { components: [row] });
|
||||||
const filter = res => {
|
const interactions = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
|
||||||
const content = res.content.toLowerCase().replace(/’/g, '\'');
|
|
||||||
return res.author.id === msg.author.id && answers.includes(content);
|
|
||||||
};
|
|
||||||
const msgs = await msg.channel.awaitMessages(filter, {
|
|
||||||
max: 1,
|
max: 1,
|
||||||
time: 30000
|
time: 30000
|
||||||
});
|
});
|
||||||
if (!msgs.size) {
|
if (!interactions.size) {
|
||||||
await msg.say('Sorry, time is up!');
|
|
||||||
win = 'time';
|
win = 'time';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
const choice = msgs.first().content.toLowerCase().replace(/’/g, '\'');
|
buttonPress = interactions.first();
|
||||||
|
const choice = interactions.first().customID;
|
||||||
if (choice === 'end') {
|
if (choice === 'end') {
|
||||||
forceGuess = true;
|
forceGuess = true;
|
||||||
} else if (choice === 'back') {
|
} else if (choice === 'back') {
|
||||||
@@ -90,7 +107,7 @@ module.exports = class AkinatorCommand extends Command {
|
|||||||
await aki.back();
|
await aki.back();
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
ans = answers.indexOf(choice);
|
ans = aki.answers.indexOf(choice);
|
||||||
}
|
}
|
||||||
if ((aki.progress >= 90 && !guessResetNum) || forceGuess) {
|
if ((aki.progress >= 90 && !guessResetNum) || forceGuess) {
|
||||||
timesGuessed++;
|
timesGuessed++;
|
||||||
@@ -98,7 +115,6 @@ module.exports = class AkinatorCommand extends Command {
|
|||||||
await aki.win();
|
await aki.win();
|
||||||
const guess = aki.answers.filter(g => !guessBlacklist.includes(g.id))[0];
|
const guess = aki.answers.filter(g => !guessBlacklist.includes(g.id))[0];
|
||||||
if (!guess) {
|
if (!guess) {
|
||||||
await msg.say('I can\'t think of anyone.');
|
|
||||||
win = true;
|
win = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -113,26 +129,33 @@ module.exports = class AkinatorCommand extends Command {
|
|||||||
`)
|
`)
|
||||||
.setThumbnail(guess.absolute_picture_path || null)
|
.setThumbnail(guess.absolute_picture_path || null)
|
||||||
.setFooter(forceGuess ? 'Final Guess' : `Guess ${timesGuessed}`);
|
.setFooter(forceGuess ? 'Final Guess' : `Guess ${timesGuessed}`);
|
||||||
await msg.embed(embed);
|
const guessRow = new MessageActionRow().addComponents(
|
||||||
const verification = await verify(msg.channel, msg.author);
|
new MessageButton().setCustomID('true').setLabel('Yes').setStyle('SUCCESS'),
|
||||||
if (verification === 0) {
|
new MessageButton().setCustomID('false').setLabel('No').setStyle('DANGER')
|
||||||
|
);
|
||||||
|
await buttonPress.update('Is this your character?', { embed, components: [guessRow] });
|
||||||
|
const verification = await gameMsg.awaitMessageComponentInteractions(res => res.user.id === msg.author.id, {
|
||||||
|
max: 1,
|
||||||
|
time: 30000
|
||||||
|
});
|
||||||
|
if (!verification.size) {
|
||||||
win = 'time';
|
win = 'time';
|
||||||
break;
|
break;
|
||||||
} else if (verification) {
|
}
|
||||||
|
const guessResult = verification.first();
|
||||||
|
if (guessResult.customID === 'true') {
|
||||||
win = false;
|
win = false;
|
||||||
break;
|
break;
|
||||||
} else if (timesGuessed >= 3 || forceGuess) {
|
} else if (timesGuessed >= 3 || forceGuess) {
|
||||||
win = true;
|
win = true;
|
||||||
break;
|
break;
|
||||||
} else {
|
|
||||||
await msg.say('Hmm... Is that so? I can keep going!');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
if (win === 'time') return msg.say('I guess your silence means I have won.');
|
if (win === 'time') return buttonPress.update('I guess your silence means I have won.', { components: [] });
|
||||||
if (win) return msg.say('Bravo, you have defeated me.');
|
if (win) return buttonPress.update('Bravo, you have defeated me.', { components: [] });
|
||||||
return msg.say('Guessed right one more time! I love playing with you!');
|
return buttonPress.update('Guessed right one more time! I love playing with you!', { components: [] });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.client.games.delete(msg.channel.id);
|
this.client.games.delete(msg.channel.id);
|
||||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||||
|
|||||||
Reference in New Issue
Block a user