bakit 1.0.0-beta.2 → 1.0.0-beta.3
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.
|
@@ -33,13 +33,13 @@ type Context<Cached extends boolean = boolean, InGuild extends boolean = boolean
|
|
|
33
33
|
type CommandConstructor = new (...args: unknown[]) => object;
|
|
34
34
|
declare function use(command: RootCommandEntry): (target: CommandConstructor) => void;
|
|
35
35
|
declare function getRoot(constructor: CommandConstructor): RootCommandEntry | undefined;
|
|
36
|
-
declare function CommandFactory(options:
|
|
36
|
+
declare function CommandFactory(options: CreateCommandOptions<BaseCommandEntryOptions> | string): RootCommandEntry;
|
|
37
37
|
declare const Command: typeof CommandFactory & {
|
|
38
38
|
use: typeof use;
|
|
39
39
|
getRoot: typeof getRoot;
|
|
40
40
|
};
|
|
41
41
|
|
|
42
|
-
declare enum
|
|
42
|
+
declare enum CommandHookExecutionState {
|
|
43
43
|
Main = "main",
|
|
44
44
|
Pre = "pre",
|
|
45
45
|
Post = "post",
|
|
@@ -47,7 +47,7 @@ declare enum HookExecutionState {
|
|
|
47
47
|
}
|
|
48
48
|
type CommandHookMethod = (ctx: Context, ...args: any[]) => Awaitable<void>;
|
|
49
49
|
interface CommandHook {
|
|
50
|
-
state:
|
|
50
|
+
state: CommandHookExecutionState;
|
|
51
51
|
method: CommandHookMethod;
|
|
52
52
|
entry: CommandEntry;
|
|
53
53
|
}
|
|
@@ -56,7 +56,7 @@ interface BaseCommandEntryOptions {
|
|
|
56
56
|
description: string;
|
|
57
57
|
nsfw?: boolean;
|
|
58
58
|
}
|
|
59
|
-
type
|
|
59
|
+
type CreateCommandOptions<T extends BaseCommandEntryOptions> = SetOptional<T, "description"> | string;
|
|
60
60
|
declare const HOOKS_KEY: unique symbol;
|
|
61
61
|
declare abstract class BaseCommandEntry {
|
|
62
62
|
options: BaseCommandEntryOptions;
|
|
@@ -74,7 +74,7 @@ declare abstract class BaseCommandEntry {
|
|
|
74
74
|
}
|
|
75
75
|
declare abstract class BaseCommandGroupEntry extends BaseCommandEntry {
|
|
76
76
|
children: Collection<string, CommandGroupEntry | SubcommandEntry>;
|
|
77
|
-
subcommand(options:
|
|
77
|
+
subcommand(options: CreateCommandOptions<BaseCommandEntryOptions>): SubcommandEntry;
|
|
78
78
|
}
|
|
79
79
|
declare class CommandGroupEntry extends BaseCommandGroupEntry {
|
|
80
80
|
options: BaseCommandEntryOptions;
|
|
@@ -85,7 +85,7 @@ declare class CommandGroupEntry extends BaseCommandGroupEntry {
|
|
|
85
85
|
declare class RootCommandEntry extends BaseCommandGroupEntry {
|
|
86
86
|
options: BaseCommandEntryOptions;
|
|
87
87
|
constructor(options: BaseCommandEntryOptions);
|
|
88
|
-
group(options:
|
|
88
|
+
group(options: CreateCommandOptions<BaseCommandEntryOptions>): CommandGroupEntry;
|
|
89
89
|
}
|
|
90
90
|
declare class SubcommandEntry extends BaseCommandEntry {
|
|
91
91
|
options: BaseCommandEntryOptions;
|
|
@@ -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,
|
|
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 };
|
package/dist/command/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as Arg, d as ArgumentOptions, b as ArgumentType, c as BaseArgumentOptions, i as BaseCommandEntry,
|
|
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';
|
|
2
2
|
import 'discord.js';
|
|
3
3
|
import 'type-fest';
|
package/dist/command/index.js
CHANGED
|
@@ -106,13 +106,13 @@ var Arg = {
|
|
|
106
106
|
|
|
107
107
|
// src/command/CommandEntry.ts
|
|
108
108
|
import { Collection } from "discord.js";
|
|
109
|
-
var
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
return
|
|
115
|
-
})(
|
|
109
|
+
var CommandHookExecutionState = /* @__PURE__ */ ((CommandHookExecutionState2) => {
|
|
110
|
+
CommandHookExecutionState2["Main"] = "main";
|
|
111
|
+
CommandHookExecutionState2["Pre"] = "pre";
|
|
112
|
+
CommandHookExecutionState2["Post"] = "post";
|
|
113
|
+
CommandHookExecutionState2["Error"] = "error";
|
|
114
|
+
return CommandHookExecutionState2;
|
|
115
|
+
})(CommandHookExecutionState || {});
|
|
116
116
|
var HOOKS_KEY = Symbol("hooks");
|
|
117
117
|
var BaseCommandEntry = class _BaseCommandEntry {
|
|
118
118
|
constructor(options) {
|
|
@@ -467,9 +467,9 @@ export {
|
|
|
467
467
|
Command,
|
|
468
468
|
CommandFactory,
|
|
469
469
|
CommandGroupEntry,
|
|
470
|
+
CommandHookExecutionState,
|
|
470
471
|
CommandRegistry,
|
|
471
472
|
HOOKS_KEY,
|
|
472
|
-
HookExecutionState,
|
|
473
473
|
MessageContext,
|
|
474
474
|
RootCommandEntry,
|
|
475
475
|
SubcommandEntry,
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as Arg, d as ArgumentOptions, b as ArgumentType, a as BakitClient, B as BakitClientOptions, c as BaseArgumentOptions, i as BaseCommandEntry,
|
|
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';
|
|
2
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
3
3
|
import 'discord.js';
|
|
4
4
|
import 'type-fest';
|
package/dist/index.js
CHANGED
|
@@ -122,13 +122,13 @@ var Arg = {
|
|
|
122
122
|
};
|
|
123
123
|
|
|
124
124
|
// src/command/CommandEntry.ts
|
|
125
|
-
var
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
return
|
|
131
|
-
})(
|
|
125
|
+
var CommandHookExecutionState = /* @__PURE__ */ ((CommandHookExecutionState2) => {
|
|
126
|
+
CommandHookExecutionState2["Main"] = "main";
|
|
127
|
+
CommandHookExecutionState2["Pre"] = "pre";
|
|
128
|
+
CommandHookExecutionState2["Post"] = "post";
|
|
129
|
+
CommandHookExecutionState2["Error"] = "error";
|
|
130
|
+
return CommandHookExecutionState2;
|
|
131
|
+
})(CommandHookExecutionState || {});
|
|
132
132
|
var HOOKS_KEY = Symbol("hooks");
|
|
133
133
|
var BaseCommandEntry = class _BaseCommandEntry {
|
|
134
134
|
constructor(options) {
|
|
@@ -564,13 +564,13 @@ var ArgumentResolver = class _ArgumentResolver {
|
|
|
564
564
|
return this.options.message.client;
|
|
565
565
|
}
|
|
566
566
|
static create(message) {
|
|
567
|
-
const
|
|
568
|
-
const { enableMentionPrefix } =
|
|
567
|
+
const client2 = message.client;
|
|
568
|
+
const { enableMentionPrefix } = client2.options;
|
|
569
569
|
const prefixes = [
|
|
570
570
|
// Custom prefixes specified in options
|
|
571
|
-
...
|
|
571
|
+
...client2.options.prefixes ?? [],
|
|
572
572
|
// Use bot mention as prefix if enabled
|
|
573
|
-
...enableMentionPrefix ? [
|
|
573
|
+
...enableMentionPrefix ? [client2.user.toString()] : []
|
|
574
574
|
];
|
|
575
575
|
const prefix = prefixes.find((p) => message.content.startsWith(p)) ?? null;
|
|
576
576
|
if (!prefix) {
|
|
@@ -812,6 +812,133 @@ var StateBox = class _StateBox {
|
|
|
812
812
|
}
|
|
813
813
|
};
|
|
814
814
|
|
|
815
|
+
// src/listener/ListenerEntry.ts
|
|
816
|
+
var HOOKS_KEY2 = Symbol("hooks");
|
|
817
|
+
var ListenerEntry = class _ListenerEntry {
|
|
818
|
+
constructor(options) {
|
|
819
|
+
this.options = options;
|
|
820
|
+
}
|
|
821
|
+
static cache = /* @__PURE__ */ new WeakMap();
|
|
822
|
+
main = _ListenerEntry.createMainHookDecorator("main" /* Main */, this);
|
|
823
|
+
pre = _ListenerEntry.createMainHookDecorator("pre" /* Pre */, this);
|
|
824
|
+
post = _ListenerEntry.createMainHookDecorator("post" /* Post */, this);
|
|
825
|
+
error = _ListenerEntry.createErrorHookDecorator("error" /* Error */, this);
|
|
826
|
+
static getHooks(constructor, init = false) {
|
|
827
|
+
let hooks = this.cache.get(constructor) ?? Reflect.getMetadata(HOOKS_KEY2, constructor);
|
|
828
|
+
if (!hooks) {
|
|
829
|
+
hooks = [];
|
|
830
|
+
if (init) {
|
|
831
|
+
Reflect.defineMetadata(HOOKS_KEY2, hooks, constructor);
|
|
832
|
+
this.cache.set(constructor, hooks);
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
return init ? hooks : Object.freeze([...hooks]);
|
|
836
|
+
}
|
|
837
|
+
static createMainHookDecorator(state, entry) {
|
|
838
|
+
return (target, _key, descriptor) => {
|
|
839
|
+
this.addHook(target, state, descriptor.value, entry);
|
|
840
|
+
};
|
|
841
|
+
}
|
|
842
|
+
static createErrorHookDecorator(state, entry) {
|
|
843
|
+
return (target, _key, descriptor) => {
|
|
844
|
+
this.addHook(target, state, descriptor.value, entry);
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
static addHook(target, state, method, entry) {
|
|
848
|
+
const { constructor } = target;
|
|
849
|
+
const hooks = this.getHooks(constructor, true);
|
|
850
|
+
if (typeof method !== "function") {
|
|
851
|
+
throw new Error("CommandEntry decorator must be used with a class method.");
|
|
852
|
+
}
|
|
853
|
+
if (hooks.some((hook2) => hook2.state === state && hook2.entry === entry)) {
|
|
854
|
+
throw new Error(
|
|
855
|
+
`Hook "${state}" is already defined for entry "${String(entry.options.name)}".`
|
|
856
|
+
);
|
|
857
|
+
}
|
|
858
|
+
const hook = {
|
|
859
|
+
state,
|
|
860
|
+
entry,
|
|
861
|
+
method
|
|
862
|
+
};
|
|
863
|
+
hooks.push(hook);
|
|
864
|
+
}
|
|
865
|
+
};
|
|
866
|
+
|
|
867
|
+
// src/listener/Listener.ts
|
|
868
|
+
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);
|
|
877
|
+
}
|
|
878
|
+
var ENTRY_KEY = Symbol("entry");
|
|
879
|
+
function use2(listener) {
|
|
880
|
+
return (target) => {
|
|
881
|
+
Reflect.defineMetadata(ENTRY_KEY, listener, target);
|
|
882
|
+
};
|
|
883
|
+
}
|
|
884
|
+
function getEntry(constructor) {
|
|
885
|
+
return Reflect.getMetadata(ENTRY_KEY, constructor);
|
|
886
|
+
}
|
|
887
|
+
var Listener = Object.assign(ListenerFactory, {
|
|
888
|
+
use: use2,
|
|
889
|
+
getEntry
|
|
890
|
+
});
|
|
891
|
+
|
|
892
|
+
// src/listener/ListenerRegistry.ts
|
|
893
|
+
var client;
|
|
894
|
+
var constructors = /* @__PURE__ */ new Set();
|
|
895
|
+
var instances = /* @__PURE__ */ new WeakMap();
|
|
896
|
+
function add(constructor) {
|
|
897
|
+
const entry = Listener.getEntry(constructor);
|
|
898
|
+
if (!entry) {
|
|
899
|
+
throw new Error(`No entry found for "${constructor.name}"`);
|
|
900
|
+
}
|
|
901
|
+
const { options } = entry;
|
|
902
|
+
if (!options.emitter) {
|
|
903
|
+
if (!client) {
|
|
904
|
+
throw new Error("Client is not ready.");
|
|
905
|
+
}
|
|
906
|
+
options.emitter = client;
|
|
907
|
+
}
|
|
908
|
+
constructors.add(constructor);
|
|
909
|
+
instances.set(constructor, new constructor());
|
|
910
|
+
}
|
|
911
|
+
function remove(constructor) {
|
|
912
|
+
const entry = Listener.getEntry(constructor);
|
|
913
|
+
if (!entry) {
|
|
914
|
+
return;
|
|
915
|
+
}
|
|
916
|
+
constructors.delete(constructor);
|
|
917
|
+
instances.delete(constructor);
|
|
918
|
+
const hook = ListenerEntry.getHooks(constructor).find((hook2) => hook2.entry === entry);
|
|
919
|
+
if (!hook) {
|
|
920
|
+
return;
|
|
921
|
+
}
|
|
922
|
+
const { name, emitter } = entry.options;
|
|
923
|
+
emitter?.removeListener(name, hook.method);
|
|
924
|
+
}
|
|
925
|
+
function removeAll() {
|
|
926
|
+
for (const constructor of constructors) {
|
|
927
|
+
remove(constructor);
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
function setClient(newClient) {
|
|
931
|
+
client = newClient;
|
|
932
|
+
}
|
|
933
|
+
var ListenerRegistry = {
|
|
934
|
+
constructors,
|
|
935
|
+
instances,
|
|
936
|
+
add,
|
|
937
|
+
setClient,
|
|
938
|
+
remove,
|
|
939
|
+
removeAll
|
|
940
|
+
};
|
|
941
|
+
|
|
815
942
|
// src/BakitClient.ts
|
|
816
943
|
var BakitClient = class _BakitClient extends Client {
|
|
817
944
|
constructor(options) {
|
|
@@ -819,9 +946,10 @@ var BakitClient = class _BakitClient extends Client {
|
|
|
819
946
|
options.getSyntaxErrorMessage = _BakitClient.getSyntaxErrorMessage;
|
|
820
947
|
}
|
|
821
948
|
super(options);
|
|
949
|
+
ListenerRegistry.setClient(this);
|
|
822
950
|
this.once(
|
|
823
951
|
Events.ClientReady,
|
|
824
|
-
(
|
|
952
|
+
(client2) => void this.registerApplicationCommands(client2)
|
|
825
953
|
);
|
|
826
954
|
this.on(Events.InteractionCreate, (interaction) => void this.handleInteraction(interaction));
|
|
827
955
|
this.on(Events.MessageCreate, (message) => void this.handleMessage(message));
|
|
@@ -841,9 +969,9 @@ var BakitClient = class _BakitClient extends Client {
|
|
|
841
969
|
content
|
|
842
970
|
};
|
|
843
971
|
};
|
|
844
|
-
async registerApplicationCommands(
|
|
972
|
+
async registerApplicationCommands(client2) {
|
|
845
973
|
const commands = CommandRegistry.constructors.map((c) => CommandRegistry.buildSlashCommand(c));
|
|
846
|
-
await
|
|
974
|
+
await client2.application.commands.set(commands);
|
|
847
975
|
}
|
|
848
976
|
async handleMessage(message) {
|
|
849
977
|
if (message.author.bot) {
|
|
@@ -999,7 +1127,7 @@ var BakitClient = class _BakitClient extends Client {
|
|
|
999
1127
|
return { root, group, subcommand: sub };
|
|
1000
1128
|
}
|
|
1001
1129
|
createHookRecord(hooks) {
|
|
1002
|
-
return Object.values(
|
|
1130
|
+
return Object.values(CommandHookExecutionState).reduce(
|
|
1003
1131
|
(acc, state) => {
|
|
1004
1132
|
acc[state] = hooks.find((h) => h.state === state);
|
|
1005
1133
|
return acc;
|
|
@@ -1019,11 +1147,11 @@ export {
|
|
|
1019
1147
|
Command,
|
|
1020
1148
|
CommandFactory,
|
|
1021
1149
|
CommandGroupEntry,
|
|
1150
|
+
CommandHookExecutionState,
|
|
1022
1151
|
CommandRegistry,
|
|
1023
1152
|
CommandSyntaxError,
|
|
1024
1153
|
CommandSyntaxErrorType,
|
|
1025
1154
|
HOOKS_KEY,
|
|
1026
|
-
HookExecutionState,
|
|
1027
1155
|
MessageContext,
|
|
1028
1156
|
RootCommandEntry,
|
|
1029
1157
|
StateBox,
|