cloudservices/src/Client.ts

132 lines
4.3 KiB
TypeScript
Raw Normal View History

2019-10-14 15:46:10 -04:00
import Eris from 'eris';
import Redis from 'ioredis';
2019-10-14 15:46:10 -04:00
import mongoose from 'mongoose';
import signale from 'signale';
2019-10-14 15:46:10 -04:00
import fs from 'fs-extra';
import config from './config.json';
2019-11-16 19:23:50 -05:00
import { Server } from './api';
2019-10-18 18:16:32 -04:00
import { Account, AccountInterface, Moderation, ModerationInterface, Domain, DomainInterface } from './models';
2019-10-15 20:34:13 -04:00
import { emojis } from './stores';
2019-10-31 14:32:59 -04:00
import { Command, Util, Collection } from './class';
2019-10-31 18:23:59 -04:00
import * as commands from './commands';
2019-10-14 15:46:10 -04:00
2019-10-14 18:01:05 -04:00
2019-10-14 15:46:10 -04:00
export default class Client extends Eris.Client {
2019-11-16 19:45:43 -05:00
public config: { 'token': string; 'cloudflare': string; 'prefix': string; 'emailPass': string; 'mongoURL': string; 'port': number; 'keyPair': { 'publicKey': string; 'privateKey': string; }; };
2019-10-14 23:32:37 -04:00
public util: Util;
2019-10-31 16:12:08 -04:00
public commands: Collection<Command>;
2019-10-14 23:32:37 -04:00
2019-10-17 15:42:57 -04:00
public db: { Account: mongoose.Model<AccountInterface>; Domain: mongoose.Model<DomainInterface>; Moderation: mongoose.Model<ModerationInterface>; };
2019-10-14 23:32:37 -04:00
public redis: Redis.Redis;
2019-10-14 23:32:37 -04:00
public stores: { emojis: { success: string, loading: string, error: string }; };
public signale: signale.Signale;
2019-11-16 19:23:50 -05:00
public server: Server;
2019-11-30 18:14:50 -05:00
public updating: boolean;
public buildError: boolean
2019-11-18 15:21:06 -05:00
2019-10-14 15:46:10 -04:00
constructor() {
2019-10-14 23:32:37 -04:00
super(config.token, { getAllUsers: true, restMode: true, defaultImageFormat: 'png' });
2019-10-14 15:46:10 -04:00
2019-10-27 20:17:29 -04:00
process.title = 'cloudservices';
this.config = config;
2019-10-14 23:32:37 -04:00
this.util = new Util(this);
2019-10-31 16:32:12 -04:00
this.commands = new Collection<Command>();
2019-10-17 15:42:57 -04:00
this.db = { Account, Domain, Moderation };
this.redis = new Redis();
2019-10-14 23:32:37 -04:00
this.stores = { emojis };
this.signale = signale;
this.signale.config({
displayDate: true,
displayTimestamp: true,
displayFilename: true,
});
2019-11-18 15:21:06 -05:00
this.updating = false;
2019-11-30 18:14:50 -05:00
this.buildError = false;
this.events();
this.loadFunctions();
this.init();
}
private async events() {
process.on('unhandledRejection', (error) => {
this.signale.error(error);
});
}
private async loadFunctions() {
const functions = await fs.readdir('./functions');
functions.forEach(async (func) => {
2020-03-17 01:35:43 -04:00
if (func === 'index.ts' || func === 'index.js' || func === 'dataConversion.js') return;
try {
2019-10-28 18:11:08 -04:00
(require(`./functions/${func}`).default)(this);
} catch (error) {
this.signale.error(`Error occured loading ${func}`);
await this.util.handleError(error);
}
});
2019-10-14 15:46:10 -04:00
}
2019-10-31 18:23:59 -04:00
public loadCommand(CommandFile: any) {
2019-10-14 15:46:10 -04:00
// eslint-disable-next-line no-useless-catch
try {
// eslint-disable-next-line
2019-10-31 18:23:59 -04:00
const command: Command = new CommandFile(this);
2019-10-31 14:32:59 -04:00
if (command.subcmds.length) {
command.subcmds.forEach((C) => {
const cmd: Command = new C(this);
command.subcommands.add(cmd.name, cmd);
});
}
2019-10-31 16:26:01 -04:00
delete command.subcmds;
2019-10-31 14:32:59 -04:00
this.commands.add(command.name, command);
this.signale.complete(`Loaded command ${command.name}`);
2019-10-14 15:46:10 -04:00
} catch (err) { throw err; }
}
public async init() {
const evtFiles = await fs.readdir('./events/');
2019-10-31 18:23:59 -04:00
Object.values(commands).forEach((c: Function) => this.loadCommand(c));
2019-10-14 23:32:37 -04:00
evtFiles.forEach((file) => {
2019-10-14 15:46:10 -04:00
const eventName = file.split('.')[0];
if (file === 'index.js') return;
// eslint-disable-next-line
const event = new (require(`./events/${file}`).default)(this);
this.signale.complete(`Loaded event ${eventName}`);
2019-10-14 15:46:10 -04:00
this.on(eventName, (...args) => event.run(...args));
delete require.cache[require.resolve(`./events/${file}`)];
});
2019-10-27 23:05:24 -04:00
await mongoose.connect(config.mongoURL, { useNewUrlParser: true, useUnifiedTopology: true });
2019-10-27 20:10:23 -04:00
await this.connect();
2019-10-29 12:15:20 -04:00
this.on('ready', () => {
this.signale.info(`Connected to Discord as ${this.user.username}#${this.user.discriminator}`);
});
2019-11-09 23:14:02 -05:00
const intervals = await fs.readdir('./intervals');
intervals.forEach((interval) => {
// eslint-disable-next-line
if (interval === 'index.js') return;
require(`./intervals/${interval}`).default(this);
this.signale.complete(`Loaded interval ${interval.split('.')[0]}`);
});
2019-11-16 19:23:50 -05:00
this.server = new Server(this, { port: this.config.port });
2019-11-30 17:55:53 -05:00
2020-03-17 01:39:18 -04:00
const corepath = '/opt/CloudServices/dist';
const cmdFiles = await fs.readdir('/opt/CloudServices/dist/commands');
2019-12-16 20:27:12 -05:00
cmdFiles.forEach((f) => delete require.cache[`${corepath}/${f}`]);
delete require.cache[`${corepath}/config.json`];
delete require.cache[`${corepath}/class/Util`];
2019-10-14 15:46:10 -04:00
}
2019-10-14 23:32:37 -04:00
}
// eslint-disable-next-line
new Client();