bakit 1.0.0-beta.3 → 1.0.0-beta.5

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.
@@ -201,4 +201,4 @@ declare class BakitClient<Ready extends boolean = boolean> extends Client<Ready>
201
201
  private createHookRecord;
202
202
  }
203
203
 
204
- export { Arg as A, type BakitClientOptions as B, CommandHookExecutionState as C, CommandSyntaxErrorType as D, type CommandSyntaxErrorOptions as E, CommandSyntaxError as F, type GetSyntaxErrorMessageFunction as G, HOOKS_KEY as H, type IntegerArgumentOptions as I, type MemberArgumentOptions as M, type NumberArgumentOptions as N, RootCommandEntry as R, type StringArgumentOptions as S, type UserArgumentOptions as U, BakitClient as a, ArgumentType as b, type BaseArgumentOptions as c, type ArgumentOptions as d, type CommandHookMethod as e, type CommandHook as f, type BaseCommandEntryOptions as g, type CreateCommandOptions as h, BaseCommandEntry as i, BaseCommandGroupEntry as j, CommandGroupEntry as k, SubcommandEntry as l, type CommandEntry as m, type CommandConstructor as n, getRoot as o, CommandFactory as p, Command as q, CommandRegistry as r, type ChatInputContextSendOptions as s, type MessageContextSendOptions as t, use as u, type ContextSendOptions as v, BaseContext as w, ChatInputContext as x, MessageContext as y, type Context as z };
204
+ export { Arg as A, BakitClient as B, CommandHookExecutionState as C, CommandSyntaxError as D, type GetSyntaxErrorMessageFunction as G, HOOKS_KEY as H, type IntegerArgumentOptions as I, type MemberArgumentOptions as M, type NumberArgumentOptions as N, RootCommandEntry as R, type StringArgumentOptions as S, type UserArgumentOptions as U, type BakitClientOptions as a, ArgumentType as b, type BaseArgumentOptions as c, type ArgumentOptions as d, type CommandHookMethod as e, type CommandHook as f, type BaseCommandEntryOptions as g, type CreateCommandOptions as h, BaseCommandEntry as i, BaseCommandGroupEntry as j, CommandGroupEntry as k, SubcommandEntry as l, type CommandEntry as m, type CommandConstructor as n, CommandFactory as o, Command as p, CommandRegistry as q, type ChatInputContextSendOptions as r, type MessageContextSendOptions as s, type ContextSendOptions as t, BaseContext as u, ChatInputContext as v, MessageContext as w, type Context as x, CommandSyntaxErrorType as y, type CommandSyntaxErrorOptions as z };
@@ -1,3 +1,3 @@
1
- export { A as Arg, d as ArgumentOptions, b as ArgumentType, c as BaseArgumentOptions, i as BaseCommandEntry, g as BaseCommandEntryOptions, j as BaseCommandGroupEntry, w as BaseContext, x as ChatInputContext, s as ChatInputContextSendOptions, q as Command, n as CommandConstructor, m as CommandEntry, p as CommandFactory, k as CommandGroupEntry, f as CommandHook, C as CommandHookExecutionState, e as CommandHookMethod, r as CommandRegistry, z as Context, v as ContextSendOptions, h as CreateCommandOptions, H as HOOKS_KEY, I as IntegerArgumentOptions, M as MemberArgumentOptions, y as MessageContext, t as MessageContextSendOptions, N as NumberArgumentOptions, R as RootCommandEntry, S as StringArgumentOptions, l as SubcommandEntry, U as UserArgumentOptions, o as getRoot, u as use } from '../BakitClient-BWs93qO-.js';
1
+ export { A as Arg, d as ArgumentOptions, b as ArgumentType, c as BaseArgumentOptions, i as BaseCommandEntry, g as BaseCommandEntryOptions, j as BaseCommandGroupEntry, u as BaseContext, v as ChatInputContext, r as ChatInputContextSendOptions, p as Command, n as CommandConstructor, m as CommandEntry, o as CommandFactory, k as CommandGroupEntry, f as CommandHook, C as CommandHookExecutionState, e as CommandHookMethod, q as CommandRegistry, x as Context, t as ContextSendOptions, h as CreateCommandOptions, H as HOOKS_KEY, I as IntegerArgumentOptions, M as MemberArgumentOptions, w as MessageContext, s as MessageContextSendOptions, N as NumberArgumentOptions, R as RootCommandEntry, S as StringArgumentOptions, l as SubcommandEntry, U as UserArgumentOptions } from '../BakitClient-CgtDS6Hr.js';
2
2
  import 'discord.js';
