From 45b97bfb7e8015b269f3f0e2ebcab1a3d26f1456 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 28 Oct 2020 15:28:27 -0600 Subject: [PATCH] Response saving, retrieving, and sending --- commands/response.js | 12 +++++++++--- models/responses.js | 9 +++++++++ util/response/getresponse.js | 9 +++++++++ util/{ => response}/parseresponse.js | 10 ++++++++-- util/response/saveresponse.js | 15 +++++++++++++++ util/{ => response}/sendresponse.js | 0 6 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 models/responses.js create mode 100644 util/response/getresponse.js rename util/{ => response}/parseresponse.js (89%) create mode 100644 util/response/saveresponse.js rename util/{ => response}/sendresponse.js (100%) diff --git a/commands/response.js b/commands/response.js index f97a01a..cab0c61 100644 --- a/commands/response.js +++ b/commands/response.js @@ -1,8 +1,10 @@ const Discord = require('discord.js'); const GuildData = require('../models/guild'); -const sendResponse = require('../util/sendresponse'); -const parseResponse = require('../util/parseresponse'); +const sendResponse = require('../util/response/sendresponse'); +const parseResponse = require('../util/response/parseresponse'); +const saveResponse = require('../util/response/saveresponse'); +const getResponse = require('../util/response/getresponse'); module.exports = { name: "response", @@ -18,6 +20,10 @@ module.exports = { if (!tg && !['q', 'quick'].includes(args[0].toLowerCase()) && (tg.staffrole.length && !message.member.roles.cache.has(tg.staffrole)) && message.member.permissions.has("ADMINISTRATOR")) {return message.reply("you need to be staff or admin in this server in order to edit those settings.");} if (!args.length) {return message.channel.send(`Syntax: \`${prefix}response \``);} - if (['q', 'quick'].includes(args[0].toLowerCase())) {return sendResponse(message.channel, 'quick', client, await parseResponse(message, client, args));} + if (args.length < 1) {return message.reply("You have to tell me what I'm supposed to find or save!");} + + if (['q', 'quick'].includes(args[0].toLowerCase())) {return await sendResponse(message.channel, 'quick', client, await parseResponse(message, client, args));} + if (['n', 'new', 's', 'save'].includes(args[0].toLowerCase())) {return await saveResponse(await parseResponse(message, client, args), message);} + if (['t', 'test', 'send'].includes(args[0].toLowerCase())) {return await sendResponse(message.channel, 'quick', client, await getResponse(message, args[1]));} } }; \ No newline at end of file diff --git a/models/responses.js b/models/responses.js new file mode 100644 index 0000000..4f83bbf --- /dev/null +++ b/models/responses.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); + +const ResponseSchema = new mongoose.Schema({ + gid: {type: String, unique: true}, + responses: {type: Map, default: new Map()}, + bindings: {type: Map, default: new Map()} +}); + +module.exports = mongoose.model('responses', ResponseSchema); \ No newline at end of file diff --git a/util/response/getresponse.js b/util/response/getresponse.js new file mode 100644 index 0000000..55bc7f0 --- /dev/null +++ b/util/response/getresponse.js @@ -0,0 +1,9 @@ +const Responses = require('../../models/responses'); + +module.exports = async (message, name) => { + let tr = await Responses.findOne({gid: message.guild.id}); + if (!tr) {message.reply("This server does not have any responses saved!"); return null;} + if (!tr.responses.has(name.toLowerCase())) {message.reply("I don't have that response saved here."); return null;} + message.delete(); + return tr.responses.get(name.toLowerCase()); +}; \ No newline at end of file diff --git a/util/parseresponse.js b/util/response/parseresponse.js similarity index 89% rename from util/parseresponse.js rename to util/response/parseresponse.js index 0c4deed..4f91600 100644 --- a/util/parseresponse.js +++ b/util/response/parseresponse.js @@ -1,5 +1,5 @@ -const {Tag} = require('./tag'); -const {TagFilter} = require('./tagfilter'); +const {Tag} = require('../tag'); +const {TagFilter} = require('../tagfilter'); module.exports = async (message, client, args) => { let options = new TagFilter([ @@ -51,5 +51,11 @@ module.exports = async (message, client, args) => { if (options.channel && options.channel.length) {if (!options.channel.match(/^<#(?:\d+)>$/) && !message.guild.channels.cache.has(options.channel.slice(options.channel.search(/\d/), options.channel.search(">")))) {message.reply("You must use a valid channel in this server."); return null;}} + if (options.name && options.name.length) { + options.name = options.name.toLowerCase(); + if (options.name.length > 10) {message.reply("The option name must be less than 10 characters."); return null;} + if (!options.name.match(/^[a-z0-9-_]+$/)) {message.reply("You can only use a-z, numbers, hyphens, and underscores."); return null;} + } + return options; }; \ No newline at end of file diff --git a/util/response/saveresponse.js b/util/response/saveresponse.js new file mode 100644 index 0000000..f983666 --- /dev/null +++ b/util/response/saveresponse.js @@ -0,0 +1,15 @@ +const Responses = require('../../models/responses'); + +module.exports = async (options, message) => { + try { + if (!options) {return null;} + if (!options.name || !options.name.length) {message.reply("You need to have a name in order to save a response."); return null;} + let sr = await Responses.findOne({gid: message.guild.id}) ? await Responses.findOne({gid: message.guild.id}) : new Responses({gid: message.guild.id}); + if (sr.responses.has(options.name)) {message.reply("You already have a response with that name. Use `edit` instead."); return null;} + sr.responses.set(options.name, options); + sr.save(); + message.channel.send("Response added!"); + } catch {message.reply("There seems to have been an error in saving your response. If this persists, please contact the developers or join the support sever."); return null;} + + return options; +}; \ No newline at end of file diff --git a/util/sendresponse.js b/util/response/sendresponse.js similarity index 100% rename from util/sendresponse.js rename to util/response/sendresponse.js