2020-04-13 23:29:08 +02:00
|
|
|
const { EventEmitter } = require('events');
|
2021-06-06 10:46:41 +02:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-08-14 16:13:39 +02:00
|
|
|
const { inspect } = require('util');
|
2021-06-08 22:28:52 +02:00
|
|
|
const os = require('os');
|
2020-04-08 16:27:34 +02:00
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
const ShardManager = require('./middleware/ShardManager.js');
|
2020-04-13 22:38:10 +02:00
|
|
|
const Logger = require('./middleware/logger/Logger.js');
|
2020-04-08 16:01:31 +02:00
|
|
|
|
|
|
|
class Manager extends EventEmitter {
|
|
|
|
|
2020-04-08 16:27:34 +02:00
|
|
|
constructor(options) {
|
|
|
|
|
2020-04-09 16:30:52 +02:00
|
|
|
super();
|
|
|
|
|
2020-04-09 23:08:28 +02:00
|
|
|
this.shardManager = new ShardManager('./structure/client/DiscordClient.js', options);
|
2020-04-14 08:41:42 +02:00
|
|
|
this.logger = new Logger(this);
|
2020-04-08 16:27:34 +02:00
|
|
|
|
|
|
|
this._built = false;
|
2020-08-14 16:13:39 +02:00
|
|
|
this.readyAt = null;
|
2020-04-14 10:26:59 +02:00
|
|
|
|
|
|
|
this.shardManager.on('message', this._handleMessage.bind(this));
|
2020-04-16 14:37:04 +02:00
|
|
|
// this.on('built', () => {
|
|
|
|
// this.shardManager.broadcast({ _built: true });
|
|
|
|
// });
|
2020-04-13 23:29:08 +02:00
|
|
|
|
2020-04-08 16:27:34 +02:00
|
|
|
}
|
2020-04-13 23:29:08 +02:00
|
|
|
|
2020-04-14 10:26:59 +02:00
|
|
|
_handleMessage(shard, message) {
|
2020-04-14 17:05:27 +02:00
|
|
|
|
2020-08-14 16:13:39 +02:00
|
|
|
if (message._mEval) return this.eval(shard, message);
|
2021-06-08 22:28:52 +02:00
|
|
|
// Messages coming from the API that have the _logger flag will be caught here, i.e. both flags should be on
|
2020-08-14 16:13:39 +02:00
|
|
|
if (message._logger) return this.logger._handleMessage(shard, message);
|
|
|
|
if (message._webhook) return undefined; //todo
|
2021-06-08 22:28:52 +02:00
|
|
|
if (message._api) return this.apiRequest(shard, message);
|
|
|
|
|
2020-04-14 10:26:59 +02:00
|
|
|
}
|
|
|
|
|
2020-04-08 16:27:34 +02:00
|
|
|
async build() {
|
|
|
|
|
2021-06-06 11:35:55 +02:00
|
|
|
this.info('Spawning shards...');
|
2020-04-16 14:37:04 +02:00
|
|
|
await this.shardManager.spawn();
|
2021-06-06 11:35:55 +02:00
|
|
|
// G:\Documents\My programs\GBot\New GBot\api\index.cjs
|
|
|
|
this.info('Shards done, booting up API...');
|
2021-06-09 13:33:46 +02:00
|
|
|
const API = await import('./api/index.js').catch(() => this.warn(`Import of API Manager failed. Skipping API.`));
|
2021-06-09 02:19:22 +02:00
|
|
|
if (API) {
|
|
|
|
const { default: APIManager } = API;
|
2021-06-06 15:30:53 +02:00
|
|
|
this.apiManager = new APIManager(this);
|
|
|
|
await this.apiManager.init();
|
|
|
|
this.info('API done. Manager built.');
|
|
|
|
} else this.info('Missing API source, skipping');
|
2020-04-09 16:30:52 +02:00
|
|
|
|
2020-04-08 16:27:34 +02:00
|
|
|
this._built = true;
|
2020-04-16 14:37:04 +02:00
|
|
|
this.emit('built');
|
2020-08-14 16:13:39 +02:00
|
|
|
this.readyAt = Date.now();
|
2020-04-08 16:27:34 +02:00
|
|
|
|
|
|
|
}
|
2020-04-08 16:01:31 +02:00
|
|
|
|
2020-08-14 16:13:39 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {*} shard The shard from which the eval came and to which it will be returned
|
|
|
|
* @param {*} script The script to be executed
|
|
|
|
* @memberof Manager
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
async eval(shard, { script }) {
|
|
|
|
|
2021-06-06 11:35:55 +02:00
|
|
|
this.info(`Incoming manager eval from shard ${shard.id}:\n${script}`);
|
2020-08-14 16:13:39 +02:00
|
|
|
let result = null,
|
|
|
|
error = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line no-eval
|
|
|
|
result = eval(script);
|
|
|
|
if (result instanceof Promise) result = await result;
|
|
|
|
//if(typeof result !== 'string') result = inspect(result);
|
|
|
|
} catch (err) {
|
|
|
|
error = err.stack || err;
|
|
|
|
}
|
|
|
|
|
|
|
|
return shard.send({
|
|
|
|
_evalResult: true,
|
|
|
|
script,
|
|
|
|
result,
|
|
|
|
error
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-08 22:28:52 +02:00
|
|
|
async apiRequest(shard, message) {
|
|
|
|
|
|
|
|
const { type } = message;
|
|
|
|
switch (type) {
|
|
|
|
case 'statsQuery':
|
2021-06-09 02:19:22 +02:00
|
|
|
break;
|
2021-06-08 22:28:52 +02:00
|
|
|
// TODO: Figure out best strategy for stats querying - push/pull, prometheus, simple polling
|
|
|
|
//return this.aggregateStatistics();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async aggregateStatistics() {
|
|
|
|
|
|
|
|
const managerData = {
|
|
|
|
memory: process.memoryUsage().heapUsed, // Bytes
|
|
|
|
uptime: Date.now() - this.readyAt,
|
|
|
|
library: {
|
|
|
|
name: 'Discord.js',
|
|
|
|
versionersion: require('discord.js').version
|
|
|
|
},
|
|
|
|
managedShards: this.shardManager.shards.size
|
|
|
|
};
|
|
|
|
|
|
|
|
const CPU = os.cpus();
|
|
|
|
const freeMemory = os.freemem();
|
|
|
|
const totalMemory = os.totalmem();
|
|
|
|
const systemData = {
|
|
|
|
cpu: CPU[0].model,
|
|
|
|
cores: CPU.length,
|
|
|
|
memory: {
|
|
|
|
used: totalMemory - freeMemory,
|
|
|
|
total: totalMemory
|
|
|
|
},
|
|
|
|
osType: os.type(),
|
|
|
|
uptime: os.uptime(),
|
|
|
|
hostname: os.hostname()
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-14 16:13:39 +02:00
|
|
|
get uptime() {
|
|
|
|
return this.readyAt !== null ? Date.now() - this.readyAt : 0;
|
|
|
|
}
|
|
|
|
|
2021-06-06 11:35:55 +02:00
|
|
|
info(message) {
|
|
|
|
this.logger.write('info', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(message) {
|
|
|
|
this.logger.write('warn', message);
|
|
|
|
}
|
|
|
|
|
|
|
|
error(message) {
|
|
|
|
this.logger.write('error', message);
|
|
|
|
}
|
|
|
|
|
2020-04-08 16:01:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Manager;
|