3
3
  import 'type-fest';
@@ -472,7 +472,5 @@ export {
472
472
  HOOKS_KEY,
473
473
  MessageContext,
474
474
  RootCommandEntry,
475
- SubcommandEntry,
476
- getRoot,
477
- use
475
+ SubcommandEntry
478
476
  };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- export { A as Arg, d as ArgumentOptions, b as ArgumentType, a as BakitClient, B as BakitClientOptions, c as BaseArgumentOptions, i as BaseCommandEntry, g as BaseCommandEntryOptions, j as BaseCommandGroupEntry, w as BaseContext, x as ChatInputContext, s as ChatInputContextSendOptions, q as Command, n as CommandConstructor, m as CommandEntry, p as CommandFactory, k as CommandGroupEntry, f as CommandHook, C as CommandHookExecutionState, e as CommandHookMethod, r as CommandRegistry, F as CommandSyntaxError, E as CommandSyntaxErrorOptions, D as CommandSyntaxErrorType, z as Context, v as ContextSendOptions, h as CreateCommandOptions, G as GetSyntaxErrorMessageFunction, H as HOOKS_KEY, I as IntegerArgumentOptions, M as MemberArgumentOptions, y as MessageContext, t as MessageContextSendOptions, N as NumberArgumentOptions, R as RootCommandEntry, S as StringArgumentOptions, l as SubcommandEntry, U as UserArgumentOptions, o as getRoot, u as use } from './BakitClient-BWs93qO-.js';
1
+ import { B as BakitClient } from './BakitClient-CgtDS6Hr.js';
2
+ export { A as Arg, d as ArgumentOptions, b as ArgumentType, a as BakitClientOptions, c as BaseArgumentOptions, i as BaseCommandEntry, g as BaseCommandEntryOptions, j as BaseCommandGroupEntry, u as BaseContext, v as ChatInputContext, r as ChatInputContextSendOptions, p as Command, n as CommandConstructor, m as CommandEntry, o as CommandFactory, k as CommandGroupEntry, f as CommandHook, C as CommandHookExecutionState, e as CommandHookMethod, q as CommandRegistry, D as CommandSyntaxError, z as CommandSyntaxErrorOptions, y as CommandSyntaxErrorType, x as Context, t as ContextSendOptions, h as CreateCommandOptions, G as GetSyntaxErrorMessageFunction, H as HOOKS_KEY, I as IntegerArgumentOptions, M as MemberArgumentOptions, w as MessageContext, s as MessageContextSendOptions, N as NumberArgumentOptions, R as RootCommandEntry, S as StringArgumentOptions, l as SubcommandEntry, U as UserArgumentOptions } from './BakitClient-CgtDS6Hr.js';
2
3
  import { AsyncLocalStorage } from 'node:async_hooks';
3
- import 'discord.js';
4
- import 'type-fest';
4
+ import { SetOptional } from 'type-fest';
5
+ import { Awaitable, ClientEvents } from 'discord.js';
6
+ import EventEmitter from 'node:events';
5
7
 
6
8
  declare function extractId(value: string): string | null;
7
9
 
@@ -15,4 +17,63 @@ declare class StateBox {
15
17
  static use<T extends object>(defaultValue?: unknown): (target: T, key: keyof T) => void;
16
18
  }
17
19
 
