Make smash cache a Collection

This commit is contained in:
Dragon Fire
2021-05-30 09:58:11 -04:00
parent 87c3b0877a
commit c53a5a462e
+5 -6
View File
@@ -1,4 +1,5 @@
const Command = require('../../structures/Command'); const Command = require('../../structures/Command');
const Collection = require('@discordjs/collection');
const { MessageEmbed } = require('discord.js'); const { MessageEmbed } = require('discord.js');
const request = require('node-superfetch'); const request = require('node-superfetch');
@@ -28,7 +29,7 @@ module.exports = class SmashBrosCommand extends Command {
] ]
}); });
this.cache = null; this.cache = new Collection();
} }
async run(msg, { query }) { async run(msg, { query }) {
@@ -59,9 +60,8 @@ module.exports = class SmashBrosCommand extends Command {
} }
async fetchFighters() { async fetchFighters() {
if (this.cache) return this.cache; if (this.cache.size) return this.cache;
const { body } = await request.get('https://www.smashbros.com/assets_v2/data/fighter.json'); const { body } = await request.get('https://www.smashbros.com/assets_v2/data/fighter.json');
const fighters = [];
for (const fighter of body.fighters) { for (const fighter of body.fighters) {
const data = { const data = {
name: fighter.displayName.en_US.replaceAll('<br>', ''), name: fighter.displayName.en_US.replaceAll('<br>', ''),
@@ -77,10 +77,9 @@ module.exports = class SmashBrosCommand extends Command {
dlc: Boolean(fighter.dlc), dlc: Boolean(fighter.dlc),
slug: this.makeSlug(fighter.displayName.en_US) slug: this.makeSlug(fighter.displayName.en_US)
}; };
fighters.push(data); this.cache.set(fighter.id, data);
} }
this.cache = fighters; setTimeout(() => this.cache.clear(), 8.64e+7);
setTimeout(() => { this.cache = null; }, 8.64e+7);
return this.cache; return this.cache;
} }