From 5fd90c3156dcd52a7a0761e1258da533e4823ad5 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Fri, 30 Oct 2020 04:03:05 -0600 Subject: [PATCH] tidy bugs and add response deletion and bindings deletion detection --- commands/response.js | 22 +++++++++++++-- commands/welcome.js | 49 ++++++++++++++++++++++++++++++++++ events/guildMemberAdd.js | 10 +++++++ util/response/parseresponse.js | 1 + util/response/sendresponse.js | 22 +++++++-------- 5 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 commands/welcome.js diff --git a/commands/response.js b/commands/response.js index 5bddf7c..b635930 100644 --- a/commands/response.js +++ b/commands/response.js @@ -1,5 +1,6 @@ const Discord = require('discord.js'); const GuildData = require('../models/guild'); +const Responses = require('../models/responses'); const sendResponse = require('../util/response/sendresponse'); const parseResponse = require('../util/response/parseresponse'); @@ -22,8 +23,25 @@ module.exports = { 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, message.channel, 'quick', client, await parseResponse(message, client, args));} + if (['q', 'quick'].includes(args[0].toLowerCase())) {return await sendResponse(message.member, 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, message.channel, 'quick', client, await getResponse(message, args[1]));} + if (['t', 'test', 'send'].includes(args[0].toLowerCase())) {return await sendResponse(message.member, message.channel, 'quick', client, await getResponse(message, args[1]));} + if (['r', 'remove', 'd', 'delete', 'del'].includes(args[0].toLowerCase())) { + let tr = await Responses.findOne({gid: message.guild.id}); + if (!tr) {return message.reply("This server has no responses for me to delete.");} + if (!tr.responses.has(args[1].toLowerCase())) {return message.reply("I can't find that response.");} + tr.responses.delete(args[1].toLowerCase()); + let hadBinding = false; + let bm = ''; + tr.bindings.forEach((v, k) => {if (v === args[1].toLowerCase()) { + tr.bindings.delete(v); + hadBinding = true; + bm += `This response was bound to \`${k}\`, so that has also been removed.\n`; + }}); + tr.save(); + return message.channel.send(`I removed the response \`${args[1].toLowerCase()}\`.${hadBinding ? `\n\n${bm}` : ''}`); + } + + return message.channel.send(`Syntax: \`${prefix}response \``); } }; \ No newline at end of file diff --git a/commands/welcome.js b/commands/welcome.js new file mode 100644 index 0000000..40a3adb --- /dev/null +++ b/commands/welcome.js @@ -0,0 +1,49 @@ +const Discord = require('discord.js'); +const GuildData = require('../models/guild'); +const Responses = require('../models/responses'); +const sendResponse = require('../util/response/sendresponse'); + +module.exports = { + name: "welcome", + aliases: ['wel', 'welcomemsg', 'welcomemessage', 'welcomechannel', 'wch', 'wmsg', 'welcomech'], + help: new Discord.MessageEmbed() + .setTitle("Help -> Welcome Messages") + .setDescription("Set the channel and message for your welcome messages!") + .addField("Syntax", "`welcome `") + .addField("Notice", "You must be a staff or admin in your server to edit these settings.") + .addField("Responses", "Your welcome message should be generated through a response using my `response` command, and then bound to the welcome message by providing your response's name."), + async execute(message, msg, args, cmd, prefix, mention, client) { + if (!message.guild) {return message.reply("This command is server-only.");} + let tg = await GuildData.findOne({gid: message.guild.id}) ? await GuildData.findOne({gid: message.guild.id}) : new GuildData({gid: message.guild.id}); + if (!args.length) {return message.channel.send(`Syntax: \`${prefix}welcome \``);} + if (['v', 'view', 'c', 'check'].includes(args[0].toLowerCase())) {} + if ((!tg.staffrole.length || !message.member.roles.cache.has(tg.staffrole)) && !message.member.permissions.has("ADMINISTRATOR")) {return message.reply("You can't do that without staff or admin permissions, silly!");} + + if (['s', 'set'].includes(args[0].toLowerCase())) { + if (!args[1]) {return message.reply("You need to specify a channel for your welcome messages to be sent in!");} + let ch = message.mentions.channels.first() && args[1].match(/^<#(?:\d+)>$/) ? message.mentions.channels.first().id : message.guild.channels.cache.has(args[1]) ? message.guild.channels.cache.get(args[1]).id : null; + if (!ch) {return message.reply("I can't find that channel!");} + if (!message.guild.channels.cache.get(ch).permissionsFor(client.user.id).has("SEND_MESSAGES")) {return message.reply("I can't send messages in that channel. Try fixing the permissions or using a different channel!");} + if (!args[2]) {return message.reply(`You have to specify a response to use! You can make one with \`${prefix}response new\`.`);} + let tr = await Responses.findOne({gid: message.guild.id}) ? await Responses.findOne({gid: message.guild.id}) : new Responses({gid: message.guild.id}); + if (!tr.responses.has(args[2].toLowerCase())) {return message.reply("Silly, I can't welcome someone with a response that doesn't exist! Try making one or make sure you spelled the name correctly.");} + tg.wch = ch; + tg.save(); + tr.bindings.set('welcome', args[2].toLowerCase()); + tr.save(); + return message.channel.send(new Discord.MessageEmbed() + .setTitle("Welcome Channel/Message Updated") + .setDescription(`This server's member-welcoming settings have been altered by ${message.author.tag}.\n\n**Channel**: <#${ch}>\n**Response Name**: \`${args[2].toLowerCase()}\``) + .setColor('c375f0') + .setFooter("Natsuki", client.user.avatarURL()) + .setTimestamp() + ) + } + + if (['t', 'test'].includes(args[0].toLowerCase())) { + let tr = await Responses.findOne({gid: message.guild.id}); + if (!tr || !tr.bindings.has('welcome') || !tr.responses.has(tr.bindings.get('welcome'))) {return message.reply("I can't test your welcome message because the response doesn't exist, a welcome response isn't set, or you haven't made any responses in this server.");} + await sendResponse(message.member, message.channel, 'this shit aint matter anymore lol', client, tr.responses.get(tr.bindings.get('welcome'))); + } + } +}; \ No newline at end of file diff --git a/events/guildMemberAdd.js b/events/guildMemberAdd.js index 5eb5b6b..5985e4f 100644 --- a/events/guildMemberAdd.js +++ b/events/guildMemberAdd.js @@ -1,8 +1,18 @@ const GuildData = require('../models/guild'); +const Responses = require('../models/responses'); +const sendResponse = require('../util/response/sendresponse'); module.exports = async (client, member) => { let tg = await GuildData.findOne({gid: member.guild.id}); + let tr = await Responses.findOne({gid: member.guild.id}); if (tg && tg.joinrole.length && member.guild.roles.cache.has(tg.joinrole)) { if (member.guild.members.cache.get(client.user.id).permissions.has("MANAGE_ROLES")) {member.roles.add(tg.joinrole);} } + if ( + tr && tr.bindings.has('welcome') && tr.responses.has(tr.bindings.get('welcome')) + && tg.wch.length && member.guild.channels.cache.has(tg.wch) + && member.guild.channels.cache.get(tg.wch).permissionsFor(client.user.id).has("SEND_MESSAGES") + ) { + member.guild.channels.cache.get(tg.wch).send(await sendResponse(member, member.guild.channels.cache.get(tg.wch), 'xdlol', client, tr.responses.get(tr.bindings.get('welcome')))); + } }; \ No newline at end of file diff --git a/util/response/parseresponse.js b/util/response/parseresponse.js index 4f91600..5705614 100644 --- a/util/response/parseresponse.js +++ b/util/response/parseresponse.js @@ -47,6 +47,7 @@ module.exports = async (message, client, args) => { 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;} + if (!options.text || !options.text.length) {return message.reply("You must specify -text for your message.");} } 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;}} diff --git a/util/response/sendresponse.js b/util/response/sendresponse.js index a22d597..1b55938 100644 --- a/util/response/sendresponse.js +++ b/util/response/sendresponse.js @@ -2,21 +2,19 @@ const Discord = require('discord.js'); const filterResponse = require('./filterresponse'); -module.exports = async(message, channel, mode, client, options) => { +module.exports = async(member, 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(await filterResponse(message.member, client, options.description)); - if (options.fieldnames && options.fieldnames.length) {let i; for (i=0;i