From 3959a4765c8ce9cd845aa50f7d0c6796826199fc Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 12 Jan 2022 20:01:19 -0700 Subject: [PATCH] startup is now blazing fast bc of "multithreading" --- bot.js | 4 +++ events/ready.js | 1 + util/cache.js | 87 +++++++++++++++++++++++---------------------- util/cache/anime.js | 38 ++++++++++++-------- util/cache/ar.js | 23 ++++++++---- util/cache/bl.js | 37 +++++++++++++------ util/cache/char.js | 36 ++++++++++++------- util/cache/chest.js | 19 +++++++--- util/cache/lr.js | 20 ++++++++--- util/cache/lxp.js | 25 ++++++++----- util/cache/monit.js | 34 +++++++++++------- 11 files changed, 207 insertions(+), 117 deletions(-) diff --git a/bot.js b/bot.js index 1f32fc4..766ff39 100644 --- a/bot.js +++ b/bot.js @@ -15,6 +15,7 @@ const {TagFilter} = require('./util/tagfilter'); const flags = Discord.Intents.FLAGS; let fl = []; Object.keys(flags).forEach(flag => fl.push(flags[flag])); let client = new Discord.Client({intents: fl, partials: ["CHANNEL", "REACTION", "MESSAGE"]}); +let botReadyResolver; client.misc = { savers: ['497598953206841375', '480535078150340609', '468903364533420074'], @@ -69,6 +70,7 @@ client.misc = { gradients: false, spinners: false }, + botFinished: new Promise(r => {botReadyResolver = r;}), fullyReady: false }; @@ -159,6 +161,8 @@ async function init() { client.guildconfig.logs = new Map(); + botReadyResolver(0); + await require('./util/wait')(5000); if (!client.misc.readied) {client.misc.forcedReady = true; await require('./events/ready')(client);} } diff --git a/events/ready.js b/events/ready.js index c87a803..a654aef 100644 --- a/events/ready.js +++ b/events/ready.js @@ -17,6 +17,7 @@ let prefix = 'n?'; module.exports = async client => { if (client.misc.readied) {return;} client.misc.readied = true; + await client.misc.botFinished; const config = client.config; diff --git a/util/cache.js b/util/cache.js index 34e2f3c..09773b9 100644 --- a/util/cache.js +++ b/util/cache.js @@ -1,46 +1,49 @@ -const ora = require('ora'); +const gs = require('gradient-string'); +const spinnies = require('dreidels'); const chalk = require('chalk'); module.exports = async (client) => { - console.log(''); - let ora_arCache = ora("Caching ARs...").start(); - await require('./cache/ar')(client); - ora_arCache.stop(); ora_arCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.ar.size}`)} ${chalk.blueBright(`guilds with auto responses.`)}`); - - let ora_blCache = ora("Caching Blacklists...").start(); - await require('./cache/bl')(client); - ora_blCache.stop(); ora_blCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.bl.guild.length}`)} ${chalk.blueBright(`guild blacklists`)}`); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.bl.user.length}`)} ${chalk.blueBright(`user blacklists`)}`); - - let ora_lxpCache = ora("Caching Local XPs...").start(); - await require('./cache/lxp')(client); - ora_lxpCache.stop(); ora_lxpCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.enabled.length}`)} ${chalk.blueBright(`guilds with XP enabled.`)}`); - - let ora_lrCache = ora("Caching Level Roles...").start(); - await require('./cache/lr')(client); - ora_lrCache.stop(); ora_lrCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.hasLevelRoles.length}`)} ${chalk.blueBright(`guilds with Level Roles enabled.`)}`); - - let ora_moCache = ora("Caching Monitors...").start(); - await require('./cache/monit')(client); - ora_moCache.stop(); ora_moCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${Object.keys(client.misc.cache.monit).length}`)} ${chalk.blueBright(`guilds with Monitors enabled.`)}`); - - let ora_anCache = ora("Caching Animes...").start(); - await require('./cache/anime')(client); - ora_anCache.stop(); ora_anCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.animeNum}`)} ${chalk.blueBright(`animes into lookup registry.`)}`); - - let ora_chCache = ora("Caching Characters...").start(); - await require('./cache/char')(client); - ora_chCache.stop(); ora_chCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.charsNum}`)} ${chalk.blueBright(`characters into lookup registry.`)} ${chalk.gray(`(${client.misc.cache.chars.size} // NN)`)}`); - - let ora_ctCache = ora("Caching Chests...").start(); - await require('./cache/chest')(client); - ora_ctCache.stop(); ora_ctCache.clear(); - console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.chests.enabled.length}`)} ${chalk.blueBright("guilds that spawn chests.")}`); + return new Promise(async resolve => { + const loaders = []; + const spin = new spinnies(); + + console.log(''); + + let arCache = spin.add("ar", {text: "Caching ARs..."}); + loaders.push(require('./cache/ar')(client, arCache)); + + let blCache = spin.add("bl", {text: "Caching Guild Blacklists..."}); + let bluCache = spin.add("blu", {text: "Caching User Blacklists..."}); + loaders.push(require('./cache/bl')(client, blCache, bluCache)); + + let lxpCache = spin.add("xp", {text: "Caching Local XPs..."}); + loaders.push(require('./cache/lxp')(client, lxpCache)); + + let lrCache = spin.add("lr", {text: "Caching Level Roles..."}); + loaders.push(require('./cache/lr')(client, lrCache)); + + let moCache = spin.add("m", {text: "Caching Monitors..."}); + loaders.push(require('./cache/monit')(client, moCache)); + + let anCache = spin.add("a", {text: "Caching Animes..."}); + loaders.push(require('./cache/anime')(client, anCache)); + + let chCache = spin.add("ch", {text: "Caching Characters..."}); + loaders.push(require('./cache/char')(client, chCache)); + + let ctCache = spin.add("cht", {text: "Caching Chests..."}); + loaders.push(require('./cache/chest')(client, ctCache)); + + await Promise.all(loaders); + + /*console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.bl.guild.length}`)} ${chalk.blueBright(`guild blacklists`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.bl.user.length}`)} ${chalk.blueBright(`user blacklists`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.enabled.length}`)} ${chalk.blueBright(`guilds with XP enabled.`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.hasLevelRoles.length}`)} ${chalk.blueBright(`guilds with Level Roles enabled.`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${Object.keys(client.misc.cache.monit).length}`)} ${chalk.blueBright(`guilds with Monitors enabled.`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.animeNum}`)} ${chalk.blueBright(`animes into lookup registry.`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.charsNum}`)} ${chalk.blueBright(`characters into lookup registry.`)} ${chalk.gray(`(${client.misc.cache.chars.size} // NN)`)}`); + console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.chests.enabled.length}`)} ${chalk.blueBright("guilds that spawn chests.")}`);*/ + resolve(0); + }); }; \ No newline at end of file diff --git a/util/cache/anime.js b/util/cache/anime.js index 2e88d48..5d089e5 100644 --- a/util/cache/anime.js +++ b/util/cache/anime.js @@ -1,21 +1,31 @@ const Discord = require('discord.js'); +const chalk = require('chalk'); const AniData = require('../../models/anime'); -module.exports = async client => { - client.misc.cache.anime = new Discord.Collection(); - client.misc.cache.animeID = new Discord.Collection(); - client.misc.cache.animeLove = new Discord.Collection(); - client.misc.cache.animeNum = 0; +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.anime = new Discord.Collection(); + client.misc.cache.animeID = new Discord.Collection(); + client.misc.cache.animeLove = new Discord.Collection(); + client.misc.cache.animeNum = 0; + let amount = 0; - for await (const ani of AniData.find()) { - if (ani.queued !== true) { - client.misc.cache.anime.set(ani.japname, ani.id); - client.misc.cache.anime.set(ani.name, ani.id); - if (ani.altNames) {ani.altNames.forEach(altName => client.misc.cache.anime.set(altName, ani.id));} - client.misc.cache.animeID.set(ani.id, ani.name); - client.misc.cache.animeLove.set(ani.id, ani.watchers); - client.misc.cache.animeNum++; + for await (const ani of AniData.find()) { + if (ani.queued !== true) { + client.misc.cache.anime.set(ani.japname, ani.id); + client.misc.cache.anime.set(ani.name, ani.id); + if (ani.altNames) {ani.altNames.forEach(altName => client.misc.cache.anime.set(altName, ani.id));} + client.misc.cache.animeID.set(ani.id, ani.name); + client.misc.cache.animeLove.set(ani.id, ani.watchers); + client.misc.cache.animeNum++; + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`animes into lookup registry.`)}`}); + amount++; + } } - } + + spinner.status('non-spinnable'); + + resolve(0); + }); } \ No newline at end of file diff --git a/util/cache/ar.js b/util/cache/ar.js index f096a75..d4a4665 100644 --- a/util/cache/ar.js +++ b/util/cache/ar.js @@ -1,11 +1,20 @@ const AR = require('../../models/ar'); +const chalk = require('chalk'); -module.exports = async client => { - client.misc.cache.ar = new Map(); - client.misc.cache.arIgnore = new Map(); +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.ar = new Map(); + client.misc.cache.arIgnore = new Map(); + let amount = 0; - for await (const ar of AR.find()) { - client.misc.cache.ar.set(ar.gid, ar.triggers); - if (ar.ignoreChs.length) {client.misc.cache.arIgnore.set(ar.gid, ar.ignoreChs);} - } + for await (const ar of AR.find()) { + client.misc.cache.ar.set(ar.gid, ar.triggers); + if (ar.ignoreChs.length) {client.misc.cache.arIgnore.set(ar.gid, ar.ignoreChs);} + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guilds with auto responses.`)}`}); + amount++; + } + + spinner.status('non-spinnable'); + resolve(0); + }); }; \ No newline at end of file diff --git a/util/cache/bl.js b/util/cache/bl.js index a74d16f..c49ba69 100644 --- a/util/cache/bl.js +++ b/util/cache/bl.js @@ -1,17 +1,32 @@ const GuildData = require('../../models/guild'); const UserData = require('../../models/user'); +const chalk = require('chalk'); -module.exports = async (client) => { - client.misc.cache.bl = { - guild: [], - user: [] - }; +module.exports = async (client, spinner, spinner2) => { + return new Promise(async resolve => { + client.misc.cache.bl = { + guild: [], + user: [] + }; + let amount = 0; + let amount2 = 0; - for await (const guild of GuildData.find()) { - if (guild.blacklisted) {client.misc.cache.bl.guild.push(guild.gid);} - } + for await (const guild of GuildData.find()) { + if (guild.blacklisted) {client.misc.cache.bl.guild.push(guild.gid); amount++;} + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guild blacklists.`)}`}); + } - for await (const user of UserData.find()) { - if (user.blackisted) {client.misc.cache.bl.user.push(user.uid);} - } + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guild blacklists.`)}`}); + spinner.status('non-spinnable'); + + for await (const user of UserData.find()) { + if (user.blackisted) {client.misc.cache.bl.user.push(user.uid); amount2++;} + spinner2.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount2}`)} ${chalk.blueBright(`user blacklists.`)}`}); + } + + spinner2.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount2}`)} ${chalk.blueBright(`user blacklists.`)}`}); + spinner2.status('non-spinnable'); + + resolve(0); + }); }; \ No newline at end of file diff --git a/util/cache/char.js b/util/cache/char.js index cb4aa88..4059362 100644 --- a/util/cache/char.js +++ b/util/cache/char.js @@ -1,20 +1,30 @@ const Discord = require('discord.js'); +const chalk = require('chalk'); const CharData = require('../../models/char'); -module.exports = async client => { - client.misc.cache.chars = new Discord.Collection(); - client.misc.cache.charsID = new Discord.Collection(); - client.misc.cache.charsLove = new Discord.Collection(); - client.misc.cache.charsNum = 0; +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.chars = new Discord.Collection(); + client.misc.cache.charsID = new Discord.Collection(); + client.misc.cache.charsLove = new Discord.Collection(); + client.misc.cache.charsNum = 0; + let amount = 0; - for await (const char of CharData.find()) { - if (char.queued !== true) { - client.misc.cache.chars.set(char.name, char.id); - char.nicknames.forEach(nn => client.misc.cache.chars.set(nn, char.id)); - client.misc.cache.charsID.set(char.id, char.name); - client.misc.cache.charsNum++; - client.misc.cache.charsLove.set(char.id, char.loved); + for await (const char of CharData.find()) { + if (char.queued !== true) { + client.misc.cache.chars.set(char.name, char.id); + char.nicknames.forEach(nn => client.misc.cache.chars.set(nn, char.id)); + client.misc.cache.charsID.set(char.id, char.name); + client.misc.cache.charsNum++; + client.misc.cache.charsLove.set(char.id, char.loved); + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`characters into lookup registry.`)} ${chalk.gray(`(${client.misc.cache.chars.size} // NN)`)}`}); + amount++; + } } - } + + spinner.status('non-spinnable'); + + resolve(0); + }); } \ No newline at end of file diff --git a/util/cache/chest.js b/util/cache/chest.js index 4ab945c..8b668f3 100644 --- a/util/cache/chest.js +++ b/util/cache/chest.js @@ -1,9 +1,18 @@ const Chests = require('../../models/chests'); +const chalk = require('chalk'); -module.exports = async client => { - client.misc.cache.chests.enabled = []; +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.chests.enabled = []; + let amount = 0; - for await (const chest of Chests.find()) { - client.misc.cache.chests.enabled.push(chest.gid); - } + for await (const chest of Chests.find()) { + client.misc.cache.chests.enabled.push(chest.gid); + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guilds that spawn chests.`)}`}); + amount++; + } + + spinner.status('non-spinnable'); + resolve(0); + }); }; \ No newline at end of file diff --git a/util/cache/lr.js b/util/cache/lr.js index 1b0bb7b..c99fa21 100644 --- a/util/cache/lr.js +++ b/util/cache/lr.js @@ -1,9 +1,19 @@ const LR = require('../../models/levelroles'); +const chalk = require('chalk'); -module.exports = async client => { - client.misc.cache.lxp.hasLevelRoles = []; +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.lxp.hasLevelRoles = []; + let amount = 0; - for await (const lr of LR.find()) { - client.misc.cache.lxp.hasLevelRoles.push(lr.gid); - } + for await (const lr of LR.find()) { + client.misc.cache.lxp.hasLevelRoles.push(lr.gid); + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guilds with Level Roles enabled.`)}`}); + amount++; + } + + spinner.status('non-spinnable'); + + resolve(0); + }); }; \ No newline at end of file diff --git a/util/cache/lxp.js b/util/cache/lxp.js index 3dc45c8..16a99d7 100644 --- a/util/cache/lxp.js +++ b/util/cache/lxp.js @@ -1,12 +1,21 @@ const LXP = require('../../models/localxp'); +const chalk = require('chalk'); -module.exports = async client => { - client.misc.cache.lxp.enabled = []; - client.misc.cache.lxp.disabledChannels = new Map(); - client.misc.cache.chests.enabled = new Map(); +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.lxp.enabled = []; + client.misc.cache.lxp.disabledChannels = new Map(); + client.misc.cache.chests.enabled = new Map(); + let amount = 0; - for await (const xp of LXP.find()) { - client.misc.cache.lxp.enabled.push(xp.gid); - if (xp.noGains && xp.noGains.length) {client.misc.cache.lxp.disabledChannels.set(xp.gid, xp.noGains);} - } + for await (const xp of LXP.find()) { + client.misc.cache.lxp.enabled.push(xp.gid); + if (xp.noGains && xp.noGains.length) {client.misc.cache.lxp.disabledChannels.set(xp.gid, xp.noGains);} + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guilds with XP enabled.`)}`}); + amount++; + } + + spinner.status('non-spinnable'); + resolve(0); + }); }; \ No newline at end of file diff --git a/util/cache/monit.js b/util/cache/monit.js index 3c845e3..877f28a 100644 --- a/util/cache/monit.js +++ b/util/cache/monit.js @@ -1,15 +1,25 @@ const Monitor = require('../../models/monitor'); +const chalk = require('chalk'); -module.exports = async client => { - client.misc.cache.monit = {}; - client.misc.cache.monitEnabled = []; - - for await (const tm of Monitor.find()) { - client.misc.cache.monit[tm.gid] = { - messages: tm.messages, - voice: tm.voice, - expiry: new Date() - }; - client.misc.cache.monitEnabled.push(tm.gid); - } +module.exports = async (client, spinner) => { + return new Promise(async resolve => { + client.misc.cache.monit = {}; + client.misc.cache.monitEnabled = []; + let amount = 0; + + for await (const tm of Monitor.find()) { + client.misc.cache.monit[tm.gid] = { + messages: tm.messages, + voice: tm.voice, + expiry: new Date() + }; + client.misc.cache.monitEnabled.push(tm.gid); + spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guilds with Monitors enabled.`)}`}); + amount++; + } + + spinner.status('non-spinnable'); + + resolve(0); + }); } \ No newline at end of file