2022-04-24 14:46:37 +01:00
|
|
|
import { Client } from "discord.js";
|
|
|
|
import * as dotenv from "dotenv";
|
|
|
|
import { createConnection } from "typeorm";
|
|
|
|
import ICommandItem from "../contracts/ICommandItem";
|
|
|
|
import IEventItem from "../contracts/IEventItem";
|
|
|
|
import { Command } from "../type/command";
|
|
|
|
import { Event } from "../type/event";
|
|
|
|
|
|
|
|
import { Events } from "./events";
|
|
|
|
import { Util } from "./util";
|
|
|
|
|
|
|
|
export class CoreClient extends Client {
|
|
|
|
private static _commandItems: ICommandItem[];
|
|
|
|
private static _eventItems: IEventItem[];
|
|
|
|
|
|
|
|
private _events: Events;
|
|
|
|
private _util: Util;
|
|
|
|
|
|
|
|
public static get commandItems(): ICommandItem[] {
|
|
|
|
return this._commandItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get eventItems(): IEventItem[] {
|
|
|
|
return this._eventItems;
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:24:40 +01:00
|
|
|
constructor(intents: number[]) {
|
2022-04-24 14:46:37 +01:00
|
|
|
super({ intents: intents });
|
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
CoreClient._commandItems = [];
|
|
|
|
CoreClient._eventItems = [];
|
|
|
|
|
|
|
|
this._events = new Events();
|
|
|
|
this._util = new Util();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async start() {
|
|
|
|
if (!process.env.BOT_TOKEN) {
|
|
|
|
console.error("BOT_TOKEN is not defined in .env");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await createConnection().catch(e => {
|
|
|
|
console.error(e);
|
|
|
|
return;
|
|
|
|
});
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
super.on("interactionCreate", this._events.onInteractionCreate);
|
2022-04-24 14:46:37 +01:00
|
|
|
super.on("ready", this._events.onReady);
|
|
|
|
|
2022-09-18 11:57:22 +01:00
|
|
|
await super.login(process.env.BOT_TOKEN);
|
2022-04-24 14:46:37 +01:00
|
|
|
|
|
|
|
this._util.loadEvents(this, CoreClient._eventItems);
|
2022-09-18 11:57:22 +01:00
|
|
|
this._util.loadSlashCommands(this);
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static RegisterCommand(name: string, command: Command, serverId?: string) {
|
|
|
|
const item: ICommandItem = {
|
|
|
|
Name: name,
|
|
|
|
Command: command,
|
|
|
|
ServerId: serverId,
|
|
|
|
};
|
|
|
|
|
|
|
|
CoreClient._commandItems.push(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static RegisterEvent(event: Event) {
|
|
|
|
const item: IEventItem = {
|
|
|
|
Event: event,
|
|
|
|
};
|
|
|
|
|
|
|
|
CoreClient._eventItems.push(item);
|
|
|
|
}
|
|
|
|
}
|