From a459a39a2e06684f3697d81cb8c1b4862958ad14 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Fri, 27 Nov 2020 23:51:40 -0700 Subject: [PATCH] Status caching and ping warning ratelimit --- bot.js | 10 +++++++--- commands/afk.js | 4 ++++ commands/blacklist.js | 4 ++-- commands/dnd.js | 4 ++++ events/message.js | 9 ++++++++- events/ready.js | 27 ++++++++++++++++++++------- models/statuses.js | 8 ++++++++ package-lock.json | 21 --------------------- package.json | 1 - util/cachestatus.js | 11 +++++++++++ util/mention.js | 11 +++++++++-- util/siftstatuses.js | 25 +++++++++++++++++++++++++ 12 files changed, 98 insertions(+), 37 deletions(-) create mode 100644 models/statuses.js create mode 100644 util/cachestatus.js create mode 100644 util/siftstatuses.js diff --git a/bot.js b/bot.js index 5919018..65e3280 100644 --- a/bot.js +++ b/bot.js @@ -13,9 +13,13 @@ async function init() { ['command', 'event'].forEach(x => require(`./handle/${x}`)(client)); client.developers = ["330547934951112705", "673477059904929802"]; - client.misc = {}; - client.misc.savers = ['497598953206841375']; - client.misc.activeDMs = new Discord.Collection(); + + client.misc = { + savers: ['497598953206841375'], + activeDMs: new Discord.Collection(), + statusPings: new Discord.Collection() + }; + client.utils = {}; client.utils.logch = async () => {return client.guilds.cache.get('762707532417335296').channels.cache.get('762732961753595915');}; diff --git a/commands/afk.js b/commands/afk.js index ec46965..d73f251 100644 --- a/commands/afk.js +++ b/commands/afk.js @@ -24,7 +24,11 @@ module.exports = { if (reason.length > 150) {return message.reply("That status a bit long; keep it under 150 characters.");} tu.statustype = 'afk'; tu.statusmsg = reason.trim(); + tu.statussetat = new Date(); + let tempDate = new Date(); + tu.statusclearat = tempDate.setHours(tempDate.getHours() + 12); tu.save(); + require('../util/cachestatus')(message.author.id, tempDate.setHours(tempDate.getHours() + 12)); return message.reply(`I set your ${tu.statusclearmode === 'auto' ? 'automatically' : 'manually'}-clearing AFK message to: ${reason.trim()}`); } }; \ No newline at end of file diff --git a/commands/blacklist.js b/commands/blacklist.js index 265ed32..e6e59cd 100644 --- a/commands/blacklist.js +++ b/commands/blacklist.js @@ -60,7 +60,7 @@ module.exports = { if (!blacklistUser) {return message.reply("You must specify a user to blacklist!");} let usersData = await UserData.findOne( { uid: blacklistUser } ) || new UserData({uid: blacklistUser}); - if (!checkPerms(tu, usersData);) {return;} + if (!checkPerms(tu, usersData)) {return;} if (usersData.blacklisted === true) {return message.reply('they\'re already blacklisted :eyes:');} @@ -73,7 +73,7 @@ module.exports = { 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}); - if (!checkPerms(tu, userData);) {return;} + if (!checkPerms(tu, userData)) {return;} if(userData.blacklisted === false) {return message.reply('hate to break it you... they\'re not even blacklisted!');} diff --git a/commands/dnd.js b/commands/dnd.js index fee8705..b19057f 100644 --- a/commands/dnd.js +++ b/commands/dnd.js @@ -24,7 +24,11 @@ module.exports = { if (reason.length > 150) {return message.reply("That status a bit long; keep it under 150 characters.");} tu.statustype = 'dnd'; tu.statusmsg = reason.trim(); + tu.statussetat = new Date(); + let tempDate = new Date(); + tu.statusclearat = tempDate.setHours(tempDate.getHours() + 12); tu.save(); + require('../util/cachestatus')(message.author.id, tempDate.setHours(tempDate.getHours() + 12)); return message.reply(`I set your ${tu.statusclearmode === 'auto' ? 'automatically' : 'manually'}-clearing Do not Disturb message to: ${reason.trim()}`); } }; \ No newline at end of file diff --git a/events/message.js b/events/message.js index e3b5939..b58e5ad 100644 --- a/events/message.js +++ b/events/message.js @@ -1,8 +1,10 @@ const Discord = require('discord.js'); -const mongoose = require('mongoose'); const chalk = require('chalk'); + const wait = require('../util/wait'); + const UserData = require('../models/user'); +const StatusCache = require('../models/statuses'); module.exports = async (client, message) => { if (message.author.bot) {return undefined;} @@ -36,6 +38,11 @@ module.exports = async (client, message) => { tu.statusmsg = ''; tu.statustype = ''; tu.save(); + const statuses = await StatusCache.findOne({f: 'lol'}); + let status; for (status of statuses.statuses) { + if (status.id === message.author.id) {delete statuses.statuses.indexOf(status);} + } + statuses.save(); 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.'); } diff --git a/events/ready.js b/events/ready.js index d76675e..c0a7818 100644 --- a/events/ready.js +++ b/events/ready.js @@ -2,7 +2,11 @@ const Discord = require('discord.js'); const chalk = require('chalk'); const moment = require('moment'); const mongoose = require('mongoose'); + const GuildSettings = require('../models/guild'); +const BotDataSchema = require('../models/bot'); + +const siftStatuses = require('../util/siftstatuses'); var prefix = 'n?'; @@ -11,7 +15,7 @@ module.exports = async client => { try { await mongoose.connect(`mongodb+srv://${config.database.user}:${config.database.password}@${config.database.cluster}.3jpp4.mongodb.net/test`, { useFindAndModify: false, useNewUrlParser: true, dbName: 'Natsuki-Main', useUnifiedTopology: true, useCreateIndex: true - }); + }); } catch (e) { let date = new Date; date = date.toString().slice(date.toString().search(":") - 2, date.toString().search(":") + 6); console.error(`\n${chalk.red('[ERROR]')} >> ${chalk.yellow(`At [${date}] | Occurred while trying to connect to Mongo Cluster`)}`, e); @@ -30,27 +34,36 @@ module.exports = async client => { let responses = { "PLAYING": [ - `in ${client.guilds.cache.size} servers` + `with my darling`, 'RAIN: Shadow Lords' + ,`in ${client.guilds.cache.size} servers` ], "WATCHING": [ - `for ${client.commands.size} commands` + `for ${client.commands.size} commands`, + "I'm not a bad slime, slurp!", "Lelouch rule the world!", + "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" ,`over ${client.guilds.cache.size} servers` ] }; const setR = () => { let type = Object.keys(responses)[Math.floor(Math.random() * Object.keys(responses).length)]; - client.user.setActivity(responses[type][Math.floor(Math.random() * responses[type].length)] + " | " + prefix + "help", {type: type});}; + if (type === "PLAYING") {client.user.setActivity(responses[type][Math.floor(Math.random() * responses[type].length)] + " | " + prefix + "help");} + else {client.user.setActivity(responses[type][Math.floor(Math.random() * responses[type].length)] + " | " + prefix + "help", {type: type});} + } setR(); + setInterval(setR, 14400000); - - const BotDataSchema = require('../models/bot'); const setP = async () => {let tg; for (tg of Array.from(client.guilds.cache.values)) { let tguild = await GuildSettings.findOne({gid: tg.id}); if (tguild && tguild.prefix && tguild.prefix.length) {client.guildconfig.prefixes.set(tg.id, tguild.prefix);} }}; setP(); - setInterval(setP, 120000); + siftStatuses(); + + setInterval(() => {setP(); siftStatuses(client, null);}, 120000); let botData = await BotDataSchema.findOne({finder: 'lel'}) ? await BotDataSchema.findOne({finder: 'lel'}) diff --git a/models/statuses.js b/models/statuses.js new file mode 100644 index 0000000..e070c1a --- /dev/null +++ b/models/statuses.js @@ -0,0 +1,8 @@ +const mongoose = require('mongoose'); + +const StatusSchema = new mongoose.Schema({ + f: String, + statuses: [{id: String, clear: Date}] +}); + +module.exports = new mongoose.model('statuses', StatusSchema); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 8566a6a..93dd4f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3789,11 +3789,6 @@ "ip-address": "^5.8.8" } }, - "@lavaclient/types": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/@lavaclient/types/-/types-1.0.2.tgz", - "integrity": "sha512-7OmlW8PD0mU4n4qD79YiT86mJDMmV0qoPLdXnepqv067gf9204p5tXRrs/cr+vGljqewELyUtUuQd74rRTZViw==" - }, "@nodelib/fs.scandir": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.3.tgz", @@ -7315,22 +7310,6 @@ "resolved": "https://registry.npmjs.org/kuler/-/kuler-2.0.0.tgz", "integrity": "sha512-Xq9nH7KlWZmXAtodXDDRE7vs6DU1gTU8zYDHDiWLSip45Egwq3plLHzPn27NgvzL2r1LMPC1vdqh98sQxtqj4A==" }, - "lavaclient": { - "version": "3.1.2", - "resolved": "https://registry.npmjs.org/lavaclient/-/lavaclient-3.1.2.tgz", - "integrity": "sha512-kfY6/3zkWyv3fc+G/Br8aEWrRBxwhgQy8xA3cJmIAxRGey8TC4kCM0gXKbAQDqcEErSAakRln18f+uwUkOExPA==", - "requires": { - "@lavaclient/types": "1.0.2", - "ws": "^7.3.1" - }, - "dependencies": { - "ws": { - "version": "7.3.1", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.3.1.tgz", - "integrity": "sha512-D3RuNkynyHmEJIpD2qrgVkc9DQ23OrN/moAwZX4L8DfvszsJxpjQuUq3LMx6HoYji9fbIOBY18XWBsAux1ZZUA==" - } - } - }, "lavalink": { "version": "2.10.2", "resolved": "https://registry.npmjs.org/lavalink/-/lavalink-2.10.2.tgz", diff --git a/package.json b/package.json index d11bfef..de3ec0a 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,6 @@ "ffmpeg-static": "^4.2.7", "gblapi.js": "^2.0.5", "heroku": "^7.42.1", - "lavaclient": "^3.1.2", "lavaqueue": "^3.1.6", "manyitems": "^1.0.2", "moment": "^2.28.0", diff --git a/util/cachestatus.js b/util/cachestatus.js new file mode 100644 index 0000000..da88db0 --- /dev/null +++ b/util/cachestatus.js @@ -0,0 +1,11 @@ +const StatusCache = require('../models/statuses'); + +module.exports = async (id, time) => { + let statuses = await StatusCache.findOne({f: 'lol'}) || new StatusCache({f: 'lol', statuses: []}); + let exists = false; + let status; for (status of statuses.statuses) { + if (status.id === id) {statuses.statuses[statuses.statuses.indexOf(status)].clear = time; exists = true;} + } + if (!exists) {statuses.statuses.push({id: id, clear: time});} + return statuses.save(); +}; \ No newline at end of file diff --git a/util/mention.js b/util/mention.js index dbae079..14ead11 100644 --- a/util/mention.js +++ b/util/mention.js @@ -1,4 +1,6 @@ -const mongooes= require('mongoose'); +const Discord = require('discord.js'); +const moment = require('moment'); + const UserData = require('../models/user'); const GuildSettings = require('../models/guild'); @@ -6,5 +8,10 @@ module.exports = async(message, msg, args, cmd, prefix, mention, client) => { let tu = await UserData.findOne({uid: mention.id}); let tg = message.guild ? await GuildSettings.findOne({gid: message.guild.id}) : null; if (tg && tg.nostatus) {return;} - if (tu) {if (tu.statusmsg.length) {return message.reply(`That user ${tu.statustype === 'dnd' ? 'wishes not to be disturbed' : 'is AFK'}. Reason: ${tu.statusmsg}`);}} + if (client.misc.statusPings.has(message.guild.id) && client.misc.statusPings.get(message.guild.id).has(mention.id) && new Date().getTime() - client.misc.statusPings.get(message.guild.id).get(mention.id).getTime() < 300000) {return;} + if (tu && tu.statusmsg.length) { + if (!client.misc.statusPings.has(message.guild.id)) {client.misc.statusPings.set(message.guild.id, new Discord.Collection());} + client.misc.statusPings.get(message.guild.id).set(mention.id, new Date()); + return message.reply(`That user ${tu.statustype === 'dnd' ? 'wishes not to be disturbed' : 'is AFK'}. Reason: \`${tu.statusmsg}\`. (This status was set ${moment(tu.statussetat.getTime()).fromNow()})`); + } }; \ No newline at end of file diff --git a/util/siftstatuses.js b/util/siftstatuses.js new file mode 100644 index 0000000..7cfe378 --- /dev/null +++ b/util/siftstatuses.js @@ -0,0 +1,25 @@ +const UserData = require('../models/user'); +const StatusCache = require('../models/statuses'); + +module.exports = async function (client, lookFor) { + let statusesm = await StatusCache.findOne({f: 'lol'}) || new StatusCache({f: 'lol', statuses: []}); + let statuses = statusesm.statuses; + let date = new Date(); + let ns = []; + if (!client) {return 'no client found or given';} + let status; for (status of statuses) { + if (date.getTime() > status.clear.getTime()) { + if (lookFor && status.id !== lookFor) {continue;} + let tu = await UserData.findOne({uid: status.id}); + if (tu) { + tu.statusmsg = ''; + tu.statustype = ''; + tu.save(); + let u = await client.users.fetch(status.id); + if (u) {u.send("Heya! Your status has been set for 12 hours, so I've cleared it for you.").catch(() => {});} + } + } else {ns.push(status);} + } + statusesm.statuses = ns; + return statusesm.save(); +}; \ No newline at end of file