From 505f8ba5531314f17a341407eafcc43b18d99a85 Mon Sep 17 00:00:00 2001 From: Your Mother Date: Tue, 4 Aug 2020 14:16:35 -0400 Subject: [PATCH] Command Handling --- src/commands/command-admin.js | 12 +++ src/commands/command-channel.js | 129 +++++++++++++++++++++++++++++ src/commands/command-dm.js | 142 ++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 src/commands/command-admin.js create mode 100644 src/commands/command-channel.js create mode 100644 src/commands/command-dm.js diff --git a/src/commands/command-admin.js b/src/commands/command-admin.js new file mode 100644 index 0000000..037e211 --- /dev/null +++ b/src/commands/command-admin.js @@ -0,0 +1,12 @@ +module.exports = (WDR, Message) => { + let command = Message.content.split(" ")[0].slice(1); + try { + if (WDR.Fs.existsSync(WDR.Dir + "/src/commands/admin/" + command.toLowerCase() + ".js")) { + Message.delete(); + let Cmd = require(WDR.Dir + "/src/commands/admin/" + command.toLowerCase() + ".js"); + Cmd(WDR, Message); + } + } catch (error) { + + } +} \ No newline at end of file diff --git a/src/commands/command-channel.js b/src/commands/command-channel.js new file mode 100644 index 0000000..8a8fdc4 --- /dev/null +++ b/src/commands/command-channel.js @@ -0,0 +1,129 @@ +module.exports = (WDR, Message) => { + + if (!Message.member) { + return; + } + + Message.member.isAdmin = Message.member.hasPermission("ADMINISTRATOR") ? true : false; + + Message.member.isMod = Message.member.hasPermission("MANAGE_ROLES") ? true : false; + + WDR.Discords.forEach(async (Server, index) => { + + if (Message.guild.id == Server.id && Server.command_channels.indexOf(Message.channel.id) >= 0) { + + Message.Discord = Server; + + // DELETE THE MESSAGE + //if (!WDR.Config.Tidy_Channel || WDR.Config.Tidy_Channel == "ENABLED") { + Message.delete(); + //} + + // FETCH THE GUILD MEMBER AND CHECK IF A DONOR + if (Message.member.isAdmin || Message.member.isMod) { + /* DO NOTHING */ + } else if (Server.donor_role && !Message.member.roles.cache.has(Server.donor_role)) { + if (WDR.Config.log_channel) { + let nondonor_embed = new WDR.DiscordJS.MessageEmbed() + .setColor("ff0000") + .addField("User attempted to use a subsciption command, not a donor. ", Message.member.user.username); + if (WDR.Config.donor_info) { + Message.donor_info = WDR.Config.donor_info; + } else { + Message.donor_info = ""; + } + Message.guild.members.fetch(Message.author.id).then(TARGET => { + TARGET.send("This feature is only for donors. " + Message.donor_info).catch(console.error); + }); + return WDR.Send_Embed(WDR, "member", 0, Server.id, "", nondonor_embed, WDR.Config.log_channel); + } else { + return console.log(WDR.Color.red + "User attempted to use DM command, not a donor. " + Message.member.user.username + WDR.Color.reset); + } + } + + // LOAD DATABASE RECORD BASED OFF OF ORIGIN SERVER_ID AND AUTHOR_ID + WDR.wdrDB.query( + `SELECT + * + FROM + wdr_users + WHERE + user_id = ${Message.member.id} + AND guild_id = ${Message.guild.id};`, + async function(error, user, fields) { + if (!user || !user[0]) { + Message.member.db = await WDR.Save_User(WDR, Message, Server); + Message.reply("Created database record for you. Please repeat the previous command.").then(m => m.delete({ + timeout: 5000 + })).catch(console.error); + } else { + Message.member.db = user[0]; + + let command = Message.content.split(" ")[0].slice(1); + switch (command) { + case "p": + command = "pokemon"; + break; + case "r": + command = "raid"; + break; + case "q": + command = "quest"; + break; + case "l": + command = "location"; + break; + case "a": + command = "area"; + break; + } + //try { + if (WDR.Fs.existsSync(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js")) { + let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js"); + if (Cmd) { + Cmd(WDR, Message); + } + } else if (WDR.Fs.existsSync(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + ".js")) { + let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + ".js"); + if (Cmd) { + Cmd(WDR, Message); + } + } else { + WDR.Console.error("[handlers/commands.js] " + Message.content + " command does not exist."); + } + // } catch (error) { + // try { + + // console.log("2", command.toLowerCase()); + // } catch (error) { + // console.log("3", command.toLowerCase()); + // //WDR.Console.error("[handlers/commands.js] Error Initializing Command", [command, error]); + // } + // } + } + if (user.user_name != Message.member.user.username) { + WDR.wdrDB.query(` + UPDATE + wdr_users + SET + user_name = '${Message.member.user.username}' + WHERE + user_id = ${Message.member.id}; + `); + WDR.wdrDB.query(` + UPDATE + wdr_subscriptions + SET + user_name = '${Message.member.user.username}' + WHERE + user_id = ${Message.member.id}; + `); + } + + // END + return; + } + ); + } + }); +} \ No newline at end of file diff --git a/src/commands/command-dm.js b/src/commands/command-dm.js new file mode 100644 index 0000000..b23c572 --- /dev/null +++ b/src/commands/command-dm.js @@ -0,0 +1,142 @@ +module.exports = (WDR, Message) => { + + Message.author.user_guilds = []; + + WDR.Discords.forEach(async (Server, index) => { + let guild = WDR.Bot.guilds.cache.get(Server.id); + if (!guild) { + WDR.Console.error(WDR, "[handlers/commands.js] Guild ID `" + Server.id + "` found in the database that does not match any guilds in the config."); + } else { + let member = WDR.Bot.guilds.cache.get(Server.id).members.cache.get(Message.author.id); + if (member && member.roles.cache.has(Server.donor_role)) { + Message.author.user_guilds.push({ + id: guild.id, + name: Server.name + }); + } + } + }); + + if (Message.author.user_guilds.length == 1) { + WDR.wdrDB.query( + `SELECT + * + FROM + users + WHERE + user_id = ${Message.author.id} + AND guild_id = ${user_guilds[0]}`, + async function(error, user, fields) { + if (!user || !user[0]) { + return Message.reply("Before you can create and modify subscriptions via DM, you must first use the subsciption channel in your scanner discord."); + } else { + Message.author.db = user[0]; + } + + let command = Message.content.split(" ")[0].slice(1); + switch (command) { + case "p": + command = "pokemon"; + break; + case "r": + command = "raid"; + break; + case "q": + command = "quest"; + break; + } + + let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js") + if (Cmd) { + Cmd(WDR, Message); + } + } + ); + } else if (Message.author.user_guilds.length > 1) { + + let list = ""; + Message.author.user_guilds.forEach((guild, i) => { + list += (i + 1) + " - " + guild.name + "\n"; + }); + list = list.slice(0, -1); + + let request_action = new WDR.DiscordJS.MessageEmbed() + .setAuthor(Message.author.username, Message.author.displayAvatarURL()) + .setTitle("Which Discord would you like to modify Subscriptions for?") + .setDescription(list) + .setFooter("Type the number of the discord."); + + Message.channel.send(request_action).catch(console.error).then(BotMsg => { + + const filter = CollectedMsg => CollectedMsg.author.id == Message.author.id; + const collector = Message.channel.createMessageCollector(filter, { + time: 60000 + }); + + collector.on("collect", CollectedMsg => { + + let num = parseInt(CollectedMsg.content); + + switch (true) { + case (isNaN(CollectedMsg.content)): + return CollectedMsg.reply("`" + CollectedMsg.content + "` is not a Number. Type the number next to the Discord name above.").then(m => m.delete({ + timeout: 5000 + })).catch(console.error); + case (num > 0 && num <= list.length): + return collector.stop((num - 1)); + default: + return CollectedMsg.reply("`" + CollectedMsg.content + "` is not a valid # selection. Type the number next to the Discord name above.").then(m => m.delete({ + timeout: 5000 + })).catch(console.error); + } + }); + + collector.on("end", (collected, num) => { + + console.log(Message.author.user_guilds); + console.log("num", Message.author.user_guilds[num]) + + BotMsg.delete(); + let query = ` + SELECT + * + FROM + wdr_users + WHERE + user_id = ${Message.author.id} + AND guild_id = ${Message.author.user_guilds[num].id}; + `; + console.log(query); + WDR.wdrDB.query( + query, + async function(error, user, fields) { + if (error) { + WDR.Console.error(WDR, "[src/handlers/commands.js] DM: Error Fetching User From DB", [query, error]) + } + Message.author.db = user[0]; + + let command = Message.content.split(" ")[0].slice(1); + switch (command) { + case "p": + command = "pokemon"; + break; + case "r": + command = "raid"; + break; + case "q": + command = "quest"; + break; + } + + let Cmd = require(WDR.Dir + "/src/commands/subscription/" + command.toLowerCase() + "/begin.js") + if (Cmd) { + Cmd(WDR, Message); + } + } + ); + }); + }); + } else { + return Message.reply("I did not find any Discords in which you are a Donor. Please go to your discord's subscribe website before modifying subscriptions"); + } +} \ No newline at end of file