feature/5-drop-command (#17)
#5 Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/17 Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
parent
51d97bacd5
commit
58d1541e47
21 changed files with 382 additions and 48 deletions
|
@ -7,14 +7,23 @@ import { Command } from "../type/command";
|
|||
|
||||
import { Events } from "./events";
|
||||
import { Util } from "./util";
|
||||
import CardSetupFunction from "../Functions/CardSetupFunction";
|
||||
import CardDataSource from "../database/dataSources/cardDataSource";
|
||||
import CardDropHelper from "../helpers/CardDropHelper";
|
||||
import IButtonEventItem from "../contracts/IButtonEventItem";
|
||||
import { ButtonEvent } from "../type/buttonEvent";
|
||||
import AppDataSource from "../database/dataSources/appDataSource";
|
||||
|
||||
export class CoreClient extends Client {
|
||||
private static _commandItems: ICommandItem[];
|
||||
private static _eventItems: IEventItem[];
|
||||
private static _buttonEvents: IButtonEventItem[];
|
||||
|
||||
private _events: Events;
|
||||
private _util: Util;
|
||||
private _cardSetupFunc: CardSetupFunction;
|
||||
|
||||
public static ClaimId: string;
|
||||
|
||||
public static get commandItems(): ICommandItem[] {
|
||||
return this._commandItems;
|
||||
|
@ -24,15 +33,21 @@ export class CoreClient extends Client {
|
|||
return this._eventItems;
|
||||
}
|
||||
|
||||
public static get buttonEvents(): IButtonEventItem[] {
|
||||
return this._buttonEvents;
|
||||
}
|
||||
|
||||
constructor(intents: number[]) {
|
||||
super({ intents: intents });
|
||||
dotenv.config();
|
||||
|
||||
CoreClient._commandItems = [];
|
||||
CoreClient._eventItems = [];
|
||||
CoreClient._buttonEvents = [];
|
||||
|
||||
this._events = new Events();
|
||||
this._util = new Util();
|
||||
this._cardSetupFunc = new CardSetupFunction();
|
||||
}
|
||||
|
||||
public async start() {
|
||||
|
@ -42,12 +57,18 @@ export class CoreClient extends Client {
|
|||
}
|
||||
|
||||
await AppDataSource.initialize()
|
||||
.then(() => console.log("Data Source Initialized"))
|
||||
.catch((err) => console.error("Error Initialising Data Source", err));
|
||||
.then(() => console.log("App Data Source Initialised"))
|
||||
.catch(err => console.error("Error initialising App Data Source", err));
|
||||
|
||||
await CardDataSource.initialize()
|
||||
.then(() => console.log("Card Data Source Initialised"))
|
||||
.catch(err => console.error("Error initialising Card Data Source", err));
|
||||
|
||||
super.on("interactionCreate", this._events.onInteractionCreate);
|
||||
super.on("ready", this._events.onReady);
|
||||
|
||||
await this._cardSetupFunc.Execute();
|
||||
|
||||
await super.login(process.env.BOT_TOKEN);
|
||||
|
||||
this._util.loadEvents(this, CoreClient._eventItems);
|
||||
|
@ -72,4 +93,13 @@ export class CoreClient extends Client {
|
|||
|
||||
CoreClient._eventItems.push(item);
|
||||
}
|
||||
|
||||
public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) {
|
||||
const item: IButtonEventItem = {
|
||||
ButtonId: buttonId,
|
||||
Event: event,
|
||||
};
|
||||
|
||||
CoreClient._buttonEvents.push(item);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,29 +1,18 @@
|
|||
import { Interaction } from "discord.js";
|
||||
import ICommandItem from "../contracts/ICommandItem";
|
||||
import { CoreClient } from "./client";
|
||||
import ChatInputCommand from "./interactionCreate/ChatInputCommand";
|
||||
import Button from "./interactionCreate/Button";
|
||||
|
||||
export class Events {
|
||||
public async onInteractionCreate(interaction: Interaction) {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
if (!interaction.guildId) return;
|
||||
|
||||
const item = CoreClient.commandItems.find(x => x.Name == interaction.commandName && !x.ServerId);
|
||||
const itemForServer = CoreClient.commandItems.find(x => x.Name == interaction.commandName && x.ServerId == interaction.guildId);
|
||||
|
||||
let itemToUse: ICommandItem;
|
||||
|
||||
if (!itemForServer) {
|
||||
if (!item) {
|
||||
await interaction.reply('Command not found');
|
||||
return;
|
||||
}
|
||||
|
||||
itemToUse = item;
|
||||
} else {
|
||||
itemToUse = itemForServer;
|
||||
if (interaction.isChatInputCommand()) {
|
||||
ChatInputCommand.onChatInput(interaction);
|
||||
}
|
||||
|
||||
itemToUse.Command.execute(interaction);
|
||||
if (interaction.isButton()) {
|
||||
Button.onButtonClicked(interaction);
|
||||
}
|
||||
}
|
||||
|
||||
// Emit when bot is logged in and ready to use
|
||||
|
|
17
src/client/interactionCreate/Button.ts
Normal file
17
src/client/interactionCreate/Button.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { ButtonInteraction, Interaction } from "discord.js";
|
||||
import { CoreClient } from "../client";
|
||||
|
||||
export default class Button {
|
||||
public static async onButtonClicked(interaction: ButtonInteraction) {
|
||||
if (!interaction.isButton) return;
|
||||
|
||||
const item = CoreClient.buttonEvents.find(x => x.ButtonId == interaction.customId.split(' ')[0]);
|
||||
|
||||
if (!item) {
|
||||
await interaction.reply('Event not found');
|
||||
return;
|
||||
}
|
||||
|
||||
item.Event.execute(interaction);
|
||||
}
|
||||
}
|
27
src/client/interactionCreate/ChatInputCommand.ts
Normal file
27
src/client/interactionCreate/ChatInputCommand.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { Interaction } from "discord.js";
|
||||
import { CoreClient } from "../client";
|
||||
import ICommandItem from "../../contracts/ICommandItem";
|
||||
|
||||
export default class ChatInputCommand {
|
||||
public static async onChatInput(interaction: Interaction) {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
|
||||
const item = CoreClient.commandItems.find(x => x.Name == interaction.commandName && !x.ServerId);
|
||||
const itemForServer = CoreClient.commandItems.find(x => x.Name == interaction.commandName && x.ServerId == interaction.guildId);
|
||||
|
||||
let itemToUse: ICommandItem;
|
||||
|
||||
if (!itemForServer) {
|
||||
if (!item) {
|
||||
await interaction.reply('Command not found');
|
||||
return;
|
||||
}
|
||||
|
||||
itemToUse = item;
|
||||
} else {
|
||||
itemToUse = itemForServer;
|
||||
}
|
||||
|
||||
itemToUse.Command.execute(interaction);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue