2020-04-14 13:15:33 -04:00
|
|
|
import eris from 'eris';
|
2020-04-15 15:13:44 -04:00
|
|
|
import mongoose from 'mongoose';
|
2020-04-14 13:15:33 -04:00
|
|
|
import { promises as fs } from 'fs';
|
2020-05-16 15:03:21 -04:00
|
|
|
import { Collection, Command, Util, ServerManagement, Event } from '.';
|
2020-05-05 19:13:18 -04:00
|
|
|
import { Member, MemberInterface, Moderation, ModerationInterface, Redirect, RedirectInterface } from '../models';
|
2020-05-16 15:03:21 -04:00
|
|
|
import * as eventFiles from '../events';
|
|
|
|
import * as commandFiles from '../commands';
|
2020-04-14 13:15:33 -04:00
|
|
|
|
|
|
|
export default class Client extends eris.Client {
|
2020-04-15 15:13:44 -04:00
|
|
|
public config: { token: string, prefix: string, guildID: string, mongoDB: string };
|
2020-04-14 13:15:33 -04:00
|
|
|
|
|
|
|
public commands: Collection<Command>;
|
|
|
|
|
2020-05-16 15:03:21 -04:00
|
|
|
public events: Collection<Event>;
|
2020-05-06 19:32:20 -04:00
|
|
|
|
2020-04-15 15:13:44 -04:00
|
|
|
public intervals: Collection<NodeJS.Timeout>;
|
|
|
|
|
2020-04-14 13:15:33 -04:00
|
|
|
public util: Util;
|
|
|
|
|
2020-05-05 19:13:18 -04:00
|
|
|
public serverManagement: ServerManagement;
|
|
|
|
|
2020-05-16 15:03:21 -04:00
|
|
|
public db: { Member: mongoose.Model<MemberInterface>, Moderation: mongoose.Model<ModerationInterface>, Redirect: mongoose.Model<RedirectInterface> };
|
2020-04-15 15:13:44 -04:00
|
|
|
|
2020-04-14 13:15:33 -04:00
|
|
|
constructor(token: string, options?: eris.ClientOptions) {
|
|
|
|
super(token, options);
|
|
|
|
this.commands = new Collection<Command>();
|
2020-05-16 15:03:21 -04:00
|
|
|
this.events = new Collection<Event>();
|
2020-04-15 15:13:44 -04:00
|
|
|
this.intervals = new Collection<NodeJS.Timeout>();
|
2020-05-16 15:03:21 -04:00
|
|
|
this.db = { Member, Moderation, Redirect };
|
2020-04-15 15:13:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async loadDatabase() {
|
|
|
|
await mongoose.connect(this.config.mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
|
2020-04-14 13:15:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public loadPlugins() {
|
|
|
|
this.util = new Util(this);
|
2020-05-05 19:13:18 -04:00
|
|
|
this.serverManagement = new ServerManagement(this);
|
2020-04-14 13:15:33 -04:00
|
|
|
}
|
|
|
|
|
2020-04-15 15:13:44 -04:00
|
|
|
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}`);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-14 13:15:33 -04:00
|
|
|
public async loadEvents() {
|
2020-05-16 15:03:21 -04:00
|
|
|
const evtFiles = Object.entries<new(client: Client) => 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.util.signale.success(`Successfully loaded event: ${name}`);
|
|
|
|
delete require.cache[require.resolve(`${__dirname}/../events/${name}`)];
|
|
|
|
}
|
2020-04-14 13:15:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public async loadCommands() {
|
2020-05-16 15:03:21 -04:00
|
|
|
const cmdFiles = Object.values<new(client: Client) => Command>(commandFiles);
|
|
|
|
for (const Cmd of cmdFiles) {
|
|
|
|
const command = new Cmd(this);
|
2020-04-14 13:15:33 -04:00
|
|
|
this.commands.add(command.name, command);
|
2020-04-14 21:33:34 -04:00
|
|
|
this.util.signale.success(`Successfully loaded command: ${command.name}`);
|
2020-05-16 15:03:21 -04:00
|
|
|
}
|
2020-04-14 13:15:33 -04:00
|
|
|
}
|
|
|
|
}
|