Response saving, retrieving, and sending

master
Kit Kasune 4 years ago
parent 5da56dfa60
commit 45b97bfb7e
  1. 12
      commands/response.js
  2. 9
      models/responses.js
  3. 9
      util/response/getresponse.js
  4. 10
      util/response/parseresponse.js
  5. 15
      util/response/saveresponse.js
  6. 0
      util/response/sendresponse.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 <new|edit|view|list|delete|test|quick>\``);}
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]));}
}
};

@ -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);

@ -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());
};

@ -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;
};

@ -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;
};
Loading…
Cancel
Save