From 90e1b218154881548ec9c9190024270063857502 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Fri, 12 Feb 2021 19:00:02 -0700 Subject: [PATCH] AR caching and functionality --- bot.js | 5 ++++- commands/ar.js | 1 + events/message.js | 8 +++++++- events/ready.js | 15 +++++++++++++-- package-lock.json | 31 +++++++++++++++++++++++++++++++ util/cache/ar.js | 9 +++++++++ 6 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 util/cache/ar.js diff --git a/bot.js b/bot.js index 47ef014..1c375c4 100644 --- a/bot.js +++ b/bot.js @@ -10,7 +10,10 @@ client.misc = { activeDMs: new Discord.Collection(), statusPings: new Discord.Collection(), startup: new Date(), - startupNoConnect: null + startupNoConnect: null, + cache: { + ar: new Map() + } }; //const config = require('./config.js'); diff --git a/commands/ar.js b/commands/ar.js index 4127619..05485cf 100644 --- a/commands/ar.js +++ b/commands/ar.js @@ -43,6 +43,7 @@ module.exports = { if (tar.triggers.length === 20) {return message.channel.send("Because of data storage concerns, your ARs are capped at 20 per server. You can join the official support server and talk to the devs if you have a legitimate reason for raising this limit and they can see about raising it for you!");} let h = false; let ar; for (ar of tar.triggers) {if (ar.toLowerCase() === `${trigger}`.toLowerCase()) {h = true;}} if (!h) {tar.triggers.push(trigger);} + client.misc.cache.ar.set(message.guild.id, tar.triggers); tar.ars[`${trigger}`.trim().toLowerCase()] = `${response}`.trim(); tar.markModified('tar.ars'); tar.save(); diff --git a/events/message.js b/events/message.js index a6a5de8..148828e 100644 --- a/events/message.js +++ b/events/message.js @@ -4,7 +4,7 @@ const chalk = require('chalk'); const wait = require('../util/wait'); const UserData = require('../models/user'); -const StatusCache = require('../models/statuses'); +const AR = require('../models/ar'); module.exports = async (client, message) => { if (message.author.bot) {return undefined;} @@ -42,6 +42,12 @@ module.exports = async (client, message) => { message.reply('Hey there! You asked me to clear your status when you send a message next, so I went ahead and did that for you.').then(m => {m.delete({timeout: 5000});}); }}); + if (client.misc.cache.ar.has(message.guild.id) && client.misc.cache.ar.get(message.guild.id).includes(msg.trim())) { + AR.findOne({gid: message.guild.id}).then(ar => { + if (ar && ar.triggers.length && ar.triggers.includes(msg.trim()) && Object.keys(ar.ars).includes(msg.trim())) {return message.channel.send(ar.ars[msg.trim()]);} + }); + } + 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)); diff --git a/events/ready.js b/events/ready.js index 52ae3b8..9ee4ebb 100644 --- a/events/ready.js +++ b/events/ready.js @@ -2,6 +2,7 @@ const Discord = require('discord.js'); const chalk = require('chalk'); const moment = require('moment'); const mongoose = require('mongoose'); +const ora = require('ora'); const GuildSettings = require('../models/guild'); const BotDataSchema = require('../models/bot'); @@ -27,7 +28,11 @@ module.exports = async client => { let responses = { "PLAYING": [ - `with my darling`, 'RAIN: Shadow Lords' + `with my darling`, 'RAIN: Shadow Lords', "with my waifu", "with the neko formula", + "with magic", "terrible anime games", "anime OSTs at max volume", + `${Math.ceil(Math.random() * 100)} days of trying to become a samurai`, + "with the sauce", "witch hats are >", "explosion magic is the best magic", + "with Kazuma's sanity" ,`in ${client.guilds.cache.size} servers` ], "WATCHING": [ @@ -36,7 +41,7 @@ module.exports = async client => { "a slime somehow start an empire", "a fox-maid get her tail fluffed", "a raccoon-girl and some guy with a shield", "some chick with unusually red hair", "Mob hit 100", "a really bad harem anime", "The Black Swordsman", - "The Misfit of Demon King Academy" + "The Misfit of Demon King Academy", "Akame ga Kill" ,`over ${client.guilds.cache.size} servers` ] }; @@ -66,6 +71,12 @@ 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.`)}`); + let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) : new BotDataSchema({ diff --git a/package-lock.json b/package-lock.json index 2bf4609..5961207 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1388,9 +1388,11 @@ "integrity": "sha512-2N7AmszH/WPPpl5Z3XMw1HAP+8d+xugnKQAeKvxFZ/04dbT/CAznqwbl+7eSr3HkwdepNwtb2yx3CAMQWvG01Q==", "requires": { "clone-response": "^1.0.2", + "get-stream": "^4.0.0", "http-cache-semantics": "^4.0.0", "keyv": "^3.0.0", "lowercase-keys": "^1.0.1", + "normalize-url": "^3.1.0", "responselike": "^1.0.2" } }, @@ -1438,6 +1440,14 @@ "ms": "2.1.2" } }, + "decompress-response": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/decompress-response/-/decompress-response-3.3.0.tgz", + "integrity": "sha1-gKTdMjdIOEv6JICDYirt7Jgq3/M=", + "requires": { + "mimic-response": "^1.0.0" + } + }, "doctrine": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/doctrine/-/doctrine-3.0.0.tgz", @@ -1559,6 +1569,14 @@ "write": "1.0.3" } }, + "get-stream": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/get-stream/-/get-stream-4.1.0.tgz", + "integrity": "sha512-GMat4EJ5161kIy2HevLlr4luNjBgvmj413KaQA7jt4V8B4RDsfpHk7WQ9GVqfYyyx8OS/L66Kox+rJRNklLK7w==", + "requires": { + "pump": "^3.0.0" + } + }, "globals": { "version": "12.3.0", "resolved": "https://registry.npmjs.org/globals/-/globals-12.3.0.tgz", @@ -1572,8 +1590,11 @@ "@sindresorhus/is": "^0.14.0", "@szmarczak/http-timer": "^1.1.2", "cacheable-request": "^6.0.0", + "decompress-response": "^3.3.0", "duplexer3": "^0.1.4", + "get-stream": "^4.1.0", "lowercase-keys": "^1.0.1", + "mimic-response": "^1.0.1", "p-cancelable": "^1.0.0", "to-readable-stream": "^1.0.0", "url-parse-lax": "^3.0.0" @@ -1701,6 +1722,11 @@ "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-1.2.0.tgz", "integrity": "sha512-jf84uxzwiuiIVKiOLpfYk7N46TSy8ubTonmneY9vrpHNAnp0QBt2BxWV9dO3/j+BoVAb+a5G6YDPW3M5HOdMWQ==" }, + "mimic-response": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/mimic-response/-/mimic-response-1.0.1.tgz", + "integrity": "sha512-j5EctnkH7amfV/q5Hgmoal1g2QHFJRraOtmx0JpIqkxhBhI/lJSl1nMpQ45hVarwNETOoWEimndZ4QK0RHxuxQ==" + }, "mute-stream": { "version": "0.0.8", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.8.tgz", @@ -1727,6 +1753,11 @@ } } }, + "normalize-url": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-3.3.0.tgz", + "integrity": "sha512-U+JJi7duF1o+u2pynbp2zXDW2/PADgC30f0GsHZtRh+HOcXHnw137TrNlyxxRvWW5fjKd3bcLHPxofWuCjaeZg==" + }, "onetime": { "version": "5.1.0", "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.0.tgz", diff --git a/util/cache/ar.js b/util/cache/ar.js new file mode 100644 index 0000000..8f8bfb2 --- /dev/null +++ b/util/cache/ar.js @@ -0,0 +1,9 @@ +const AR = require('../../models/ar'); + +module.exports = async client => { + client.misc.cache.ar = new Map(); + + for await (const ar of AR.find()) { + client.misc.cache.ar.set(ar.gid, ar.triggers); + } +}; \ No newline at end of file