-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathmoderation.js
213 lines (202 loc) · 5.94 KB
/
moderation.js
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
//None of these commands actually work. Disabling them for now
exports.commands = [
"myid",
//"perm",
//"votekick",
"kick"
]
var votekicks = {};
function usersOnline(server){
var online = 0;
for(var i = 0; i < server.members.length; i++){
if(server.members[i].status != 'offline') online += 1;
}
return online;
}
function resolveMention(usertxt){
var userid = usertxt;
if(usertxt.startsWith('<@!')){
userid = usertxt.substr(3,usertxt.length-4);
} else {
if(usertxt.startsWith('<@')){
userid = usertxt.substr(2,usertxt.length-3);
}
}
return userid;
}
function resolveUser(msgContext,usertxt){
try {
var userid = usertxt;
if(usertxt.startsWith('<@')){
userid = usertxt.substr(2,usertxt.length-3);
}
var user = msgContext.guild.members.get(userid);
/*if(!user){
var users = msg.guild.members.findAll("username",usertxt);
if(users.length == 1){
user = users[0];
} else {
return null;
}
}*/
return user;
}catch(e){
console.error(e);
}
}
exports.myid = {
description: "returns the user id of the sender",
process: function(bot,msg){msg.channel.send(msg.author.id);}
}
exports.perm = {
usage: "[user]",
description: "Returns the user's permissions in this channel",
process: function(bot,msg,suffix) {
var user = resolveUser(msg,suffix);
if(!user){
user = msg.author;
}
msg.channel.send("permissions of " + user + ':\n' + JSON.stringify(msg.channel.permissionsFor(user).serialize(),null,2));
}
}
exports.votekick = {
usage: "<user|user id>",
description: "Vote to kick a user. Requires the majority of online users to vote for the kick, and always at least 2 users",
process: function(bot,msg,suffix) {
if(suffix){
//first check if the bot can kick
if(!msg.channel.permissionsFor(bot.user).hasPermission("kickMembers")){
msg.channel.send( "I don't have permission to kick people!");
return;
}
var vote = function(user){
if(votekicks.hasOwnProperty(user.id)){
var votes = votekicks[user.id];
votes.count += 1;
if(votes.voters.indexOf(msg.author.id) > -1){
msg.channel.send(msg.author + " you can only vote once!");
return;
}
votes.voters.push(msg.author.id);
if(votes.count > usersOnline(msg.channel.server)/2){
msg.channel.send("Vote passed!\nKicking " + user + " from " + msg.channel.server + "!",
function() {
bot.kickMember(users[0],msg.channel.server);
});
}
} else {
votekicks[user.id] = { count:1, voters:[msg.author.id]};
msg.channel.send("Starting votekick for user " + user + "!");
}
};
if(suffix.startsWith("<@")){
suffix = suffix.substr(2,suffix.length-3);
}
var user = msg.channel.server.members.get("id",suffix);
if(user){
vote(user);
return;
}
var users = msg.channel.server.members.getAll("username",suffix);
if(users.length > 1){
msg.channel.send("Multiple people match " + suffix + "!")
} else if(users.length == 1){
vote(users[0]);
} else {
msg.channel.send("I couldn't find a user " + suffix);
}
} else {
msg.channel.send("You must specify a user to kick!");
}
}
}
exports.kick = {
usage: "<user>",
description: "Kick a user with a message! Requires both the author of the message and the bot to have kick permission",
process: function(bot,msg,suffix) {
let args = suffix.split(" ");
if(args.length > 0 && args[0]){
//first check if the bot can kick
let hasPermissonToKick = msg.guild.members.get(bot.user.id).permissions.has("KICK_MEMBERS");
if(!hasPermissonToKick){
msg.channel.send( "I don't have permission to kick people!");
return;
}
//now check if the user can kick
if(!msg.guild.members.get(msg.author.id).permissions.has("KICK_MEMBERS")){
msg.channel.send( "You don't have permission to kick people!");
return;
}
var targetId = resolveMention(args[0]);
let target = msg.guild.members.get(targetId);
if(target != undefined){
if(!target.kickable){
msg.channel.send("I can't kick " + target + ". Do they have the same or a higher role than me?");
return;
}
if(args.length > 1) {
let reason = args.slice(1).join(" ");
target.kick(reason).then(x => {
msg.channel.send("Kicking " + target + " from " + msg.guild + " for " + reason + "!");
}).catch(err => msg.channel.send("Kicking " + target + " failed:\n"));
} else {
target.kick().then(x => {
msg.channel.send("Kicking " + target + " from " + msg.guild + "!");
}).catch(err => msg.channel.send("Kicking " + target + " failed:\n"));
}
} else {
msg.channel.send("I couldn't find a user " + args[0]);
}
} else {
msg.channel.send("You must specify a user to kick!");
}
}
}
exports.bans = {
description: "returns the list of users who have been banned from this server",
process: function(bot,msg,suffix){
bot.getBans(msg.channel.server,function(error,users){
if(users.length == 0){
msg.channel.send("No one has been banned from this server!");
} else {
var response = "Banned users:";
for(var user in users){
response += "\n" + user.username;
}
msg.channel.send(response);
}
});
}
}
exports.ban = {
usage: "<user> [days of messages to delete]",
description: "bans the user, optionally deleting messages from them in the last x days",
process: function(bot,msg,suffix){
var args = suffix.split(' ');
var usertxt = args.shift();
var days = args.shift();
var user = resolveUser(msg,usertxt);
if(user){
bot.banMember(user,msg.server,days,function(){
msg.channel.send("banned user " + user + " id:" + user.id);
});
} else {
msg.channel.send("couldn't uniquely resolve " + usertxt);
}
}
}
exports.unban = {
usage: "<user>",
description: "unbans the user.",
process: function(bot,msg,suffix){
var args = suffix.split(' ');
var usertxt = args.shift();
var days = args.shift();
var user = resolveUser(msg,usertxt);
if(user){
bot.unbanMember(user,msg.server);
} else {
bot.send("couldn't uniquely resolve " + usertxt);
}
}
}