-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec8973c
commit 5ccfe9c
Showing
4 changed files
with
172 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"folders": [ | ||
{ | ||
"path": "." | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |