-
Notifications
You must be signed in to change notification settings - Fork 2
/
bot.js
243 lines (200 loc) · 6.85 KB
/
bot.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
const { Client, MessageEmbed, Intents } = require('discord.js');
const client = new Client({ ws: { intents: Intents.ALL } });
// require('discord-buttons')(client);
const { MongoClient } = require("mongodb");
const DB = require("./db.js");
require('dotenv').config();
require('toml-require').install();
const fs = require('fs');
const toml = require('toml');
const concat = require('concat-stream');
var ready = false;
if (process.env.MONGO_DB_URL) client.mongo = new MongoClient(process.env.MONGO_DB_URL, { useUnifiedTopology: true });
client.config = require("./config.toml");
client.modules = {};
client.modulesConstants = {
dm: [],
core: [],
default: []
}
client.path = module.path;
client.dbSystem = new DB(client);
async function loadModules() {
if (process.env.MONGO_DB_URL) console.log("MongoDB connected");
const files = await fs.promises.readdir('./modules');
for (const file of files) {
try {
const path = './modules/' + file
const stat = await fs.promises.stat(path);
if (stat.isDirectory()) {
const mainfile = require(path + '/main.js');
const mod = new mainfile.MainClass(client);
client.modules[mod.commandText] = mod;
if (mod.dmEnabled) client.modulesConstants.dm.push(mod.commandText);
if (mod.core) client.modulesConstants.core.push(mod.commandText);
if (!mod.startDisabled && !mod.core && !mod.hidden) client.modulesConstants.default.push(mod.commandText);
console.log("Loaded module " + mod.name + " (" + mod.description + "), command text: " + process.env.PREFIX + mod.commandText + "");
}
} catch(e) {
console.error("Failed to load module from " + file + ": ", e);
}
}
client.enabledModules = await client.dbSystem.load("core", "modules", {});
client.admins = await client.dbSystem.load("core", "admins", {});
for (var guildID of Object.keys(client.enabledModules)) client.enabledModules[guildID] = client.enabledModules[guildID].filter(e => !client.modulesConstants.core.includes(e));
ready = true;
}
client.on('ready', () => {
console.log("Loading potential database");
if (!client.mongo) {
console.log("No database");
loadModules();
} else if (client.mongo.isConnected()) {
console.log("Already loaded!");
loadModules();
} else {
console.log("Waiting for connection...");
client.mongo.connect(err => {
if (err) {
console.error(err);
return;
}
console.log("Connected!");
loadModules();
});
}
});
client.on('message', message => {
if (!message.author.bot && ready) {
client.checkModulesOnInput(message, "on_message");
}
});
client.on('guildMemberAdd', member => {
if (!member.bot && ready) {
client.checkModulesOnInput(member, "on_guildMemberAdd");
}
});
client.checkModulesOnInput = function(input, method) {
var modules = [...client.modulesConstants.dm];
if (input.guild) {
if (!client.admins[input.guild.id]) {
client.admins[input.guild.id] = {
users: [ input.guild.owner.id ],
roles: []
};
client.dbSystem.save("core", "admins", client.admins);
}
if (!client.enabledModules[input.guild.id]) {
client.enabledModules[input.guild.id] = [...client.modulesConstants.default];
client.dbSystem.save("core", "modules", client.enabledModules);
}
modules = [...client.enabledModules[input.guild.id]];
modules.push(...client.modulesConstants.core);
}
modules.forEach((key) => {
var element = client.modules[key];
if (!element) return;
if (!element.ready) return;
try {
element[method](input);
} catch(e) {
client.error(input.channel, element.name, e);
}
});
}
process.on('uncaughtException', function (err) {
client.error(null, "Unknown", err);
});
process.on('unhandledRejection', function (err) {
client.error(null, "Unknown", err);
});
client.error = function(channel, name, error) {
console.error("Error caused by " + name + " module: ", error);
embed = new MessageEmbed()
.setTitle("Error caused by " + name + " module")
.setColor(0xff0000)
.setDescription("```js\n" + error.stack + "```")
client.channels.cache.get("474301772463341569").send(embed); //"<@240947137750237185>"
if (channel)
channel.send(embed.setFooter("This message will be deleted in one minute")).then(message => {
message.delete({ timeout: 60000 });
}).catch(console.error);
}
client.getUserFromMention = function(mention) {
if (!mention) return;
if (mention.startsWith('<@') && mention.endsWith('>')) {
mention = mention.slice(2, -1);
if (mention.startsWith('!')) {
mention = mention.slice(1);
}
return client.users.cache.get(mention);
}
}
client.getRoleFromMention = function(guild, mention) {
if (!mention) return;
if (mention.startsWith('<@') && mention.endsWith('>')) {
mention = mention.slice(2, -1);
if (mention.startsWith('&')) {
mention = mention.slice(1);
}
return guild.roles.cache.get(mention);
}
}
console.log("Starting bot...");
client.login(process.env.BOT_TOKEN).catch(console.error);
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200);
res.end('ok');
});
server.listen(3000);
/**
* Discord.js client.
* @external Client
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/Client|Client}
*/
/**
* Twitter Snowflake.
* @external Snowflake
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/typedef/Snowflake|Snowflake}
*/
/**
* Discord.js User.
* @external User
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/User|User}
*/
/**
* Discord.js Message.
* @external Message
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/Message|Message}
*/
/**
* Discord.js Message Embed.
* @external MessageEmbed
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/MessageEmbed|MessageEmbed}
*/
/**
* Discord.js Message Reaction.
* @external MessageReaction
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/MessageReaction|MessageReaction}
*/
/**
* Discord.js Text Channel.
* @external TextChannel
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/TextChannel|TextChannel}
*/
/**
* Discord.js DM Channel.
* @external DMChannel
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/class/DMChannel|DMChannel}
*/
/*
* Discord.js EmojiIdentifierResolvable.
* @external EmojiIdentifierResolvable
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/main/stable/typedef/EmojiIdentifierResolvable|EmojiIdentifierResolvable}
*/
/**
* Collection.
* @external Collection
* @see {@link https://discord.js.org/?source=post_page---------------------------#/docs/collection/master/class/Collection|Collection}
*/