-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
143 lines (118 loc) · 4.16 KB
/
index.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
const path = require('path');
const util = require('util');
const fs = require('fs');
const trovojs = require('trovo.js');
var DEV = false;
const Bot = require(path.join(__dirname, 'modules', 'Bot.js'));
const cookiesJson = fs.readFileSync("cookies.json");
const cookies = JSON.parse(cookiesJson);
const client = new trovojs.BrowserClient({ logger: Bot.log, headless: !DEV });
Bot.setClient(client);
Bot.setRoot(path.resolve(__dirname));
Bot.setData(path.join(__dirname, 'data'));
if (DEV) {
Bot.loadSettings(path.join(__dirname, "..", 'settings.dev.json'));
} else {
Bot.loadSettings(path.join(__dirname, 'settings.json'));
}
Bot.loadLocalizationFiles(path.resolve(__dirname, 'localization')).then(() => {
Bot.loadServices(path.join(__dirname, 'services'));
Bot.loadPlugins(path.join(__dirname, 'plugins'));
Bot.loadProcessors(path.join(__dirname, 'processors'));
if (Bot.settings.console) {
Bot.loadConsoleCommands();
}
}).catch((e) => {
Bot.log("Utilizing Fallback Language: en");
Bot.defaultFallbackLocalization();
Bot.loadServices(path.join(__dirname, 'services'));
Bot.loadPlugins(path.join(__dirname, 'plugins'));
Bot.loadProcessors(path.join(__dirname, 'processors'));
if (Bot.settings.console) {
Bot.loadConsoleCommands();
}
});
const cooldowns = new Map();
client.on('chatEvent', (type, data) => {
//Bot.log(`chatEvent/r/n` + util.inspect(data, false, null, true /* enable colors */))
if (data.user === Bot.settings.trovo.name && type === 'userJoined') return;
Bot.triggerEvents(data.chatType, client, data);
});
client.on('chatMessage', (message) => {
//Bot.log(`chatMessage/r/n` + util.inspect(message, false, null, true /* enable colors */))
Bot.processProcessors(message, client).then((skip) => {
if (skip) return;
if (!message || message.user === undefined) return;
if (message.user === Bot.settings.trovo.name) return;
if (!message.content) return;
if (!message.content.startsWith(Bot.settings.prefix, 0)) return;
const args = message.content.slice(Bot.settings.prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = Bot.getChatCommand(commandName);
if (!command) return;
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Map());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (
timestamps.has(message.user) &&
(message.badges === undefined ||
message.badges.indexOf('moderator') <= -1 ||
message.badges.indexOf('creator') <= -1)
) {
const expirationTime = timestamps.get(message.user) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
client.sendMessage(Bot.translate("bot.cooldown", {
timeLeft: timeLeft.toFixed(1),
name: command.name
}));
return;
}
}
timestamps.set(message.user, now);
setTimeout(() => timestamps.delete(message.user), cooldownAmount);
if (
command.permissions !== undefined &&
command.permissions.length !== 0 &&
(!message.badges ||
command.permissions.filter((value) => message.badges.includes(value)).length === 0)
) {
client.sendMessage(Bot.translate("bot.missing_permissions"));
return;
}
try {
message.args = args;
message.prefix = Bot.settings.prefix;
message.command = commandName;
command.execute(client, message);
} catch (err) {
client.sendMessage(Bot.translate("bot.cmd_error", {
name: commandName,
err: err
}));
client.sendMessage(Bot.translate("bot.contact_creator"));
}
}).catch((e) => {
Bot.log(e);
if (e) {
client.sendMessage(Bot.translate("bot.process_error", {
err: e
}));
client.sendMessage(Bot.translate("bot.contact_creator"));
}
});
});
client.on('ready', () => {
Bot.log(Bot.translate("bot.ready"));
});
client.login(
Bot.settings.trovo.page,
Bot.settings.trovo.email,
Bot.settings.trovo.password,
cookies,
Bot.settings.owner.email || null,
Bot.settings.owner.password || null
);