mirror of
https://github.com/sern-handler/tools
synced 2026-06-06 01:16:59 +00:00
update docs and add for published and fix guild publiushing
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
title: Localizer
|
||||
description: Translate your bot for the world
|
||||
sidebar:
|
||||
order: 1
|
||||
order: 2
|
||||
---
|
||||
|
||||
|
||||
|
||||
106
packages/publisher/index.mdx
Normal file
106
packages/publisher/index.mdx
Normal file
@@ -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=<YOUR_TOKEN>
|
||||
APPLICATION_ID=<YOUR_APPLICATION_ID>
|
||||
NODE_ENV=<production|development>
|
||||
```
|
||||
- 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')
|
||||
}
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
@@ -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<string, CommandModule[]> = 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();
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user