18
- export { StateBox, type States, extractId };
20
+ type EventsLike = Record<keyof unknown, unknown[]>;
21
+ declare enum ListenerHookExecutionState {
22
+ Main = "main",
23
+ Pre = "pre",
24
+ Post = "post",
25
+ Error = "error"
26
+ }
27
+ type MainListenerHookMethod<Args extends unknown[]> = (...args: Args) => Awaitable<void>;
28
+ type ErrorListenerHookMethod<Args extends unknown[]> = (error: unknown, ...args: Args) => Awaitable<void>;
29
+ interface ListenerHook<E extends EventsLike, K extends keyof E> {
30
+ state: ListenerHookExecutionState;
31
+ method: MainListenerHookMethod<E[K] & unknown[]> | ErrorListenerHookMethod<E[K] & unknown[]>;
32
+ entry: ListenerEntry<E, K>;
33
+ }
34
+ interface ListenerEntryOptions<E extends EventsLike, K extends keyof E> {
35
+ name: K;
36
+ once: boolean;
37
+ emitter?: EventEmitter;
38
+ }
39
+ declare class ListenerEntry<E extends EventsLike, K extends keyof E> {
40
+ options: ListenerEntryOptions<E, K>;
41
+ static hooksKey: symbol;
42
+ private static cache;
43
+ main: <T extends MainListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
44
+ pre: <T extends MainListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
45
+ post: <T extends MainListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
46
+ error: <T extends ErrorListenerHookMethod<E[K] & unknown[]>>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
47
+ constructor(options: ListenerEntryOptions<E, K>);
48
+ static getHooks<E extends EventsLike, K extends keyof E>(constructor: ListenerConstructor, init?: boolean): ListenerHook<E, K>[];
49
+ private static createMainHookDecorator;
50
+ private static createErrorHookDecorator;
51
+ private static addHook;
52
+ }
53
+
54
+ type ListenerConstructor = new (...args: unknown[]) => object;
55
+ type CreateListenerOptions<E extends EventsLike, K extends keyof E> = SetOptional<ListenerEntryOptions<E, K>, "once"> | K;
56
+ declare function ListenerFactory<E extends EventsLike = ClientEvents, K extends keyof E = keyof E>(options: CreateListenerOptions<E, K>): ListenerEntry<E, K>;
57
+ declare const LISTENER_ENTRY_KEY: unique symbol;
58
+ declare function use<E extends EventsLike = ClientEvents>(listener: ListenerEntry<E, keyof E>): (target: ListenerConstructor) => void;
59
+ declare function getEntry<E extends EventsLike = ClientEvents, K extends keyof E = keyof E>(constructor: ListenerConstructor): ListenerEntry<E, K> | undefined;
60
+ declare const Listener: typeof ListenerFactory & {
61
+ use: typeof use;
62
+ getEntry: typeof getEntry;
63
+ };
64
+
65
+ declare function add(constructor: ListenerConstructor): void;
66
+ declare function remove(constructor: ListenerConstructor): void;
67
+ declare function removeAll(): void;
68
+ declare function setClient(newClient: BakitClient): void;
69
+ declare const ListenerRegistry: {
70
+ constructors: Set<ListenerConstructor>;
71
+ instances: WeakMap<ListenerConstructor, object>;
72
+ executors: WeakMap<object, (...args: unknown[]) => Promise<void>>;
73
+ add: typeof add;
74
+ setClient: typeof setClient;
75
+ remove: typeof remove;
76
+ removeAll: typeof removeAll;
77
+ };
78
+
79
+ export { BakitClient, type CreateListenerOptions, type ErrorListenerHookMethod, type EventsLike, LISTENER_ENTRY_KEY, Listener, type ListenerConstructor, ListenerEntry, type ListenerEntryOptions, ListenerFactory, type ListenerHook, ListenerHookExecutionState, ListenerRegistry, type MainListenerHookMethod, StateBox, type States, extractId };
package/dist/index.js CHANGED
@@ -813,26 +813,37 @@ var StateBox = class _StateBox {
813
813
  };
814
814
 
815
815
  // src/listener/ListenerEntry.ts
