forked from ScootKit/CustomDCBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
219 lines (206 loc) · 9 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
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
const Discord = require('discord.js');
const client = new Discord.Client({partials: ['MESSAGE', 'CHANNEL', 'REACTION'], disableMentions: 'everyone'});
const fs = require('fs');
const {Sequelize} = require('sequelize');
const {asyncForEach} = require('./src/functions/helpers');
// Parsing parameters
let config;
let confDir = `${__dirname}/config`;
let dataDir = `${__dirname}/data`;
const args = process.argv.slice(2);
let scnxSetup = false; // If enabled some other (closed-sourced) files get imported and executed
if (process.argv.includes('--scnx-enabled')) scnxSetup = true;
if (args[0] === '--help' || args[0] === '-h') {
console.log('node main.js <configDir> <dataDir>');
process.exit();
}
if (args[0] && args[1]) {
confDir = args[0];
dataDir = args[1];
}
// Loading config
try {
config = require(`${confDir}/config.json`);
} catch (e) {
console.error('[FATAL] Missing config.json! Run "npm run generate-config <ConfDir>" (Parameter ConfDir is optional) to generate it');
process.exit(1);
}
let moduleConf = {};
try {
moduleConf = require(`${confDir}/modules.json`);
} catch (e) {
console.log('[INFO] Missing moduleConfig-file. Automatically disabling all modules and overwriting modules.json later');
}
const configChecker = require('./src/functions/checkConfig');
const models = {}; // Object with all models
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.events = new Discord.Collection();
client.modules = {};
client.guildID = config['guildID'];
client.config = config;
client.configDir = confDir;
client.dataDir = dataDir;
// Connecting to Database
const db = new Sequelize({
dialect: 'sqlite',
storage: `${dataDir}/database.sqlite`,
logging: false
});
// Starting bot
db.authenticate().then(async () => {
await loadModules();
await loadCommandsInDir('./src/commands');
await loadEventsInDir('./src/events');
await db.sync();
console.log('[DB] Synced db');
await client.login(config.token).catch(console.error);
console.log(`[BOT] Client logged in as ${client.user.tag} and is now online!`);
client.models = models;
await checkAllConfigs();
client.strings = require(`${confDir}/strings.json`);
if (scnxSetup) await require('./src/functions/scnx-integration').init(client);
client.emit('botReady');
client.botReadyAt = new Date();
console.log('[BOT] The bot initiated successfully and is now listening to commands.');
});
// Checking every (module AND bot) config file.
async function checkAllConfigs() {
console.log('[INFO] Checking configs...');
return new Promise(async resolve => {
await fs.readdir(`${__dirname}/config-generator/`, async (err, files) => {
await asyncForEach(files, async f => {
await configChecker.checkBuildInConfig(f);
});
await fs.readdir(`${__dirname}/modules/`, async (err, moduleFiles) => {
let needOverwrite = false;
await asyncForEach(moduleFiles, async f => {
if (moduleConf[f]) {
if (client.modules[f]['config']['on-checked-config-event']) await configChecker.checkModuleConfig(f, require(`./modules/${f}/${client.modules[f]['config']['on-checked-config-event']}`));
else await configChecker.checkModuleConfig(f);
} else if (typeof moduleConf[f] === 'undefined') needOverwrite = true;
});
if (needOverwrite) await configChecker.generateModulesConfOverwrite(moduleConf, moduleFiles);
console.log('[INFO] Done with checking.');
resolve();
});
});
});
}
// Load all modules
async function loadModules() {
const files = fs.readdirSync(`${__dirname}/modules/`);
for (const f of files) {
if (moduleConf[f]) {
console.log(`[MODULE] Loading module ${f}`);
const moduleConfig = require(`${__dirname}/modules/${f}/module.json`);
client.modules[f] = {};
client.modules[f]['config'] = moduleConfig;
if (moduleConfig['models-dir']) await loadModelsInDir(`./modules/${f}${moduleConfig['models-dir']}`, f);
if (moduleConfig['commands-dir']) await loadCommandsInDir(`./modules/${f}${moduleConfig['commands-dir']}`, f);
if (moduleConfig['events-dir']) await loadEventsInDir(`./modules/${f}${moduleConfig['events-dir']}`, f);
if (moduleConfig['on-load-event']) require(`./modules/${f}/${moduleConfig['on-load-event']}`).onLoad(client.modules[f]);
} else console.log(`[MODULE] Module ${f} is disabled`);
}
}
// Load every command in a dictionary
async function loadCommandsInDir(dir, moduleName = null) {
const files = fs.readdirSync(`${__dirname}/${dir}`);
for (const f of files) {
const stats = fs.lstatSync(`${__dirname}/${dir}/${f}`);
if (!stats) return console.error('no stats');
if (stats.isFile()) {
const props = require(`${__dirname}/${dir}/${f}`);
console.log(`[COMMANDS] Loaded ${dir}/${f}`);
props.fileName = `${dir}/${f}`;
client.commands.set(props.help.name, props);
props.help.aliases.forEach(alias => {
client.aliases.set(alias, props.help.name);
});
if (moduleName) {
if (client.modules[moduleName]) {
if (!client.modules[moduleName]['aliases']) client.modules[moduleName]['aliases'] = new Discord.Collection();
if (!client.modules[moduleName]['commands']) client.modules[moduleName]['commands'] = [];
client.modules[moduleName]['commands'].push(props.help.name);
props.help.aliases.forEach(alias => {
client.modules[moduleName]['aliases'].set(alias, props.help.name);
});
}
}
} else {
console.log(`[COMMANDS] Loading commands in subdir ${dir}/${f}`);
await loadCommandsInDir(`${dir}/${f}`);
}
}
}
// Loading every event in a dictionary
async function loadEventsInDir(dir, moduleName = null) {
fs.readdir(`${__dirname}/${dir}`, (err, files) => {
if (err) return console.error(err);
files.forEach(f => {
fs.lstat(`${__dirname}/${dir}/${f}`, async (err, stats) => {
if (!stats) return;
if (stats.isFile()) {
const eventFunction = require(`${__dirname}/${dir}/${f}`);
const eventStart = eventFunction.run.bind(null, client);
const eventName = f.split('.')[0];
client.events.set(eventName, eventStart);
if (moduleName) {
if (client.modules[moduleName]) {
if (!client.modules[moduleName]['events']) client.modules[moduleName]['events'] = [];
client.modules[moduleName]['events'].push(f.split('.js').join(''));
}
}
client.on(eventName, (...cArgs) => eventFunction.run(client, ...cArgs));
console.log(`[EVENTS] Loaded ${dir}/${f}`);
} else {
console.log(`[EVENTS] Loading events in ${dir}/${f}`);
await loadEventsInDir(`${dir}/${f}/`);
}
});
});
});
}
// load every database model in a dictionary
async function loadModelsInDir(dir, moduleName = null) {
return new Promise(async resolve => {
await fs.readdir(`${__dirname}/${dir}`, (async (err, files) => {
if (err) {
console.error(err);
process.exit(1);
}
for await (const file of files) {
await loadModule(dir, file, moduleName);
}
resolve();
}));
});
}
// load one database model
async function loadModule(dir, file, moduleName) {
return new Promise(async resolve => {
const stats = fs.lstatSync(`${__dirname}/${dir}/${file}`);
if (!stats) return;
if (stats.isFile()) {
const model = require(`${__dirname}/${dir}/${file}`);
await model.init(db);
if (moduleName) {
if (client.modules[moduleName]) {
if (!client.modules[moduleName]['models']) client.modules[moduleName]['models'] = [];
client.modules[moduleName]['models'].push(file.split('.js').join(''));
if (!models[moduleName]) models[moduleName] = {};
models[moduleName][model.config.name] = model;
}
} else models[model.config.name] = model;
console.log(`[DB] Loaded model ${dir}/${file}`);
resolve(true);
} else {
console.log(`[DB] Loading modules in dir ${dir}/${file}`);
await loadModules(`${dir}/${file}`);
}
});
}
module.exports.models = models;
module.exports.client = client;
module.exports.dataDir = dataDir;
module.exports.confDir = confDir;