-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
254 lines (214 loc) · 7.29 KB
/
app.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
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
/*
* CLUtopia bot by JERisBRISK
*
* Features
* - disallowed names (grandfathering certain IDs)
* - temporary mutes
* - temporary bans
* - permanent bans
* - warnings (x strikes => you're out)
* - forgiveness
* - infractions / rapsheet
*/
// include
const Discord = require('discord.js');
require('dotenv').config();
const AppName = "CLUtopia"
const Version = "0.1.0";
// #ifdef DEBUG
const Flavor = "Debug";
// #else
const Flavor = "Release";
// #endif
const Author = "JERisBRISK";
const CommandPrefix = '!';
const CommandPrefixLength = CommandPrefix.length;
class ValidationResult {
constructor(success, reason) {
this.success = success;
this.reason = reason;
}
}
function validate(args, shapes) {
// split the string, removing empty entries
let a = args.split(' ').filter(Boolean);
// validate args
if (!a || (shapes && shapes.length > a.length)) {
return new ValidationResult(false, "not enough arguments were provided");
}
// validate each positional argument in order
// against a possible RegEx in the same position
for (let i = 0; i < shapes.length; i++) {
let shape = shapes[i];
let arg = a[i];
if (shape instanceof RegExp) {
// #ifdef DEBUG
debug(` ${shape} is a RegExp, validating ${arg}`);
// #endif
if (!shape.test(arg)) {
let reason = `${arg} does NOT match ${shape}`
// #ifdef DEBUG
debug(` ${reason}`)
// #endif
return new ValidationResult(false, reason);
}
}
// null or other shape types are skipped
}
// if we have not failed by this point, return success
return new ValidationResult(true, "");
}
// data for ban command
const RolesImmuneToBan = process.env.BOT_ROLES_IMMUNE_TO_BAN.split(',').filter(Boolean)
//!ban @member reason
//Bans a member. Buh-bye.
function ban(msg, args) {
// #ifdef DEBUG
debug(`ban('${args}')`);
// #endif
var vr = validate(args,
[
// @member, which Discord expands to <@!0123456789012345> (! only if they have a nickname)
/(?:^<@!?[\d]{18}>)/i,
// reason, which we can be anything
null,
]);
if (!vr.success) {
msg.reply(`${vr.reason}.\nTry: \`${CommandPrefix}ban @member reason\``);
return;
}
let split = splitAtFirstSpace(args);
if (split.length > 1) {
var member = split[0];
var reason = split[1];
} else {
var member = split[0];
var reason = "";
}
var user = msg.mentions.users.first();
var member = msg.guild.member(user.id);
var serverOwner = msg.guild.owner;
// see if the target is a member of a protected class
if (member) {
// #ifdef DEBUG
debug(`${member.displayName} has roles: ${member.roles ? member.roles.cache.map(r => `${r.name}`).join(', ') : 'none'}`);
// #endif
if (member === serverOwner) {
msg.reply(`member <@!${user.id}> has immunity because they own the place.`);
return;
} else if (member.roles.cache.some(role => RolesImmuneToBan.includes(role.name))) {
audit(`${user.id} was not banned because they possess one or more of the following roles: ${RolesImmuneToBan.join(', ')}`)
msg.reply(`member <@!${user.id}> has role-based immunity.`);
return;
}
}
msg.reply(`member <@!${user.id}> was banned with reason: **${reason}**`);
}
// globals
var Commands = {
ban: ban,
//!cleanup [@member] [count] reason
//Deletes a number of *un-pinned* comments from the channel.
//If member is specified, only messages by that member are removed.
//(Pins are kept so we can clean up spam/dreck around valuable messages)
cleanup: "cleanup",
//!forgive @member [all | count] reason
// all - sets their infraction count to zero
// count - removes that many infractions from their record
forgive: "forgive",
//!infractions @member
//Shows information on a member's current infractions
infractions: "infractions",
//!kick @member reason
//Kicks a member from the server
kick: "kick",
//!silence @member duration reason
//Temporarily blocks a member of the server from talking in Voice channels
//Duration is either 'min' or 'h', e.g. "15min" or "1h"
silence: "silence",
//!tempban @member duration reason
//Temporarily bans a member from the server.
//Duration is either 'min' or 'h', e.g. "15min" or "1h"
tempban: "tempban",
//!tempmute @member duration reason
//Temporarily blocks a member of the server from typing in Text channels And joining or talking in Voice channels
//Duration is either 'min', 'h', 'd', e.g. "15min" or "1h" or "7d"
tempmute: "tempmute",
//!unban @member reason
//Un-Bans a member. Welcome back!
unban: "ban",
//!warn @member [count] reason
//Warns a member an optional count of times. Default is 1 count.
//3 warnings (either all at once or over time) results in an automatic 30 Day Mute
warn: "warn",
};
// init
const client = new Discord.Client();
client.login(process.env.BOT_TOKEN);
client.on('message', (msg) => {
audit(`${msg.author.username} ${msg.author} said '${msg.content}' in ${msg.channel.name} ${msg.channel}`);
// this is not a bot command, bail out
if (!msg.content.startsWith(CommandPrefix)) {
// #ifdef DEBUG
debug("Not a bot command. Ignoring.");
// #endif
return;
}
// trim the starting CommandPrefix character
let text = msg.content.substring(CommandPrefixLength);
// whitespace immediately followed the CommandPrefix, bail out
if (text[0] === ' ') {
// #ifdef DEBUG
debug('Command was prefixed with whitespace. Ignoring message.')
// #endif
return;
};
// split input into keyword and arguments
let split = splitAtFirstSpace(text);
if (split.length > 1) {
var keyword = split[0];
var args = split[1];
} else {
var keyword = split[0];
var args = "";
}
// #ifdef DEBUG
debug(`Provided keyword: ${keyword}`);
debug(`Provided arguments: ${args}`);
// #endif
// handle various commands
if (keyword in Commands) {
// #ifdef DEBUG
debug(`Identified keyword: ${keyword}`);
// #endif
// don't allow the bot to be used against itself
var target = msg.mentions.users.first();
if (client.user.id == target.id) {
msg.reply(`your mind powers will not work on me.`);
return;
}
// execute the command
(Commands[keyword])(msg, args);
} else {
// #ifdef DEBUG
debug(`'${keyword}' is not a recognized command.`);
// #endif
}
});
// #ifdef DEBUG
function debug(s) {
console.debug(`DEBUG|${s}`);
}
// #endif
function audit(s) {
console.log(`AUDIT|${new Date().toUTCString()}: ${s.replace(/[\r\n]/g,' ')}`);
}
function splitAtFirstSpace(s) {
let firstSpaceIndex = s.indexOf(' ');
if (firstSpaceIndex != -1) {
return [s.substring(0, firstSpaceIndex),
s.substr(firstSpaceIndex + 1)];
}
return [s];
}
audit(`Starting ${AppName} ${Version} [${Flavor}] by ${Author}`);