@saykit/carbon 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,13 +16,12 @@ pnpm add @saykit/carbon saykit @buape/carbon
16
16
 
17
17
  ```ts
18
18
  import { Client, Command, type CommandInteraction } from '@buape/carbon';
19
- import { SayPlugin, withSay } from '@saykit/carbon';
20
- import { type Catalogue } from 'saykit';
21
- import catalogue from './i18n.js';
19
+ import { SayPlugin } from '@saykit/carbon';
20
+ import { catalogue, withSay } from './i18n.js';
22
21
 
23
22
  class PingCommand extends withSay(Command) {
24
- constructor(catalogue: Catalogue) {
25
- super(catalogue, (say) => ({
23
+ constructor() {
24
+ super((say) => ({
26
25
  name: say`ping`,
27
26
  description: say`Ping the bot!`,
28
27
  }));
@@ -33,7 +32,7 @@ class PingCommand extends withSay(Command) {
33
32
  }
34
33
  }
35
34
 
36
- const client = new Client({/* options */}, { commands: [new PingCommand(catalogue)] }, [
35
+ const client = new Client({/* options */}, { commands: [new PingCommand()] }, [
37
36
  new SayPlugin(catalogue),
38
37
  ]);
39
38
  ```
package/dist/index.d.mts CHANGED
@@ -20,22 +20,52 @@ declare function applyGuildExtension(): () => void;
20
20
  type Keys = 'name' | 'description' | 'label' | 'title' | 'placeholder' | 'content' | 'options' | 'components' | 'subcommands' | 'subcommandGroups';
21
21
  type AbstractConstructor<Args extends any[] = any[], Instance extends object = object> = abstract new (...args: Args) => Instance;
22
22
  type SayProps<T> = Pick<T, Extract<keyof T, Keys>>;
23
+ interface WithSay {
24
+ /**
25
+ * Enhances a {@link BaseCommand} subclass with support for localisation.
26
+ *
27
+ * @param Base Abstract command constructor to extend.
28
+ * @returns A new constructor that accepts a properties-mapping function and
29
+ * the original constructor arguments.
30
+ */
31
+ <Args extends unknown[], Instance extends BaseCommand>(Base: AbstractConstructor<Args, Instance>): AbstractConstructor<[properties: (say: View) => SayProps<Instance>, ...args: Args], Instance & Partial<Record<Keys, unknown>>>;
32
+ /**
33
+ * Enhances a {@link BaseComponent} or {@link Modal} subclass with
34
+ * support for localisation.
35
+ *
36
+ * @param Base Abstract component or modal constructor to extend.
37
+ * @returns A new constructor that accepts a set of properties.
38
+ */
39
+ <Args extends unknown[], Instance extends BaseComponent | Modal>(Base: AbstractConstructor<Args, Instance>): AbstractConstructor<[properties: SayProps<Instance>, ...args: Args], Instance & Partial<Record<Keys, unknown>>>;
40
+ }
23
41
  /**
24
- * Enhances a {@link BaseCommand} subclass with support for localisation.
42
+ * Bind a `withSay` to a {@link Catalogue}, normally once beside the catalogue
43
+ * itself.
25
44
  *
26
- * @param Base Abstract command constructor to extend.
27
- * @returns A new constructor that accepts a {@link Catalogue}, a
28
- * properties-mapping function, and the original constructor arguments.
29
- */
30
- declare function withSay<Args extends unknown[], Instance extends BaseCommand>(Base: AbstractConstructor<Args, Instance>): AbstractConstructor<[catalogue: Catalogue, properties: (say: View) => SayProps<Instance>, ...args: Args], Instance & Partial<Record<Keys, unknown>>>;
31
- /**
32
- * Enhances a {@link BaseComponent} or {@link Modal} subclass with
33
- * support for localisation.
45
+ * `withSay` wraps a Carbon base class so a command's name, description and
46
+ * options are built once per locale in the catalogue and registered with
47
+ * Discord as localisations, without the command having to reach for the
48
+ * catalogue itself.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * // i18n.ts
53
+ * export const withSay = createWithSay(catalogue);
54
+ *
55
+ * // commands/pick.ts
56
+ * export class PickCommand extends withSay(Command) {
57
+ * constructor() {
58
+ * super((say) => ({ name: say`pick`, description: say`What we are reading.` }));
59
+ * }
60
+ * }
61
+ * ```
34
62
  *
35
- * @param Base Abstract component or modal constructor to extend.
36
- * @returns A new constructor that accepts a set of properties.
63
+ * @param catalogue The catalogue to take views from
64
+ * @returns A `withSay` bound to that catalogue
65
+ * @throws If the base class is neither a {@link BaseCommand} nor a
66
+ * {@link BaseComponent}
37
67
  */