816
- var HOOKS_KEY2 = Symbol("hooks");
816
+ var ListenerHookExecutionState = /* @__PURE__ */ ((ListenerHookExecutionState2) => {
817
+ ListenerHookExecutionState2["Main"] = "main";
818
+ ListenerHookExecutionState2["Pre"] = "pre";
819
+ ListenerHookExecutionState2["Post"] = "post";
820
+ ListenerHookExecutionState2["Error"] = "error";
821
+ return ListenerHookExecutionState2;
822
+ })(ListenerHookExecutionState || {});
817
823
  var ListenerEntry = class _ListenerEntry {
818
824
  constructor(options) {
819
825
  this.options = options;
820
826
  }
827
+ static hooksKey = Symbol("hooks");
821
828
  static cache = /* @__PURE__ */ new WeakMap();
822
829
  main = _ListenerEntry.createMainHookDecorator("main" /* Main */, this);
823
830
  pre = _ListenerEntry.createMainHookDecorator("pre" /* Pre */, this);
824
831
  post = _ListenerEntry.createMainHookDecorator("post" /* Post */, this);
825
832
  error = _ListenerEntry.createErrorHookDecorator("error" /* Error */, this);
826
833
  static getHooks(constructor, init = false) {
827
- let hooks = this.cache.get(constructor) ?? Reflect.getMetadata(HOOKS_KEY2, constructor);
834
+ const { cache: cache2 } = this;
835
+ let hooks = cache2.get(constructor) ?? Reflect.getMetadata(this.hooksKey, constructor);
828
836
  if (!hooks) {
829
837
  hooks = [];
830
838
  if (init) {
831
- Reflect.defineMetadata(HOOKS_KEY2, hooks, constructor);
832
- this.cache.set(constructor, hooks);
839
+ Reflect.defineMetadata(this.hooksKey, hooks, constructor);
840
+ this.cache.set(
841
+ constructor,
842
+ hooks
843
+ );
833
844
  }
834
845
  }
835
- return init ? hooks : Object.freeze([...hooks]);
846
+ return init ? hooks : [...hooks];
836
847
  }
837
848
  static createMainHookDecorator(state, entry) {
838
849
  return (target, _key, descriptor) => {
@@ -866,23 +877,17 @@ var ListenerEntry = class _ListenerEntry {
866
877
 
867
878
  // src/listener/Listener.ts
868
879
  function ListenerFactory(options) {
869
- if (typeof options !== "object") {
870
- options = {
871
- name: options,
872
- once: false
873
- };
874
- }
875
- options.once = Boolean(options.once);
876
- return new ListenerEntry(options);
880
+ const normalizedOptions = typeof options === "object" ? { once: false, ...options } : { name: options, once: false };
881
+ return new ListenerEntry(normalizedOptions);
877
882
  }
878
- var ENTRY_KEY = Symbol("entry");
883
+ var LISTENER_ENTRY_KEY = Symbol("entry");
879
884
  function use2(listener) {
880
885
  return (target) => {
881
- Reflect.defineMetadata(ENTRY_KEY, listener, target);
886
+ Reflect.defineMetadata(LISTENER_ENTRY_KEY, listener, target);
882
887
  };
883
888
  }
884
889
  function getEntry(constructor) {
885
- return Reflect.getMetadata(ENTRY_KEY, constructor);
890
+ return Reflect.getMetadata(LISTENER_ENTRY_KEY, constructor);
886
891
  }
887
892
  var Listener = Object.assign(ListenerFactory, {
888
893
  use: use2,
@@ -893,6 +898,7 @@ var Listener = Object.assign(ListenerFactory, {
893
898
  var client;
894
899
  var constructors = /* @__PURE__ */ new Set();
895
900
  var instances = /* @__PURE__ */ new WeakMap();
901
+ var executors = /* @__PURE__ */ new WeakMap();
896
902
  function add(constructor) {
897
903
  const entry = Listener.getEntry(constructor);
898
904
  if (!entry) {
@@ -905,8 +911,14 @@ function add(constructor) {
905
911
  }
906
912
  options.emitter = client;
907
913
  }
914
+ const instance = new constructor();
908
915
  constructors.add(constructor);
909
- instances.set(constructor, new constructor());
916
+ instances.set(constructor, instance);
917
+ const executor = createExecutor(constructor, instance);
918
+ executors.set(instance, executor);
919
+ options.emitter[options.once ? "once" : "on"](options.name, (...args) => {
920
+ void executor(...args);
921
+ });
910
922
  }
911
923
  function remove(constructor) {
912
924
  const entry = Listener.getEntry(constructor);
@@ -914,13 +926,18 @@ function remove(constructor) {
914
926
  return;
915
927
  }
916
928
  constructors.delete(constructor);
929
+ const instance = instances.get(constructor);
930
+ if (!instance) {
931
+ return;
932
+ }
917
933
  instances.delete(constructor);
918
- const hook = ListenerEntry.getHooks(constructor).find((hook2) => hook2.entry === entry);
919
- if (!hook) {
934
+ const executor = executors.get(instance);
935
+ if (!executor) {
920
936
  return;
921
937
  }
922
938
  const { name, emitter } = entry.options;
923
- emitter?.removeListener(name, hook.method);
939
+ emitter?.removeListener(name, executor);
940
+ executors.delete(instance);
924
941
  }
925
942
  function removeAll() {
926
943
  for (const constructor of constructors) {
@@ -930,9 +947,53 @@ function removeAll() {
930
947
  function setClient(newClient) {
931
948
  client = newClient;
932
949
  }
950
+ function createExecutor(constructor, instance) {
951
+ const entry = Listener.getEntry(constructor);
952
+ if (!entry) {
953
+ throw new Error("Missing listener entry");
954
+ }
955
+ const hooks = ListenerEntry.getHooks(constructor).filter((hook) => hook.entry === entry);
956
+ const hookGroup = makeHookGroup(hooks);
957
+ return async function(...args) {
958
+ if (!hookGroup.main) {
959
+ return;
960
+ }
961
+ try {
962
+ if (hookGroup.pre) {
963
+ const preMethod = hookGroup.pre.method;
964
+ await preMethod.call(instance, ...args);
965
+ }
966
+ await hookGroup.main.method.call(instance, ...args);
967
+ if (hookGroup.post) {
968
+ const postMethod = hookGroup.post.method;
969
+ await postMethod.call(instance, ...args);
970
+ }
971
+ } catch (error) {
972
+ if (hookGroup.error) {
973
+ const errorMethod = hookGroup.error.method;
974
+ await errorMethod.call(instance, error, ...args);
975
+ } else {
976
+ throw error;
977
+ }
978
+ }
979
+ };
980
+ }
981
+ function makeHookGroup(hooks) {
982
+ const hooksByType = {
983
+ ["pre" /* Pre */]: void 0,
984
+ ["main" /* Main */]: void 0,
985
+ ["post" /* Post */]: void 0,
986
+ ["error" /* Error */]: void 0
987
+ };
988
+ for (const hook of hooks) {
989
+ hooksByType[hook.state] = hook;
990
+ }
991
+ return hooksByType;
992
+ }
933
993
  var ListenerRegistry = {
934
994
  constructors,
935
995
  instances,
996
+ executors,
936
997
  add,
937
998
  setClient,
938
999
  remove,
@@ -1152,11 +1213,15 @@ export {
1152
1213
  CommandSyntaxError,
1153
1214
  CommandSyntaxErrorType,
1154
1215
  HOOKS_KEY,
1216
+ LISTENER_ENTRY_KEY,
1217
+ Listener,
1218
+ ListenerEntry,
1219
+ ListenerFactory,
1220
+ ListenerHookExecutionState,
1221
+ ListenerRegistry,
1155
1222
  MessageContext,
1156
1223
  RootCommandEntry,
1157
1224
  StateBox,
1158
1225
  SubcommandEntry,
1159
- extractId,
1160
- getRoot,
1161
- use
1226
+ extractId
1162
1227
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bakit",
3
- "version": "1.0.0-beta.3",
3
+ "version": "1.0.0-beta.5",
4
4
  "description": "A framework for discord.js",
5
5
  "type": "module",
6
6
  "exports": {