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'; import { Command, Util, Collection, Server, Event } from '.'; 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; public events: Collection; public db: { Account: mongoose.Model; Domain: mongoose.Model; Moderation: mongoose.Model; Tier: mongoose.Model; }; public redis: Redis.Redis; public stores: { emojis: { success: string, loading: string, error: string }; }; public functions: Collection; 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(); this.events = new Collection(); this.functions = new Collection(); 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; this.errorEvents(); } public async errorEvents() { process.on('unhandledRejection', (error) => { this.signale.error(error); }); } public async loadFunctions() { const functions = await fs.readdir('../functions'); functions.forEach(async (func) => { if (func === 'index.ts' || func === 'index.js') return; try { const funcRequire: Function = require(`../functions/${func}`).default; 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; } } public async loadEvents(eventFiles: { [s: string]: typeof Event; } | ArrayLike) { const evtFiles = Object.entries(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}`)]; } } public async loadCommands(commandFiles: { [s: string]: typeof Command; } | ArrayLike) { const cmdFiles = Object.values(commandFiles); for (const Cmd of cmdFiles) { const command = new Cmd(this); this.commands.add(command.name, command); this.signale.success(`Successfully loaded command: ${command.name}`); } } public async init() { 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}`); }); 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]}`); }); 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`]; } }