From 07d13f38a7521d6bd414b7a927a81815e23d1032 Mon Sep 17 00:00:00 2001 From: Matthew R Date: Mon, 29 Jun 2020 17:13:54 -0400 Subject: [PATCH] fix attempts --- src/class/Client.ts | 47 +++++++++++++++++++++---------------- src/class/Event.ts | 15 ++++++++++++ src/class/index.ts | 1 + src/events/messageCreate.ts | 6 +++-- src/main.ts | 12 +++++++++- 5 files changed, 58 insertions(+), 23 deletions(-) create mode 100644 src/class/Event.ts diff --git a/src/class/Client.ts b/src/class/Client.ts index 4f314c6..256a352 100644 --- a/src/class/Client.ts +++ b/src/class/Client.ts @@ -7,8 +7,7 @@ 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 } from '.'; -import * as commands from '../commands'; +import { Command, Util, Collection, Server, Event } from '.'; export default class Client extends Eris.Client { @@ -18,6 +17,8 @@ export default class Client extends Eris.Client { public commands: Collection; + public events: Collection; + public db: { Account: mongoose.Model; Domain: mongoose.Model; Moderation: mongoose.Model; Tier: mongoose.Model; }; public redis: Redis.Redis; @@ -41,6 +42,7 @@ export default class Client extends Eris.Client { 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(); @@ -53,18 +55,16 @@ export default class Client extends Eris.Client { }); this.updating = false; this.buildError = false; - this.events(); - this.loadFunctions(); - this.init(); + this.errorEvents(); } - private async events() { + public async errorEvents() { process.on('unhandledRejection', (error) => { this.signale.error(error); }); } - private async loadFunctions() { + public async loadFunctions() { const functions = await fs.readdir('../functions'); functions.forEach(async (func) => { if (func === 'index.ts' || func === 'index.js') return; @@ -95,20 +95,27 @@ export default class Client extends Eris.Client { } 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() { - const evtFiles = await fs.readdir('../events/'); - Object.values(commands).forEach((c: Function) => this.loadCommand(c)); - - evtFiles.forEach((file) => { - 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}`); - this.on(eventName, (...args) => event.run(...args)); - delete require.cache[require.resolve(`../events/${file}`)]; - }); - await mongoose.connect(config.mongoURL, { useNewUrlParser: true, useUnifiedTopology: true }); await this.connect(); this.on('ready', () => { diff --git a/src/class/Event.ts b/src/class/Event.ts new file mode 100644 index 0000000..658f18c --- /dev/null +++ b/src/class/Event.ts @@ -0,0 +1,15 @@ +import { Client } from '.'; + +export default class Event { + public client: Client + + public event: string; + + constructor(client: Client) { + this.client = client; + this.event = ''; + this.run = this.run.bind(this); + } + + public async run(...args: any[]): Promise { return Promise.resolve(); } +} diff --git a/src/class/index.ts b/src/class/index.ts index 4e0bb2e..ad33446 100644 --- a/src/class/index.ts +++ b/src/class/index.ts @@ -2,6 +2,7 @@ export { default as AccountUtil } from './AccountUtil'; export { default as Client } from './Client'; export { default as Collection } from './Collection'; export { default as Command } from './Command'; +export { default as Event } from './Event'; export { default as RichEmbed } from './RichEmbed'; export { default as Route } from './Route'; export { default as Security } from './Security'; diff --git a/src/events/messageCreate.ts b/src/events/messageCreate.ts index 21b303c..5c956c3 100644 --- a/src/events/messageCreate.ts +++ b/src/events/messageCreate.ts @@ -1,11 +1,13 @@ import { Message, TextChannel } from 'eris'; -import { Client } from '../class'; +import { Client, Event } from '../class'; -export default class { +export default class extends Event { public client: Client constructor(client: Client) { + super(client); this.client = client; + this.event = 'messageCreate'; } public async run(message: Message) { diff --git a/src/main.ts b/src/main.ts index e971265..65bc183 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,4 +1,14 @@ import { Client } from './class'; +import * as eventFiles from './events'; +import * as commandFiles from './commands'; // eslint-disable-next-line no-new -new Client(); +async function main() { + const client = new Client(); + await client.loadCommands(commandFiles); + await client.loadEvents(eventFiles); + await client.loadFunctions(); + await client.init(); +} + +main();