mirror of
https://github.com/sern-handler/tools
synced 2026-07-05 05:59:42 +00:00
publisher
This commit is contained in:
@@ -3,15 +3,15 @@
|
||||
"version": "1.0.3",
|
||||
"description": "Dependency Injection system",
|
||||
"main": "dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"module": "./dist/index.js",
|
||||
"exports": {
|
||||
"." : {
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.js"
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"./global": {
|
||||
"import": "./dist/global.js",
|
||||
"require": "./dist/global.js"
|
||||
"import": "./dist/global.js",
|
||||
"require": "./dist/global.js"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
|
||||
@@ -67,6 +67,7 @@ export const local = (i: string, local: string) => {
|
||||
/**
|
||||
* An init plugin to add localization fields to a command module.
|
||||
* Your localization configuration should look like,
|
||||
* sets nloc and dloc on locals field of module.
|
||||
* @param root {string} If you have conflicting command names, you may configure the root of the name. (= command/{root})
|
||||
* Below is es-ES.json (spanish)
|
||||
* ```json
|
||||
@@ -88,16 +89,13 @@ export const localize = (root?: string) =>
|
||||
CommandInitPlugin(({ module, deps }) => {
|
||||
if(module.type === CommandType.Slash || module.type === CommandType.Both) {
|
||||
const { localizer, '@sern/logger':log } = deps
|
||||
log?.info({ message: "Localizing "+ module.name });
|
||||
const resolvedRoot = 'command/'+(root??module.name);
|
||||
dfsApplyLocalization(module.options ?? [], deps, [resolvedRoot]);
|
||||
log?.info({ message: "Localizing "+ resolvedRoot });
|
||||
//@ts-ignore
|
||||
return controller.next({
|
||||
locals: {
|
||||
nloc: localizer.translationsFor(resolvedRoot+".name"),
|
||||
dloc: localizer.translationsFor(resolvedRoot+'.description')
|
||||
}
|
||||
});
|
||||
dfsApplyLocalization(module.options ?? [], deps, [resolvedRoot]);
|
||||
Reflect.set(module.locals, 'nloc', localizer.translationsFor(resolvedRoot+".name"))
|
||||
Reflect.set(module.locals, 'dloc', localizer.translationsFor(resolvedRoot+'.description'))
|
||||
return controller.next();
|
||||
} else {
|
||||
//@ts-ignore
|
||||
return controller.stop("Cannot localize this type of module " + module.name);
|
||||
|
||||
@@ -39,8 +39,6 @@ const serializePerms = (perms: unknown) => {
|
||||
|
||||
const BASE_URL = new URL('https://discord.com/api/v10/applications/');
|
||||
const PUBLISHABLE = 0b1110;
|
||||
const PUBLISH = Symbol.for('@sern/publish')
|
||||
|
||||
|
||||
export class Publisher implements Init {
|
||||
constructor(private modules: Map<string, CommandModule>,
|
||||
@@ -63,21 +61,26 @@ export class Publisher implements Init {
|
||||
throw e;
|
||||
}
|
||||
const GLOBAL_URL = new URL(`${appid}/commands`, BASE_URL);
|
||||
|
||||
interface LocalPublish {
|
||||
guildIds?: string[]
|
||||
default_member_permissions: string,
|
||||
integration_types: string[],
|
||||
contexts: number[]
|
||||
}
|
||||
const listener = async () => {
|
||||
this.logger.info({ message: 'publishing modules' });
|
||||
const modules =
|
||||
Array.from(this.modules.values())
|
||||
.filter(module => (module.type & PUBLISHABLE) != 0)
|
||||
.map(module => {
|
||||
const publish = module.locals.publish as LocalPublish || {}
|
||||
return {
|
||||
//@ts-ignore
|
||||
guildIds: module.publish.guildIds ?? [],
|
||||
guildIds: publish?.guildIds ?? [],
|
||||
toJSON() {
|
||||
const applicationType = intoApplicationType(module.type);
|
||||
const { default_member_permissions,
|
||||
integration_types=['Guild'],//@ts-ignore
|
||||
contexts } = module.publish ?? {};
|
||||
integration_types,
|
||||
contexts } = publish;
|
||||
return {
|
||||
name: module.name, type: applicationType,
|
||||
//@ts-ignore we know description is at least empty str or filled
|
||||
@@ -85,8 +88,7 @@ export class Publisher implements Init {
|
||||
//@ts-ignore shutup
|
||||
options: optionsTransformer(module?.options),
|
||||
default_member_permissions,
|
||||
integration_types,
|
||||
contexts,
|
||||
integration_types, contexts,
|
||||
//@ts-ignore
|
||||
name_localizations: module.locals.nloc,
|
||||
//@ts-ignore
|
||||
@@ -96,6 +98,7 @@ export class Publisher implements Init {
|
||||
}
|
||||
})
|
||||
const [globalCommands, guildedCommands] = modules.reduce(
|
||||
//technically these aren't sern/handler modules.
|
||||
([globals, guilded], module) => {
|
||||
const isPublishableGlobally = !module.guildIds || module.guildIds.length === 0;
|
||||
if (isPublishableGlobally) {
|
||||
@@ -119,7 +122,7 @@ export class Publisher implements Init {
|
||||
const guildIdMap: Map<string, CommandModule[]> = new Map();
|
||||
const responsesMap = new Map();
|
||||
guildedCommands.forEach((entry) => {
|
||||
const guildIds: string[] = entry[PUBLISH].guildIds ?? [];
|
||||
const guildIds: string[] = entry.guildIds ?? [];
|
||||
if (guildIds) {
|
||||
guildIds.forEach((guildId) => {
|
||||
if (guildIdMap.has(guildId)) {
|
||||
@@ -197,6 +200,7 @@ const IntegrationType = {
|
||||
* the publishConfig plugin.
|
||||
* If your commandModule requires extra properties such as publishing for certain guilds, you would
|
||||
* put those options in there.
|
||||
* sets 'publish' on locals field for modules.
|
||||
* @param {ValidPublishOptions} config options to configure how this module is published
|
||||
*/
|
||||
export const publishConfig = (config: ValidPublishOptions) => {
|
||||
@@ -207,20 +211,16 @@ export const publishConfig = (config: ValidPublishOptions) => {
|
||||
}
|
||||
let _config=config
|
||||
if(typeof _config === 'function') {
|
||||
_config = _config(absPath, module);
|
||||
_config = _config(absPath, module as CommandModule);
|
||||
}
|
||||
const { contexts, defaultMemberPermissions, integrationTypes:integration_types, guildIds } = _config
|
||||
//@ts-ignore
|
||||
return controller.next({
|
||||
locals: {
|
||||
publish: {
|
||||
guildIds,
|
||||
contexts,
|
||||
integration_types: integration_types?.map(i => Reflect.get(IntegrationType, i)),
|
||||
default_member_permissions: serializePerms(defaultMemberPermissions),
|
||||
}
|
||||
}
|
||||
});
|
||||
Reflect.set(module.locals, 'publish', {
|
||||
guildIds,
|
||||
contexts,
|
||||
integration_types: integration_types?.map(i => Reflect.get(IntegrationType, i)),
|
||||
default_member_permissions: serializePerms(defaultMemberPermissions),
|
||||
})
|
||||
return controller.next();
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"watch": "tsc --watch",
|
||||
"test": "vitest --run"
|
||||
"test": "exit 0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sern/handler": "^3.3.0",
|
||||
|
||||
@@ -2448,7 +2448,7 @@ __metadata:
|
||||
|
||||
"typescript@patch:typescript@^5.0.0#~builtin<compat/typescript>":
|
||||
version: 5.4.5
|
||||
resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin<compat/typescript>::version=5.4.5&hash=f3b441"
|
||||
resolution: "typescript@patch:typescript@npm%3A5.4.5#~builtin<compat/typescript>::version=5.4.5&hash=14eedb"
|
||||
bin:
|
||||
tsc: bin/tsc
|
||||
tsserver: bin/tsserver
|
||||
|
||||
Reference in New Issue
Block a user