From 606dbc17d6fc93b97ba4a7b6d242d9e86e03175d Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sun, 14 Feb 2021 03:52:58 -0700 Subject: [PATCH] ar fixed and 100% done; channel ignores --- bot.js | 1 + commands/ar.js | 75 +++++++++++++++++++++++++++++++++++++++-------- events/message.js | 4 +-- models/ar.js | 2 +- util/cache/ar.js | 2 ++ 5 files changed, 68 insertions(+), 16 deletions(-) diff --git a/bot.js b/bot.js index 841b004..ee77cf5 100644 --- a/bot.js +++ b/bot.js @@ -13,6 +13,7 @@ client.misc = { startupNoConnect: null, cache: { ar: new Map(), + arIgnore: new Map(), bl: { guild: [], user: [] diff --git a/commands/ar.js b/commands/ar.js index 7e633aa..af9831f 100644 --- a/commands/ar.js +++ b/commands/ar.js @@ -23,15 +23,14 @@ module.exports = { if (!message.guild) {return message.channel.send("You must be in a server in order to use this command.");} if (!args.length) {return message.channel.send(`Syntax: \`${prefix}\``);} const tg = await GuildData.findOne({gid: message.guild.id}); - if (['a', 'add', 'e', 'edit', 'delete', 'd'].includes(args[0].toLowerCase()) && ((!tg || !tg.staffrole || !tg.staffrole.length || !message.member.roles.cache.has(tg.staffrole)) && !message.member.permissions.has("ADMINISTRATOR"))) {return message.channel.send("You must have the staff role or be an administrator in this server in order to edit AR settings.");} + if (['a', 'add', 'e', 'edit', 'delete', 'd', 's', 'settings'].includes(args[0].toLowerCase()) && ((!tg || !tg.staffrole || !tg.staffrole.length || !message.member.roles.cache.has(tg.staffrole)) && !message.member.permissions.has("ADMINISTRATOR"))) {return message.channel.send("You must have the staff role or be an administrator in this server in order to edit AR settings.");} function sortARs(tar) { let t = tar.triggers; let ar = tar.ars; - let f = []; let s = ''; - for (let i=0;i ${ar[tt]}\n\n`;} - return [s, f]; + for (let i=0;i ${ar[i]}\n\n`;} + return s; } function viewARs(string) { @@ -46,17 +45,16 @@ module.exports = { if (['a', 'add'].includes(args[0].toLowerCase())) { let trigger = await ask(message, "What would you like the trigger to be? This is the message that will make your AR work.", 120000); if (!trigger) {return;} if (`${trigger}`.length > 150) {return message.channel.send("Your trigger needs to be less than 150 characters, please!");} - console.log(trigger); let response = await ask(message, "What would you like my response to be?", 120000); if (!response) {return;} if (`${response}`.length > 300) {return message.channel.send("Your response needs to be less than 300 characters, please!");} let tar = await AR.findOne({gid: message.guild.id}) || new AR({gid: message.guild.id}); 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 && ar.toLowerCase() === `${trigger}`.toLowerCase()) {h = true;}} - if (!h) {tar.triggers.push(trigger);} + if (h) {return message.channel.send("You seem to already have that trigger. Try using `edit` instead!");} + 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.ars.push(response); tar.save(); return message.channel.send("AR added!"); } @@ -66,7 +64,7 @@ module.exports = { if (!tar || !tar.triggers.length) {return message.channel.send("You can't edit any auto-responses... because there aren't any here...");} let sar = sortARs(tar); - await message.channel.send(viewARs(sar[0]).addField("Editing", "Please say the **number** of the AR you wish to edit.")); + await message.channel.send(viewARs(sar).addField("Editing", "Please say the **number** of the AR you wish to edit.")); let collected; try {collected = await message.channel.awaitMessages(m => m.author.id === message.author.id, {errors: ['time'], time: 60001, max: 1});} catch {return message.channel.send("This question has timed out. Please try again!");} @@ -77,7 +75,10 @@ module.exports = { try { let response = await ask(message, "What would you like the new response to be?", 120000); if (!response) {return;} if (`${response}`.length > 300) {return message.channel.send("Your response needs to be less than 300 characters, please!");} - tar.ars[sar[1][id-1]] = response; + let tarars = tar.ars; + tarars[id-1] = response; + tar.ars = tarars; + tar.markModified('ars'); tar.save(); return message.channel.send("Yeah, that response seems to fit better than the last one."); } catch {return message.channel.send("There seemed to have been a problem deleting that AR. Contact my devs if the problem persists.");} @@ -88,7 +89,7 @@ module.exports = { if (!tar || !tar.triggers.length) {return message.channel.send("It's not like this server has any ARs for me to delete in the first place!");} let sar = sortARs(tar); - await message.channel.send(viewARs(sar[0]).addField("Deletion", "Please say the **number** of the AR you wish to delete.")); + await message.channel.send(viewARs(sar).addField("Deletion", "Please say the **number** of the AR you wish to delete.")); let collected; try {collected = await message.channel.awaitMessages(m => m.author.id === message.author.id, {errors: ['time'], time: 60000, max: 1});} catch {return message.channel.send("This question has timed out. Please try again!");} @@ -97,14 +98,62 @@ module.exports = { let id = Number(collected); if (id < 1 || id > tar.triggers.length) {return message.channel.send("Your number was either below 1 or doesn't have a trigger to match it.");} try { - tar.triggers.forEach(t => {if (t === sar[1][id-1]) {delete sar[1][id-1]; delete tar.ars[sar[1][id-1]]}}); - tar.triggers = sar[1]; + let temptt = tar.triggers; + temptt.splice(id-1, 1); + let tempar = tar.ars; + tempar.splice(id-1, 1); + tar.triggers = temptt; tar.ars = tempar; + tar.markModified('triggers'); tar.markModified('ars'); tar.save(); client.misc.cache.ar.set(message.guild.id, tar.triggers); return message.channel.send("I didn't like saying that anyway."); } catch {return message.channel.send("There seemed to have been a problem deleting that AR. Contact my devs if the problem persists.");} } + if (['v', 'view', 'l', 'list'].includes(args[0].toLowerCase())) { + let tar = await AR.findOne({gid: message.guild.id}); + if (!tar || !tar.triggers.length) {return message.channel.send("This server has no ARs!");} + return message.channel.send(viewARs(sortARs(tar))); + } + + if (['s', 'settings'].includes(args[0].toLowerCase())) { + args.shift(); + if (!args.length) {return message.reply("You can `view` your server's AR settings or `ignore` a channel");} + + let tar = await AR.findOne({gid: message.guild.id}); + if (!tar || !tar.triggers.length) {return message.channel.send("This server doesn't have any auto-responses, so changing or viewing the settings would be pointless...");} + + if (['v', 'view'].includes(args[0].toLowerCase())) { + if (!tar.ignoreChs.length) {return message.channel.send("This server doesn't have any channels set to be ignored for ARs.");} + let s = ''; + let c; for (c of tar.ignoreChs) {s += `<#${c}>, `} + return message.channel.send(`Channels I won't do auto-responses in: ${s.slice(0, s.length - 2)}`); + } + + if (['i', 'ignore'].includes(args[0].toLowerCase())) { + let ch; + if (args[1]) { + ch = message.mentions.channels.first() || message.guild.channels.cache.get(args[1]); + if (!ch) {return message.channel.send("I couldn't find that channel. Please try again!");} + } else {ch = message.channel;} + ch = ch.id; + if (tar.ignoreChs.includes(ch)) { + let ti = tar.ignoreChs; + ti.splice(ti.indexOf(ch), 1); + tar.ignoreChs = ti; + tar.markModified('ignoreChs'); + tar.save(); + client.misc.cache.arIgnore.set(message.guild.id, tar.ignoreChs); + return message.channel.send("I'll start replying to Auto Responses in this channel from now on."); + } else { + tar.ignoreChs.push(ch); + tar.save(); + client.misc.cache.arIgnore.set(message.guild.id, tar.ignoreChs); + return message.channel.send("Got it. I'll ignore Auto Responses here from now on."); + } + } + } + return message.channel.send(`That's not a valid argument! Try \`${prefix}help ar\``); } }; \ No newline at end of file diff --git a/events/message.js b/events/message.js index a61640d..dd1d8c3 100644 --- a/events/message.js +++ b/events/message.js @@ -42,9 +42,9 @@ 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())) { + if (message.guild && client.misc.cache.ar.has(message.guild.id) && client.misc.cache.ar.get(message.guild.id).includes(msg.trim()) && !(client.misc.cache.arIgnore.has(message.guild.id) && client.misc.cache.arIgnore.get(message.guild.id).includes(message.channel.id))) { 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()]);} + if (ar && ar.triggers.length && ar.triggers.includes(msg.trim())) {return message.channel.send(ar.ars[ar.triggers.indexOf(msg.trim())]);} }); } diff --git a/models/ar.js b/models/ar.js index 1380f7e..1d4659e 100644 --- a/models/ar.js +++ b/models/ar.js @@ -2,7 +2,7 @@ const mongoose = require('mongoose'); const AR = new mongoose.Schema({ gid: {type: String, unique: true}, - ars: {type: Object, default: {}}, + ars: {type: [String], default: []}, triggers: {type: [String], default: []}, ignoreChs: {type: [String], default: []} }); diff --git a/util/cache/ar.js b/util/cache/ar.js index 8f8bfb2..f096a75 100644 --- a/util/cache/ar.js +++ b/util/cache/ar.js @@ -2,8 +2,10 @@ const AR = require('../../models/ar'); module.exports = async client => { client.misc.cache.ar = new Map(); + client.misc.cache.arIgnore = new Map(); 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);} } }; \ No newline at end of file