From 5ccfe9c3f0836895326f743f56cefbb5cfa26c3e Mon Sep 17 00:00:00 2001 From: Karl Essinger Date: Wed, 26 Sep 2018 20:07:23 +0200 Subject: [PATCH] Version 1.0.0 --- default_config.json | 4 - package.json | 2 +- rm.code-workspace | 7 ++ rolemanager.js | 256 ++++++++++++++++++++++++++++---------------- 4 files changed, 172 insertions(+), 97 deletions(-) create mode 100644 rm.code-workspace diff --git a/default_config.json b/default_config.json index c251dd2..f8f9743 100644 --- a/default_config.json +++ b/default_config.json @@ -1,14 +1,10 @@ { - "_description": "The bot token from https://discordapp.com/developers/", "token": "add-your-token-here", - "_description": "Prefix for discord commands.", "prefix": "+", - "_description": "Bot's avatar url or path", "avatarURL": "https://karlofduty.com/img/tardisIcon.jpg", - "_description": "Keys are the arguments users use in their commands and values are the names of the corresponding discord roles. Remember to make the bot a higher rank than these roles.", "roles": [ { "scp": "SCP:SL Player" }, { "dystopia": "Dystopia Player" }, diff --git a/package.json b/package.json index 0b4e159..560e56a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discordbot", - "version": "0.1.0", + "version": "1.0.0", "description": "A Discord bot for users to be able to add or remove some roles.", "main": "rolemanager.js", "scripts": { diff --git a/rm.code-workspace b/rm.code-workspace new file mode 100644 index 0000000..362d7c2 --- /dev/null +++ b/rm.code-workspace @@ -0,0 +1,7 @@ +{ + "folders": [ + { + "path": "." + } + ] +} \ No newline at end of file diff --git a/rolemanager.js b/rolemanager.js index 8f62b6d..76656b6 100644 --- a/rolemanager.js +++ b/rolemanager.js @@ -1,125 +1,197 @@ const Discord = require("discord.js"); const { token, prefix, avatarURL, roles } = require("./config.json"); +var fs = require('fs'); -const client = new Discord.Client(); +const discordClient = new Discord.Client(); -client.on("ready", () => { - console.log("Discord connection established!"); - client.user.setAvatar(avatarURL); -}); - -client.on("message", (message) => +function manageUser(message, roleTag, addRole) { - //Abort if message does not start with the prefix - if (!message.content.startsWith(prefix) || message.author.bot) + var doesNotExist = true; + //Finds the role the user requested from the config + for (var i = 0; i < roles.length; i++) { - return; - } - - //Cut message into base command and arguments - const args = message.content.slice(prefix.length).split(/ +/); - const command = args.shift().toLowerCase(); - - //Join command - if (command === "join" || command === "add") - { - //Cancel if there are no arguments - if (!args.length) - { - return message.channel.send("You didn't specify a role, " + message.author + "!"); - } - - const roleArg = args.shift().toLowerCase(); - //Adds all roles from the config to the user - if (roleArg === "all") + const key = Object.keys(roles[i])[0]; + if (key === roleTag || roleTag === "all") { - for (var i = 0; i < roles.length; i++) + doesNotExist = false; + const role = message.guild.roles.find("name", roles[i][key]); + if(addRole) { - //Set role for user - const key = Object.keys(roles[i])[0]; - const role = message.guild.roles.find("name", roles[i][key]); message.member.addRole(role).catch((err) => { console.error(err); - message.channel.send("Internal error occured, does the bot have permission to do that?"); + message.channel.send("```diff\n- Error occured, does the bot have permission to do that?```"); }); + message.channel.send("```diff\n+ Role \"" + role.name + "\" granted.```"); + } + else + { + message.member.removeRole(role).catch((err) => + { + console.error(err); + message.channel.send("```diff\n- Error occured, does the bot have permission to do that?```"); + }); + message.channel.send("```diff\n+ Role \"" + role.name + "\" revoked.```"); } - message.channel.send("Roles added, " + message.author + "!"); console.log("Executed \"" + message.content + "\" successfully on " + message.member.user.tag + "."); } - else + } + if(doesNotExist) + { + message.channel.send("```diff\n- Invalid role provided.```"); + } +} + +function removeRoleCommand(message, command) +{ + for(var i = 0; i < roles.length; i++) + { + const key = Object.keys(roles[i])[0]; + if(key === command) { - //Finds the role the user requested from the config - for (var i = 0; i < roles.length; i++) + roles.splice(i, 1); + fs.writeFile("config.json", JSON.stringify({ token, prefix, avatarURL, roles }, null, 4), (err) => { - const key = Object.keys(roles[i])[0]; - if (key === roleArg) + if (err) { - //Set role for user - const role = message.guild.roles.find("name", roles[i][key]); - message.member.addRole(role).catch((err) => - { - console.error(err); - message.channel.send("Internal error occured, does the bot have permission to do that?"); - }); - message.channel.send("Role added, " + message.author + "!"); - console.log("Executed \"" + message.content + "\" successfully on " + message.member.user.tag + "."); - return; + message.channel.send("```diff\n- Internal error occured, could not write to config file.```"); + console.log(err); } - } - message.channel.send("Invalid role provided, " + message.author + "!"); + else + { + message.channel.send("```diff\n+ Command removed.```"); + } + }); + return; } } - //Leave command - if (command === "leave" ||command === "remove") + message.channel.send("```diff\n- Invalid command \"" + command + "\" provided.```"); +} + +function helpCommand(message) +{ + var helpMessage = "```md"; + helpMessage += ("\n# Member commands"); + for(var i = 0; i < roles.length; i++) + { + const key = Object.keys(roles[i])[0]; + helpMessage += ("\n- < " + prefix + "join " + key + " > gives the role < " + roles[i][key] + " >"); + } + helpMessage += ("\n- < " + prefix + "join all > gives all of the above roles"); + helpMessage += ("\n"); + for(var i = 0; i < roles.length; i++) { - //Cancel if there are no arguments - if (!args.length) + const key = Object.keys(roles[i])[0]; + helpMessage += ("\n- < " + prefix + "leave " + key + " > removes the role < " + roles[i][key] + " >"); + } + helpMessage += ("\n- < " + prefix + "join all > removes all of the above roles"); + helpMessage += ("\n"); + helpMessage += ("\n# Admin commands"); + helpMessage += ("\n- < " + prefix + "addrole (command) (role name) > adds a new command to rolemanager."); + helpMessage += ("\n- < " + prefix + "removerole (command) > removes a command from rolemanager"); + helpMessage += "\n```"; + message.channel.send(helpMessage); +} + +function addRoleCommand(message, command, role) +{ + for(var i = 0; i < roles.length; i++) + { + if(roles[i].key === command) { - return message.channel.send("You didn\"t specify a role, " + message.author + "!"); + message.channel.send("```diff\n- That command already exists.```"); + return; } - - const roleArg = args.shift().toLowerCase(); - //Remove all roles from the config to the user - if (roleArg === "all") + } + roles.push(JSON.parse("{\"" + command + "\":\"" + role +"\"}")); + fs.writeFile("config.json", JSON.stringify({ token, prefix, avatarURL, roles }, null, 4), (err) => + { + if (err) { - for (var i = 0; i < roles.length; i++) - { - //Set role for user - const key = Object.keys(roles[i])[0]; - const role = message.guild.roles.find("name", roles[i][key]); - message.member.removeRole(role).catch((err) => - { - console.error(err); - message.channel.send("Internal error occured, does the bot have permission to do that?"); - }); - } - message.channel.send("Roles removed, " + message.author + "!"); - console.log("Executed \"" + message.content + "\" successfully on " + message.member.user.tag + "."); + message.channel.send("```diff\n- Internal error occured, could not write to config file.```"); + console.log(err); } else { - //Finds the role the user requested from the config - for (var i = 0; i < roles.length; i++) + message.channel.send("```diff\n+ Command added.```"); + } + }); +} +discordClient.on("ready", () => +{ + console.log("Discord connection established!"); + discordClient.user.setAvatar(avatarURL); + discordClient.user.setActivity(prefix + "help", + { + type: "LISTENING" + }); +}); + +discordClient.on("message", (message) => +{ + //Abort if message does not start with the prefix + if (!message.content.startsWith(prefix) || message.author.bot) + { + return; + } + + //Cut message into base command and arguments + const args = message.content.slice(prefix.length).split(/ +/); + const command = args.shift().toLowerCase(); + switch(command) + { + case "join": + if (!args.length) { - const key = Object.keys(roles[i])[0]; - if (key === roleArg) - { - //Remove role for user - const role = message.guild.roles.find("name", roles[i][key]); - message.member.removeRole(role).catch((err) => - { - console.error(err); - message.channel.send("Internal error occured, does the bot have permission to do that?"); - }); - message.channel.send("Role removed, " + message.author + "!"); - console.log("Executed \"" + message.content + "\" successfully on " + message.member.user.tag + "."); - return; - } + return message.channel.send("```diff\n- You didn't specify a role.```"); } - message.channel.send("Invalid role provided, " + message.author + "!"); - } + manageUser(message, args.shift().toLowerCase(), true); + break; + case "leave": + if (!args.length) + { + return message.channel.send("```diff\n- You didn't specify a role.```"); + } + manageUser(message, args.shift().toLowerCase(), false); + break; + case "addrole": + if (args.length < 2) + { + return message.channel.send("```diff\n- Missing arguments.```"); + } + if (!message.member.hasPermission("ADMINISTRATOR")) + { + return message.channel.send("```diff\n- You don't have permission to do that.```"); + } + addRoleCommand(message, args.shift().toLowerCase(), args.join(" ")); + break; + case "removerole": + if (!args.length) + { + return message.channel.send("```diff\n- You didn't specify a role command to remove.```"); + } + if (!message.member.hasPermission("ADMINISTRATOR")) + { + return message.channel.send("```diff\n- You don't have permission to do that.```"); + } + removeRoleCommand(message, args.shift().toLowerCase()); + break; + case "help": + helpCommand(message); + break; } }); -client.login(token); +discordClient.login(token); + +// Runs when the server shuts down +function shutdown() +{ + console.log("Signing out of Discord..."); + discordClient.destroy(); +} +process.on("exit", () => shutdown()); +process.on("SIGINT", () => shutdown()); +process.on("SIGUSR1", () => shutdown()); +process.on("SIGUSR2", () =>shutdown()); +process.on("SIGHUP", () => shutdown()); \ No newline at end of file