wumbly 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +275 -2
- package/dist/client.cjs +1437 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +3 -0
- package/dist/client.d.ts +3 -0
- package/dist/client.js +1400 -0
- package/dist/client.js.map +1 -0
- package/dist/commands.cjs +244 -0
- package/dist/commands.cjs.map +1 -0
- package/dist/commands.d.cts +3 -0
- package/dist/commands.d.ts +3 -0
- package/dist/commands.js +209 -0
- package/dist/commands.js.map +1 -0
- package/dist/events.cjs +133 -0
- package/dist/events.cjs.map +1 -0
- package/dist/events.d.cts +3 -0
- package/dist/events.d.ts +3 -0
- package/dist/events.js +97 -0
- package/dist/events.js.map +1 -0
- package/dist/index-0P9mmVy0.d.cts +597 -0
- package/dist/index-CR7RtVXn.d.ts +597 -0
- package/dist/index.cjs +1578 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +1494 -0
- package/dist/index.js.map +1 -0
- package/dist/loader.cjs +208 -0
- package/dist/loader.cjs.map +1 -0
- package/dist/loader.d.cts +3 -0
- package/dist/loader.d.ts +3 -0
- package/dist/loader.js +178 -0
- package/dist/loader.js.map +1 -0
- package/dist/permissions.cjs +250 -0
- package/dist/permissions.cjs.map +1 -0
- package/dist/permissions.d.cts +135 -0
- package/dist/permissions.d.ts +135 -0
- package/dist/permissions.js +214 -0
- package/dist/permissions.js.map +1 -0
- package/dist/voice.cjs +125 -0
- package/dist/voice.cjs.map +1 -0
- package/dist/voice.d.cts +3 -0
- package/dist/voice.d.ts +3 -0
- package/dist/voice.js +97 -0
- package/dist/voice.js.map +1 -0
- package/package.json +81 -4
package/dist/commands.js
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
// src/commands/types.ts
|
|
2
|
+
var COMMAND_BRAND = /* @__PURE__ */ Symbol.for("wumbly.command");
|
|
3
|
+
|
|
4
|
+
// src/commands/command.ts
|
|
5
|
+
function normalizeCooldown(cooldown) {
|
|
6
|
+
if (cooldown === void 0) return null;
|
|
7
|
+
const value = typeof cooldown === "number" ? { limit: 1, window: cooldown, scope: "user" } : { ...cooldown };
|
|
8
|
+
if (!Number.isFinite(value.limit) || value.limit <= 0) {
|
|
9
|
+
throw new TypeError("cooldown.limit must be a positive number");
|
|
10
|
+
}
|
|
11
|
+
if (!Number.isFinite(value.window) || value.window <= 0) {
|
|
12
|
+
throw new TypeError("cooldown.window must be a positive number");
|
|
13
|
+
}
|
|
14
|
+
return Object.freeze({
|
|
15
|
+
limit: Math.floor(value.limit),
|
|
16
|
+
window: Math.floor(value.window),
|
|
17
|
+
scope: value.scope ?? "user"
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
var Command = class {
|
|
21
|
+
[COMMAND_BRAND] = true;
|
|
22
|
+
name;
|
|
23
|
+
description;
|
|
24
|
+
aliases;
|
|
25
|
+
category;
|
|
26
|
+
guildOnly;
|
|
27
|
+
ownerOnly;
|
|
28
|
+
cooldown;
|
|
29
|
+
middlewares;
|
|
30
|
+
run;
|
|
31
|
+
constructor(options) {
|
|
32
|
+
const name = options.name.trim();
|
|
33
|
+
if (!name) throw new TypeError("Command name is required");
|
|
34
|
+
if (/\s/.test(name)) throw new TypeError("Command names cannot contain whitespace");
|
|
35
|
+
this.name = name;
|
|
36
|
+
this.description = options.description ?? "";
|
|
37
|
+
this.aliases = Object.freeze([...new Set(options.aliases ?? [])]);
|
|
38
|
+
this.category = options.category ?? null;
|
|
39
|
+
this.guildOnly = options.guildOnly ?? false;
|
|
40
|
+
this.ownerOnly = options.ownerOnly ?? false;
|
|
41
|
+
this.cooldown = normalizeCooldown(options.cooldown);
|
|
42
|
+
this.middlewares = Object.freeze([...options.middlewares ?? []]);
|
|
43
|
+
this.run = options.run;
|
|
44
|
+
Object.freeze(this);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
function isCommand(value) {
|
|
48
|
+
return typeof value === "object" && value !== null && value[COMMAND_BRAND] === true;
|
|
49
|
+
}
|
|
50
|
+
function createCommandContext(client, message, command, args, input) {
|
|
51
|
+
const frozenArgs = Object.freeze([...args]);
|
|
52
|
+
return {
|
|
53
|
+
client,
|
|
54
|
+
message,
|
|
55
|
+
command,
|
|
56
|
+
author: message.author,
|
|
57
|
+
args: frozenArgs,
|
|
58
|
+
input,
|
|
59
|
+
get rest() {
|
|
60
|
+
return frozenArgs.join(" ");
|
|
61
|
+
},
|
|
62
|
+
get channel() {
|
|
63
|
+
return message.channel ?? null;
|
|
64
|
+
},
|
|
65
|
+
get guild() {
|
|
66
|
+
return message.guild ?? null;
|
|
67
|
+
},
|
|
68
|
+
get member() {
|
|
69
|
+
return message.member ?? null;
|
|
70
|
+
},
|
|
71
|
+
reply(options) {
|
|
72
|
+
return message.reply(options);
|
|
73
|
+
},
|
|
74
|
+
send(options) {
|
|
75
|
+
const channel = message.channel;
|
|
76
|
+
if (!channel?.send) {
|
|
77
|
+
return Promise.reject(new Error("The message has no text-based channel"));
|
|
78
|
+
}
|
|
79
|
+
return channel.send(options);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function createCommand(options) {
|
|
84
|
+
return new Command(options);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/commands/cooldown.ts
|
|
88
|
+
function scopeKey(ctx, scope) {
|
|
89
|
+
switch (scope) {
|
|
90
|
+
case "user":
|
|
91
|
+
return ctx.author.id;
|
|
92
|
+
case "guild":
|
|
93
|
+
return ctx.message.guildId ?? "dm";
|
|
94
|
+
case "channel":
|
|
95
|
+
return ctx.message.channelId ?? "unknown";
|
|
96
|
+
case "global":
|
|
97
|
+
return "*";
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
var CooldownStore = class {
|
|
101
|
+
buckets = /* @__PURE__ */ new Map();
|
|
102
|
+
/** Register a use, or report how long the caller has to wait. */
|
|
103
|
+
consume(command, ctx) {
|
|
104
|
+
const cooldown = command.cooldown;
|
|
105
|
+
if (!cooldown) return { ok: true };
|
|
106
|
+
const key = `${command.name}:${cooldown.scope ?? "user"}:${scopeKey(
|
|
107
|
+
ctx,
|
|
108
|
+
cooldown.scope ?? "user"
|
|
109
|
+
)}`;
|
|
110
|
+
const now = Date.now();
|
|
111
|
+
const windowStart = now - cooldown.window;
|
|
112
|
+
const hits = (this.buckets.get(key) ?? []).filter((time) => time > windowStart);
|
|
113
|
+
if (hits.length >= cooldown.limit) {
|
|
114
|
+
const oldest = hits[0] ?? now;
|
|
115
|
+
this.buckets.set(key, hits);
|
|
116
|
+
return {
|
|
117
|
+
ok: false,
|
|
118
|
+
retryAfter: Math.max(0, oldest + cooldown.window - now)
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
hits.push(now);
|
|
122
|
+
this.buckets.set(key, hits);
|
|
123
|
+
return { ok: true };
|
|
124
|
+
}
|
|
125
|
+
clear() {
|
|
126
|
+
this.buckets.clear();
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
// src/commands/middleware.ts
|
|
131
|
+
var Middleware = class {
|
|
132
|
+
name;
|
|
133
|
+
run;
|
|
134
|
+
constructor(options) {
|
|
135
|
+
this.name = options.name ?? null;
|
|
136
|
+
this.run = options.run;
|
|
137
|
+
Object.freeze(this);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
function createMiddleware(options) {
|
|
141
|
+
return new Middleware(options);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// src/commands/store.ts
|
|
145
|
+
function assertFree(key, command, names, aliases) {
|
|
146
|
+
const existing = names.get(key) ?? aliases.get(key);
|
|
147
|
+
if (existing && existing !== command) {
|
|
148
|
+
throw new Error(`Command key "${key}" is already used by "${existing.name}"`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
var CommandStore = class {
|
|
152
|
+
byName = /* @__PURE__ */ new Map();
|
|
153
|
+
byAlias = /* @__PURE__ */ new Map();
|
|
154
|
+
/** Register one command. Throws if its name or an alias is already taken. */
|
|
155
|
+
add(command) {
|
|
156
|
+
this.addMany([command]);
|
|
157
|
+
}
|
|
158
|
+
/** Register several commands, validating every key before committing. */
|
|
159
|
+
addMany(commands) {
|
|
160
|
+
const names = new Map(this.byName);
|
|
161
|
+
const aliases = new Map(this.byAlias);
|
|
162
|
+
for (const command of commands) {
|
|
163
|
+
const nameKey = command.name.toLowerCase();
|
|
164
|
+
assertFree(nameKey, command, names, aliases);
|
|
165
|
+
names.set(nameKey, command);
|
|
166
|
+
const localAliases = /* @__PURE__ */ new Set();
|
|
167
|
+
for (const alias of command.aliases) {
|
|
168
|
+
const aliasKey = alias.toLowerCase();
|
|
169
|
+
if (!aliasKey || aliasKey === nameKey || localAliases.has(aliasKey)) continue;
|
|
170
|
+
assertFree(aliasKey, command, names, aliases);
|
|
171
|
+
localAliases.add(aliasKey);
|
|
172
|
+
aliases.set(aliasKey, command);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
this.byName = names;
|
|
176
|
+
this.byAlias = aliases;
|
|
177
|
+
}
|
|
178
|
+
/** Find a command by name or alias. */
|
|
179
|
+
get(name) {
|
|
180
|
+
const key = name.toLowerCase();
|
|
181
|
+
return this.byName.get(key) ?? this.byAlias.get(key) ?? null;
|
|
182
|
+
}
|
|
183
|
+
has(name) {
|
|
184
|
+
return this.get(name) !== null;
|
|
185
|
+
}
|
|
186
|
+
/** All registered commands, in insertion order. */
|
|
187
|
+
list() {
|
|
188
|
+
return [...this.byName.values()];
|
|
189
|
+
}
|
|
190
|
+
get size() {
|
|
191
|
+
return this.byName.size;
|
|
192
|
+
}
|
|
193
|
+
clear() {
|
|
194
|
+
this.byName.clear();
|
|
195
|
+
this.byAlias.clear();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
export {
|
|
199
|
+
COMMAND_BRAND,
|
|
200
|
+
Command,
|
|
201
|
+
CommandStore,
|
|
202
|
+
CooldownStore,
|
|
203
|
+
Middleware,
|
|
204
|
+
createCommand,
|
|
205
|
+
createCommandContext,
|
|
206
|
+
createMiddleware,
|
|
207
|
+
isCommand
|
|
208
|
+
};
|
|
209
|
+
//# sourceMappingURL=commands.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/commands/types.ts","../src/commands/command.ts","../src/commands/cooldown.ts","../src/commands/middleware.ts","../src/commands/store.ts"],"sourcesContent":["import type { Channel, Guild, GuildMember, Message, User } from '../platform/types';\nimport type { Client } from '../client';\nimport type { Awaitable } from '../types';\nimport type { Command } from './command';\nimport type { Middleware } from './middleware';\n\n/** Runtime brand used by the loader to recognise commands. */\nexport const COMMAND_BRAND: unique symbol = Symbol.for('wumbly.command');\n\n/** Where a cooldown bucket is stored. Default: `user`. */\nexport type CooldownScope = 'user' | 'guild' | 'channel' | 'global';\n\n/** Sliding-window cooldown. */\nexport interface CooldownOptions {\n /** How many uses are allowed inside the window. */\n limit: number;\n /** Window size in milliseconds. */\n window: number;\n /** Bucket scope. Default: `user`. */\n scope?: CooldownScope;\n}\n\n/** Options accepted by {@link CommandContext.reply}. */\nexport type ReplyInput = string | Record<string, unknown>;\n\n/** Everything a command receives when it runs. */\nexport interface CommandContext {\n readonly client: Client;\n readonly message: Message;\n readonly command: Command;\n readonly author: User;\n readonly args: readonly string[];\n /** Raw text after the prefix, including the command name. */\n readonly input: string;\n /** `args` joined back together. */\n readonly rest: string;\n readonly channel: Channel | null;\n readonly guild: Guild | null;\n readonly member: GuildMember | null;\n /** Reply to the triggering message. */\n reply(options: ReplyInput): Promise<Message>;\n /** Send in the same channel without a reply reference. */\n send(options: ReplyInput): Promise<Message>;\n}\n\n/** Options accepted by {@link Command}. */\nexport interface CommandOptions {\n name: string;\n description?: string;\n aliases?: readonly string[];\n category?: string;\n guildOnly?: boolean;\n ownerOnly?: boolean;\n /** Number means \"one use every N milliseconds\". */\n cooldown?: CooldownOptions | number;\n middlewares?: readonly Middleware[];\n run: (ctx: CommandContext) => Awaitable<unknown>;\n}\n","import type { Message } from '../platform/types';\nimport type { Client } from '../client';\nimport {\n COMMAND_BRAND,\n type CommandContext,\n type CommandOptions,\n type CooldownOptions,\n type ReplyInput,\n} from './types';\nimport type { Middleware } from './middleware';\n\nfunction normalizeCooldown(\n cooldown: CommandOptions['cooldown'],\n): CooldownOptions | null {\n if (cooldown === undefined) return null;\n\n const value =\n typeof cooldown === 'number'\n ? { limit: 1, window: cooldown, scope: 'user' as const }\n : { ...cooldown };\n\n if (!Number.isFinite(value.limit) || value.limit <= 0) {\n throw new TypeError('cooldown.limit must be a positive number');\n }\n if (!Number.isFinite(value.window) || value.window <= 0) {\n throw new TypeError('cooldown.window must be a positive number');\n }\n\n return Object.freeze({\n limit: Math.floor(value.limit),\n window: Math.floor(value.window),\n scope: value.scope ?? 'user',\n });\n}\n\n/**\n * An immutable command definition.\n *\n * @example\n * export default new Command({\n * name: 'ping',\n * description: 'Check the latency',\n * run: (ctx) => ctx.reply('Pong!'),\n * });\n */\nexport class Command {\n readonly [COMMAND_BRAND] = true as const;\n readonly name: string;\n readonly description: string;\n readonly aliases: readonly string[];\n readonly category: string | null;\n readonly guildOnly: boolean;\n readonly ownerOnly: boolean;\n readonly cooldown: CooldownOptions | null;\n readonly middlewares: readonly Middleware[];\n readonly run: (ctx: CommandContext) => unknown | Promise<unknown>;\n\n constructor(options: CommandOptions) {\n const name = options.name.trim();\n if (!name) throw new TypeError('Command name is required');\n if (/\\s/.test(name)) throw new TypeError('Command names cannot contain whitespace');\n\n this.name = name;\n this.description = options.description ?? '';\n this.aliases = Object.freeze([...new Set(options.aliases ?? [])]);\n this.category = options.category ?? null;\n this.guildOnly = options.guildOnly ?? false;\n this.ownerOnly = options.ownerOnly ?? false;\n this.cooldown = normalizeCooldown(options.cooldown);\n this.middlewares = Object.freeze([...(options.middlewares ?? [])]);\n this.run = options.run;\n Object.freeze(this);\n }\n}\n\n/** Type guard used by the directory loader. */\nexport function isCommand(value: unknown): value is Command {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as Record<PropertyKey, unknown>)[COMMAND_BRAND] === true\n );\n}\n\n/** Build the object passed to a command's `run`. */\nexport function createCommandContext(\n client: Client,\n message: Message,\n command: Command,\n args: readonly string[],\n input: string,\n): CommandContext {\n const frozenArgs = Object.freeze([...args]);\n\n return {\n client,\n message,\n command,\n author: message.author,\n args: frozenArgs,\n input,\n get rest() {\n return frozenArgs.join(' ');\n },\n get channel() {\n return message.channel ?? null;\n },\n get guild() {\n return message.guild ?? null;\n },\n get member() {\n return message.member ?? null;\n },\n reply(options: ReplyInput) {\n return message.reply(options);\n },\n send(options: ReplyInput) {\n const channel = message.channel as any;\n if (!channel?.send) {\n return Promise.reject(new Error('The message has no text-based channel'));\n }\n return channel.send(options);\n },\n };\n}\n\n/** Create a command definition. */\nexport function createCommand(options: CommandOptions): Command {\n return new Command(options);\n}\n","import type { Command } from './command';\nimport type { CommandContext, CooldownScope } from './types';\n\nfunction scopeKey(ctx: CommandContext, scope: CooldownScope): string {\n switch (scope) {\n case 'user':\n return ctx.author.id;\n case 'guild':\n return ctx.message.guildId ?? 'dm';\n case 'channel':\n return ctx.message.channelId ?? 'unknown';\n case 'global':\n return '*';\n }\n}\n\n/** Sliding-window cooldown buckets keyed by command and scope. */\nexport class CooldownStore {\n private readonly buckets = new Map<string, number[]>();\n\n /** Register a use, or report how long the caller has to wait. */\n consume(\n command: Command,\n ctx: CommandContext,\n ): { ok: true } | { ok: false; retryAfter: number } {\n const cooldown = command.cooldown;\n if (!cooldown) return { ok: true };\n\n const key = `${command.name}:${cooldown.scope ?? 'user'}:${scopeKey(\n ctx,\n cooldown.scope ?? 'user',\n )}`;\n const now = Date.now();\n const windowStart = now - cooldown.window;\n const hits = (this.buckets.get(key) ?? []).filter((time) => time > windowStart);\n\n if (hits.length >= cooldown.limit) {\n const oldest = hits[0] ?? now;\n this.buckets.set(key, hits);\n return {\n ok: false,\n retryAfter: Math.max(0, oldest + cooldown.window - now),\n };\n }\n\n hits.push(now);\n this.buckets.set(key, hits);\n return { ok: true };\n }\n\n clear(): void {\n this.buckets.clear();\n }\n}\n","import type { Awaitable } from '../types';\nimport type { CommandContext } from './types';\n\n/** Call to continue to the next middleware or to the command itself. */\nexport type MiddlewareNext = () => Promise<void>;\n\n/** Options accepted by {@link Middleware}. */\nexport interface MiddlewareOptions {\n /** Optional label used in logs and errors. */\n name?: string;\n run: (ctx: CommandContext, next: MiddlewareNext) => Awaitable<unknown>;\n}\n\n/**\n * Code that runs before a command.\n *\n * Call `next()` to continue. Skip it to stop the command, optionally replying\n * through `ctx.reply()` first.\n *\n * @example\n * export default createMiddleware({\n * name: 'guild-only',\n * run: async (ctx, next) => {\n * if (!ctx.guild) {\n * await ctx.reply('This command only works in a server.');\n * return;\n * }\n * await next();\n * },\n * });\n */\nexport class Middleware {\n readonly name: string | null;\n readonly run: (ctx: CommandContext, next: MiddlewareNext) => Awaitable<unknown>;\n\n constructor(options: MiddlewareOptions) {\n this.name = options.name ?? null;\n this.run = options.run;\n Object.freeze(this);\n }\n}\n\n/** Create a middleware. */\nexport function createMiddleware(options: MiddlewareOptions): Middleware {\n return new Middleware(options);\n}\n","import type { Command } from './command';\n\nfunction assertFree(\n key: string,\n command: Command,\n names: Map<string, Command>,\n aliases: Map<string, Command>,\n): void {\n const existing = names.get(key) ?? aliases.get(key);\n if (existing && existing !== command) {\n throw new Error(`Command key \"${key}\" is already used by \"${existing.name}\"`);\n }\n}\n\n/** Registry for commands and their aliases. Lookups are case-insensitive. */\nexport class CommandStore {\n private byName = new Map<string, Command>();\n private byAlias = new Map<string, Command>();\n\n /** Register one command. Throws if its name or an alias is already taken. */\n add(command: Command): void {\n this.addMany([command]);\n }\n\n /** Register several commands, validating every key before committing. */\n addMany(commands: Iterable<Command>): void {\n const names = new Map(this.byName);\n const aliases = new Map(this.byAlias);\n\n for (const command of commands) {\n const nameKey = command.name.toLowerCase();\n assertFree(nameKey, command, names, aliases);\n names.set(nameKey, command);\n\n const localAliases = new Set<string>();\n for (const alias of command.aliases) {\n const aliasKey = alias.toLowerCase();\n if (!aliasKey || aliasKey === nameKey || localAliases.has(aliasKey)) continue;\n assertFree(aliasKey, command, names, aliases);\n localAliases.add(aliasKey);\n aliases.set(aliasKey, command);\n }\n }\n\n this.byName = names;\n this.byAlias = aliases;\n }\n\n /** Find a command by name or alias. */\n get(name: string): Command | null {\n const key = name.toLowerCase();\n return this.byName.get(key) ?? this.byAlias.get(key) ?? null;\n }\n\n has(name: string): boolean {\n return this.get(name) !== null;\n }\n\n /** All registered commands, in insertion order. */\n list(): Command[] {\n return [...this.byName.values()];\n }\n\n get size(): number {\n return this.byName.size;\n }\n\n clear(): void {\n this.byName.clear();\n this.byAlias.clear();\n }\n}\n"],"mappings":";AAOO,IAAM,gBAA+B,uBAAO,IAAI,gBAAgB;;;ACIvE,SAAS,kBACP,UACwB;AACxB,MAAI,aAAa,OAAW,QAAO;AAEnC,QAAM,QACJ,OAAO,aAAa,WAChB,EAAE,OAAO,GAAG,QAAQ,UAAU,OAAO,OAAgB,IACrD,EAAE,GAAG,SAAS;AAEpB,MAAI,CAAC,OAAO,SAAS,MAAM,KAAK,KAAK,MAAM,SAAS,GAAG;AACrD,UAAM,IAAI,UAAU,0CAA0C;AAAA,EAChE;AACA,MAAI,CAAC,OAAO,SAAS,MAAM,MAAM,KAAK,MAAM,UAAU,GAAG;AACvD,UAAM,IAAI,UAAU,2CAA2C;AAAA,EACjE;AAEA,SAAO,OAAO,OAAO;AAAA,IACnB,OAAO,KAAK,MAAM,MAAM,KAAK;AAAA,IAC7B,QAAQ,KAAK,MAAM,MAAM,MAAM;AAAA,IAC/B,OAAO,MAAM,SAAS;AAAA,EACxB,CAAC;AACH;AAYO,IAAM,UAAN,MAAc;AAAA,EACnB,CAAU,aAAa,IAAI;AAAA,EAClB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA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AAAA,IACF;AAEA,SAAK,SAAS;AACd,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA,EAGA,IAAI,MAA8B;AAChC,UAAM,MAAM,KAAK,YAAY;AAC7B,WAAO,KAAK,OAAO,IAAI,GAAG,KAAK,KAAK,QAAQ,IAAI,GAAG,KAAK;AAAA,EAC1D;AAAA,EAEA,IAAI,MAAuB;AACzB,WAAO,KAAK,IAAI,IAAI,MAAM;AAAA,EAC5B;AAAA;AAAA,EAGA,OAAkB;AAChB,WAAO,CAAC,GAAG,KAAK,OAAO,OAAO,CAAC;AAAA,EACjC;AAAA,EAEA,IAAI,OAAe;AACjB,WAAO,KAAK,OAAO;AAAA,EACrB;AAAA,EAEA,QAAc;AACZ,SAAK,OAAO,MAAM;AAClB,SAAK,QAAQ,MAAM;AAAA,EACrB;AACF;","names":[]}
|
package/dist/events.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/events/index.ts
|
|
21
|
+
var events_exports = {};
|
|
22
|
+
__export(events_exports, {
|
|
23
|
+
EVENT_BRAND: () => EVENT_BRAND,
|
|
24
|
+
Event: () => Event,
|
|
25
|
+
EventStore: () => EventStore,
|
|
26
|
+
LISTENER_BRAND: () => LISTENER_BRAND,
|
|
27
|
+
Listener: () => Listener,
|
|
28
|
+
ListenerStore: () => ListenerStore,
|
|
29
|
+
createEvent: () => createEvent,
|
|
30
|
+
createListener: () => createListener,
|
|
31
|
+
isEvent: () => isEvent,
|
|
32
|
+
isListener: () => isListener
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(events_exports);
|
|
35
|
+
|
|
36
|
+
// src/events/event.ts
|
|
37
|
+
var EVENT_BRAND = /* @__PURE__ */ Symbol.for("wumbly.event");
|
|
38
|
+
var Event = class {
|
|
39
|
+
[EVENT_BRAND] = true;
|
|
40
|
+
event;
|
|
41
|
+
once;
|
|
42
|
+
run;
|
|
43
|
+
constructor(input) {
|
|
44
|
+
if (!input.event) throw new TypeError("Event name is required");
|
|
45
|
+
this.event = input.event;
|
|
46
|
+
this.once = input.once ?? false;
|
|
47
|
+
this.run = input.run;
|
|
48
|
+
Object.freeze(this);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function isEvent(value) {
|
|
52
|
+
return typeof value === "object" && value !== null && value[EVENT_BRAND] === true;
|
|
53
|
+
}
|
|
54
|
+
function createEvent(input) {
|
|
55
|
+
return new Event(input);
|
|
56
|
+
}
|
|
57
|
+
function createListener(input) {
|
|
58
|
+
return new Event(input);
|
|
59
|
+
}
|
|
60
|
+
var LISTENER_BRAND = EVENT_BRAND;
|
|
61
|
+
var Listener = Event;
|
|
62
|
+
function isListener(value) {
|
|
63
|
+
return isEvent(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/events/event-store.ts
|
|
67
|
+
var EventStore = class {
|
|
68
|
+
items = /* @__PURE__ */ new Set();
|
|
69
|
+
handlers = /* @__PURE__ */ new Map();
|
|
70
|
+
add(event) {
|
|
71
|
+
this.items.add(event);
|
|
72
|
+
}
|
|
73
|
+
addMany(events) {
|
|
74
|
+
for (const event of events) this.add(event);
|
|
75
|
+
}
|
|
76
|
+
list() {
|
|
77
|
+
return [...this.items];
|
|
78
|
+
}
|
|
79
|
+
get size() {
|
|
80
|
+
return this.items.size;
|
|
81
|
+
}
|
|
82
|
+
/** Attach a event if it has not been attached yet. */
|
|
83
|
+
attachOne(client, event) {
|
|
84
|
+
if (this.handlers.has(event)) return;
|
|
85
|
+
const handler = (...args) => {
|
|
86
|
+
try {
|
|
87
|
+
Promise.resolve(
|
|
88
|
+
event.run(client, ...args)
|
|
89
|
+
).catch((error) => {
|
|
90
|
+
void client.handleEventError(error, event, args);
|
|
91
|
+
});
|
|
92
|
+
} catch (error) {
|
|
93
|
+
void client.handleEventError(error, event, args);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
if (event.once) {
|
|
97
|
+
client.once(event.event, handler);
|
|
98
|
+
} else {
|
|
99
|
+
client.on(event.event, handler);
|
|
100
|
+
}
|
|
101
|
+
this.handlers.set(event, handler);
|
|
102
|
+
}
|
|
103
|
+
/** Attach every known event. */
|
|
104
|
+
attach(client) {
|
|
105
|
+
for (const event of this.items) this.attachOne(client, event);
|
|
106
|
+
}
|
|
107
|
+
/** Remove every attached handler from the client. */
|
|
108
|
+
detach(client) {
|
|
109
|
+
for (const [event, handler] of this.handlers) {
|
|
110
|
+
client.off(event.event, handler);
|
|
111
|
+
}
|
|
112
|
+
this.handlers.clear();
|
|
113
|
+
}
|
|
114
|
+
clear() {
|
|
115
|
+
this.items.clear();
|
|
116
|
+
this.handlers.clear();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var ListenerStore = EventStore;
|
|
120
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
121
|
+
0 && (module.exports = {
|
|
122
|
+
EVENT_BRAND,
|
|
123
|
+
Event,
|
|
124
|
+
EventStore,
|
|
125
|
+
LISTENER_BRAND,
|
|
126
|
+
Listener,
|
|
127
|
+
ListenerStore,
|
|
128
|
+
createEvent,
|
|
129
|
+
createListener,
|
|
130
|
+
isEvent,
|
|
131
|
+
isListener
|
|
132
|
+
});
|
|
133
|
+
//# sourceMappingURL=events.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/events/index.ts","../src/events/event.ts","../src/events/event-store.ts"],"sourcesContent":["export {\n EVENT_BRAND,\n LISTENER_BRAND,\n createEvent,\n createListener,\n isEvent,\n isListener,\n Event,\n Listener,\n type EventOptions,\n type ListenerOptions,\n} from './event';\nexport { EventStore, ListenerStore } from './event-store';\n","import type { ClientEventName, ClientEvents } from '../platform/types';\nimport type { Client } from '../client';\nimport type { Awaitable } from '../types';\n\n/** Runtime brand used by the loader to recognise events. */\nexport const EVENT_BRAND: unique symbol = Symbol.for('wumbly.event');\n\n/** Options accepted by {@link Event}. */\nexport interface EventOptions<K extends ClientEventName> {\n event: K;\n once?: boolean;\n run: (client: Client, ...args: ClientEvents[K]) => Awaitable<unknown>;\n}\n\n/**\n * An immutable event definition.\n *\n * @example\n * export default new Event({\n * event: 'ready',\n * once: true,\n * run: (client) => client.logger.info(`Logged in as ${client.user?.username}`),\n * });\n */\nexport class Event<K extends ClientEventName = ClientEventName> {\n readonly [EVENT_BRAND] = true as const;\n readonly event: K;\n readonly once: boolean;\n readonly run: (client: Client, ...args: ClientEvents[K]) => Awaitable<unknown>;\n\n constructor(input: EventOptions<K>) {\n if (!input.event) throw new TypeError('Event name is required');\n\n this.event = input.event;\n this.once = input.once ?? false;\n this.run = input.run;\n Object.freeze(this);\n }\n}\n\n/** Type guard used by the directory loader. */\nexport function isEvent(value: unknown): value is Event {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as Record<PropertyKey, unknown>)[EVENT_BRAND] === true\n );\n}\n\n/** Create an event definition. */\nexport function createEvent<K extends ClientEventName>(\n input: EventOptions<K>,\n): Event<K> {\n return new Event(input);\n}\n\n/** @deprecated Use `createEvent`. */\nexport function createListener<K extends ClientEventName>(\n input: EventOptions<K>,\n): Event<K> {\n return new Event(input);\n}\n\n/** @deprecated Use `EVENT_BRAND`. */\nexport const LISTENER_BRAND = EVENT_BRAND;\n/** @deprecated Use `EventOptions`. */\nexport type ListenerOptions<K extends ClientEventName> = EventOptions<K>;\n/** @deprecated Use `Event`. */\nexport const Listener = Event;\n/** @deprecated Use `Event`. */\nexport type Listener<K extends ClientEventName = ClientEventName> = Event<K>;\n/** @deprecated Use `isEvent`. */\nexport function isListener(value: unknown): value is Event {\n return isEvent(value);\n}\n","import type { Client } from '../client';\nimport type { Event } from './event';\n\ntype Handler = (...args: unknown[]) => void;\n\n/** Keeps events registered and attaches them to the Fluxer client. */\nexport class EventStore {\n private readonly items = new Set<Event>();\n private readonly handlers = new Map<Event, Handler>();\n\n add(event: Event): void {\n this.items.add(event);\n }\n\n addMany(events: Iterable<Event>): void {\n for (const event of events) this.add(event);\n }\n\n list(): Event[] {\n return [...this.items];\n }\n\n get size(): number {\n return this.items.size;\n }\n\n /** Attach a event if it has not been attached yet. */\n attachOne(client: Client, event: Event): void {\n if (this.handlers.has(event)) return;\n\n const handler: Handler = (...args) => {\n try {\n Promise.resolve(\n (event.run as (...input: unknown[]) => unknown)(client, ...args),\n ).catch((error) => {\n void client.handleEventError(error, event, args);\n });\n } catch (error) {\n void client.handleEventError(error, event, args);\n }\n };\n\n if (event.once) {\n client.once(event.event as never, handler as never);\n } else {\n client.on(event.event as never, handler as never);\n }\n\n this.handlers.set(event, handler);\n }\n\n /** Attach every known event. */\n attach(client: Client): void {\n for (const event of this.items) this.attachOne(client, event);\n }\n\n /** Remove every attached handler from the client. */\n detach(client: Client): void {\n for (const [event, handler] of this.handlers) {\n client.off(event.event as never, handler as never);\n }\n this.handlers.clear();\n }\n\n clear(): void {\n this.items.clear();\n this.handlers.clear();\n }\n}\n\n/** @deprecated Use `EventStore`. */\nexport const ListenerStore = EventStore;\n/** @deprecated Use `EventStore`.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G;AAE9B,UAAM,UAAmB,IAAI,SAAS;AACpC,UAAI;AACF,gBAAQ;AAAA,UACL,MAAM,IAAyC,QAAQ,GAAG,IAAI;AAAA,QACjE,EAAE,MAAM,CAAC,UAAU;AACjB,eAAK,OAAO,iBAAiB,OAAO,OAAO,IAAI;AAAA,QACjD,CAAC;AAAA,MACH,SAAS,OAAO;AACd,aAAK,OAAO,iBAAiB,OAAO,OAAO,IAAI;AAAA,MACjD;AAAA,IACF;AAEA,QAAI,MAAM,MAAM;AACd,aAAO,KAAK,MAAM,OAAgB,OAAgB;AAAA,IACpD,OAAO;AACL,aAAO,GAAG,MAAM,OAAgB,OAAgB;AAAA,IAClD;AAEA,SAAK,SAAS,IAAI,OAAO,OAAO;AAAA,EAClC;AAAA;AAAA,EAGA,OAAO,QAAsB;AAC3B,eAAW,SAAS,KAAK,MAAO,MAAK,UAAU,QAAQ,KAAK;AAAA,EAC9D;AAAA;AAAA,EAGA,OAAO,QAAsB;AAC3B,eAAW,CAAC,OAAO,OAAO,KAAK,KAAK,UAAU;AAC5C,aAAO,IAAI,MAAM,OAAgB,OAAgB;AAAA,IACnD;AACA,SAAK,SAAS,MAAM;AAAA,EACtB;AAAA,EAEA,QAAc;AACZ,SAAK,MAAM,MAAM;AACjB,SAAK,SAAS,MAAM;AAAA,EACtB;AACF;AAGO,IAAM,gBAAgB;","names":[]}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { E as EVENT_BRAND, o as Event, p as EventOptions, q as EventStore, L as LISTENER_BRAND, t as Listener, u as ListenerOptions, v as ListenerStore, a0 as createEvent, a1 as createListener, a4 as isEvent, a5 as isListener } from './index-0P9mmVy0.cjs';
|
|
2
|
+
import './permissions.cjs';
|
|
3
|
+
import 'node:events';
|
package/dist/events.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { E as EVENT_BRAND, o as Event, p as EventOptions, q as EventStore, L as LISTENER_BRAND, t as Listener, u as ListenerOptions, v as ListenerStore, a0 as createEvent, a1 as createListener, a4 as isEvent, a5 as isListener } from './index-CR7RtVXn.js';
|
|
2
|
+
import './permissions.js';
|
|
3
|
+
import 'node:events';
|
package/dist/events.js
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// src/events/event.ts
|
|
2
|
+
var EVENT_BRAND = /* @__PURE__ */ Symbol.for("wumbly.event");
|
|
3
|
+
var Event = class {
|
|
4
|
+
[EVENT_BRAND] = true;
|
|
5
|
+
event;
|
|
6
|
+
once;
|
|
7
|
+
run;
|
|
8
|
+
constructor(input) {
|
|
9
|
+
if (!input.event) throw new TypeError("Event name is required");
|
|
10
|
+
this.event = input.event;
|
|
11
|
+
this.once = input.once ?? false;
|
|
12
|
+
this.run = input.run;
|
|
13
|
+
Object.freeze(this);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function isEvent(value) {
|
|
17
|
+
return typeof value === "object" && value !== null && value[EVENT_BRAND] === true;
|
|
18
|
+
}
|
|
19
|
+
function createEvent(input) {
|
|
20
|
+
return new Event(input);
|
|
21
|
+
}
|
|
22
|
+
function createListener(input) {
|
|
23
|
+
return new Event(input);
|
|
24
|
+
}
|
|
25
|
+
var LISTENER_BRAND = EVENT_BRAND;
|
|
26
|
+
var Listener = Event;
|
|
27
|
+
function isListener(value) {
|
|
28
|
+
return isEvent(value);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/events/event-store.ts
|
|
32
|
+
var EventStore = class {
|
|
33
|
+
items = /* @__PURE__ */ new Set();
|
|
34
|
+
handlers = /* @__PURE__ */ new Map();
|
|
35
|
+
add(event) {
|
|
36
|
+
this.items.add(event);
|
|
37
|
+
}
|
|
38
|
+
addMany(events) {
|
|
39
|
+
for (const event of events) this.add(event);
|
|
40
|
+
}
|
|
41
|
+
list() {
|
|
42
|
+
return [...this.items];
|
|
43
|
+
}
|
|
44
|
+
get size() {
|
|
45
|
+
return this.items.size;
|
|
46
|
+
}
|
|
47
|
+
/** Attach a event if it has not been attached yet. */
|
|
48
|
+
attachOne(client, event) {
|
|
49
|
+
if (this.handlers.has(event)) return;
|
|
50
|
+
const handler = (...args) => {
|
|
51
|
+
try {
|
|
52
|
+
Promise.resolve(
|
|
53
|
+
event.run(client, ...args)
|
|
54
|
+
).catch((error) => {
|
|
55
|
+
void client.handleEventError(error, event, args);
|
|
56
|
+
});
|
|
57
|
+
} catch (error) {
|
|
58
|
+
void client.handleEventError(error, event, args);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
if (event.once) {
|
|
62
|
+
client.once(event.event, handler);
|
|
63
|
+
} else {
|
|
64
|
+
client.on(event.event, handler);
|
|
65
|
+
}
|
|
66
|
+
this.handlers.set(event, handler);
|
|
67
|
+
}
|
|
68
|
+
/** Attach every known event. */
|
|
69
|
+
attach(client) {
|
|
70
|
+
for (const event of this.items) this.attachOne(client, event);
|
|
71
|
+
}
|
|
72
|
+
/** Remove every attached handler from the client. */
|
|
73
|
+
detach(client) {
|
|
74
|
+
for (const [event, handler] of this.handlers) {
|
|
75
|
+
client.off(event.event, handler);
|
|
76
|
+
}
|
|
77
|
+
this.handlers.clear();
|
|
78
|
+
}
|
|
79
|
+
clear() {
|
|
80
|
+
this.items.clear();
|
|
81
|
+
this.handlers.clear();
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
var ListenerStore = EventStore;
|
|
85
|
+
export {
|
|
86
|
+
EVENT_BRAND,
|
|
87
|
+
Event,
|
|
88
|
+
EventStore,
|
|
89
|
+
LISTENER_BRAND,
|
|
90
|
+
Listener,
|
|
91
|
+
ListenerStore,
|
|
92
|
+
createEvent,
|
|
93
|
+
createListener,
|
|
94
|
+
isEvent,
|
|
95
|
+
isListener
|
|
96
|
+
};
|
|
97
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/events/event.ts","../src/events/event-store.ts"],"sourcesContent":["import type { ClientEventName, ClientEvents } from '../platform/types';\nimport type { Client } from '../client';\nimport type { Awaitable } from '../types';\n\n/** Runtime brand used by the loader to recognise events. */\nexport const EVENT_BRAND: unique symbol = Symbol.for('wumbly.event');\n\n/** Options accepted by {@link Event}. */\nexport interface EventOptions<K extends ClientEventName> {\n event: K;\n once?: boolean;\n run: (client: Client, ...args: ClientEvents[K]) => Awaitable<unknown>;\n}\n\n/**\n * An immutable event definition.\n *\n * @example\n * export default new Event({\n * event: 'ready',\n * once: true,\n * run: (client) => client.logger.info(`Logged in as ${client.user?.username}`),\n * });\n */\nexport class Event<K extends ClientEventName = ClientEventName> {\n readonly [EVENT_BRAND] = true as const;\n readonly event: K;\n readonly once: boolean;\n readonly run: (client: Client, ...args: ClientEvents[K]) => Awaitable<unknown>;\n\n constructor(input: EventOptions<K>) {\n if (!input.event) throw new TypeError('Event name is required');\n\n this.event = input.event;\n this.once = input.once ?? false;\n this.run = input.run;\n Object.freeze(this);\n }\n}\n\n/** Type guard used by the directory loader. */\nexport function isEvent(value: unknown): value is Event {\n return (\n typeof value === 'object' &&\n value !== null &&\n (value as Record<PropertyKey, unknown>)[EVENT_BRAND] === true\n );\n}\n\n/** Create an event definition. */\nexport function createEvent<K extends ClientEventName>(\n input: EventOptions<K>,\n): Event<K> {\n return new Event(input);\n}\n\n/** @deprecated Use `createEvent`. */\nexport function createListener<K extends ClientEventName>(\n input: EventOptions<K>,\n): Event<K> {\n return new Event(input);\n}\n\n/** @deprecated Use `EVENT_BRAND`. */\nexport const LISTENER_BRAND = EVENT_BRAND;\n/** @deprecated Use `EventOptions`. */\nexport type ListenerOptions<K extends ClientEventName> = EventOptions<K>;\n/** @deprecated Use `Event`. */\nexport const Listener = Event;\n/** @deprecated Use `Event`. */\nexport type Listener<K extends ClientEventName = ClientEventName> = Event<K>;\n/** @deprecated Use `isEvent`. */\nexport function isListener(value: unknown): value is Event {\n return isEvent(value);\n}\n","import type { Client } from '../client';\nimport type { Event } from './event';\n\ntype Handler = (...args: unknown[]) => void;\n\n/** Keeps events registered and attaches them to the Fluxer client. */\nexport class EventStore {\n private readonly items = new Set<Event>();\n private readonly handlers = new Map<Event, Handler>();\n\n add(event: Event): void {\n this.items.add(event);\n }\n\n addMany(events: Iterable<Event>): void {\n for (const event of events) this.add(event);\n }\n\n list(): Event[] {\n return [...this.items];\n }\n\n get size(): number {\n return this.items.size;\n }\n\n /** Attach a event if it has not been attached yet. */\n attachOne(client: Client, event: Event): void {\n if (this.handlers.has(event)) return;\n\n const handler: Handler = (...args) => {\n try {\n Promise.resolve(\n (event.run as (...input: unknown[]) => unknown)(client, ...args),\n ).catch((error) => {\n void client.handleEventError(error, event, args);\n });\n } catch (error) {\n void client.handleEventError(error, event, args);\n }\n };\n\n if (event.once) {\n client.once(event.event as never, handler as never);\n } else {\n client.on(event.event as never, handler as never);\n }\n\n this.handlers.set(event, handler);\n }\n\n /** Attach every known event. */\n attach(client: Client): void {\n for (const event of this.items) this.attachOne(client, event);\n }\n\n /** Remove every attached handler from the client. */\n detach(client: Client): void {\n for (const [event, handler] of this.handlers) {\n client.off(event.event as never, handler as never);\n }\n this.handlers.clear();\n }\n\n clear(): void {\n this.items.clear();\n this.handlers.clear();\n }\n}\n\n/** @deprecated Use `EventStore`. */\nexport const ListenerStore = EventStore;\n/** @deprecated Use `EventStore`. */\nexport type ListenerStore = EventStore;\n"],"mappings":";AAKO,IAAM,cAA6B,uBAAO,IAAI,cAAc;AAmB5D,IAAM,QAAN,MAAyD;AAAA,EAC9D,CAAU,WAAW,IAAI;AAAA,EAChB;AAAA,EACA;AAAA,EACA;AAAA,EAET,YAAY,OAAwB;AAClC,QAAI,CAAC,MAAM,MAAO,OAAM,IAAI,UAAU,wBAAwB;AAE9D,SAAK,QAAQ,MAAM;AACnB,SAAK,OAAO,MAAM,QAAQ;AAC1B,SAAK,MAAM,MAAM;AACjB,WAAO,OAAO,IAAI;AAAA,EACpB;AACF;AAGO,SAAS,QAAQ,OAAgC;AACtD,SACE,OAAO,UAAU,YACjB,UAAU,QACT,MAAuC,WAAW,MAAM;AAE7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