seyfert 4.3.1-dev-27001512861.0 → 4.4.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/lib/api/utils/constants.d.ts +1 -1
- package/lib/api/utils/constants.js +1 -1
- package/lib/client/base.d.ts +0 -15
- package/lib/client/base.js +5 -51
- package/lib/client/index.d.ts +0 -1
- package/lib/client/index.js +0 -1
- package/lib/commands/handle.d.ts +2 -10
- package/lib/commands/handle.js +139 -179
- package/lib/commands/index.d.ts +0 -1
- package/lib/components/handler.js +29 -32
- package/lib/websocket/constants/index.js +1 -1
- package/package.json +2 -2
- package/lib/client/plugins.d.ts +0 -22
- package/lib/client/plugins.js +0 -152
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export declare const DefaultUserAgent: "DiscordBot (https://seyfert.dev, v4.
|
|
1
|
+
export declare const DefaultUserAgent: "DiscordBot (https://seyfert.dev, v4.4.0)";
|
|
2
2
|
export declare const ALLOWED_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif"];
|
|
3
3
|
export declare const ALLOWED_STICKER_EXTENSIONS: readonly ["png", "json", "gif"];
|
|
4
4
|
export declare const ALLOWED_SIZES: readonly [16, 32, 64, 100, 128, 256, 512, 1024, 2048, 4096];
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ALLOWED_TAG_BADGE_EXTENSIONS = exports.ALLOWED_SOUNDS_EXTENSIONS = exports.ALLOWED_SIZES = exports.ALLOWED_STICKER_EXTENSIONS = exports.ALLOWED_EXTENSIONS = exports.DefaultUserAgent = void 0;
|
|
4
|
-
exports.DefaultUserAgent = 'DiscordBot (https://seyfert.dev, v4.
|
|
4
|
+
exports.DefaultUserAgent = 'DiscordBot (https://seyfert.dev, v4.4.0)';
|
|
5
5
|
exports.ALLOWED_EXTENSIONS = ['webp', 'png', 'jpg', 'jpeg', 'gif'];
|
|
6
6
|
exports.ALLOWED_STICKER_EXTENSIONS = ['png', 'json', 'gif'];
|
|
7
7
|
exports.ALLOWED_SIZES = [16, 32, 64, 100, 128, 256, 512, 1_024, 2_048, 4_096];
|
package/lib/client/base.d.ts
CHANGED
|
@@ -15,9 +15,7 @@ import { ComponentHandler } from '../components/handler';
|
|
|
15
15
|
import { LangsHandler } from '../langs/handler';
|
|
16
16
|
import type { ChatInputCommandInteraction, ComponentInteraction, EntryPointInteraction, MessageCommandInteraction, ModalSubmitInteraction, UserCommandInteraction } from '../structures';
|
|
17
17
|
import type { APIInteraction, APIInteractionResponse, LocaleString, RESTPostAPIChannelMessageJSONBody } from '../types';
|
|
18
|
-
import { type SeyfertPlugin } from './plugins';
|
|
19
18
|
import type { MessageStructure } from './transformers';
|
|
20
|
-
export type ContextScope = <T>(context: unknown, run: () => Awaitable<T>) => Awaitable<T>;
|
|
21
19
|
export declare class BaseClient {
|
|
22
20
|
rest: ApiHandler;
|
|
23
21
|
cache: Cache;
|
|
@@ -48,9 +46,6 @@ export declare class BaseClient {
|
|
|
48
46
|
private _botId?;
|
|
49
47
|
middlewares?: Record<string, MiddlewareContext>;
|
|
50
48
|
protected static getBotIdFromToken(token: string): string;
|
|
51
|
-
readonly plugins: readonly SeyfertPlugin[];
|
|
52
|
-
private pluginsSetupPromise?;
|
|
53
|
-
private pluginsClosePromise?;
|
|
54
49
|
options: BaseClientOptions;
|
|
55
50
|
constructor(options?: BaseClientOptions);
|
|
56
51
|
get proxy(): import("../api").APIRoutes;
|
|
@@ -61,14 +56,6 @@ export declare class BaseClient {
|
|
|
61
56
|
setServices({ rest, cache, langs, middlewares, handleCommand }: ServicesOptions): void;
|
|
62
57
|
protected execute(..._options: unknown[]): Promise<void>;
|
|
63
58
|
start(options?: Pick<DeepPartial<StartOptions>, 'langsDir' | 'commandsDir' | 'connection' | 'token' | 'componentsDir'>): Promise<void>;
|
|
64
|
-
private setupPlugins;
|
|
65
|
-
/**
|
|
66
|
-
* Closes resources managed by the plugin lifecycle.
|
|
67
|
-
*
|
|
68
|
-
* This waits for in-flight plugin setup and runs `SeyfertPlugin.teardown`.
|
|
69
|
-
* It does not close the gateway, REST client, or cache adapter.
|
|
70
|
-
*/
|
|
71
|
-
close(): Promise<void>;
|
|
72
59
|
protected onPacket(..._packet: unknown[]): Promise<any>;
|
|
73
60
|
/**
|
|
74
61
|
*
|
|
@@ -100,8 +87,6 @@ export declare class BaseClient {
|
|
|
100
87
|
}>;
|
|
101
88
|
}
|
|
102
89
|
export interface BaseClientOptions {
|
|
103
|
-
plugins?: readonly SeyfertPlugin[];
|
|
104
|
-
contextScopes?: readonly ContextScope[];
|
|
105
90
|
context?: (interaction: ChatInputCommandInteraction<boolean> | UserCommandInteraction<boolean> | MessageCommandInteraction<boolean> | ComponentInteraction | ModalSubmitInteraction | EntryPointInteraction<boolean> | When<InferWithPrefix, MessageStructure, never>) => Record<string, unknown>;
|
|
106
91
|
globalMiddlewares?: readonly (keyof RegisteredMiddlewares)[];
|
|
107
92
|
commands?: {
|
package/lib/client/base.js
CHANGED
|
@@ -16,7 +16,6 @@ const soundboard_1 = require("../common/shorters/soundboard");
|
|
|
16
16
|
const voiceStates_1 = require("../common/shorters/voiceStates");
|
|
17
17
|
const handler_2 = require("../components/handler");
|
|
18
18
|
const handler_3 = require("../langs/handler");
|
|
19
|
-
const plugins_1 = require("./plugins");
|
|
20
19
|
class BaseClient {
|
|
21
20
|
rest = new api_1.ApiHandler({ token: 'INVALID' });
|
|
22
21
|
cache = new cache_1.Cache(0, new cache_1.MemoryAdapter(), {}, this);
|
|
@@ -51,14 +50,11 @@ class BaseClient {
|
|
|
51
50
|
static getBotIdFromToken(token) {
|
|
52
51
|
return Buffer.from(token.split('.')[0], 'base64').toString('ascii');
|
|
53
52
|
}
|
|
54
|
-
plugins = [];
|
|
55
|
-
pluginsSetupPromise;
|
|
56
|
-
pluginsClosePromise;
|
|
57
53
|
options;
|
|
58
54
|
/**@internal */
|
|
59
55
|
static _seyfertCfWorkerConfig;
|
|
60
56
|
constructor(options) {
|
|
61
|
-
|
|
57
|
+
this.options = (0, common_1.MergeOptions)({
|
|
62
58
|
commands: {
|
|
63
59
|
defaults: {
|
|
64
60
|
onRunError(context, error) {
|
|
@@ -107,10 +103,7 @@ class BaseClient {
|
|
|
107
103
|
},
|
|
108
104
|
},
|
|
109
105
|
},
|
|
110
|
-
};
|
|
111
|
-
const resolved = (0, plugins_1.resolveClientPlugins)(defaults, options);
|
|
112
|
-
this.options = resolved.options;
|
|
113
|
-
this.plugins = resolved.plugins;
|
|
106
|
+
}, options);
|
|
114
107
|
}
|
|
115
108
|
get proxy() {
|
|
116
109
|
return this.rest.proxy;
|
|
@@ -192,6 +185,9 @@ class BaseClient {
|
|
|
192
185
|
}
|
|
193
186
|
}
|
|
194
187
|
async start(options = {}) {
|
|
188
|
+
await this.loadLangs(options.langsDir);
|
|
189
|
+
await this.loadCommands(options.commandsDir);
|
|
190
|
+
await this.loadComponents(options.componentsDir);
|
|
195
191
|
const { token: tokenRC, debug } = await this.getRC();
|
|
196
192
|
const token = options.token ?? tokenRC;
|
|
197
193
|
(0, common_1.assertString)(token, 'token is not a string');
|
|
@@ -200,51 +196,9 @@ class BaseClient {
|
|
|
200
196
|
this.rest.debug = debug;
|
|
201
197
|
if (!this.handleCommand)
|
|
202
198
|
this.handleCommand = new handle_1.HandleCommand(this);
|
|
203
|
-
await this.setupPlugins();
|
|
204
199
|
// The reason of this method is so for adapters that need to connect somewhere, have time to connect.
|
|
205
200
|
// Or maybe clear cache?
|
|
206
201
|
await this.cache.adapter.start();
|
|
207
|
-
await this.loadLangs(options.langsDir);
|
|
208
|
-
await this.loadCommands(options.commandsDir);
|
|
209
|
-
await this.loadComponents(options.componentsDir);
|
|
210
|
-
}
|
|
211
|
-
async setupPlugins() {
|
|
212
|
-
if (this.pluginsClosePromise)
|
|
213
|
-
await this.pluginsClosePromise;
|
|
214
|
-
this.pluginsSetupPromise ??= (0, plugins_1.setupClientPlugins)(this, this.plugins);
|
|
215
|
-
try {
|
|
216
|
-
await this.pluginsSetupPromise;
|
|
217
|
-
}
|
|
218
|
-
catch (error) {
|
|
219
|
-
this.pluginsSetupPromise = undefined;
|
|
220
|
-
throw error;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Closes resources managed by the plugin lifecycle.
|
|
225
|
-
*
|
|
226
|
-
* This waits for in-flight plugin setup and runs `SeyfertPlugin.teardown`.
|
|
227
|
-
* It does not close the gateway, REST client, or cache adapter.
|
|
228
|
-
*/
|
|
229
|
-
async close() {
|
|
230
|
-
const setup = this.pluginsSetupPromise;
|
|
231
|
-
if (!setup)
|
|
232
|
-
return;
|
|
233
|
-
const close = this.pluginsClosePromise ??
|
|
234
|
-
(async () => {
|
|
235
|
-
await setup;
|
|
236
|
-
await (0, plugins_1.teardownClientPlugins)(this, this.plugins);
|
|
237
|
-
})();
|
|
238
|
-
this.pluginsClosePromise = close;
|
|
239
|
-
try {
|
|
240
|
-
await close;
|
|
241
|
-
}
|
|
242
|
-
finally {
|
|
243
|
-
if (this.pluginsClosePromise === close) {
|
|
244
|
-
this.pluginsSetupPromise = undefined;
|
|
245
|
-
this.pluginsClosePromise = undefined;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
202
|
}
|
|
249
203
|
async onPacket(..._packet) {
|
|
250
204
|
throw new common_1.SeyfertError('FUNCTION_NOT_IMPLEMENTED', { metadata: { detail: 'Function not implemented' } });
|
package/lib/client/index.d.ts
CHANGED
package/lib/client/index.js
CHANGED
|
@@ -16,6 +16,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./client"), exports);
|
|
18
18
|
__exportStar(require("./httpclient"), exports);
|
|
19
|
-
__exportStar(require("./plugins"), exports);
|
|
20
19
|
__exportStar(require("./transformers"), exports);
|
|
21
20
|
__exportStar(require("./workerclient"), exports);
|
package/lib/commands/handle.d.ts
CHANGED
|
@@ -29,17 +29,9 @@ export declare class HandleCommand {
|
|
|
29
29
|
resolveCommandFromContent(content: string, _prefix: string, _message: GatewayMessageCreateDispatchData): CommandFromContent & {
|
|
30
30
|
argsContent?: string;
|
|
31
31
|
};
|
|
32
|
-
getCommandFromContent(commandRaw: string[]
|
|
33
|
-
/**
|
|
34
|
-
* Resolves a message command by its full name.
|
|
35
|
-
*
|
|
36
|
-
* Guild-scoped commands require a matching `guildId`; without one, they intentionally resolve to `undefined`.
|
|
37
|
-
*/
|
|
38
|
-
resolveByName(fullName: string, guildId?: string): CommandFromContent | undefined;
|
|
39
|
-
private resolveCommandFromNameParts;
|
|
32
|
+
getCommandFromContent(commandRaw: string[]): CommandFromContent;
|
|
40
33
|
makeResolver(...args: Parameters<(typeof Transformers)['OptionResolver']>): OptionResolverStructure;
|
|
41
|
-
getParentMessageCommand(rawParentName: string
|
|
42
|
-
private commandCanRunInGuild;
|
|
34
|
+
getParentMessageCommand(rawParentName: string): Command | ContextMenuCommand | undefined;
|
|
43
35
|
getCommand<T extends Command | ContextMenuCommand | EntryPointCommand>(data: {
|
|
44
36
|
guild_id?: string;
|
|
45
37
|
name: string;
|
package/lib/commands/handle.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.HandleCommand = void 0;
|
|
4
|
-
const plugins_1 = require("../client/plugins");
|
|
5
4
|
const transformers_1 = require("../client/transformers");
|
|
6
5
|
const constants_1 = require("../common/it/constants");
|
|
7
6
|
const components_1 = require("../components");
|
|
@@ -38,38 +37,36 @@ class HandleCommand {
|
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
async contextMenu(command, interaction, context) {
|
|
41
|
-
|
|
40
|
+
try {
|
|
41
|
+
if (context.guildId && command.botPermissions) {
|
|
42
|
+
const permissions = this.checkPermissions(interaction.appPermissions, command.botPermissions);
|
|
43
|
+
if (permissions)
|
|
44
|
+
return await command.onBotPermissionsFail?.(context, permissions);
|
|
45
|
+
}
|
|
46
|
+
await command.onBeforeMiddlewares?.(context);
|
|
47
|
+
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
48
|
+
if (typeof resultGlobal === 'boolean')
|
|
49
|
+
return;
|
|
50
|
+
const resultMiddle = await this.runMiddlewares(command, context);
|
|
51
|
+
if (typeof resultMiddle === 'boolean')
|
|
52
|
+
return;
|
|
42
53
|
try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (permissions)
|
|
46
|
-
return await command.onBotPermissionsFail?.(context, permissions);
|
|
47
|
-
}
|
|
48
|
-
await command.onBeforeMiddlewares?.(context);
|
|
49
|
-
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
50
|
-
if (typeof resultGlobal === 'boolean')
|
|
51
|
-
return;
|
|
52
|
-
const resultMiddle = await this.runMiddlewares(command, context);
|
|
53
|
-
if (typeof resultMiddle === 'boolean')
|
|
54
|
-
return;
|
|
55
|
-
try {
|
|
56
|
-
await command.run(context);
|
|
57
|
-
await command.onAfterRun?.(context, undefined);
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
await command.onRunError?.(context, error);
|
|
61
|
-
await command.onAfterRun?.(context, error);
|
|
62
|
-
}
|
|
54
|
+
await command.run(context);
|
|
55
|
+
await command.onAfterRun?.(context, undefined);
|
|
63
56
|
}
|
|
64
57
|
catch (error) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
catch (err) {
|
|
69
|
-
this.client.logger.error(`[${command.name}] Internal error:`, err);
|
|
70
|
-
}
|
|
58
|
+
await command.onRunError?.(context, error);
|
|
59
|
+
await command.onAfterRun?.(context, error);
|
|
71
60
|
}
|
|
72
|
-
}
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
try {
|
|
64
|
+
await command.onInternalError?.(this.client, command, error);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
this.client.logger.error(`[${command.name}] Internal error:`, err);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
73
70
|
}
|
|
74
71
|
contextMenuMessage(command, interaction, context) {
|
|
75
72
|
return this.contextMenu(command, interaction, context);
|
|
@@ -78,82 +75,78 @@ class HandleCommand {
|
|
|
78
75
|
return this.contextMenu(command, interaction, context);
|
|
79
76
|
}
|
|
80
77
|
async entryPoint(command, interaction, context) {
|
|
81
|
-
|
|
78
|
+
try {
|
|
79
|
+
if (context.guildId && command.botPermissions) {
|
|
80
|
+
const permissions = this.checkPermissions(interaction.appPermissions, command.botPermissions);
|
|
81
|
+
if (permissions)
|
|
82
|
+
return await command.onBotPermissionsFail(context, permissions);
|
|
83
|
+
}
|
|
84
|
+
await command.onBeforeMiddlewares?.(context);
|
|
85
|
+
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
86
|
+
if (typeof resultGlobal === 'boolean')
|
|
87
|
+
return;
|
|
88
|
+
const resultMiddle = await this.runMiddlewares(command, context);
|
|
89
|
+
if (typeof resultMiddle === 'boolean')
|
|
90
|
+
return;
|
|
82
91
|
try {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (permissions)
|
|
86
|
-
return await command.onBotPermissionsFail(context, permissions);
|
|
87
|
-
}
|
|
88
|
-
await command.onBeforeMiddlewares?.(context);
|
|
89
|
-
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
90
|
-
if (typeof resultGlobal === 'boolean')
|
|
91
|
-
return;
|
|
92
|
-
const resultMiddle = await this.runMiddlewares(command, context);
|
|
93
|
-
if (typeof resultMiddle === 'boolean')
|
|
94
|
-
return;
|
|
95
|
-
try {
|
|
96
|
-
await command.run(context);
|
|
97
|
-
await command.onAfterRun?.(context, undefined);
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
await command.onRunError(context, error);
|
|
101
|
-
await command.onAfterRun?.(context, error);
|
|
102
|
-
}
|
|
92
|
+
await command.run(context);
|
|
93
|
+
await command.onAfterRun?.(context, undefined);
|
|
103
94
|
}
|
|
104
95
|
catch (error) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
catch (err) {
|
|
109
|
-
this.client.logger.error(`[${command.name}] Internal error:`, err);
|
|
110
|
-
}
|
|
96
|
+
await command.onRunError(context, error);
|
|
97
|
+
await command.onAfterRun?.(context, error);
|
|
111
98
|
}
|
|
112
|
-
}
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
try {
|
|
102
|
+
await command.onInternalError(this.client, command, error);
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
this.client.logger.error(`[${command.name}] Internal error:`, err);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
113
108
|
}
|
|
114
109
|
async chatInput(command, interaction, resolver, context) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return await command.onBotPermissionsFail?.(context, permissions);
|
|
122
|
-
}
|
|
123
|
-
if (command.defaultMemberPermissions) {
|
|
124
|
-
const permissions = this.checkPermissions(interaction.member.permissions, command.defaultMemberPermissions);
|
|
125
|
-
if (permissions)
|
|
126
|
-
return await command.onPermissionsFail?.(context, permissions);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
await command.onBeforeOptions?.(context);
|
|
130
|
-
if (!(await this.runOptions(command, context, resolver)))
|
|
131
|
-
return;
|
|
132
|
-
await command.onBeforeMiddlewares?.(context);
|
|
133
|
-
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
134
|
-
if (typeof resultGlobal === 'boolean')
|
|
135
|
-
return;
|
|
136
|
-
const resultMiddle = await this.runMiddlewares(command, context);
|
|
137
|
-
if (typeof resultMiddle === 'boolean')
|
|
138
|
-
return;
|
|
139
|
-
try {
|
|
140
|
-
await command.run(context);
|
|
141
|
-
await command.onAfterRun?.(context, undefined);
|
|
110
|
+
try {
|
|
111
|
+
if (context.guildId) {
|
|
112
|
+
if (command.botPermissions) {
|
|
113
|
+
const permissions = this.checkPermissions(interaction.appPermissions, command.botPermissions);
|
|
114
|
+
if (permissions)
|
|
115
|
+
return await command.onBotPermissionsFail?.(context, permissions);
|
|
142
116
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
117
|
+
if (command.defaultMemberPermissions) {
|
|
118
|
+
const permissions = this.checkPermissions(interaction.member.permissions, command.defaultMemberPermissions);
|
|
119
|
+
if (permissions)
|
|
120
|
+
return await command.onPermissionsFail?.(context, permissions);
|
|
146
121
|
}
|
|
147
122
|
}
|
|
123
|
+
await command.onBeforeOptions?.(context);
|
|
124
|
+
if (!(await this.runOptions(command, context, resolver)))
|
|
125
|
+
return;
|
|
126
|
+
await command.onBeforeMiddlewares?.(context);
|
|
127
|
+
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
128
|
+
if (typeof resultGlobal === 'boolean')
|
|
129
|
+
return;
|
|
130
|
+
const resultMiddle = await this.runMiddlewares(command, context);
|
|
131
|
+
if (typeof resultMiddle === 'boolean')
|
|
132
|
+
return;
|
|
133
|
+
try {
|
|
134
|
+
await command.run(context);
|
|
135
|
+
await command.onAfterRun?.(context, undefined);
|
|
136
|
+
}
|
|
148
137
|
catch (error) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
}
|
|
152
|
-
catch (err) {
|
|
153
|
-
this.client.logger.error(`[${command.name}] Internal error:`, err);
|
|
154
|
-
}
|
|
138
|
+
await command.onRunError?.(context, error);
|
|
139
|
+
await command.onAfterRun?.(context, error);
|
|
155
140
|
}
|
|
156
|
-
}
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
try {
|
|
144
|
+
await command.onInternalError?.(this.client, command, error);
|
|
145
|
+
}
|
|
146
|
+
catch (err) {
|
|
147
|
+
this.client.logger.error(`[${command.name}] Internal error:`, err);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
157
150
|
}
|
|
158
151
|
async modal(interaction) {
|
|
159
152
|
const context = new components_1.ModalContext(this.client, interaction);
|
|
@@ -280,55 +273,53 @@ class HandleCommand {
|
|
|
280
273
|
//@ts-expect-error
|
|
281
274
|
const extendContext = self.options?.context?.(message) ?? {};
|
|
282
275
|
Object.assign(context, extendContext);
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
return
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
return await command.onPermissionsFail?.(context, memberPermissions.keys(permissions));
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (command.botPermissions) {
|
|
306
|
-
const appPermissions = await self.members.permissions(rawMessage.guild_id, self.botId);
|
|
307
|
-
const permissions = this.checkPermissions(appPermissions, command.botPermissions);
|
|
308
|
-
if (permissions) {
|
|
309
|
-
return await command.onBotPermissionsFail?.(context, permissions);
|
|
310
|
-
}
|
|
276
|
+
if (errors.length) {
|
|
277
|
+
return await command.onOptionsError?.(context, Object.fromEntries(errors.map(x => {
|
|
278
|
+
return [
|
|
279
|
+
x.name,
|
|
280
|
+
{
|
|
281
|
+
failed: true,
|
|
282
|
+
value: x.error,
|
|
283
|
+
parseError: x.fullError,
|
|
284
|
+
},
|
|
285
|
+
];
|
|
286
|
+
})));
|
|
287
|
+
}
|
|
288
|
+
if (rawMessage.guild_id) {
|
|
289
|
+
if (command.defaultMemberPermissions) {
|
|
290
|
+
const memberPermissions = await self.members.permissions(rawMessage.guild_id, rawMessage.author.id);
|
|
291
|
+
const permissions = this.checkPermissions(memberPermissions, command.defaultMemberPermissions);
|
|
292
|
+
const guild = await this.client.guilds.raw(rawMessage.guild_id);
|
|
293
|
+
if (permissions && guild.owner_id !== rawMessage.author.id) {
|
|
294
|
+
return await command.onPermissionsFail?.(context, memberPermissions.keys(permissions));
|
|
311
295
|
}
|
|
312
296
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
return;
|
|
320
|
-
const resultMiddle = await this.runMiddlewares(command, context);
|
|
321
|
-
if (typeof resultMiddle === 'boolean')
|
|
322
|
-
return;
|
|
323
|
-
try {
|
|
324
|
-
await command.run(context);
|
|
325
|
-
await command.onAfterRun?.(context, undefined);
|
|
326
|
-
}
|
|
327
|
-
catch (error) {
|
|
328
|
-
await command.onRunError?.(context, error);
|
|
329
|
-
await command.onAfterRun?.(context, error);
|
|
297
|
+
if (command.botPermissions) {
|
|
298
|
+
const appPermissions = await self.members.permissions(rawMessage.guild_id, self.botId);
|
|
299
|
+
const permissions = this.checkPermissions(appPermissions, command.botPermissions);
|
|
300
|
+
if (permissions) {
|
|
301
|
+
return await command.onBotPermissionsFail?.(context, permissions);
|
|
302
|
+
}
|
|
330
303
|
}
|
|
331
|
-
}
|
|
304
|
+
}
|
|
305
|
+
await command.onBeforeOptions?.(context);
|
|
306
|
+
if (!(await this.runOptions(command, context, optionsResolver)))
|
|
307
|
+
return;
|
|
308
|
+
await command.onBeforeMiddlewares?.(context);
|
|
309
|
+
const resultGlobal = await this.runGlobalMiddlewares(command, context);
|
|
310
|
+
if (typeof resultGlobal === 'boolean')
|
|
311
|
+
return;
|
|
312
|
+
const resultMiddle = await this.runMiddlewares(command, context);
|
|
313
|
+
if (typeof resultMiddle === 'boolean')
|
|
314
|
+
return;
|
|
315
|
+
try {
|
|
316
|
+
await command.run(context);
|
|
317
|
+
await command.onAfterRun?.(context, undefined);
|
|
318
|
+
}
|
|
319
|
+
catch (error) {
|
|
320
|
+
await command.onRunError?.(context, error);
|
|
321
|
+
await command.onAfterRun?.(context, error);
|
|
322
|
+
}
|
|
332
323
|
}
|
|
333
324
|
catch (error) {
|
|
334
325
|
try {
|
|
@@ -350,7 +341,7 @@ class HandleCommand {
|
|
|
350
341
|
const result = this.getCommandFromContent(content
|
|
351
342
|
.split(' ')
|
|
352
343
|
.filter(x => x)
|
|
353
|
-
.slice(0, 3)
|
|
344
|
+
.slice(0, 3));
|
|
354
345
|
if (!result.command)
|
|
355
346
|
return result;
|
|
356
347
|
let newContent = content;
|
|
@@ -362,44 +353,19 @@ class HandleCommand {
|
|
|
362
353
|
argsContent: newContent.slice(1),
|
|
363
354
|
};
|
|
364
355
|
}
|
|
365
|
-
getCommandFromContent(commandRaw
|
|
366
|
-
return this.resolveCommandFromNameParts(commandRaw, guildId);
|
|
367
|
-
}
|
|
368
|
-
/**
|
|
369
|
-
* Resolves a message command by its full name.
|
|
370
|
-
*
|
|
371
|
-
* Guild-scoped commands require a matching `guildId`; without one, they intentionally resolve to `undefined`.
|
|
372
|
-
*/
|
|
373
|
-
resolveByName(fullName, guildId) {
|
|
374
|
-
const parts = fullName
|
|
375
|
-
.trim()
|
|
376
|
-
.split(/\s+/)
|
|
377
|
-
.filter(x => x)
|
|
378
|
-
.slice(0, 3);
|
|
379
|
-
if (!parts.length)
|
|
380
|
-
return undefined;
|
|
381
|
-
const resolved = this.resolveCommandFromNameParts(parts, guildId, false);
|
|
382
|
-
return resolved.command ? resolved : undefined;
|
|
383
|
-
}
|
|
384
|
-
resolveCommandFromNameParts(commandRaw, guildId, allowFallback = true) {
|
|
356
|
+
getCommandFromContent(commandRaw) {
|
|
385
357
|
const rawParentName = commandRaw[0];
|
|
386
358
|
const rawGroupName = commandRaw.length === 3 ? commandRaw[1] : undefined;
|
|
387
359
|
const rawSubcommandName = rawGroupName ? commandRaw[2] : commandRaw[1];
|
|
388
|
-
const parent = this.getParentMessageCommand(rawParentName
|
|
360
|
+
const parent = this.getParentMessageCommand(rawParentName);
|
|
389
361
|
const fullCommandName = `${rawParentName}${rawGroupName ? ` ${rawGroupName} ${rawSubcommandName}` : `${rawSubcommandName ? ` ${rawSubcommandName}` : ''}`}`;
|
|
390
362
|
if (!(parent instanceof _1.Command))
|
|
391
363
|
return { fullCommandName };
|
|
392
|
-
if (rawGroupName && !parent.groups?.[rawGroupName] && !parent.groupsAliases?.[rawGroupName])
|
|
393
|
-
|
|
394
|
-
return { fullCommandName };
|
|
395
|
-
return this.resolveCommandFromNameParts([rawParentName, rawGroupName], guildId);
|
|
396
|
-
}
|
|
364
|
+
if (rawGroupName && !parent.groups?.[rawGroupName] && !parent.groupsAliases?.[rawGroupName])
|
|
365
|
+
return this.getCommandFromContent([rawParentName, rawGroupName]);
|
|
397
366
|
if (rawSubcommandName &&
|
|
398
|
-
!parent.options?.some(x => x instanceof _1.SubCommand && (x.name === rawSubcommandName || x.aliases?.includes(rawSubcommandName))))
|
|
399
|
-
|
|
400
|
-
return { fullCommandName };
|
|
401
|
-
return this.resolveCommandFromNameParts([rawParentName], guildId);
|
|
402
|
-
}
|
|
367
|
+
!parent.options?.some(x => x instanceof _1.SubCommand && (x.name === rawSubcommandName || x.aliases?.includes(rawSubcommandName))))
|
|
368
|
+
return this.getCommandFromContent([rawParentName]);
|
|
403
369
|
const groupName = rawGroupName ? parent.groupsAliases?.[rawGroupName] || rawGroupName : undefined;
|
|
404
370
|
const command = groupName || rawSubcommandName
|
|
405
371
|
? parent.options?.find(opt => {
|
|
@@ -424,16 +390,10 @@ class HandleCommand {
|
|
|
424
390
|
makeResolver(...args) {
|
|
425
391
|
return transformers_1.Transformers.OptionResolver(...args);
|
|
426
392
|
}
|
|
427
|
-
getParentMessageCommand(rawParentName
|
|
393
|
+
getParentMessageCommand(rawParentName) {
|
|
428
394
|
return this.client.commands.values.find(x => (!('ignore' in x) || x.ignore !== _1.IgnoreCommand.Message) &&
|
|
429
|
-
this.commandCanRunInGuild(x, guildId) &&
|
|
430
395
|
(x.name === rawParentName || ('aliases' in x ? x.aliases?.includes(rawParentName) : false)));
|
|
431
396
|
}
|
|
432
|
-
commandCanRunInGuild(command, guildId) {
|
|
433
|
-
if (!command.guildId?.length)
|
|
434
|
-
return true;
|
|
435
|
-
return guildId ? command.guildId.includes(guildId) : false;
|
|
436
|
-
}
|
|
437
397
|
getCommand(data) {
|
|
438
398
|
return this.client.commands.values.find(command => {
|
|
439
399
|
if (data.guild_id) {
|
package/lib/commands/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ComponentHandler = void 0;
|
|
4
|
-
const plugins_1 = require("../client/plugins");
|
|
5
4
|
const collection_1 = require("../collection");
|
|
6
5
|
const commands_1 = require("../commands");
|
|
7
6
|
const common_1 = require("../common");
|
|
@@ -259,41 +258,39 @@ class ComponentHandler extends common_1.BaseHandler {
|
|
|
259
258
|
}
|
|
260
259
|
}
|
|
261
260
|
async execute(i, context) {
|
|
262
|
-
|
|
261
|
+
try {
|
|
262
|
+
await i.onBeforeMiddlewares?.(context);
|
|
263
|
+
const resultRunGlobalMiddlewares = await commands_1.BaseCommand.__runMiddlewares(context, (context.client.options?.globalMiddlewares ?? []), true);
|
|
264
|
+
if (resultRunGlobalMiddlewares.pass) {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
if ('error' in resultRunGlobalMiddlewares) {
|
|
268
|
+
return await i.onMiddlewaresError?.(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
|
269
|
+
}
|
|
270
|
+
const resultRunMiddlewares = await commands_1.BaseCommand.__runMiddlewares(context, i.middlewares, false);
|
|
271
|
+
if (resultRunMiddlewares.pass) {
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
if ('error' in resultRunMiddlewares) {
|
|
275
|
+
return await i.onMiddlewaresError?.(context, resultRunMiddlewares.error ?? 'Unknown error');
|
|
276
|
+
}
|
|
263
277
|
try {
|
|
264
|
-
await i.
|
|
265
|
-
|
|
266
|
-
if (resultRunGlobalMiddlewares.pass) {
|
|
267
|
-
return;
|
|
268
|
-
}
|
|
269
|
-
if ('error' in resultRunGlobalMiddlewares) {
|
|
270
|
-
return await i.onMiddlewaresError?.(context, resultRunGlobalMiddlewares.error ?? 'Unknown error');
|
|
271
|
-
}
|
|
272
|
-
const resultRunMiddlewares = await commands_1.BaseCommand.__runMiddlewares(context, i.middlewares, false);
|
|
273
|
-
if (resultRunMiddlewares.pass) {
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
if ('error' in resultRunMiddlewares) {
|
|
277
|
-
return await i.onMiddlewaresError?.(context, resultRunMiddlewares.error ?? 'Unknown error');
|
|
278
|
-
}
|
|
279
|
-
try {
|
|
280
|
-
await i.run(context);
|
|
281
|
-
await i.onAfterRun?.(context, undefined);
|
|
282
|
-
}
|
|
283
|
-
catch (error) {
|
|
284
|
-
await i.onRunError?.(context, error);
|
|
285
|
-
await i.onAfterRun?.(context, error);
|
|
286
|
-
}
|
|
278
|
+
await i.run(context);
|
|
279
|
+
await i.onAfterRun?.(context, undefined);
|
|
287
280
|
}
|
|
288
281
|
catch (error) {
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
catch (err) {
|
|
293
|
-
this.client.logger.error(`[${i.customId ?? 'Component/Modal command'}] Internal error:`, err);
|
|
294
|
-
}
|
|
282
|
+
await i.onRunError?.(context, error);
|
|
283
|
+
await i.onAfterRun?.(context, error);
|
|
295
284
|
}
|
|
296
|
-
}
|
|
285
|
+
}
|
|
286
|
+
catch (error) {
|
|
287
|
+
try {
|
|
288
|
+
await i.onInternalError?.(this.client, error);
|
|
289
|
+
}
|
|
290
|
+
catch (err) {
|
|
291
|
+
this.client.logger.error(`[${i.customId ?? 'Component/Modal command'}] Internal error:`, err);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
297
294
|
}
|
|
298
295
|
async executeComponent(context) {
|
|
299
296
|
for (const i of this.commands) {
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.properties = exports.WorkerManagerDefaults = exports.ShardManagerDefaults = exports.COMPRESS = void 0;
|
|
4
4
|
const COMPRESS = false;
|
|
5
5
|
exports.COMPRESS = COMPRESS;
|
|
6
|
-
const propertiesVersion = 'Seyfert v4.
|
|
6
|
+
const propertiesVersion = 'Seyfert v4.4.0 (https://seyfert.dev)';
|
|
7
7
|
const properties = {
|
|
8
8
|
os: process.platform,
|
|
9
9
|
browser: propertiesVersion,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "seyfert",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "The most advanced framework for discord bots",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib/index.js",
|
|
@@ -70,4 +70,4 @@
|
|
|
70
70
|
"esbuild"
|
|
71
71
|
]
|
|
72
72
|
}
|
|
73
|
-
}
|
|
73
|
+
}
|
package/lib/client/plugins.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { UsingClient } from '../commands';
|
|
2
|
-
import type { Awaitable } from '../common/types/util';
|
|
3
|
-
import type { BaseClientOptions, ContextScope } from './base';
|
|
4
|
-
type ClientOptionsFragment = Partial<Omit<BaseClientOptions, 'plugins'>>;
|
|
5
|
-
export type SeyfertPluginClient = UsingClient & {
|
|
6
|
-
plugins: readonly SeyfertPlugin[];
|
|
7
|
-
};
|
|
8
|
-
export interface SeyfertPlugin {
|
|
9
|
-
name: string;
|
|
10
|
-
options?(current: Readonly<BaseClientOptions>): ClientOptionsFragment;
|
|
11
|
-
setup?(client: SeyfertPluginClient): Awaitable<void>;
|
|
12
|
-
teardown?(client: SeyfertPluginClient): Awaitable<void>;
|
|
13
|
-
}
|
|
14
|
-
export interface ResolvedClientPlugins {
|
|
15
|
-
plugins: readonly SeyfertPlugin[];
|
|
16
|
-
options: BaseClientOptions;
|
|
17
|
-
}
|
|
18
|
-
export declare function resolveClientPlugins(defaults: BaseClientOptions, options?: BaseClientOptions): ResolvedClientPlugins;
|
|
19
|
-
export declare function setupClientPlugins(client: SeyfertPluginClient, plugins: readonly SeyfertPlugin[]): Promise<void>;
|
|
20
|
-
export declare function teardownClientPlugins(client: SeyfertPluginClient, plugins: readonly SeyfertPlugin[]): Promise<void>;
|
|
21
|
-
export declare function runContextScopes<T>(scopes: readonly ContextScope[] | undefined, context: unknown, run: () => Awaitable<T>): Awaitable<T>;
|
|
22
|
-
export {};
|
package/lib/client/plugins.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.resolveClientPlugins = resolveClientPlugins;
|
|
4
|
-
exports.setupClientPlugins = setupClientPlugins;
|
|
5
|
-
exports.teardownClientPlugins = teardownClientPlugins;
|
|
6
|
-
exports.runContextScopes = runContextScopes;
|
|
7
|
-
const common_1 = require("../common");
|
|
8
|
-
const commandHookKeys = [
|
|
9
|
-
'onBeforeMiddlewares',
|
|
10
|
-
'onBeforeOptions',
|
|
11
|
-
'onRunError',
|
|
12
|
-
'onPermissionsFail',
|
|
13
|
-
'onBotPermissionsFail',
|
|
14
|
-
'onInternalError',
|
|
15
|
-
'onMiddlewaresError',
|
|
16
|
-
'onOptionsError',
|
|
17
|
-
'onAfterRun',
|
|
18
|
-
];
|
|
19
|
-
const componentHookKeys = [
|
|
20
|
-
'onBeforeMiddlewares',
|
|
21
|
-
'onRunError',
|
|
22
|
-
'onInternalError',
|
|
23
|
-
'onMiddlewaresError',
|
|
24
|
-
'onAfterRun',
|
|
25
|
-
];
|
|
26
|
-
const modalHookKeys = [
|
|
27
|
-
'onBeforeMiddlewares',
|
|
28
|
-
'onRunError',
|
|
29
|
-
'onInternalError',
|
|
30
|
-
'onMiddlewaresError',
|
|
31
|
-
'onAfterRun',
|
|
32
|
-
];
|
|
33
|
-
function resolveClientPlugins(defaults, options = {}) {
|
|
34
|
-
const plugins = options.plugins ?? [];
|
|
35
|
-
const userOptions = omitPlugins(options);
|
|
36
|
-
const pluginOptions = [];
|
|
37
|
-
for (const plugin of plugins) {
|
|
38
|
-
const current = (0, common_1.MergeOptions)(defaults, ...pluginOptions, userOptions);
|
|
39
|
-
const fragment = plugin.options?.(current);
|
|
40
|
-
if (fragment)
|
|
41
|
-
pluginOptions.push(fragment);
|
|
42
|
-
}
|
|
43
|
-
const merged = (0, common_1.MergeOptions)(defaults, ...pluginOptions, userOptions);
|
|
44
|
-
merged.plugins = plugins;
|
|
45
|
-
composeContext(merged, defaults, pluginOptions, userOptions);
|
|
46
|
-
composeContextScopes(merged, defaults, pluginOptions, userOptions);
|
|
47
|
-
composeGlobalMiddlewares(merged, defaults, pluginOptions, userOptions);
|
|
48
|
-
composeDefaults(merged.commands?.defaults, defaults.commands?.defaults, pluginOptions.map(fragment => fragment.commands?.defaults), userOptions.commands?.defaults, commandHookKeys);
|
|
49
|
-
composeDefaults(merged.components?.defaults, defaults.components?.defaults, pluginOptions.map(fragment => fragment.components?.defaults), userOptions.components?.defaults, componentHookKeys);
|
|
50
|
-
composeDefaults(merged.modals?.defaults, defaults.modals?.defaults, pluginOptions.map(fragment => fragment.modals?.defaults), userOptions.modals?.defaults, modalHookKeys);
|
|
51
|
-
return { plugins, options: merged };
|
|
52
|
-
}
|
|
53
|
-
async function setupClientPlugins(client, plugins) {
|
|
54
|
-
const completed = [];
|
|
55
|
-
try {
|
|
56
|
-
for (const plugin of plugins) {
|
|
57
|
-
await plugin.setup?.(client);
|
|
58
|
-
completed.push(plugin);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
catch (setupError) {
|
|
62
|
-
try {
|
|
63
|
-
await teardownClientPlugins(client, completed);
|
|
64
|
-
}
|
|
65
|
-
catch (teardownError) {
|
|
66
|
-
throw new AggregateError([setupError, teardownError], 'Seyfert plugin setup failed and cleanup also failed.');
|
|
67
|
-
}
|
|
68
|
-
throw setupError;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
async function teardownClientPlugins(client, plugins) {
|
|
72
|
-
const errors = [];
|
|
73
|
-
for (const plugin of [...plugins].reverse()) {
|
|
74
|
-
try {
|
|
75
|
-
await plugin.teardown?.(client);
|
|
76
|
-
}
|
|
77
|
-
catch (error) {
|
|
78
|
-
errors.push(error);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (errors.length)
|
|
82
|
-
throw new AggregateError(errors, 'Seyfert plugin teardown failed.');
|
|
83
|
-
}
|
|
84
|
-
function runContextScopes(scopes, context, run) {
|
|
85
|
-
if (!scopes?.length)
|
|
86
|
-
return run();
|
|
87
|
-
const scopedRun = scopes.reduceRight((next, scope) => () => scope(context, next), run);
|
|
88
|
-
return scopedRun();
|
|
89
|
-
}
|
|
90
|
-
function omitPlugins(options) {
|
|
91
|
-
const { plugins: _plugins, ...rest } = options;
|
|
92
|
-
return rest;
|
|
93
|
-
}
|
|
94
|
-
function composeContext(target, defaults, pluginOptions, userOptions) {
|
|
95
|
-
const callbacks = [];
|
|
96
|
-
if (typeof defaults.context === 'function')
|
|
97
|
-
callbacks.push(defaults.context);
|
|
98
|
-
callbacks.push(...pluginOptions
|
|
99
|
-
.map(fragment => fragment.context)
|
|
100
|
-
.filter((callback) => typeof callback === 'function'));
|
|
101
|
-
if (typeof userOptions.context === 'function')
|
|
102
|
-
callbacks.push(userOptions.context);
|
|
103
|
-
if (!callbacks.length)
|
|
104
|
-
return;
|
|
105
|
-
target.context = interaction => callbacks.reduce((context, callback) => Object.assign(context, callback(interaction)), {});
|
|
106
|
-
}
|
|
107
|
-
function composeContextScopes(target, defaults, pluginOptions, userOptions) {
|
|
108
|
-
const scopes = [
|
|
109
|
-
...(defaults.contextScopes ?? []),
|
|
110
|
-
...pluginOptions.flatMap(fragment => fragment.contextScopes ?? []),
|
|
111
|
-
...(userOptions.contextScopes ?? []),
|
|
112
|
-
];
|
|
113
|
-
if (scopes.length)
|
|
114
|
-
target.contextScopes = scopes;
|
|
115
|
-
}
|
|
116
|
-
function composeGlobalMiddlewares(target, defaults, pluginOptions, userOptions) {
|
|
117
|
-
const middlewares = [
|
|
118
|
-
...(defaults.globalMiddlewares ?? []),
|
|
119
|
-
...pluginOptions.flatMap(fragment => fragment.globalMiddlewares ?? []),
|
|
120
|
-
];
|
|
121
|
-
middlewares.push(...(userOptions.globalMiddlewares ?? []));
|
|
122
|
-
if (middlewares.length)
|
|
123
|
-
target.globalMiddlewares = middlewares;
|
|
124
|
-
}
|
|
125
|
-
function composeDefaults(target, defaults, pluginDefaults, userDefaults, keys) {
|
|
126
|
-
if (!target)
|
|
127
|
-
return;
|
|
128
|
-
for (const key of keys) {
|
|
129
|
-
const pluginHooks = pluginDefaults.map(defaults => defaults?.[key]).filter(isFunction);
|
|
130
|
-
const userHook = userDefaults?.[key];
|
|
131
|
-
const fallbackHook = defaults?.[key];
|
|
132
|
-
if (!pluginHooks.length && typeof userHook !== 'function')
|
|
133
|
-
continue;
|
|
134
|
-
const hooks = isFunction(userHook)
|
|
135
|
-
? [...pluginHooks, userHook]
|
|
136
|
-
: pluginHooks.length
|
|
137
|
-
? pluginHooks
|
|
138
|
-
: isFunction(fallbackHook)
|
|
139
|
-
? [fallbackHook]
|
|
140
|
-
: [];
|
|
141
|
-
target[key] = composeHooks(hooks);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
function composeHooks(hooks) {
|
|
145
|
-
return async (...args) => {
|
|
146
|
-
for (const hook of hooks)
|
|
147
|
-
await hook(...args);
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
function isFunction(value) {
|
|
151
|
-
return typeof value === 'function';
|
|
152
|
-
}
|