import eris from 'eris'; import mongoose from 'mongoose'; import { promises as fs } from 'fs'; import { Collection, Command, Util, ServerManagement, Event } from '.'; import { Member, MemberInterface, Moderation, ModerationInterface, Redirect, RedirectInterface } from '../models'; export default class Client extends eris.Client { public config: { token: string, prefix: string, guildID: string, mongoDB: string }; public commands: Collection; public events: Collection; public intervals: Collection; public util: Util; public serverManagement: ServerManagement; public db: { Member: mongoose.Model, Moderation: mongoose.Model, Redirect: mongoose.Model }; constructor(token: string, options?: eris.ClientOptions) { super(token, options); this.commands = new Collection(); this.events = new Collection(); this.intervals = new Collection(); this.db = { Member, Moderation, Redirect }; } public async loadDatabase() { await mongoose.connect(this.config.mongoDB, { useNewUrlParser: true, useUnifiedTopology: true }); } public loadPlugins() { this.util = new Util(this); this.serverManagement = new ServerManagement(this); } public async loadIntervals() { const intervalFiles = await fs.readdir(`${__dirname}/../intervals`); intervalFiles.forEach((file) => { const intervalName = file.split('.')[0]; if (file === 'index.js') return; const interval: NodeJS.Timeout = (require(`${__dirname}/../intervals/${file}`).default)(this); this.intervals.add(intervalName, interval); this.util.signale.success(`Successfully loaded interval: ${intervalName}`); }); } 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.util.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.util.signale.success(`Successfully loaded command: ${command.name}`); } } }