From 238aaaf7f335fc9c5ff7d3a3f423f9dacbac1f13 Mon Sep 17 00:00:00 2001 From: Jacob Nguyen <76754747+jacoobes@users.noreply.github.com> Date: Mon, 10 Jun 2024 00:21:02 -0500 Subject: [PATCH] update docs and add for published and fix guild publiushing --- packages/localizer/index.mdx | 2 +- packages/publisher/index.mdx | 106 +++++++++++++++++++++++++++++++++++ packages/publisher/index.ts | 30 +++++----- 3 files changed, 122 insertions(+), 16 deletions(-) create mode 100644 packages/publisher/index.mdx diff --git a/packages/localizer/index.mdx b/packages/localizer/index.mdx index eda90fa..500aeba 100644 --- a/packages/localizer/index.mdx +++ b/packages/localizer/index.mdx @@ -2,7 +2,7 @@ title: Localizer description: Translate your bot for the world sidebar: - order: 1 + order: 2 --- diff --git a/packages/publisher/index.mdx b/packages/publisher/index.mdx new file mode 100644 index 0000000..734ac6f --- /dev/null +++ b/packages/publisher/index.mdx @@ -0,0 +1,106 @@ +--- +title: Publisher +description: Publish application commands as a Service +sidebar: + order: 1 +--- + +## Implicits +- Requires process.env to be populated +- A common provider of this is `dotenv` +```txt title=".env" +DISCORD_TOKEN= +APPLICATION_ID= +NODE_ENV= +``` +- Calls the discord API with the [PUT route](https://discord.com/developers/docs/interactions/application-commands#bulk-overwrite-global-application-commands). Wherever your commands directory is located, publish will override the existing application commands at Discord. + +## Usage + +**Initializing the Publisher** +```ts +import { makeDependencies } from '@sern/handler'; +import { Publisher } from '@sern/localizer'; + +await makeDependencies(({ add }) => { + add('publisher', new Publisher()); +}); +``` + + + +## Features +- Automatically syncs api with your command base +- generates JSON file of output (**.sern/command-data-remote.json**) +- supports a configuration that is the same as the original publish plugin. + + +Each command file can have an extra plugin `publishConfig` that follows `ValidPublishOptions`: +## Config +```ts + +type ValidMemberPermissions = + | typeof PermissionFlagBits //discord.js enum + | typeof PermissionFlagBits[] //array of discord.js enum + | string //must be a stringified number + | bigint + +interface PublishConfig { + guildIds?: string[]; + defaultMemberPermissions?: ValidMemberPermissions; + integrationTypes?: Array<'Guild'|'User'> + contexts: number[] +} +type ValidPublishOptions = + | PublishConfig + | (absPath: string, module: CommandModule) => PublishConfig +``` + +### Example: command published with integrationTypes + +:::tip +Make sure you modify the install method in the Discord dev portal +::: + +```ts title=src/commands/ping.ts +import { commandModule, CommandType } from '@sern/handler' +import { publishConfig } from '@sern/publisher' + +export default commandModule( { + type: CommandType.Slash, + plugins: [ + publishConfig({ + integrationTypes: ['User'], + contexts: [1,2] + }) + ], + description: `hello worl`, + execute: (ctx) => { + ctx.reply('pong') + } +}) + +``` + + +### Example: command published in guild + +```ts title=src/commands/ping.ts +import { commandModule, CommandType } from '@sern/handler' +import { publishConfig } from '@sern/publisher' + +export default commandModule( { + type: CommandType.Slash, + plugins: [ + publishConfig({ + guildIds: ["889026545715400705"] + }) + ], + description: `hello worl`, + execute: (ctx) => { + ctx.reply('pong') + } +}) + +``` + diff --git a/packages/publisher/index.ts b/packages/publisher/index.ts index fc65efc..4a5726d 100644 --- a/packages/publisher/index.ts +++ b/packages/publisher/index.ts @@ -105,7 +105,7 @@ export class Publisher implements Init { }) const [globalCommands, guildedCommands] = modules.reduce( ([globals, guilded], module) => { - const isPublishableGlobally = !module[PUBLISH]?.[GUILD_IDS]; + const isPublishableGlobally = !module[PUBLISH] || !Array.isArray(module[PUBLISH].guildIds); if (isPublishableGlobally) { return [[module, ...globals], guilded]; } @@ -127,7 +127,7 @@ export class Publisher implements Init { const guildIdMap: Map = new Map(); const responsesMap = new Map(); guildedCommands.forEach((entry) => { - const guildIds: string[] = entry[GUILD_IDS] ?? []; + const guildIds: string[] = entry[PUBLISH].guildIds ?? []; if (guildIds) { guildIds.forEach((guildId) => { if (guildIdMap.has(guildId)) { @@ -192,7 +192,7 @@ export interface PublishConfig { guildIds?: string[]; defaultMemberPermissions?: ValidMemberPermissions; integrationTypes?: Array<'Guild'|'User'> - contexts: number[] + contexts?: number[] } export type ValidPublishOptions = @@ -208,18 +208,18 @@ export const publishConfig = (config: ValidPublishOptions) => { return controller.stop("Cannot publish this module"); } let _config=config - if(typeof _config === 'function') { - _config = _config(absPath, module); - } - const { contexts, defaultMemberPermissions, integrationTypes } = _config - //adding extra configuration - Reflect.set(module, PUBLISH, { - [GUILD_IDS]: _config.guildIds, - default_member_permissions: serializePermissions(defaultMemberPermissions), - integration_types: integrationTypes, - contexts - }) - return controller.next(); + if(typeof _config === 'function') { + _config = _config(absPath, module); + } + const { contexts, defaultMemberPermissions, integrationTypes } = _config + //adding extra configuration + Reflect.set(module, PUBLISH, { + guildIds: _config.guildIds, + default_member_permissions: serializePermissions(defaultMemberPermissions), + integration_types: integrationTypes, + contexts + }) + return controller.next(); }) }