UNPKG

2.48 kBJavaScriptView Raw
1exports.command = {
2 name: "demote", // Name of command to be executed (Max 10 chars)
3 autoload: true, // Should the command be autoloaded at startup
4 unloadable: true, // Can the command be unloaded dynamically
5 min_rank: 3, // Minimum rank to use to execute the command
6 display: "Lower someone's rank!", // Summary help text to show in the .help command (Max 60 chars)
7 help: "Lower someone's rank!", // Full help text when .help <command> is used
8 usage: ".demote <user>", // Full help text when .help <command> is used
9
10 // Function to execute the command
11 execute: function(socket, command, command_access) {
12 var chalk = require('chalk');
13 var me = socket;
14 var whom = command.split(' ')[0];
15 var w = null;
16 // check if we got an whom
17 if (typeof whom === 'undefined' || whom.length < 1) {
18 return me.write("Demote whom?\r\n");
19 } else { // check if it's an user
20 var wArr = command_access.getAproxUser(whom);
21 if (wArr.length === 0) return me.write("Demote whom?\r\n");
22 if (wArr.length > 1) {
23 var possibilities = "";
24 for (var p = 0; p < wArr.length - 1; p++) {
25 possibilities += wArr[p] + ", ";
26 }
27 possibilities += wArr[wArr.length - 1];
28 return me.write("Be more explicit: whom do you want to demote ("+possibilities+")?\r\n");
29 }
30 whom = wArr[0];
31 w = command_access.getUser(whom);
32 }
33 if (w.rank == 0) {
34 socket.write("How low do you think someone can be?\r\n");
35 } else if (me.db.rank > w.rank) {
36 // if user is online, do it via sockets
37 var online = command_access.getOnlineUser(whom);
38 var rankName;
39 if (online) {
40 online.db.rank = online.db.rank-1;
41 rankName = command_access.ranks.list[online.db.rank];
42 command_access.updateUser(online.username, online.db);
43 } else {
44 w.rank = w.rank-1;
45 rankName = command_access.ranks.list[w.rank];
46 command_access.updateUser(whom, w);
47 }
48 whom = whom.toLowerCase().charAt(0).toUpperCase() + whom.toLowerCase().slice(1);
49 var sentence = chalk.red(":: ") + chalk.white(me.username) + chalk.red(" has demoted ") +
50 chalk.yellow(whom) + chalk.red(" to the rank of ") + chalk.green(rankName) + chalk.red("! ::\r\n");
51 command_access.allButMe(socket,function(me,to){to.write(sentence);});
52 socket.write("You " + chalk.red("demoted ") + chalk.yellow(whom) + " to the rank of " + chalk.green(rankName) + "!\r\n");
53 } else {
54 me.write("You cannot demote someone with the same or an higher rank than yours!\r\n");
55 }
56 }
57}