-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
427 lines (379 loc) · 13.3 KB
/
main.ts
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { Db, MongoClient } from "mongodb";
import * as Discord from "discord.js";
import * as fs from "fs";
import * as perm from "./permissions";
import * as helper from "./helper";
import * as cmd_perm from "./Commands/permissions";
import * as embed from "./Commands/embed";
import * as tagesschau from "./Commands/tagesschau";
import * as space_launch from "./Commands/space_launch";
import * as roles from "./Commands/roles";
import * as channels from "./Commands/channel"
import * as insults from "./Commands/insults"
import * as useless_stuff from "./Commands/useless_stuff"
import * as debug from "./Commands/debug"
export const config = JSON.parse(fs.readFileSync("./configs/config.json", "utf8"));
export var db: Db;
export var client = new Discord.Client();
export enum args_types {
number,
text,
text_with_spaces,
user_mention,
void_channel_mention
}
export interface User {
id: string | undefined;
permission: number;
}
export interface Role {
id: number;
name: string;
}
export interface Server {
id: string;
users: Array<User>;
roles_to_manage: Array<Role>
}
interface Command {
invoke: string;
command: any;
show_on_list?: boolean
}
interface CommandGroup {
name: string;
commands: Array<Command>;
show_on_list?: boolean
}
client.on("ready", () => {
setup_mongodb();
console.log("[Discord.js] Logged in as " + client.user?.username + "...");
client.user?.setActivity(config.prefix + "help", { type: "LISTENING"}).catch(console.log)
});
export var cmdmap: Array<CommandGroup> = [
{ name: "General", commands: [
{ invoke: "help", command: cmd_help },
{ invoke: "group_help", command: cmd_group_help },
{ invoke: "command_help", command: cmd_command_help },
{ invoke: "ping", command: cmd_ping },
]},
{ name: "Tagesschau", commands: [
{ invoke: "tagesschau_search", command: tagesschau.cmd_search },
{ invoke: "tagesschau_news", command: tagesschau.cmd_news },
]},
{ name: "Permissions", commands: [
{ invoke: "permission_add", command: cmd_perm.cmd_addPermission },
{ invoke: "permission_remove", command: cmd_perm.cmd_removePermission },
{ invoke: "permission_get", command: cmd_perm.cmd_getPermission },
{ invoke: "permission_list", command: cmd_perm.cmd_permission_list },
]},
{ name: "Space Launches", commands: [
{ invoke: "spacelaunch_last", command: space_launch.cmd_lastLaunch },
{ invoke: "spacelaunch_next", command: space_launch.cmd_nextLaunch },
{ invoke: "spacelaunch_lastSpaceX", command: space_launch.cmd_lastSpaceXLaunch },
{ invoke: "spacelaunch_nextSpaceX", command: space_launch.cmd_nextSpaceXLaunch },
]},
{ name: "Roles", commands: [
{ invoke: "give_role_self", command: roles.cmd_giveRoleSelf },
{ invoke: "remove_role_self", command: roles.cmd_removeRoleSelf },
{ invoke: "list_roles", command: roles.cmd_list_roles },
{ invoke: "add_managed_role", command: roles.cmd_addManagedRole },
{ invoke: "remove_managed_role", command: roles.cmd_removeManagedRole },
{ invoke: "give_role", command: roles.cmd_giveRole },
{ invoke: "remove_role", command: roles.cmd_removeRole }
]},
{ name: "Channels", commands: [
{ invoke: "move_all_users", command: channels.cmd_move_all_to_other_channel },
{ invoke: "move_user", command: channels.cmd_move_user }
]},
{ name: "Insults", commands: [
{ invoke: "insult", command: insults.cmd_insult},
{ invoke: "insult_dm", command: insults.cmd_insult_dm}
]},
{ name: "Useless stuff", commands: [
{ invoke: "open_source", command: useless_stuff.cmd_OpenSource}
]},
{ name: "Debug", commands: [
{ invoke: "get_pid", command: debug.cmd_get_pid, show_on_list: false },
{ invoke: "eval", command: debug.cmd_eval, show_on_list: false },
{ invoke: "tmp_cmd", command: debug.cmd_temp_cmd, show_on_list: false }
], show_on_list: false}
];
client.on("message", (msg) => {
if (!msg.guild) return;
if (msg.member?.id == client.user?.id) return;
var cont = msg.content;
var author = msg.member;
try {
if (author?.id != null && client.user?.id != null) {
if (author.id != client.user.id && cont.startsWith(config.prefix)) {
var invoke = cont.split(" ")[0].substring(config.prefix.length);
var args = cont.split(" ").slice(1);
if (cmd_exists(invoke)) {
check_server_and_user(msg, args, invoke, (user) => {
if (cmd_perm.hasPermissions(user, get_cmd(invoke)(undefined, undefined, true).permission)) {
if (check_args(msg, invoke, args, get_cmd(invoke)(undefined, undefined, true).args))
get_cmd(invoke)(msg, args, undefined);
} else {
embed.error(msg.channel, "You dont have the required Permissions!", "");
}
});
} else {
embed.error(msg.channel, `The command \`${invoke}\` does not exist!`, "");
}
}
}
} catch (err) {
catch_err(err, msg);
}
});
function cmd_exists(invoke: string) {
for (var cmd_group in cmdmap) {
for (var cmd in cmdmap[cmd_group].commands) {
if (cmdmap[cmd_group].commands[cmd].invoke == invoke) {
return true;
}
}
}
return false;
}
function check_args(msg: Discord.Message, command: string,given_args: Array<string>, required_args: any) {
if (required_args.length != given_args.length && required_args[required_args.length - 1].type != args_types.text_with_spaces) {
embed.error(msg.channel, `This command needs ${required_args.length} arguments, but you gave ${given_args.length}.\n
To get more info about this command and its arguments, use \`${config.prefix}command_help ${command}\``, "");
return false;
}
for (var i = 0; i < required_args.length; i++) {
var valid = false;
for (var j = 0; j < required_args[i].type.length; j++) {
if (check_arg(msg, given_args[i], i, required_args[i].type[j], command)) valid = true;
}
if (!valid) {
embed.error(msg.channel, `The ${i + 1}. argument is wrong.\n
To get more info about this command and its arguments, use \`${config.prefix}command_help ${command}\``, "");
return false;
}
}
return true;
}
function check_arg(msg: Discord.Message, arg: string, arg_index: number, arg_type: any, command: string) {
if (arg_type == args_types.number) {
if (!+arg) {
return false;
}
} else if (arg_type == args_types.user_mention) {
var mentions = /<@!(\d+)>/g.exec(arg);
if (!mentions?.length) {
return false;
}
if (mentions[0] != arg) {
return false;
}
var users = msg.guild?.members.cache.array();
if (users == undefined) return false;
var found = false;
for (var user of users) {
if (user.id == mentions[1]) {
found = true;
break;
}
}
if (!found) {
return false;
}
} else if (arg_type == args_types.void_channel_mention) {
var mentions = /<#(\d+)>/g.exec(arg);
if (!mentions?.length) {
return false;
}
if (mentions[0] != arg) {
return false;
}
var channels = msg.guild?.channels.cache.array();
if (channels == undefined) return false;
var found = false;
for (var channel of channels) {
if (channel.id == mentions[1] && channel.type == "voice") {
found = true;
break;
}
}
if (!found) {
return false;
}
}
return true;
}
function get_cmd(invoke: string) {
for (var cmd_group in cmdmap) {
for (var cmd in cmdmap[cmd_group].commands) {
if (cmdmap[cmd_group].commands[cmd].invoke == invoke) {
return cmdmap[cmd_group].commands[cmd].command;
}
}
}
return cmdmap[0].commands[0].command;
}
export function catch_err(err: string, msg: Discord.Message) {
embed.error(msg.channel, err, "Error");
console.log("ERROR: " + err);
}
function cmd_help(msg: Discord.Message | undefined, args: Array<string> | undefined, getInfo: boolean | undefined) {
if (getInfo) {
return {
permission: perm.list.none,
description: "Get a list of all command groups.",
args: []
}
}
var base = `All of the command groups are:\n`;
var help_msg = base;
for (var cmd_group of cmdmap) {
if (cmd_group.show_on_list == true || cmd_group.show_on_list == undefined)
help_msg += "\n\u27A4\`" + cmd_group.name + "\`";
}
help_msg += `\n\nuse the command \`${config.prefix}group_help [group name]\` to get all commands from one group.`
embed.message(msg?.channel, help_msg, "");
}
function cmd_group_help(msg: Discord.Message | undefined, args: Array<string> | undefined, getInfo: boolean | undefined) {
if (getInfo) {
return {
permission: perm.list.none,
description: "Get a list and short description of all commands in one group.",
args: [
{ name: "Name of group", type: [args_types.text_with_spaces]}
]
}
}
if (args == undefined) return
var base = `All of the commands in the group \`${args.join(' ')}\` are:\n\n`;
var help_msg = base;
for (var cmd_group in cmdmap) {
if (cmdmap[cmd_group].name.toLowerCase() != args.join(' ').toLowerCase()) continue;
if (cmdmap[cmd_group].show_on_list === false) continue;
var help_msgs: Array<string> = [];
for (var cmd in cmdmap[cmd_group].commands) {
if (cmdmap[cmd_group].commands[cmd].show_on_list === false) continue;
help_msgs.push(cmdmap[cmd_group].commands[cmd].invoke);
}
for (var i = 0; i < help_msgs.length; i++) {
if (i != 0) help_msg += "\n" ;
help_msg += "\u27A4 \`" + config.prefix + help_msgs[i] + "\`: " + get_cmd(help_msgs[i])(undefined, undefined, true).description;
}
help_msg += `\n\nTo get more info on a specific command, you can try \`${config.prefix}command_help [command]\`!`;
return embed.message(msg?.channel, help_msg, "");
}
embed.error(msg?.channel, `There is no command group named \`${args.join(' ')}\`!`, "");
}
function cmd_command_help(msg: Discord.Message | undefined, args: Array<string> | undefined, getInfo: boolean | undefined) {
if (getInfo) {
return {
permission: perm.list.none,
description: "Get a list and short description of all commands in one group.",
args: [
{ name: "Name of command", type: [args_types.text]}
]
}
}
if (args?.length != 1) return;
if (!cmd_exists(args[0])) {
return embed.error(msg?.channel, `The command ${args[0]} does not exist!`, "");
}
var info = get_cmd(args[0])(undefined, undefined, true);
var help_msg = `**Description:**\n${info.description}`;
if (info.args.length != 0)
help_msg += `\n**Arguments:**`;
for (var arg of info.args)
help_msg += `\n\tName: ${arg.name} Type: ${arg_to_text(arg)}`;
help_msg += `\n**Required permission:**\n${perm_to_text(info.permission)}`;
embed.message(msg?.channel, help_msg, "");
}
function cmd_ping(msg: Discord.Message | undefined, args: Array<string> | undefined, getInfo: boolean | undefined) {
if (getInfo) {
return {
permission: perm.list.none,
description: "Test, if my connection is working.",
args: []
}
}
if (msg == undefined) return;
embed.message(msg?.channel, `This answer took me \`${Date.now() - msg?.createdTimestamp}\` ms.`, "");
}
function arg_to_text(arg: any) {
var str = "";
for (var i = 0; i < arg.type.length; i++) {
if (i != 0) str += ", or ";
switch(arg.type[i]){
case args_types.text:
str += "Text (only one word)";
break;
case args_types.number:
str += "Number (no . or ,)";
break;
case args_types.text_with_spaces:
str += "Text (multiple words)";
break;
case args_types.user_mention:
str += "User mention (e.g. <@!691979492662444073>)"
break;
case args_types.void_channel_mention:
str += "Voice channel mention"
break;
default:
str += "An error occured";
}
}
return str;
}
function perm_to_text(perm_in: any) {
var perm_entries = Object.entries(perm.list);
for (var perm_entry of perm_entries) {
if (perm_entry[1] == perm_in)
return perm_entry[0];
}
return "An error occured";
}
function check_server_and_user(msg: Discord.Message, args: Array<string>, invoke: string, callback: (user: User | undefined) => void) {
helper.isServerKnown(msg.guild?.id, (found) => {
if (!found) {
helper.add_server_to_db(msg.guild?.id, () => {
after_callback();
return;
});
} else {
after_callback();
}
function after_callback() {
helper.isUserKnown(msg.member?.id, msg.guild?.id, (found_user) => {
if (!found_user) {
if (msg.member?.id == config.ownerID) {
helper.add_new_user_to_db(msg.member?.id, perm.list.admin, msg.guild?.id, () => {
helper.getUser(msg.member?.id, msg.guild?.id, (user) => {
callback(user);
return;
});
});
} else {
helper.add_new_user_to_db(msg.member?.id, perm.list.none, msg.guild?.id, () => {
helper.getUser(msg.member?.id, msg.guild?.id, (user) => {
callback(user);
return;
});
});
}
} else {
helper.getUser(msg.member?.id, msg.guild?.id, (user) => {
callback(user);
});
}
});
}
});
}
function setup_mongodb() {
MongoClient.connect(config.mongo_url, {useNewUrlParser: true, useUnifiedTopology: true}).then(client => {
db = client.db("bgfxc4s-bot");
console.log("[Database] connected succesfully to database!");
});
}
client.login(config.token);