Add ability to register commands and events to specific environments
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2023-09-29 18:18:53 +01:00
parent 005a366883
commit 8ddf52a552
10 changed files with 66 additions and 18 deletions

View file

@ -7,10 +7,11 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.1.4 DEV BOT_VER=0.2 DEV
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=682942374040961060 BOT_CLIENTID=682942374040961060
BOT_ENV=4
ABOUT_FUNDING= ABOUT_FUNDING=
ABOUT_REPO= ABOUT_REPO=

View file

@ -7,10 +7,11 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.1.4 BOT_VER=0.2
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=1093810443589529631 BOT_CLIENTID=1093810443589529631
BOT_ENV=1
ABOUT_FUNDING= ABOUT_FUNDING=
ABOUT_REPO= ABOUT_REPO=

View file

@ -7,10 +7,11 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.1.4 BETA BOT_VER=0.2 BETA
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=1147976642942214235 BOT_CLIENTID=1147976642942214235
BOT_ENV=2
ABOUT_FUNDING= ABOUT_FUNDING=
ABOUT_REPO= ABOUT_REPO=

View file

@ -11,6 +11,7 @@ const requiredConfigs: string[] = [
"BOT_AUTHOR", "BOT_AUTHOR",
"BOT_OWNERID", "BOT_OWNERID",
"BOT_CLIENTID", "BOT_CLIENTID",
"BOT_ENV",
"DB_HOST", "DB_HOST",
"DB_PORT", "DB_PORT",
"DB_AUTH_USER", "DB_AUTH_USER",

View file

@ -12,6 +12,7 @@ import CardDataSource from "../database/dataSources/cardDataSource";
import IButtonEventItem from "../contracts/IButtonEventItem"; import IButtonEventItem from "../contracts/IButtonEventItem";
import { ButtonEvent } from "../type/buttonEvent"; import { ButtonEvent } from "../type/buttonEvent";
import AppDataSource from "../database/dataSources/appDataSource"; import AppDataSource from "../database/dataSources/appDataSource";
import { Environment } from "../constants/Environment";
export class CoreClient extends Client { export class CoreClient extends Client {
private static _commandItems: ICommandItem[]; private static _commandItems: ICommandItem[];
@ -23,6 +24,7 @@ export class CoreClient extends Client {
private _cardSetupFunc: CardSetupFunction; private _cardSetupFunc: CardSetupFunction;
public static ClaimId: string; public static ClaimId: string;
public static Environment: Environment;
public static get commandItems(): ICommandItem[] { public static get commandItems(): ICommandItem[] {
return this._commandItems; return this._commandItems;
@ -47,6 +49,9 @@ export class CoreClient extends Client {
this._events = new Events(); this._events = new Events();
this._util = new Util(); this._util = new Util();
this._cardSetupFunc = new CardSetupFunction(); this._cardSetupFunc = new CardSetupFunction();
CoreClient.Environment = Number(process.env.BOT_ENV);
console.log(`Bot Environment: ${CoreClient.Environment}`);
} }
public async start() { public async start() {
@ -68,37 +73,50 @@ export class CoreClient extends Client {
await this._cardSetupFunc.Execute(); await this._cardSetupFunc.Execute();
await super.login(process.env.BOT_TOKEN);
this._util.loadEvents(this, CoreClient._eventItems); this._util.loadEvents(this, CoreClient._eventItems);
this._util.loadSlashCommands(this); 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 = { const item: ICommandItem = {
Name: name, Name: name,
Environment: environment,
Command: command, Command: command,
ServerId: serverId, 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 = { const item: IEventItem = {
EventType: eventType, EventType: eventType,
ExecutionFunction: func, 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 = { const item: IButtonEventItem = {
ButtonId: buttonId, ButtonId: buttonId,
Event: event, Event: event,
Environment: environment,
}; };
CoreClient._buttonEvents.push(item); if (environment &= CoreClient.Environment) {
CoreClient._buttonEvents.push(item);
}
} }
} }

View file

@ -10,9 +10,15 @@ export class Util {
const globalCommands = registeredCommands.filter(x => !x.ServerId); const globalCommands = registeredCommands.filter(x => !x.ServerId);
const guildCommands = registeredCommands.filter(x => x.ServerId); const guildCommands = registeredCommands.filter(x => x.ServerId);
const globalCommandData: SlashCommandBuilder[] = globalCommands const globalCommandData: SlashCommandBuilder[] = [];
.filter(x => x.Command.CommandBuilder)
.flatMap(x => x.Command.CommandBuilder); for (let command of globalCommands) {
if (!command.Command.CommandBuilder) continue;
if ((command.Environment & CoreClient.Environment) == CoreClient.Environment) {
globalCommandData.push(command.Command.CommandBuilder);
}
}
const guildIds: string[] = []; const guildIds: string[] = [];
@ -32,12 +38,18 @@ export class Util {
); );
for (let guild of guildIds) { for (let guild of guildIds) {
const guildCommandData = guildCommands.filter(x => x.ServerId == guild) const guildCommandData: SlashCommandBuilder[] = [];
.filter(x => x.Command.CommandBuilder)
.flatMap(x => x.Command.CommandBuilder); for (let command of guildCommands.filter(x => x.ServerId == guild)) {
if (!command.Command.CommandBuilder) continue;
if ((command.Environment & CoreClient.Environment) == CoreClient.Environment) {
guildCommandData.push(command.Command.CommandBuilder);
}
}
if (!client.guilds.cache.has(guild)) continue; if (!client.guilds.cache.has(guild)) continue;
rest.put( rest.put(
Routes.applicationGuildCommands(process.env.BOT_CLIENTID!, guild), Routes.applicationGuildCommands(process.env.BOT_CLIENTID!, guild),
{ {

View file

@ -0,0 +1,8 @@
export enum Environment {
None = 0,
Production = 1 << 0,
Stage = 1 << 1,
Local = 1 << 2,
All = Production | Stage | Local,
}

View file

@ -1,6 +1,8 @@
import { Environment } from "../constants/Environment";
import { ButtonEvent } from "../type/buttonEvent"; import { ButtonEvent } from "../type/buttonEvent";
export default interface IButtonEventItem { export default interface IButtonEventItem {
ButtonId: string, ButtonId: string,
Event: ButtonEvent, Event: ButtonEvent,
Environment: Environment,
} }

View file

@ -1,7 +1,9 @@
import { Environment } from "../constants/Environment";
import { Command } from "../type/command"; import { Command } from "../type/command";
export default interface ICommandItem { export default interface ICommandItem {
Name: string, Name: string,
Command: Command, Command: Command,
Environment: Environment,
ServerId?: string, ServerId?: string,
} }

View file

@ -1,7 +1,9 @@
import { Environment } from "../constants/Environment";
import { EventType } from "../constants/EventType"; import { EventType } from "../constants/EventType";
export default interface IEventItem { export default interface IEventItem {
EventType: EventType, EventType: EventType,
ExecutionFunction: Function, ExecutionFunction: Function,
Environment: Environment,
} }