This repository has been archived by the owner on Jan 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathmain.js
73 lines (62 loc) · 2.42 KB
/
main.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
const base = require("path").resolve(".");
const logger = require(base + "/src/utils/logger");
const security = require(base + "/cfg/security.json");
const commands = require(base + "/cfg/commands.json");
const configHandler = require(base + "/src/utils/configHandler");
const hooks = require(base + "/src/core/utils/hooks");
const scriptWrapper = require(base + "/src/core/utils/scriptWrapper");
const permissionHandler = require(base + "/src/core/utils/permissionHandler");
const {DiscordBot} = require("dynobot-framework");
const Bot = new DiscordBot(security.token);
Bot.onEvent("ready", () => {
logger.info("Bot successfully started.");
Bot.onEvent("error", err => {
logger.error("Discord API error: ", err);
});
//Init hooks
let servers = Bot.getClient().getServers();
servers.forEach((server) => {
hooks.init(server);
});
Bot.onEvent("serverMemberAdd", member => {
let pathConfig = base + "/cfg/config.json",
enabled = configHandler.readJSON(pathConfig, member.getServer().getId(), "welcome_message", "enabled"),
channelName = configHandler.readJSON(pathConfig, member.getServer().getId(), "welcome_message", "channel"),
part1 = configHandler.readJSON(pathConfig, member.getServer().getId(), "welcome_message", "part1"),
part2 = configHandler.readJSON(pathConfig, member.getServer().getId(), "welcome_message", "part2");
if (enabled) {
member.getServer().getTextChannels().forEach(channel => {
if (channel.getName() === channelName) {
channel.send(`${part1}<@${member.getId()}>${part2}`);
}
});
}
});
Bot.onEvent("message", msg => {
try {
let BotUser = Bot.getClient().getUser();
if (msg.isMentioned(BotUser) && msg.getAuthor().getId() !== BotUser.getId()) {
let bAnswered = false,
i = 0;
while (!bAnswered && i < commands.commandList.length) {
let command = commands.commandList[i],
pattern = new RegExp(command.regex);
if (pattern.test(msg.getContent(true).toLowerCase().trim())) {
if (permissionHandler.hasPermissions(msg, command)) {
bAnswered = scriptWrapper.run(msg, Bot.getClient(), command);
} else {
bAnswered = true;
msg.getTextChannel().send("You don't have access to this command.");
}
}
i++;
}
if (!bAnswered) {
msg.getTextChannel().send("Sorry, I can't help you with that.");
}
}
} catch (err) {
logger.error("There was a problem while executing a command: ", err);
}
});
});