2023-12-16 16:50:26 +00:00
|
|
|
import { Client, Partials } from "discord.js";
|
2022-04-24 14:46:37 +01:00
|
|
|
import * as dotenv from "dotenv";
|
|
|
|
import { createConnection } from "typeorm";
|
2022-10-09 15:23:25 +01:00
|
|
|
import { EventType } from "../constants/EventType";
|
2022-04-24 14:46:37 +01:00
|
|
|
import ICommandItem from "../contracts/ICommandItem";
|
|
|
|
import IEventItem from "../contracts/IEventItem";
|
|
|
|
import { Command } from "../type/command";
|
|
|
|
|
|
|
|
import { Events } from "./events";
|
|
|
|
import { Util } from "./util";
|
2023-05-26 17:59:22 +01:00
|
|
|
import AppDataSource from "../database/dataSources/appDataSource";
|
2024-01-26 20:59:05 +00:00
|
|
|
import ButtonEventItem from "../contracts/ButtonEventItem";
|
|
|
|
import { ButtonEvent } from "../type/buttonEvent";
|
2022-04-24 14:46:37 +01:00
|
|
|
|
|
|
|
export class CoreClient extends Client {
|
|
|
|
private static _commandItems: ICommandItem[];
|
|
|
|
private static _eventItems: IEventItem[];
|
2024-01-26 20:59:05 +00:00
|
|
|
private static _buttonEvents: ButtonEventItem[];
|
2023-05-26 17:59:22 +01:00
|
|
|
|
2022-04-24 14:46:37 +01:00
|
|
|
private _events: Events;
|
|
|
|
private _util: Util;
|
|
|
|
|
|
|
|
public static get commandItems(): ICommandItem[] {
|
|
|
|
return this._commandItems;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static get eventItems(): IEventItem[] {
|
|
|
|
return this._eventItems;
|
|
|
|
}
|
|
|
|
|
2024-01-26 20:59:05 +00:00
|
|
|
public static get buttonEvents(): ButtonEventItem[] {
|
|
|
|
return this._buttonEvents;
|
|
|
|
}
|
|
|
|
|
2023-12-16 16:50:26 +00:00
|
|
|
constructor(intents: number[], partials: Partials[]) {
|
|
|
|
super({ intents: intents, partials: partials });
|
2022-04-24 14:46:37 +01:00
|
|
|
dotenv.config();
|
|
|
|
|
|
|
|
CoreClient._commandItems = [];
|
|
|
|
CoreClient._eventItems = [];
|
2024-01-26 20:59:05 +00:00
|
|
|
CoreClient._buttonEvents = [];
|
2022-04-24 14:46:37 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-05-26 17:59:22 +01:00
|
|
|
await AppDataSource.initialize()
|
|
|
|
.then(() => console.log("Data Source Initialized"))
|
|
|
|
.catch((err) => console.error("Error Initialising Data Source", err));
|
2022-04-24 14:46:37 +01:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2022-10-09 15:23:25 +01:00
|
|
|
public static RegisterEvent(eventType: EventType, func: Function) {
|
2022-04-24 14:46:37 +01:00
|
|
|
const item: IEventItem = {
|
2022-10-09 15:23:25 +01:00
|
|
|
EventType: eventType,
|
|
|
|
ExecutionFunction: func,
|
2022-04-24 14:46:37 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
CoreClient._eventItems.push(item);
|
|
|
|
}
|
2024-01-26 20:59:05 +00:00
|
|
|
|
|
|
|
public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) {
|
|
|
|
const item: ButtonEventItem = {
|
|
|
|
ButtonId: buttonId,
|
|
|
|
Event: event,
|
|
|
|
};
|
|
|
|
|
|
|
|
CoreClient._buttonEvents.push(item);
|
|
|
|
}
|
2022-04-24 14:46:37 +01:00
|
|
|
}
|