From 5da56dfa60eabc3e16c0fa31f26e910363237fee Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Wed, 28 Oct 2020 13:41:31 -0600 Subject: [PATCH] Add response parsing and sending; response command with 'quick' arg --- commands/response.js | 23 ++++++++++++++++++ util/parseresponse.js | 55 +++++++++++++++++++++++++++++++++++++++++++ util/sendresponse.js | 20 ++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 commands/response.js create mode 100644 util/parseresponse.js create mode 100644 util/sendresponse.js diff --git a/commands/response.js b/commands/response.js new file mode 100644 index 0000000..f97a01a --- /dev/null +++ b/commands/response.js @@ -0,0 +1,23 @@ +const Discord = require('discord.js'); +const GuildData = require('../models/guild'); + +const sendResponse = require('../util/sendresponse'); +const parseResponse = require('../util/parseresponse'); + +module.exports = { + name: "response", + aliases: ['r', 'resp'], + help: new Discord.MessageEmbed() + .setTitle("Help -> Responses") + .setDescription("Configure your server's saved responses. These are reusable and editable, and can be placed in things like welcome messages and used for announcements.") + .addField("Syntax", "`response `") + .addField("Notice", "You must have your server's staff role or be an admin to use this command."), + async execute(message, msg, args, cmd, prefix, mention, client) { + if (!message.guild) {return message.reply("You must be in a server to use this command.");} + let tg = await GuildData.findOne({gid: message.guild.id}); + 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));} + } +}; \ No newline at end of file diff --git a/util/parseresponse.js b/util/parseresponse.js new file mode 100644 index 0000000..0c4deed --- /dev/null +++ b/util/parseresponse.js @@ -0,0 +1,55 @@ +const {Tag} = require('./tag'); +const {TagFilter} = require('./tagfilter'); + +module.exports = async (message, client, args) => { + let options = new TagFilter([ + new Tag(['em', '-embed'], 'embed', 'toggle'), + new Tag(['-msg', 'message'], 'message', 'toggle'), + + new Tag(['name', 'n'], 'name', 'append'), + new Tag(['ch', 'channel'], 'channel', 'append'), + + new Tag(['text', 'txt'], 'text', 'append'), + + new Tag(['title', 't'], 'title', 'append'), + new Tag(['description', 'desc', 'd'], 'description', 'append'), + new Tag(['fieldname', 'fn', 'newfield', 'nf'], 'fieldnames', 'listAppend'), + new Tag(['fieldtext', 'ft', 'fieldcontent', 'fc'], 'fieldtexts', 'listAppend'), + new Tag(['image', 'i'], 'image', 'append'), + new Tag(['thumbnail', 'thumb', 'th'], 'thumbnail', 'append'), + new Tag(['servericonthumbnail', 'serverthumbnail', 'sit', 'st'], 'guildthumb', 'toggle'), + new Tag(['servericonimage', 'serverimage', 'sii', 'si'], 'guildimage', 'toggle'), + new Tag(['color', 'colour', 'col', 'c'], 'color', 'append'), + ]).test(args.join(" ")); + + if (options.fieldnames && options.fieldnames.length) { + if (!options.fieldtexts || !options.fieldtexts.length || options.fieldnames.length !== options.fieldtexts.length) { + message.reply("You must have the same amount of field names as you do field texts."); return null; + } + } + if (options.embed) { + if (options.fieldnames && options.fieldnames.length > 10) {message.reply("You can't have more than 10 fields!"); return null;} + if (options.color && options.color.length && (![3, 6].includes(options.color.length))) {message.reply("Your color must be a hex value 3 or 6 digits long."); return null;} + if (options.title && options.title.length > 65) {message.reply("Your title should be less than 65 characters, please :)"); return null;} + if (options.description && options.description.length > 750) {message.reply("Your description should be less than 750 characters."); return null;} + if ((!options.title || !options.title.length) || (!options.description || !options.description.length)) {message.reply("You need have a title and a description!"); return null;} + if (options.image && options.image.length > 300) {message.reply("Your image URL is a bit too long. Try shortening the URL or hosting it somewhere like imgur."); return null;} + if (options.thumbnail && options.image.thumbnail > 300) {message.reply("Your thumbnail URL is a bit too long. Try shortening the URL or hosting it somewhere like imgur."); return null;} + if (options.fieldnames) { + let fn; let ft; + for (fn of options.fieldnames) { + if (fn.length > 65) {message.reply("One of your field names is longer than 65 characters. Please shorten it!"); return null;} + } for (ft of options.fieldtexts) { + if (ft.length > 500) {message.reply("One of your field texts is longer than 500 characters. Please shorten it!"); return null;} + } + } + if (options.guildthumb) {options.thumbnail = message.guild.iconURL({size: 2048});} + if (options.guildimage) {options.image = message.guild.iconURL({size: 2048});} + } else if (options.message) { + if (options.text && options.text.length > 750) {message.reply("Please keep your message text under 750 characters!"); return null;} + } else {message.reply("You must specify either '-message' or '-embed' for the format of your response."); return null;} + + 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;}} + + return options; +}; \ No newline at end of file diff --git a/util/sendresponse.js b/util/sendresponse.js new file mode 100644 index 0000000..9bd0ac7 --- /dev/null +++ b/util/sendresponse.js @@ -0,0 +1,20 @@ +const Discord = require('discord.js'); + +module.exports = async(channel, mode, client, options) => { + if (!options) {return;} + if (options.channel && options.channel.length) {channel = channel.guild.channels.cache.get(options.channel.slice(options.channel.search(/\d/), options.channel.search('>')));} + try { + if (mode === "welcome") {} else if (mode === "leave") {} else { + if (options.embed) { + var responseEmbed = new Discord.MessageEmbed().setTitle(options.title).setDescription(options.description); + if (options.fieldnames && options.fieldnames.length) {let i; for (i=0;i