startup is now blazing fast bc of "multithreading"

master
Kit Kasune 3 years ago
parent ad047ffa19
commit 3959a4765c
  1. 4
      bot.js
  2. 1
      events/ready.js
  3. 87
      util/cache.js
  4. 38
      util/cache/anime.js
  5. 23
      util/cache/ar.js
  6. 37
      util/cache/bl.js
  7. 36
      util/cache/char.js
  8. 19
      util/cache/chest.js
  9. 20
      util/cache/lr.js
  10. 25
      util/cache/lxp.js
  11. 34
      util/cache/monit.js

@ -15,6 +15,7 @@ const {TagFilter} = require('./util/tagfilter');
const flags = Discord.Intents.FLAGS; const flags = Discord.Intents.FLAGS;
let fl = []; Object.keys(flags).forEach(flag => fl.push(flags[flag])); let fl = []; Object.keys(flags).forEach(flag => fl.push(flags[flag]));
let client = new Discord.Client({intents: fl, partials: ["CHANNEL", "REACTION", "MESSAGE"]}); let client = new Discord.Client({intents: fl, partials: ["CHANNEL", "REACTION", "MESSAGE"]});
let botReadyResolver;
client.misc = { client.misc = {
savers: ['497598953206841375', '480535078150340609', '468903364533420074'], savers: ['497598953206841375', '480535078150340609', '468903364533420074'],
@ -69,6 +70,7 @@ client.misc = {
gradients: false, gradients: false,
spinners: false spinners: false
}, },
botFinished: new Promise(r => {botReadyResolver = r;}),
fullyReady: false fullyReady: false
}; };
@ -159,6 +161,8 @@ async function init() {
client.guildconfig.logs = new Map(); client.guildconfig.logs = new Map();
botReadyResolver(0);
await require('./util/wait')(5000); await require('./util/wait')(5000);
if (!client.misc.readied) {client.misc.forcedReady = true; await require('./events/ready')(client);} if (!client.misc.readied) {client.misc.forcedReady = true; await require('./events/ready')(client);}
} }

@ -17,6 +17,7 @@ let prefix = 'n?';
module.exports = async client => { module.exports = async client => {
if (client.misc.readied) {return;} if (client.misc.readied) {return;}
client.misc.readied = true; client.misc.readied = true;
await client.misc.botFinished;
const config = client.config; const config = client.config;

@ -1,46 +1,49 @@
const ora = require('ora'); const gs = require('gradient-string');
const spinnies = require('dreidels');
const chalk = require('chalk'); const chalk = require('chalk');
module.exports = async (client) => { module.exports = async (client) => {
console.log(''); return new Promise(async resolve => {
let ora_arCache = ora("Caching ARs...").start(); const loaders = [];
await require('./cache/ar')(client); const spin = new spinnies();
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.`)}`); console.log('');
let ora_blCache = ora("Caching Blacklists...").start(); let arCache = spin.add("ar", {text: "Caching ARs..."});
await require('./cache/bl')(client); loaders.push(require('./cache/ar')(client, arCache));
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`)}`); let blCache = spin.add("bl", {text: "Caching Guild Blacklists..."});
console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.bl.user.length}`)} ${chalk.blueBright(`user blacklists`)}`); let bluCache = spin.add("blu", {text: "Caching User Blacklists..."});
loaders.push(require('./cache/bl')(client, blCache, bluCache));
let ora_lxpCache = ora("Caching Local XPs...").start();
await require('./cache/lxp')(client); let lxpCache = spin.add("xp", {text: "Caching Local XPs..."});
ora_lxpCache.stop(); ora_lxpCache.clear(); loaders.push(require('./cache/lxp')(client, lxpCache));
console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.enabled.length}`)} ${chalk.blueBright(`guilds with XP enabled.`)}`);
let lrCache = spin.add("lr", {text: "Caching Level Roles..."});
let ora_lrCache = ora("Caching Level Roles...").start(); loaders.push(require('./cache/lr')(client, lrCache));
await require('./cache/lr')(client);
ora_lrCache.stop(); ora_lrCache.clear(); let moCache = spin.add("m", {text: "Caching Monitors..."});
console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.lxp.hasLevelRoles.length}`)} ${chalk.blueBright(`guilds with Level Roles enabled.`)}`); loaders.push(require('./cache/monit')(client, moCache));
let ora_moCache = ora("Caching Monitors...").start(); let anCache = spin.add("a", {text: "Caching Animes..."});
await require('./cache/monit')(client); loaders.push(require('./cache/anime')(client, anCache));
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 chCache = spin.add("ch", {text: "Caching Characters..."});
loaders.push(require('./cache/char')(client, chCache));
let ora_anCache = ora("Caching Animes...").start();
await require('./cache/anime')(client); let ctCache = spin.add("cht", {text: "Caching Chests..."});
ora_anCache.stop(); ora_anCache.clear(); loaders.push(require('./cache/chest')(client, ctCache));
console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.animeNum}`)} ${chalk.blueBright(`animes into lookup registry.`)}`);
await Promise.all(loaders);
let ora_chCache = ora("Caching Characters...").start();
await require('./cache/char')(client); /*console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.bl.guild.length}`)} ${chalk.blueBright(`guild blacklists`)}`);
ora_chCache.stop(); ora_chCache.clear(); 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.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.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.`)}`);
let ora_ctCache = ora("Caching Chests...").start(); console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${Object.keys(client.misc.cache.monit).length}`)} ${chalk.blueBright(`guilds with Monitors enabled.`)}`);
await require('./cache/chest')(client); console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.animeNum}`)} ${chalk.blueBright(`animes into lookup registry.`)}`);
ora_ctCache.stop(); ora_ctCache.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)`)}`);
console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.chests.enabled.length}`)} ${chalk.blueBright("guilds that spawn chests.")}`); console.log(`${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${client.misc.cache.chests.enabled.length}`)} ${chalk.blueBright("guilds that spawn chests.")}`);*/
resolve(0);
});
}; };

@ -1,21 +1,31 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const chalk = require('chalk');
const AniData = require('../../models/anime'); const AniData = require('../../models/anime');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.anime = new Discord.Collection(); return new Promise(async resolve => {
client.misc.cache.animeID = new Discord.Collection(); client.misc.cache.anime = new Discord.Collection();
client.misc.cache.animeLove = new Discord.Collection(); client.misc.cache.animeID = new Discord.Collection();
client.misc.cache.animeNum = 0; client.misc.cache.animeLove = new Discord.Collection();
client.misc.cache.animeNum = 0;
let amount = 0;
for await (const ani of AniData.find()) { for await (const ani of AniData.find()) {
if (ani.queued !== true) { if (ani.queued !== true) {
client.misc.cache.anime.set(ani.japname, ani.id); client.misc.cache.anime.set(ani.japname, ani.id);
client.misc.cache.anime.set(ani.name, 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));} 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.animeID.set(ani.id, ani.name);
client.misc.cache.animeLove.set(ani.id, ani.watchers); client.misc.cache.animeLove.set(ani.id, ani.watchers);
client.misc.cache.animeNum++; 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);
});
} }

23
util/cache/ar.js vendored

@ -1,11 +1,20 @@
const AR = require('../../models/ar'); const AR = require('../../models/ar');
const chalk = require('chalk');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.ar = new Map(); return new Promise(async resolve => {
client.misc.cache.arIgnore = new Map(); client.misc.cache.ar = new Map();
client.misc.cache.arIgnore = new Map();
let amount = 0;
for await (const ar of AR.find()) { for await (const ar of AR.find()) {
client.misc.cache.ar.set(ar.gid, ar.triggers); client.misc.cache.ar.set(ar.gid, ar.triggers);
if (ar.ignoreChs.length) {client.misc.cache.arIgnore.set(ar.gid, ar.ignoreChs);} 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);
});
}; };

37
util/cache/bl.js vendored

