1
0
Fork 0

fix attempts

refactor/models
Matthew 2020-06-29 17:13:54 -04:00
parent b15b7b514f
commit 07d13f38a7
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
5 changed files with 58 additions and 23 deletions

View File

@ -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<Command>;
public events: Collection<Event>;
public db: { Account: mongoose.Model<AccountInterface>; Domain: mongoose.Model<DomainInterface>; Moderation: mongoose.Model<ModerationInterface>; Tier: mongoose.Model<TierInterface>; };
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<Command>();
this.events = new Collection<Event>();
this.functions = new Collection<Function>();
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<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}`)];
}
}
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);
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', () => {

15
src/class/Event.ts Normal file
View File

@ -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<void> { return Promise.resolve(); }
}

View File

@ -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';

View File

@ -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) {

View File

@ -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();