forked from KarlOfDuty/RoleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrolemanager.js
More file actions
364 lines (342 loc) · 12.1 KB
/
rolemanager.js
File metadata and controls
364 lines (342 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
const Discord = require("discord.js");
const roles = require("./roles.json");
var fs = require("fs");
const YAML = require("yaml");
const config = fs.readFileSync("./config.yml", "utf8");
const { token, prefix, commands } = YAML.parse(config);
const discordClient = new Discord.Client();
function manageUser(message, roleTag, addRole)
{
if (message.member == null)
{
message.channel.send("```diff\n- Discord claims you don't exist so rip you I guess.```");
return;
}
var doesNotExist = true;
//Finds the role the user requested from the config
for (var i = 0; i < roles.length; i++)
{
const key = Object.keys(roles[i])[0];
if (key === roleTag || roleTag === "all")
{
console.log("Executing \"" + message.content + "\" on " + message.member.user.tag + ".");
doesNotExist = false;
const role = message.guild.roles.find("name", roles[i][key]);
if(role == null)
{
message.channel.send("```diff\n- Error occured, does that role exist?```");
return;
}
if(message.member == null)
{
message.channel.send("```diff\n- Error occured, sender of that message was null. Discord claims you do not exist. Debug info: " + message.member + " " + message.author + "```");
return;
}
if(addRole)
{
message.member.addRole(role).then(() =>
{
message.channel.send("```diff\n+ Role \"" + role.name + "\" granted.```");
}).catch(() =>
{
message.channel.send("```diff\n- Error occured, does the bot have permission to manage that role?```");
});
}
else
{
message.member.removeRole(role).then(() =>
{
message.channel.send("```diff\n+ Role \"" + role.name + "\" revoked.```");
}).catch(() =>
{
message.channel.send("```diff\n- Error occured, does the bot have permission to manage that role?```");
});
}
}
}
if(doesNotExist)
{
message.channel.send("```diff\n- Invalid role provided.```");
}
}
function pingCommand(message, roleTag)
{
var doesNotExist = true;
//Finds the role the user requested from the config
for (var i = 0; i < roles.length; i++)
{
const key = Object.keys(roles[i])[0];
if (key === roleTag)
{
doesNotExist = false;
const role = message.guild.roles.find("name", roles[i][key]);
if (role == null)
{
message.channel.send("```diff\n- Error occured, does that role exist?```");
return;
}
if (role.mentionable)
{
const embed = {
"description": `Ping brought to you by <@${message.member.id}>`,
"color": 3768539
};
message.channel.send("<@&" + role.id + ">", { embed }).then(message.delete().catch(console.error)).catch((err) =>
{
console.error("ERROR: Could not send response message. " + err);
});
}
else
{
role.setMentionable(true).then(() =>
{
const embed = {
"description": `Ping brought to you by <@${message.member.id}>`,
"color": 3768539
};
message.channel.send("<@&" + role.id + ">", { embed }).then(() =>
{
role.setMentionable(false).catch(() =>
{
message.channel.send("```diff\n- Error occured, does the bot have permission to manage that role?```");
});
}).catch((err) =>
{
console.error("Could not send response message. " + err);
role.setMentionable(false).catch(() =>
{
message.channel.send("```diff\n- Error occured, does the bot have permission to manage that role?```");
});
});
message.delete().catch(console.error);
}).catch(() =>
{
message.channel.send("```diff\n- Error occured, does the bot have permission to manage that role?```");
});
}
}
}
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)
{
roles.splice(i, 1);
fs.writeFile("roles.json", JSON.stringify(roles, null, 4), (err) =>
{
if (err)
{
message.channel.send("```diff\n- Internal error occured, could not write to config file.```");
console.log(err);
}
else
{
message.channel.send("```diff\n+ Command removed.```");
}
});
return;
}
}
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");
helpMessage += ("\n- < " + prefix + "leave (tag) > removes any of the above roles.");
if (hasPermission(message, "addrole") || hasPermission(message, "removerole") || hasPermission(message, "ping"))
{
helpMessage += ("\n");
helpMessage += ("\n# Admin commands");
helpMessage += ("\n- < " + prefix + "addrole (tag) (role) > adds a new role to rolemanager.");
helpMessage += ("\n- < " + prefix + "removerole (tag) > removes a role from rolemanager.");
helpMessage += ("\n- < " + prefix + "ping (tag) > pings a normally unpingable role.");
}
helpMessage += "\n```";
message.channel.send(helpMessage);
}
function addRoleCommand(message, command, role)
{
for(var i = 0; i < roles.length; i++)
{
if(roles[i].key === command)
{
message.channel.send("```diff\n- That command already exists.```");
return;
}
}
roles.push(JSON.parse("{\"" + command + "\":\"" + role +"\"}"));
fs.writeFile("roles.json", JSON.stringify(roles, null, 4), (err) =>
{
if (err)
{
message.channel.send("```diff\n- Internal error occured, could not write to config file.```");
console.log(err);
}
else
{
message.channel.send("```diff\n+ Command added.```");
}
});
}
function hasPermission(message, command)
{
if (!commands[command] || message.member == null)
{
return false;
}
if (!commands[command].channels.includes(message.channel.id) && !commands[command].channels.includes("*"))
{
return false;
}
if (commands[command].permissions.roles.includes("*") || commands[command].permissions.users.includes("*"))
{
return true;
}
if (commands[command].permissions.users.includes(message.member.id))
{
return true;
}
var memberRoles = message.member.roles;
for (var [roleID, role] of memberRoles)
{
if (commands[command].permissions.roles.includes(roleID))
{
return true;
}
}
return false;
}
discordClient.on("ready", () =>
{
console.log("Discord connection established!");
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":
// Abort if no permission
if (!hasPermission(message, command))
{
message.channel.send("```diff\n- You are not allowed to do that.```");
return;
}
if (!args.length)
{
return message.channel.send("```diff\n- You didn't specify a role.```");
}
manageUser(message, args.shift().toLowerCase(), true);
break;
case "leave":
// Abort if no permission
if (!hasPermission(message, command))
{
message.channel.send("```diff\n- You are not allowed to do that.```");
return;
}
if (!args.length)
{
return message.channel.send("```diff\n- You didn't specify a role.```");
}
manageUser(message, args.shift().toLowerCase(), false);
break;
case "ping":
// Abort if no permission
if (!hasPermission(message, command))
{
message.channel.send("```diff\n- You are not allowed to do that.```");
return;
}
if (!args.length)
{
return message.channel.send("```diff\n- You didn't specify a role.```");
}
pingCommand(message, args.shift().toLowerCase());
break;
case "addrole":
// Abort if no permission
if (!hasPermission(message, command))
{
message.channel.send("```diff\n- You are not allowed to do that.```");
return;
}
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":
// Abort if no permission
if (!hasPermission(message, command))
{
message.channel.send("```diff\n- You are not allowed to do that.```");
return;
}
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":
// Abort if no permission
if (!hasPermission(message, command))
{
message.channel.send("```diff\n- You are not allowed to do that.```");
return;
}
helpCommand(message);
break;
}
});
discordClient.on("error", (e) => console.error(e));
discordClient.on("warn", (e) => console.warn(e));
//discordClient.on("debug", (e) => console.info(e));
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());