@ -1,17 +1,32 @@
const GuildData = require('../../models/guild'); const GuildData = require('../../models/guild');
const UserData = require('../../models/user'); const UserData = require('../../models/user');
const chalk = require('chalk');
module.exports = async (client) => { module.exports = async (client, spinner, spinner2) => {
client.misc.cache.bl = { return new Promise(async resolve => {
guild: [], client.misc.cache.bl = {
user: [] guild: [],
}; user: []
};
let amount = 0;
let amount2 = 0;
for await (const guild of GuildData.find()) { for await (const guild of GuildData.find()) {
if (guild.blacklisted) {client.misc.cache.bl.guild.push(guild.gid);} 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()) { spinner.update({text: `${chalk.gray('[PROC]')} >> ${chalk.blueBright(`Cached`)} ${chalk.white(`${amount}`)} ${chalk.blueBright(`guild blacklists.`)}`});
if (user.blackisted) {client.misc.cache.bl.user.push(user.uid);} 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);
});
}; };

36
util/cache/char.js vendored

@ -1,20 +1,30 @@
const Discord = require('discord.js'); const Discord = require('discord.js');
const chalk = require('chalk');
const CharData = require('../../models/char'); const CharData = require('../../models/char');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.chars = new Discord.Collection(); return new Promise(async resolve => {
client.misc.cache.charsID = new Discord.Collection(); client.misc.cache.chars = new Discord.Collection();
client.misc.cache.charsLove = new Discord.Collection(); client.misc.cache.charsID = new Discord.Collection();
client.misc.cache.charsNum = 0; client.misc.cache.charsLove = new Discord.Collection();
client.misc.cache.charsNum = 0;
let amount = 0;
for await (const char of CharData.find()) { for await (const char of CharData.find()) {
if (char.queued !== true) { if (char.queued !== true) {
client.misc.cache.chars.set(char.name, char.id); client.misc.cache.chars.set(char.name, char.id);
char.nicknames.forEach(nn => client.misc.cache.chars.set(nn, 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.charsID.set(char.id, char.name);
client.misc.cache.charsNum++; client.misc.cache.charsNum++;
client.misc.cache.charsLove.set(char.id, char.loved); 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);
});
} }

@ -1,9 +1,18 @@
const Chests = require('../../models/chests'); const Chests = require('../../models/chests');
const chalk = require('chalk');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.chests.enabled = []; return new Promise(async resolve => {
client.misc.cache.chests.enabled = [];
let amount = 0;
for await (const chest of Chests.find()) { for await (const chest of Chests.find()) {
client.misc.cache.chests.enabled.push(chest.gid); 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);
});
}; };

20
util/cache/lr.js vendored

@ -1,9 +1,19 @@
const LR = require('../../models/levelroles'); const LR = require('../../models/levelroles');
const chalk = require('chalk');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.lxp.hasLevelRoles = []; return new Promise(async resolve => {
client.misc.cache.lxp.hasLevelRoles = [];
let amount = 0;
for await (const lr of LR.find()) { for await (const lr of LR.find()) {
client.misc.cache.lxp.hasLevelRoles.push(lr.gid); 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);
});
}; };

25
util/cache/lxp.js vendored

@ -1,12 +1,21 @@
const LXP = require('../../models/localxp'); const LXP = require('../../models/localxp');
const chalk = require('chalk');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.lxp.enabled = []; return new Promise(async resolve => {
client.misc.cache.lxp.disabledChannels = new Map(); client.misc.cache.lxp.enabled = [];
client.misc.cache.chests.enabled = new Map(); client.misc.cache.lxp.disabledChannels = new Map();
client.misc.cache.chests.enabled = new Map();
let amount = 0;
for await (const xp of LXP.find()) { for await (const xp of LXP.find()) {
client.misc.cache.lxp.enabled.push(xp.gid); client.misc.cache.lxp.enabled.push(xp.gid);
if (xp.noGains && xp.noGains.length) {client.misc.cache.lxp.disabledChannels.set(xp.gid, xp.noGains);} 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);
});
}; };

@ -1,15 +1,25 @@
const Monitor = require('../../models/monitor'); const Monitor = require('../../models/monitor');
const chalk = require('chalk');
module.exports = async client => { module.exports = async (client, spinner) => {
client.misc.cache.monit = {}; return new Promise(async resolve => {
client.misc.cache.monitEnabled = []; client.misc.cache.monit = {};
client.misc.cache.monitEnabled = [];
for await (const tm of Monitor.find()) { let amount = 0;
client.misc.cache.monit[tm.gid] = {
messages: tm.messages, for await (const tm of Monitor.find()) {
voice: tm.voice, client.misc.cache.monit[tm.gid] = {
expiry: new Date() messages: tm.messages,
}; voice: tm.voice,
client.misc.cache.monitEnabled.push(tm.gid); 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);
});
} }
Loading…
Cancel
Save