From 41628dfd21f52443b7301855ac4a748acdf35930 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sat, 13 Feb 2021 21:19:34 -0700 Subject: [PATCH] Cache mgnr, bl functional+bugs, bl cache --- bot.js | 6 +++++- commands/blacklist.js | 23 +++++++++++------------ events/message.js | 6 ++++++ events/ready.js | 6 +----- util/cache.js | 16 ++++++++++++++++ util/cache/bl.js | 17 +++++++++++++++++ 6 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 util/cache.js create mode 100644 util/cache/bl.js diff --git a/bot.js b/bot.js index 1c375c4..841b004 100644 --- a/bot.js +++ b/bot.js @@ -12,7 +12,11 @@ client.misc = { startup: new Date(), startupNoConnect: null, cache: { - ar: new Map() + ar: new Map(), + bl: { + guild: [], + user: [] + } } }; diff --git a/commands/blacklist.js b/commands/blacklist.js index e6e59cd..48eda50 100644 --- a/commands/blacklist.js +++ b/commands/blacklist.js @@ -7,11 +7,10 @@ module.exports = { aliases: ['bl'], help: "Disables a user from using Natsuki ( Usage: {{p}}blacklist )", async execute(message, msg, args, cmd, prefix, mention, client) { - - let tu = await UserData.findOne({ uid: message.author.id }); + let tu = await UserData.findOne({uid: message.author.id}); if (['g', 'guild'].includes(args[0].toLowerCase())) { - if(!tu || !tu.admin) { return message.channel.send('Sorry... you have to be a Natsuki Admin to do this!');} + if (!tu || !tu.admin) {return message.channel.send('Sorry... you have to be a Natsuki Admin to do this!');} let guild = !args[1].match(/\d+/) ? message.guild ? message.guild : null : client.guilds.cache.has(args[1]) ? client.guilds.cache.get(args[1]) : null; if (!guild) {return message.channel.send("You must provide a guild ID or be in a guild that you wish to blacklist!");} @@ -21,30 +20,28 @@ module.exports = { if (args[1].match(/\d+/)) {args.shift();} if (!args[1]) {return message.channel.send("You must specify whether to `add` or `del` a guild's blacklist!");} - let tu = await UserData.findOne({uid: message.author.id}); - if (!tu || !tu.admin) {return message.reply("You must be a Natsuki Admin to blacklist!");} if (message.guild.id === "762707532417335296") {return message.reply("You can't blacklist my support server!");} if (['a', 'add'].includes(args[1].toLowerCase())) { if (tg.blacklisted) {return message.reply("That guild is already blacklisted!");} tg.blacklisted = true; tg.save(); + client.misc.cache.bl.guild.push(message.guild.id); return message.channel.send("Gotcha! This server will not be able to use my commands!"); } if (['r', 'rem', 'remove', 'd', 'del', 'delete'].includes(args[1].toLowerCase())) { - if (tg.blacklisted) {return message.reply("That guild isn't blacklisted in the first place!");} + if (!tg.blacklisted) {return message.reply("That guild isn't blacklisted in the first place!");} tg.blacklisted = false; tg.save(); + delete client.misc.cache.bl.guild[client.misc.cache.bl.guild.indexOf(message.guild.id)]; return message.channel.send("I have graced your merciful request; this server can once again make use of my wonderous abilities!"); } return message.channel.send("Valid args: `[guildID] `"); - } if (['u', 'user'].includes(args[0].toLowerCase())) { - args.shift(); if (!args[1]) {return message.channel.send("You must specify whether to `add` or `del` a user's blacklist!");} @@ -55,7 +52,7 @@ module.exports = { if (bu.developer) {message.channel.send("Developers cannot be blacklisted!"); return null;} } - if(['a', 'add'].includes(args[1].toLowerCase())) { + if (['a', 'add'].includes(args[1].toLowerCase())) { let blacklistUser = args[0].match(/^<@(?:!?)(?:\d+)>$/) && mention && client.users.cache.has(mention.id) ? mention.id : client.users.cache.has(args[0]) ? client.users.cache.get(args[0]).id : null; if (!blacklistUser) {return message.reply("You must specify a user to blacklist!");} let usersData = await UserData.findOne( { uid: blacklistUser } ) || new UserData({uid: blacklistUser}); @@ -65,10 +62,12 @@ module.exports = { if (usersData.blacklisted === true) {return message.reply('they\'re already blacklisted :eyes:');} await UserData.findOneAndUpdate({ uid: blacklistUser }, { blacklisted: true }.catch(() => {})); + client.misc.cache.bl.user.push(blacklistUser); + return message.channel.send(`Another one bites the dust! **${blacklistUser.user.tag}** has been blacklisted!`) } - if(['r', 'rem', 'remove', 'd', 'del', 'delete'].includes(args[1].toLowerCase())) { + if (['r', 'rem', 'remove', 'd', 'del', 'delete'].includes(args[1].toLowerCase())) { let blacklistedUser = args[0].match(/^<@(?:!?)(?:\d+)>$/) && mention && client.users.cache.has(mention.id) ? mention.id : client.users.cache.has(args[0]) ? client.users.cache.get(args[0]).id : null; if (!blacklistedUser) { return message.reply("You need to specify who you're letting free..." );} let userData = await UserData.findOne( { uid: blacklistedUser } ) || new UserData({uid: blacklistedUser}); @@ -78,11 +77,11 @@ module.exports = { if(userData.blacklisted === false) {return message.reply('hate to break it you... they\'re not even blacklisted!');} await UserData.findOneAndUpdate({ uid: blacklistedUser }, { blacklisted: false }.catch(() => {})); + delete client.misc.cache.bl.user[client.misc.cache.bl.user.indexOf(blacklistedUser)]; + return message.channel.send(`Alright, there you go, I unblacklisted **${blacklistedUser.user.tag}**`) } return message.channel.send("Valid args: ` `"); - } - }}; \ No newline at end of file diff --git a/events/message.js b/events/message.js index 148828e..a61640d 100644 --- a/events/message.js +++ b/events/message.js @@ -51,6 +51,12 @@ module.exports = async (client, message) => { try { if (msg.startsWith(prefix) || msg.startsWith(`<@${client.user.id}>`) || msg.startsWith(`<@!${client.user.id}>`)) { let command = client.commands.get(cmd) || client.commands.get(client.aliases.get(cmd)); + + if (command && command.name !== "blacklist") { + if (client.misc.cache.bl.guild.includes(message.guild.id)) {return message.channel.send("Your server has been blacklisted from using my commands! Shame, tsk tsk");} + if (client.misc.cache.bl.user.includes(message.author.id)) {return message.channel.send("You've been blacklisted from using my commands! Now what'd ya do to deserve that??");} + } + if (!command) {let trigger; for (trigger of client.responses.triggers) {if (await trigger[1](message, msg, args, cmd, prefix, mention, client)) {await client.responses.commands.get(trigger[0]).execute(message, msg, args, cmd, prefix, mention, client); break;}} return;} message.channel.startTyping(); await wait(800); diff --git a/events/ready.js b/events/ready.js index 9ee4ebb..2c63316 100644 --- a/events/ready.js +++ b/events/ready.js @@ -71,11 +71,7 @@ module.exports = async client => { setInterval(() => {setPL(); siftStatuses(client, null);}, 120000); - console.log(''); - let ora_arCache = ora("Caching ARs...").start(); - await require('../util/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.`)}`); + await require('../util/cache')(client); let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) diff --git a/util/cache.js b/util/cache.js new file mode 100644 index 0000000..5aba566 --- /dev/null +++ b/util/cache.js @@ -0,0 +1,16 @@ +const ora = require('ora'); +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`)}`); +}; \ No newline at end of file diff --git a/util/cache/bl.js b/util/cache/bl.js new file mode 100644 index 0000000..a74d16f --- /dev/null +++ b/util/cache/bl.js @@ -0,0 +1,17 @@ +const GuildData = require('../../models/guild'); +const UserData = require('../../models/user'); + +module.exports = async (client) => { + client.misc.cache.bl = { + guild: [], + user: [] + }; + + for await (const guild of GuildData.find()) { + if (guild.blacklisted) {client.misc.cache.bl.guild.push(guild.gid);} + } + + for await (const user of UserData.find()) { + if (user.blackisted) {client.misc.cache.bl.user.push(user.uid);} + } +}; \ No newline at end of file