From c5fc2760c411ae8c97551af0297ef4b01e507d13 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 14 Oct 2023 17:07:28 +0100 Subject: [PATCH] Add ability to register commands and events to specific environments (#50) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Add ability to register commands and events to specific environments - These will default to every environment if not specified, so existing commands will still work as is - Environment is a flag so you can specify multiple environments #38 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - This has been tested locally by making sure commands are registered that are specified as the one in the config # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/50 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- .dev.env | 3 ++- .prod.env | 3 ++- .stage.env | 3 ++- src/bot.ts | 1 + src/client/client.ts | 34 +++++++++++++++++++++++-------- src/client/util.ts | 26 ++++++++++++++++------- src/constants/Environment.ts | 8 ++++++++ src/contracts/IButtonEventItem.ts | 2 ++ src/contracts/ICommandItem.ts | 2 ++ src/contracts/IEventItem.ts | 2 ++ 10 files changed, 66 insertions(+), 18 deletions(-) create mode 100644 src/constants/Environment.ts diff --git a/.dev.env b/.dev.env index ca791ba..b275229 100644 --- a/.dev.env +++ b/.dev.env @@ -7,10 +7,11 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.1.4 DEV +BOT_VER=0.2 DEV BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=682942374040961060 +BOT_ENV=4 ABOUT_FUNDING= ABOUT_REPO= diff --git a/.prod.env b/.prod.env index f1a644e..afbba60 100644 --- a/.prod.env +++ b/.prod.env @@ -7,10 +7,11 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.1.4 +BOT_VER=0.2 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1093810443589529631 +BOT_ENV=1 ABOUT_FUNDING= ABOUT_REPO= diff --git a/.stage.env b/.stage.env index 60a01fc..c1df020 100644 --- a/.stage.env +++ b/.stage.env @@ -7,10 +7,11 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.1.4 BETA +BOT_VER=0.2 BETA BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1147976642942214235 +BOT_ENV=2 ABOUT_FUNDING= ABOUT_REPO= diff --git a/src/bot.ts b/src/bot.ts index e8c1dd3..07b919f 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -11,6 +11,7 @@ const requiredConfigs: string[] = [ "BOT_AUTHOR", "BOT_OWNERID", "BOT_CLIENTID", + "BOT_ENV", "DB_HOST", "DB_PORT", "DB_AUTH_USER", diff --git a/src/client/client.ts b/src/client/client.ts index 66907bb..a205a19 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -12,6 +12,7 @@ import CardDataSource from "../database/dataSources/cardDataSource"; import IButtonEventItem from "../contracts/IButtonEventItem"; import { ButtonEvent } from "../type/buttonEvent"; import AppDataSource from "../database/dataSources/appDataSource"; +import { Environment } from "../constants/Environment"; export class CoreClient extends Client { private static _commandItems: ICommandItem[]; @@ -23,6 +24,7 @@ export class CoreClient extends Client { private _cardSetupFunc: CardSetupFunction; public static ClaimId: string; + public static Environment: Environment; public static get commandItems(): ICommandItem[] { return this._commandItems; @@ -47,6 +49,9 @@ export class CoreClient extends Client { this._events = new Events(); this._util = new Util(); this._cardSetupFunc = new CardSetupFunction(); + + CoreClient.Environment = Number(process.env.BOT_ENV); + console.log(`Bot Environment: ${CoreClient.Environment}`); } public async start() { @@ -68,37 +73,50 @@ export class CoreClient extends Client { await this._cardSetupFunc.Execute(); - await super.login(process.env.BOT_TOKEN); - this._util.loadEvents(this, CoreClient._eventItems); this._util.loadSlashCommands(this); + + console.log(`Registered Commands: ${CoreClient._commandItems.flatMap(x => x.Name).join(", ")}`); + console.log(`Registered Events: ${CoreClient._eventItems.flatMap(x => x.EventType).join(", ")}`); + console.log(`Registered Buttons: ${CoreClient._buttonEvents.flatMap(x => x.ButtonId).join(", ")}`); + + await super.login(process.env.BOT_TOKEN); } - public static RegisterCommand(name: string, command: Command, serverId?: string) { + public static RegisterCommand(name: string, command: Command, environment: Environment = Environment.All, serverId?: string) { const item: ICommandItem = { Name: name, + Environment: environment, Command: command, ServerId: serverId, }; - CoreClient._commandItems.push(item); + if (environment &= CoreClient.Environment) { + CoreClient._commandItems.push(item); + } } - public static RegisterEvent(eventType: EventType, func: Function) { + public static RegisterEvent(eventType: EventType, func: Function, environment: Environment = Environment.All) { const item: IEventItem = { EventType: eventType, ExecutionFunction: func, + Environment: environment, }; - CoreClient._eventItems.push(item); + if (environment &= CoreClient.Environment) { + CoreClient._eventItems.push(item); + } } - public static RegisterButtonEvent(buttonId: string, event: ButtonEvent) { + public static RegisterButtonEvent(buttonId: string, event: ButtonEvent, environment: Environment = Environment.All) { const item: IButtonEventItem = { ButtonId: buttonId, Event: event, + Environment: environment, }; - CoreClient._buttonEvents.push(item); + if (environment &= CoreClient.Environment) { + CoreClient._buttonEvents.push(item); + } } } diff --git a/src/client/util.ts b/src/client/util.ts index a9c2bad..41b7f27 100644 --- a/src/client/util.ts +++ b/src/client/util.ts @@ -10,9 +10,15 @@ export class Util { const globalCommands = registeredCommands.filter(x => !x.ServerId); const guildCommands = registeredCommands.filter(x => x.ServerId); - const globalCommandData: SlashCommandBuilder[] = globalCommands - .filter(x => x.Command.CommandBuilder) - .flatMap(x => x.Command.CommandBuilder); + const globalCommandData: SlashCommandBuilder[] = []; + + for (let command of globalCommands) { + if (!command.Command.CommandBuilder) continue; + + if (command.Environment &= CoreClient.Environment) { + globalCommandData.push(command.Command.CommandBuilder); + } + } const guildIds: string[] = []; @@ -32,12 +38,18 @@ export class Util { ); for (let guild of guildIds) { - const guildCommandData = guildCommands.filter(x => x.ServerId == guild) - .filter(x => x.Command.CommandBuilder) - .flatMap(x => x.Command.CommandBuilder); + const guildCommandData: SlashCommandBuilder[] = []; + + for (let command of guildCommands.filter(x => x.ServerId == guild)) { + if (!command.Command.CommandBuilder) continue; + + if (command.Environment &= CoreClient.Environment) { + guildCommandData.push(command.Command.CommandBuilder); + } + } if (!client.guilds.cache.has(guild)) continue; - + rest.put( Routes.applicationGuildCommands(process.env.BOT_CLIENTID!, guild), { diff --git a/src/constants/Environment.ts b/src/constants/Environment.ts new file mode 100644 index 0000000..13f5ba3 --- /dev/null +++ b/src/constants/Environment.ts @@ -0,0 +1,8 @@ +export enum Environment { + None = 0, + Production = 1 << 0, + Stage = 1 << 1, + Local = 1 << 2, + + All = Production | Stage | Local, +} \ No newline at end of file diff --git a/src/contracts/IButtonEventItem.ts b/src/contracts/IButtonEventItem.ts index 6be9f6e..061a9c9 100644 --- a/src/contracts/IButtonEventItem.ts +++ b/src/contracts/IButtonEventItem.ts @@ -1,6 +1,8 @@ +import { Environment } from "../constants/Environment"; import { ButtonEvent } from "../type/buttonEvent"; export default interface IButtonEventItem { ButtonId: string, Event: ButtonEvent, + Environment: Environment, } \ No newline at end of file diff --git a/src/contracts/ICommandItem.ts b/src/contracts/ICommandItem.ts index 89acb50..3ebc670 100644 --- a/src/contracts/ICommandItem.ts +++ b/src/contracts/ICommandItem.ts @@ -1,7 +1,9 @@ +import { Environment } from "../constants/Environment"; import { Command } from "../type/command"; export default interface ICommandItem { Name: string, Command: Command, + Environment: Environment, ServerId?: string, } \ No newline at end of file diff --git a/src/contracts/IEventItem.ts b/src/contracts/IEventItem.ts index ea32a3e..b7cc7bf 100644 --- a/src/contracts/IEventItem.ts +++ b/src/contracts/IEventItem.ts @@ -1,7 +1,9 @@ +import { Environment } from "../constants/Environment"; import { EventType } from "../constants/EventType"; export default interface IEventItem { EventType: EventType, ExecutionFunction: Function, + Environment: Environment, } \ No newline at end of file