mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Finish removing "Oh no, an error occurred"
This commit is contained in:
@@ -15,6 +15,7 @@ module.exports = class AnagramicaCommand extends Command {
|
||||
group: 'games-sp',
|
||||
memberName: 'anagramica',
|
||||
description: 'Try to find all the anagrams for a given set of letters.',
|
||||
game: true,
|
||||
credit: [
|
||||
{
|
||||
name: 'Max Irwin',
|
||||
@@ -36,72 +37,63 @@ module.exports = class AnagramicaCommand extends Command {
|
||||
}
|
||||
|
||||
async run(msg, { time }) {
|
||||
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 {
|
||||
this.client.games.set(msg.channel.id, { name: this.name });
|
||||
const { valid, letters } = await this.fetchList();
|
||||
let points = 0;
|
||||
await msg.reply(stripIndents`
|
||||
**You have ${time} seconds to provide anagrams for the following letters:**
|
||||
${letters.map(letter => `\`${letter.toUpperCase()}\``).join(' ')}
|
||||
const { valid, letters } = await this.fetchList();
|
||||
let points = 0;
|
||||
await msg.reply(stripIndents`
|
||||
**You have ${time} seconds to provide anagrams for the following letters:**
|
||||
${letters.map(letter => `\`${letter.toUpperCase()}\``).join(' ')}
|
||||
|
||||
_Need to see the list again? Type \`send list\` (or \`sl\`)._
|
||||
`);
|
||||
const picked = [];
|
||||
const filter = res => {
|
||||
if (res.author.id !== msg.author.id) return false;
|
||||
const choice = res.content.toLowerCase();
|
||||
if (choice === 'send list' || choice === 'sl') {
|
||||
msg.reply(letters.map(letter => `\`${letter.toUpperCase()}\``).join(' ')).catch(() => null);
|
||||
return true;
|
||||
}
|
||||
if (picked.includes(choice)) return false;
|
||||
const score = this.getScore(letters, choice);
|
||||
if (!score) return false;
|
||||
if (!valid.includes(choice)) {
|
||||
picked.push(choice);
|
||||
reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌');
|
||||
return false;
|
||||
}
|
||||
points += score;
|
||||
picked.push(choice);
|
||||
reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅');
|
||||
_Need to see the list again? Type \`send list\` (or \`sl\`)._
|
||||
`);
|
||||
const picked = [];
|
||||
const filter = res => {
|
||||
if (res.author.id !== msg.author.id) return false;
|
||||
const choice = res.content.toLowerCase();
|
||||
if (choice === 'send list' || choice === 'sl') {
|
||||
msg.reply(letters.map(letter => `\`${letter.toUpperCase()}\``).join(' ')).catch(() => null);
|
||||
return true;
|
||||
};
|
||||
const msgs = await msg.channel.awaitMessages({
|
||||
filter,
|
||||
time: time * 1000
|
||||
});
|
||||
const highScoreGet = await this.client.redis.get('anagramica');
|
||||
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
|
||||
const highScoreUser = await this.client.redis.get('anagramica-user');
|
||||
const scoreBeat = !highScore || highScore < points;
|
||||
const user = await fetchHSUserDisplay(this.client, highScoreUser);
|
||||
if (scoreBeat) {
|
||||
await this.client.redis.set('anagramica', points);
|
||||
await this.client.redis.set('anagramica-user', msg.author.id);
|
||||
}
|
||||
this.client.games.delete(msg.channel.id);
|
||||
const moreWords = shuffle(valid.filter(word => !picked.includes(word))).slice(0, 5);
|
||||
if (!msgs.size) {
|
||||
return msg.reply(stripIndents`
|
||||
Couldn't even think of one? Ouch.
|
||||
${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore} (Held by ${user})
|
||||
|
||||
Here's some words you missed: ${moreWords.map(word => `\`${word}\``).join(', ')}
|
||||
`);
|
||||
if (picked.includes(choice)) return false;
|
||||
const score = this.getScore(letters, choice);
|
||||
if (!score) return false;
|
||||
if (!valid.includes(choice)) {
|
||||
picked.push(choice);
|
||||
reactIfAble(res, res.author, FAILURE_EMOJI_ID, '❌');
|
||||
return false;
|
||||
}
|
||||
points += score;
|
||||
picked.push(choice);
|
||||
reactIfAble(res, res.author, SUCCESS_EMOJI_ID, '✅');
|
||||
return true;
|
||||
};
|
||||
const msgs = await msg.channel.awaitMessages({
|
||||
filter,
|
||||
time: time * 1000
|
||||
});
|
||||
const highScoreGet = await this.client.redis.get('anagramica');
|
||||
const highScore = highScoreGet ? Number.parseInt(highScoreGet, 10) : null;
|
||||
const highScoreUser = await this.client.redis.get('anagramica-user');
|
||||
const scoreBeat = !highScore || highScore < points;
|
||||
const user = await fetchHSUserDisplay(this.client, highScoreUser);
|
||||
if (scoreBeat) {
|
||||
await this.client.redis.set('anagramica', points);
|
||||
await this.client.redis.set('anagramica-user', msg.author.id);
|
||||
}
|
||||
const moreWords = shuffle(valid.filter(word => !picked.includes(word))).slice(0, 5);
|
||||
if (!msgs.size) {
|
||||
return msg.reply(stripIndents`
|
||||
Nice job! Your final score was **${points}**!
|
||||
Couldn't even think of one? Ouch.
|
||||
${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore} (Held by ${user})
|
||||
|
||||
Here's some words you missed: ${moreWords.map(word => `\`${word}\``).join(', ')}
|
||||
`);
|
||||
} catch (err) {
|
||||
this.client.games.delete(msg.channel.id);
|
||||
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||||
}
|
||||
return msg.reply(stripIndents`
|
||||
Nice job! Your final score was **${points}**!
|
||||
${scoreBeat ? `**New High Score!** Old:` : `High Score:`} ${highScore} (Held by ${user})
|
||||
|
||||
Here's some words you missed: ${moreWords.map(word => `\`${word}\``).join(', ')}
|
||||
`);
|
||||
}
|
||||
|
||||
async fetchList() {
|
||||
|
||||
Reference in New Issue
Block a user