Fix button-based stuff

This commit is contained in:
Dragon Fire
2024-03-21 16:32:39 -04:00
parent 60b4d5b5b7
commit 99b71f5657
5 changed files with 63 additions and 54 deletions
+23 -22
View File
@@ -48,16 +48,16 @@ module.exports = class AkinatorCommand extends Command {
const gameMsg = await msg.reply('Welcome to Akinator! Think of a character, and I will try to guess it.', { const gameMsg = await msg.reply('Welcome to Akinator! Think of a character, and I will try to guess it.', {
components: [initialRow] components: [initialRow]
}); });
const initialVerify = await gameMsg.awaitMessageComponent({ try {
filter: res => res.user.id === msg.author.id, const initialVerify = await gameMsg.awaitMessageComponent({
max: 1, filter: res => res.user.id === msg.author.id,
time: 30000 max: 1,
}); time: 30000
if (!initialVerify.size) { });
if (initialVerify.customId === 'false') return initialVerify.update('Too bad...', { components: [] });
} catch {
return gameMsg.edit('Guess you didn\'t want to play after all...', { components: [] }); 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]); await this.sendLoadingMessage(buttonPress, [initialRow]);
const guessBlacklist = []; const guessBlacklist = [];
while (timesGuessed < 3) { while (timesGuessed < 3) {
@@ -87,16 +87,17 @@ module.exports = class AkinatorCommand extends Command {
`**${aki.currentStep + 1}.** ${aki.question} (${Math.round(Number.parseInt(aki.progress, 10))}%)`, `**${aki.currentStep + 1}.** ${aki.question} (${Math.round(Number.parseInt(aki.progress, 10))}%)`,
{ components: [row, sRow] } { components: [row, sRow] }
); );
const interactions = await gameMsg.awaitMessageComponent({ let buttonPress;
filter: res => res.user.id === msg.author.id, try {
max: 1, buttonPress = await gameMsg.awaitMessageComponent({
time: 30000 filter: res => res.user.id === msg.author.id,
}); max: 1,
if (!interactions.size) { time: 30000
});
} catch {
win = 'time'; win = 'time';
break; break;
} }
buttonPress = interactions.first();
await this.sendLoadingMessage(buttonPress, [row, sRow]); await this.sendLoadingMessage(buttonPress, [row, sRow]);
const choice = interactions.first().customId; const choice = interactions.first().customId;
if (choice === 'end') { if (choice === 'end') {
@@ -130,16 +131,16 @@ module.exports = class AkinatorCommand extends Command {
new MessageButton().setCustomId('false').setLabel('No').setStyle('DANGER') new MessageButton().setCustomId('false').setLabel('No').setStyle('DANGER')
); );
await buttonPress.editReply('Is this your character?', { embeds: [embed], components: [guessRow] }); await buttonPress.editReply('Is this your character?', { embeds: [embed], components: [guessRow] });
const verification = await gameMsg.awaitMessageComponent({ try {
filter: res => res.user.id === msg.author.id, buttonPress = await gameMsg.awaitMessageComponent({
max: 1, filter: res => res.user.id === msg.author.id,
time: 30000 max: 1,
}); time: 30000
if (!verification.size) { });
} catch {
win = 'time'; win = 'time';
break; break;
} }
buttonPress = verification.first();
await this.sendLoadingMessage(buttonPress, [guessRow]); await this.sendLoadingMessage(buttonPress, [guessRow]);
if (buttonPress.customId === 'true') { if (buttonPress.customId === 'true') {
win = false; win = false;
+12 -10
View File
@@ -56,15 +56,17 @@ module.exports = class TrueOrFalseCommand extends Command {
${decodeURIComponent(body.results[0].question)} ${decodeURIComponent(body.results[0].question)}
`, { components: [row] }); `, { components: [row] });
const filter = res => res.user.id === msg.author.id; const filter = res => res.user.id === msg.author.id;
const interactions = await questionMsg.awaitMessageComponent({ try {
filter, const ans = await questionMsg.awaitMessageComponent({
max: 1, filter,
time: 15000 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';
const ansBool = ans.customId === 'true'; if (correctBool !== ansBool) return ans.update(`Nope, sorry, it's ${correctBool}.`, { components: [] });
if (correctBool !== ansBool) return ans.update(`Nope, sorry, it's ${correctBool}.`, { components: [] }); return ans.update('Nice job! 10/10! You deserve some cake!', { components: [] });
return ans.update('Nice job! 10/10! You deserve some cake!', { components: [] }); } catch {
return questionMsg.edit(`Sorry, time is up! It was ${correctBool}.`, { components: [] });
}
} }
}; };
+12 -10
View File
@@ -56,17 +56,19 @@ module.exports = class Game {
} }
createJoinLeaveCollector() { createJoinLeaveCollector() {
const collector = this.channel.createMessageCollector(res => { const collector = this.channel.createMessageCollector({
if (res.author.bot) return false; filter: res => {
if (this.players.has(res.author.id) && res.content.toLowerCase() !== 'leave game') return false; if (res.author.bot) return false;
if (!this.players.has(res.author.id) && res.content.toLowerCase() !== 'join game') return false; if (this.players.has(res.author.id) && res.content.toLowerCase() !== 'leave game') return false;
if (this.czar.id === res.author.id || this.players.size >= 10) { if (!this.players.has(res.author.id) && res.content.toLowerCase() !== 'join game') return false;
reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌'); if (this.czar.id === res.author.id || this.players.size >= 10) {
return false; reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌');
return false;
}
if (!['join game', 'leave game'].includes(res.content.toLowerCase())) return false;
reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅');
return true;
} }
if (!['join game', 'leave game'].includes(res.content.toLowerCase())) return false;
reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅');
return true;
}); });
collector.on('collect', msg => { collector.on('collect', msg => {
if (msg.content.toLowerCase() === 'join game') this.addUser(msg.author); if (msg.content.toLowerCase() === 'join game') this.addUser(msg.author);
+13 -10
View File
@@ -53,16 +53,19 @@ module.exports = class Player {
await this.sendHand(hand, black); await this.sendHand(hand, black);
let gambled = false; let gambled = false;
let swapped = false; let swapped = false;
const collector = this.user.dmChannel.createMessageCollector(res => { const collector = this.user.dmChannel.createMessageCollector({
if (res.content.toLowerCase() === 'swap' && this.points > 0 && !swapped) return true; filter: res => {
if (res.content.toLowerCase() === 'gamble' && this.points > 0 && !gambled) return true; if (res.content.toLowerCase() === 'swap' && this.points > 0 && !swapped) return true;
const existing = hand[Number.parseInt(res.content, 10) - 1]; if (res.content.toLowerCase() === 'gamble' && this.points > 0 && !gambled) return true;
if (!existing || chosen.includes(existing)) { const existing = hand[Number.parseInt(res.content, 10) - 1];
reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌'); if (!existing || chosen.includes(existing)) {
return false; reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌');
} return false;
return true; }
}, { time: 60000 }); return true;
},
time: 60000
});
collector.on('collect', async msg => { collector.on('collect', async msg => {
const existing = hand[Number.parseInt(msg.content, 10) - 1]; const existing = hand[Number.parseInt(msg.content, 10) - 1];
if (msg.content.toLowerCase() === 'swap') { if (msg.content.toLowerCase() === 'swap') {
+2 -1
View File
@@ -359,7 +359,8 @@ module.exports = class Util {
if (joined.includes(interaction.user.id)) return false; if (joined.includes(interaction.user.id)) return false;
return true; return true;
}; };
const collector = msg.channel.createMessageComponentCollector(filter, { const collector = msg.channel.createMessageComponentCollector({
filter,
componentType: 'BUTTON', componentType: 'BUTTON',
max: max - 1, max: max - 1,
time: 120000 time: 120000