2020-06-29 03:00:27 -04:00
|
|
|
import Eris from 'eris';
|
|
|
|
import Redis from 'ioredis';
|
|
|
|
import mongoose from 'mongoose';
|
|
|
|
import signale from 'signale';
|
|
|
|
import fs from 'fs-extra';
|
|
|
|
import config from '../config.json';
|
|
|
|
import CSCLI from '../cscli/main';
|
|
|
|
import { Account, AccountInterface, Moderation, ModerationInterface, Domain, DomainInterface, Tier, TierInterface } from '../models';
|
|
|
|
import { emojis } from '../stores';
|
2020-06-29 17:13:54 -04:00
|
|
|
import { Command, Util, Collection, Server, Event } from '.';
|
2020-06-29 03:00:27 -04:00
|
|
|
|
|
|
|
|
|
|
|
export default class Client extends Eris.Client {
|
|
|
|
public config: { 'token': string; 'cloudflare': string; 'prefix': string; 'emailPass': string; 'mongoURL': string; 'port': number; 'keyPair': { 'publicKey': string; 'privateKey': string; }; };
|
|
|
|
|
|
|
|
public util: Util;
|
|
|
|
|
|
|
|
public commands: Collection<Command>;
|
|
|
|
|
2020-06-29 17:13:54 -04:00
|
|
|
public events: Collection<Event>;
|
|
|
|
|
2020-06-29 03:00:27 -04:00
|
|
|
public db: { Account: mongoose.Model<AccountInterface>; Domain: mongoose.Model<DomainInterface>; Moderation: mongoose.Model<ModerationInterface>; Tier: mongoose.Model<TierInterface>; };
|
|
|
|
|
|
|
|
public redis: Redis.Redis;
|
|
|
|
|
|
|
|
public stores: { emojis: { success: string, loading: string, error: string }; };
|
|
|
|
|
|
|
|
public functions: Collection<Function>;
|
|
|
|
|
|
|
|
public signale: signale.Signale;
|
|
|
|
|
|
|
|
public server: Server;
|
|
|
|
|
|
|
|
public updating: boolean;
|
|
|
|
|
|
|
|
public buildError: boolean
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(config.token, { getAllUsers: true, restMode: true, defaultImageFormat: 'png' });
|
|
|
|
|
|
|
|
process.title = 'cloudservices';
|
|
|
|
this.config = config;
|
|
|
|
this.util = new Util(this);
|
|
|
|
this.commands = new Collection<Command>();
|
2020-06-29 17:13:54 -04:00
|
|
|
this.events = new Collection<Event>();
|
2020-06-29 03:00:27 -04:00
|
|
|
this.functions = new Collection<Function>();
|
|
|
|
this.db = { Account, Domain, Moderation, Tier };
|
|
|
|
this.redis = new Redis();
|
|
|
|
this.stores = { emojis };
|
|
|
|
this.signale = signale;
|
|
|
|
this.signale.config({
|
|
|
|
displayDate: true,
|
|
|
|
displayTimestamp: true,
|
|
|
|
displayFilename: true,
|
|
|
|
});
|
|
|
|
this.updating = false;
|
|
|
|
this.buildError = false;
|
2020-06-29 17:13:54 -04:00
|
|
|
this.errorEvents();
|
2020-06-29 03:00:27 -04:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:13:54 -04:00
|
|
|
public async errorEvents() {
|
2020-07-28 07:52:11 -04:00
|
|
|
process.on('unhandledRejection', (error: Error) => {
|
|
|
|
this.util.handleError(error);
|
|
|
|
});
|
|
|
|
this.on('error', (error) => {
|
|
|
|
this.util.handleError(error);
|
2020-06-29 03:00:27 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:13:54 -04:00
|
|
|
public async loadFunctions() {
|
2020-06-29 17:17:08 -04:00
|
|
|
const functions = await fs.readdir(`${__dirname}/../functions`);
|
2020-06-29 03:00:27 -04:00
|
|
|
functions.forEach(async (func) => {
|
|
|
|
if (func === 'index.ts' || func === 'index.js') return;
|
|
|
|
try {
|
2020-06-29 17:17:08 -04:00
|
|
|
const funcRequire: Function = require(`${__dirname}/../functions/${func}`).default;
|
2020-06-29 03:00:27 -04:00
|
|
|
this.functions.set(func.split('.')[0], funcRequire);
|
|
|
|
} catch (error) {
|
|
|
|
this.signale.error(`Error occured loading ${func}`);
|
|
|
|
await this.util.handleError(error);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public loadCommand(CommandFile: any) {
|
|
|
|
// eslint-disable-next-line no-useless-catch
|
|
|
|
try {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
const command: Command = new CommandFile(this);
|
|
|
|
if (command.subcmds.length) {
|
|
|
|
command.subcmds.forEach((C) => {
|
|
|
|
const cmd: Command = new C(this);
|
|
|
|
command.subcommands.add(cmd.name, cmd);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
delete command.subcmds;
|
|
|
|
this.commands.add(command.name, command);
|
|
|
|
this.signale.complete(`Loaded command ${command.name}`);
|
|
|
|
} catch (err) { throw err; }
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:13:54 -04:00
|
|
|
public async loadEvents(eventFiles: { [s: string]: typeof Event; } | ArrayLike<typeof Event>) {
|
|
|
|
const evtFiles = Object.entries<typeof Event>(eventFiles);
|
|
|
|
for (const [name, Ev] of evtFiles) {
|
|
|
|
const event = new Ev(this);
|
|
|
|
this.events.add(event.event, event);
|
|
|
|
this.on(event.event, event.run);
|
|
|
|
this.signale.success(`Successfully loaded event: ${name}`);
|
|
|
|
delete require.cache[require.resolve(`${__dirname}/../events/${name}`)];
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 03:00:27 -04:00
|
|
|
|
2020-06-29 17:13:54 -04:00
|
|
|
public async loadCommands(commandFiles: { [s: string]: typeof Command; } | ArrayLike<typeof Command>) {
|
|
|
|
const cmdFiles = Object.values<typeof Command>(commandFiles);
|
|
|
|
for (const Cmd of cmdFiles) {
|
|
|
|
const command = new Cmd(this);
|
2020-06-29 17:26:39 -04:00
|
|
|
if (command.subcmds.length) {
|
|
|
|
command.subcmds.forEach((C) => {
|
|
|
|
const cmd: Command = new C(this);
|
|
|
|
command.subcommands.add(cmd.name, cmd);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
delete command.subcmds;
|
2020-06-29 17:13:54 -04:00
|
|
|
this.commands.add(command.name, command);
|
|
|
|
this.signale.success(`Successfully loaded command: ${command.name}`);
|
|
|
|
}
|
|
|
|
}
|
2020-06-29 03:00:27 -04:00
|
|
|
|
2020-06-29 17:13:54 -04:00
|
|
|
public async init() {
|
2020-06-29 03:00:27 -04:00
|
|
|
await mongoose.connect(config.mongoURL, { useNewUrlParser: true, useUnifiedTopology: true });
|
|
|
|
await this.connect();
|
|
|
|
this.on('ready', () => {
|
|
|
|
this.signale.info(`Connected to Discord as ${this.user.username}#${this.user.discriminator}`);
|
|
|
|
});
|
2020-06-29 17:17:08 -04:00
|
|
|
const intervals = await fs.readdir(`${__dirname}/../intervals`);
|
2020-06-29 03:00:27 -04:00
|
|
|
intervals.forEach((interval) => {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
if (interval === 'index.js') return;
|
2020-06-29 17:17:08 -04:00
|
|
|
require(`${__dirname}/../intervals/${interval}`).default(this);
|
2020-06-29 03:00:27 -04:00
|
|
|
this.signale.complete(`Loaded interval ${interval.split('.')[0]}`);
|
|
|
|
});
|
|
|
|
this.server = new Server(this, { port: this.config.port });
|
|
|
|
// eslint-disable-next-line no-new
|
|
|
|
new CSCLI(this);
|
|
|
|
|
|
|
|
const corepath = '/opt/CloudServices/dist';
|
|
|
|
const cmdFiles = await fs.readdir('/opt/CloudServices/dist/commands');
|
|
|
|
cmdFiles.forEach((f) => delete require.cache[`${corepath}/${f}`]);
|
|
|
|
delete require.cache[`${corepath}/config.json`];
|
|
|
|
delete require.cache[`${corepath}/class/Util`];
|
|
|
|
}
|
|
|
|
}
|