Skip to content

Commit

Permalink
Version 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Sep 26, 2018
1 parent ec8973c commit 5ccfe9c
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 97 deletions.
4 changes: 0 additions & 4 deletions default_config.json
Original file line number Diff line number Diff line change
@@ -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" },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
7 changes: 7 additions & 0 deletions rm.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"folders": [
{
"path": "."
}
]
}
256 changes: 164 additions & 92 deletions rolemanager.js
Original file line number Diff line number Diff line change
@@ -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());

0 comments on commit 5ccfe9c

Please sign in to comment.