38
- declare function withSay<Args extends unknown[], Instance extends BaseComponent | Modal>(Base: AbstractConstructor<Args, Instance>): AbstractConstructor<[properties: SayProps<Instance>, ...args: Args], Instance & Partial<Record<Keys, unknown>>>;
68
+ declare function createWithSay(catalogue: Catalogue): WithSay;
39
69
  //#endregion
40
70
  //#region src/plugin.d.ts
41
71
  /**
@@ -56,4 +86,4 @@ declare class SayPlugin extends Plugin {
56
86
  constructor(catalogue: Catalogue);
57
87
  }
58
88
  //#endregion
59
- export { SayPlugin, type applyBaseInteractionExtension, type applyGuildExtension, withSay };
89
+ export { SayPlugin, type applyBaseInteractionExtension, type applyGuildExtension, createWithSay };
package/dist/index.mjs CHANGED
@@ -21,24 +21,26 @@ function combineCommandOptions(mappedOptions, baseLocale) {
21
21
  }
22
22
  //#endregion
23
23
  //#region src/mixers/with-say.ts
24
- const ClassMap = /* @__PURE__ */ new WeakMap();
25
- function withSay(Base) {
26
- if (ClassMap.has(Base)) return ClassMap.get(Base);
27
- if (Base.prototype instanceof BaseCommand) {
28
- const Derived = createSayCommand(Base);
29
- ClassMap.set(Base, Derived);
30
- return Derived;
31
- }
32
- if (Base.prototype instanceof BaseComponent || Base === Modal) {
33
- const Derived = createSayComponent(Base);
34
- ClassMap.set(Base, Derived);
35
- return Derived;
36
- }
37
- throw new Error("Invalid base class");
24
+ function createWithSay(catalogue) {
25
+ const cache = /* @__PURE__ */ new WeakMap();
26
+ return function withSay(Base) {
27
+ if (cache.has(Base)) return cache.get(Base);
28
+ if (Base.prototype instanceof BaseCommand) {
29
+ const Derived = createSayCommand(catalogue, Base);
30
+ cache.set(Base, Derived);
31
+ return Derived;
32
+ }
33
+ if (Base.prototype instanceof BaseComponent || Base === Modal) {
34
+ const Derived = createSayComponent(Base);
35
+ cache.set(Base, Derived);
36
+ return Derived;
37
+ }
38
+ throw new Error("Invalid base class");
39
+ };
38
40
  }
39
- function createSayCommand(Base) {
41
+ function createSayCommand(catalogue, Base) {
40
42
  class SayCommand extends Base {
41
- constructor(catalogue, properties, ...args) {
43
+ constructor(properties, ...args) {
42
44
  super(...args);
43
45
  const options = combineCommandOptions(Array.from(catalogue).reduce((acc, [locale, say]) => {
44
46
  acc[locale] = properties(say);
@@ -97,4 +99,4 @@ var SayPlugin = class extends Plugin {
97
99
  }
98
100
  };
99
101
  //#endregion
100
- export { SayPlugin, withSay };
102
+ export { SayPlugin, createWithSay };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saykit/carbon",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Carbon Discord framework integration for saykit",
5
5
  "keywords": [
6
6
  "bot",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "devDependencies": {
39
39
  "@buape/carbon": "0.16.0",
40
- "saykit": "^0.10.0"
40
+ "saykit": "^0.11.0"
41
41
  },
42
42
  "peerDependencies": {
43
43
  "@buape/carbon": "*",