ludicord 2.2.5 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -4
- package/dist/activity/game-loop.d.ts +16 -0
- package/dist/activity/game-loop.js +33 -0
- package/dist/activity/index.d.ts +12 -0
- package/dist/activity/index.js +6 -0
- package/dist/activity/lifecycle.d.ts +11 -0
- package/dist/activity/lifecycle.js +41 -0
- package/dist/activity/presence.d.ts +10 -0
- package/dist/activity/presence.js +15 -0
- package/dist/activity/query.d.ts +21 -0
- package/dist/activity/query.js +148 -0
- package/dist/activity/shared-state.d.ts +10 -0
- package/dist/activity/shared-state.js +100 -0
- package/dist/activity/storage.d.ts +16 -0
- package/dist/activity/storage.js +88 -0
- package/dist/api/runtime.d.ts +5 -2
- package/dist/api/runtime.js +53 -10
- package/dist/auth/client.d.ts +0 -1
- package/dist/auth/client.js +3 -5
- package/dist/auth/index.d.ts +0 -1
- package/dist/auth/index.js +0 -1
- package/dist/auth/server.d.ts +2 -1
- package/dist/auth/server.js +161 -16
- package/dist/auth/types.d.ts +5 -1
- package/dist/auth/types.js +0 -1
- package/dist/cli/index.d.ts +0 -1
- package/dist/cli/index.js +116 -19
- package/dist/cli/project-commands.d.ts +1 -5
- package/dist/cli/project-commands.js +14 -162
- package/dist/compiler/build.d.ts +10 -1
- package/dist/compiler/build.js +124 -16
- package/dist/compiler/bundle-report.d.ts +17 -0
- package/dist/compiler/bundle-report.js +60 -0
- package/dist/compiler/client-entry.d.ts +0 -1
- package/dist/compiler/client-entry.js +28 -4
- package/dist/compiler/client-tooling.d.ts +0 -1
- package/dist/compiler/client-tooling.js +8 -2
- package/dist/compiler/index.d.ts +3 -1
- package/dist/compiler/index.js +2 -1
- package/dist/compiler/scanner.d.ts +0 -1
- package/dist/compiler/scanner.js +59 -4
- package/dist/compiler/server-bundle.d.ts +1 -2
- package/dist/compiler/server-bundle.js +11 -2
- package/dist/compiler/type-generation.d.ts +0 -1
- package/dist/compiler/type-generation.js +2 -1
- package/dist/compiler/write-manifests.d.ts +0 -1
- package/dist/compiler/write-manifests.js +0 -1
- package/dist/config/defaults.d.ts +0 -1
- package/dist/config/defaults.js +14 -2
- package/dist/config/environment.d.ts +0 -1
- package/dist/config/environment.js +0 -1
- package/dist/config/index.d.ts +0 -1
- package/dist/config/index.js +0 -1
- package/dist/config/loader.d.ts +0 -1
- package/dist/config/loader.js +0 -1
- package/dist/config/project-root.d.ts +0 -1
- package/dist/config/project-root.js +0 -1
- package/dist/config/types.d.ts +25 -1
- package/dist/config/types.js +0 -1
- package/dist/discord/assets.d.ts +5 -0
- package/dist/discord/assets.js +29 -0
- package/dist/discord/data.d.ts +38 -2
- package/dist/discord/data.js +178 -11
- package/dist/discord/events.d.ts +28 -2
- package/dist/discord/events.js +109 -39
- package/dist/discord/index.d.ts +6 -5
- package/dist/discord/index.js +3 -3
- package/dist/discord/payload.d.ts +6 -0
- package/dist/discord/payload.js +5 -0
- package/dist/discord/runtime.d.ts +0 -1
- package/dist/discord/runtime.js +0 -1
- package/dist/discord/safe-area.d.ts +0 -1
- package/dist/discord/safe-area.js +0 -1
- package/dist/discord/testing.d.ts +0 -1
- package/dist/discord/testing.js +0 -1
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/lint/index.d.ts +6 -0
- package/dist/lint/index.js +71 -0
- package/dist/manifests/types.d.ts +11 -1
- package/dist/manifests/types.js +0 -1
- package/dist/metadata/index.d.ts +13 -0
- package/dist/metadata/index.js +4 -0
- package/dist/navigation/index.d.ts +3 -1
- package/dist/navigation/index.js +1 -1
- package/dist/navigation/link.d.ts +8 -0
- package/dist/navigation/link.js +38 -0
- package/dist/router/match.d.ts +0 -1
- package/dist/router/match.js +0 -1
- package/dist/runtime/client/dev-panel.d.ts +0 -1
- package/dist/runtime/client/dev-panel.js +722 -38
- package/dist/runtime/client/embed-router.d.ts +8 -1
- package/dist/runtime/client/embed-router.js +116 -62
- package/dist/runtime/client/files.d.ts +5 -1
- package/dist/runtime/client/files.js +0 -1
- package/dist/runtime/client/minimize-card.d.ts +0 -1
- package/dist/runtime/client/minimize-card.js +0 -1
- package/dist/runtime/client/minimize.d.ts +0 -1
- package/dist/runtime/client/minimize.js +0 -1
- package/dist/runtime/client/root.d.ts +5 -3
- package/dist/runtime/client/root.js +28 -3
- package/dist/runtime/index.d.ts +1 -2
- package/dist/runtime/index.js +0 -1
- package/dist/runtime/server/dev-server.d.ts +0 -1
- package/dist/runtime/server/dev-server.js +187 -30
- package/dist/runtime/server/diagnostics.d.ts +1 -1
- package/dist/runtime/server/diagnostics.js +5 -4
- package/dist/runtime/server/index.d.ts +0 -1
- package/dist/runtime/server/index.js +0 -1
- package/dist/runtime/server/production-server.d.ts +2 -1
- package/dist/runtime/server/production-server.js +145 -25
- package/dist/security/discord.d.ts +0 -1
- package/dist/security/discord.js +0 -1
- package/dist/security/index.d.ts +0 -1
- package/dist/security/index.js +0 -1
- package/dist/security/secret-scan.d.ts +0 -1
- package/dist/security/secret-scan.js +0 -1
- package/dist/server/discord-rest.d.ts +42 -7
- package/dist/server/discord-rest.js +135 -18
- package/dist/server/index.d.ts +7 -2
- package/dist/server/index.js +3 -1
- package/dist/server/instrumentation.d.ts +16 -0
- package/dist/server/instrumentation.js +3 -0
- package/dist/server/proxy.d.ts +21 -0
- package/dist/server/proxy.js +76 -0
- package/dist/server/session.d.ts +0 -1
- package/dist/server/session.js +0 -1
- package/dist/server/types.d.ts +0 -1
- package/dist/server/types.js +0 -1
- package/dist/sessions/cookies.d.ts +0 -1
- package/dist/sessions/cookies.js +0 -1
- package/dist/sessions/crypto.d.ts +0 -1
- package/dist/sessions/crypto.js +0 -1
- package/dist/sessions/data-store.d.ts +18 -0
- package/dist/sessions/data-store.js +45 -0
- package/dist/shared/errors.d.ts +0 -1
- package/dist/shared/errors.js +0 -1
- package/dist/testing/index.d.ts +0 -1
- package/dist/testing/index.js +2 -2
- package/dist/version.d.ts +1 -2
- package/dist/version.js +1 -2
- package/dist/ws/client.d.ts +0 -1
- package/dist/ws/client.js +0 -1
- package/dist/ws/index.d.ts +3 -2
- package/dist/ws/index.js +1 -1
- package/dist/ws/server.d.ts +5 -3
- package/dist/ws/server.js +65 -19
- package/dist/ws/shared-state.d.ts +13 -0
- package/dist/ws/shared-state.js +137 -0
- package/package.json +14 -1
package/dist/config/defaults.js
CHANGED
|
@@ -15,6 +15,12 @@ export function resolveConfig(config = {}) {
|
|
|
15
15
|
defaultEmbed: config.activity?.defaultEmbed ?? "home",
|
|
16
16
|
outsideDiscord: config.activity?.outsideDiscord ?? "error",
|
|
17
17
|
},
|
|
18
|
+
react: {
|
|
19
|
+
strictMode: config.react?.strictMode ?? true,
|
|
20
|
+
},
|
|
21
|
+
build: {
|
|
22
|
+
clientAssetWarningLimit: config.build?.clientAssetWarningLimit ?? 512_000,
|
|
23
|
+
},
|
|
18
24
|
server: {
|
|
19
25
|
port: config.server?.port ?? 3000,
|
|
20
26
|
host: config.server?.host ?? "0.0.0.0",
|
|
@@ -22,12 +28,15 @@ export function resolveConfig(config = {}) {
|
|
|
22
28
|
limits: {
|
|
23
29
|
body: config.server?.limits?.body ?? "2mb",
|
|
24
30
|
},
|
|
31
|
+
requestTimeout: config.server?.requestTimeout ?? 30_000,
|
|
32
|
+
shutdownTimeout: config.server?.shutdownTimeout ?? 10_000,
|
|
25
33
|
},
|
|
26
34
|
websocket: {
|
|
27
35
|
enabled: config.websocket?.enabled ?? true,
|
|
28
36
|
heartbeatInterval: config.websocket?.heartbeatInterval ?? 30_000,
|
|
29
37
|
maxPayload: config.websocket?.maxPayload ?? 1_048_576,
|
|
30
38
|
compression: config.websocket?.compression ?? false,
|
|
39
|
+
maxMessagesPerSecond: config.websocket?.maxMessagesPerSecond ?? 120,
|
|
31
40
|
reconnect: {
|
|
32
41
|
enabled: config.websocket?.reconnect?.enabled ?? true,
|
|
33
42
|
attempts: config.websocket?.reconnect?.attempts ?? 10,
|
|
@@ -42,6 +51,10 @@ export function resolveConfig(config = {}) {
|
|
|
42
51
|
integer(resolved.server.port, 0, 65535, "server.port");
|
|
43
52
|
integer(resolved.websocket.heartbeatInterval, 1, 2_147_483_647, "websocket.heartbeatInterval");
|
|
44
53
|
integer(resolved.websocket.maxPayload, 1, 1_073_741_824, "websocket.maxPayload");
|
|
54
|
+
integer(resolved.server.requestTimeout, 1, 900_000, "server.requestTimeout");
|
|
55
|
+
integer(resolved.server.shutdownTimeout, 100, 120_000, "server.shutdownTimeout");
|
|
56
|
+
integer(resolved.build.clientAssetWarningLimit, 1_024, 100_000_000, "build.clientAssetWarningLimit");
|
|
57
|
+
integer(resolved.websocket.maxMessagesPerSecond, 1, 10_000, "websocket.maxMessagesPerSecond");
|
|
45
58
|
integer(resolved.websocket.reconnect.attempts, 0, 10000, "websocket.reconnect.attempts");
|
|
46
59
|
integer(resolved.websocket.reconnect.initialDelay, 0, 2_147_483_647, "websocket.reconnect.initialDelay");
|
|
47
60
|
integer(resolved.websocket.reconnect.maxDelay, resolved.websocket.reconnect.initialDelay, 2_147_483_647, "websocket.reconnect.maxDelay");
|
|
@@ -57,10 +70,9 @@ export function resolveConfig(config = {}) {
|
|
|
57
70
|
fail("discord.auth.session");
|
|
58
71
|
if (!Array.isArray(resolved.discord.scopes) || resolved.discord.scopes.some((scope) => typeof scope !== "string" || !scope.trim()))
|
|
59
72
|
fail("discord.scopes");
|
|
60
|
-
for (const [field, value] of Object.entries({ "discord.auth.required": resolved.discord.auth.required, "discord.auth.proxyVerification": resolved.discord.auth.proxyVerification, "discord.auth.activityInstanceVerification": resolved.discord.auth.activityInstanceVerification, "websocket.enabled": resolved.websocket.enabled, "websocket.compression": resolved.websocket.compression, "websocket.reconnect.enabled": resolved.websocket.reconnect.enabled })) {
|
|
73
|
+
for (const [field, value] of Object.entries({ "discord.auth.required": resolved.discord.auth.required, "discord.auth.proxyVerification": resolved.discord.auth.proxyVerification, "discord.auth.activityInstanceVerification": resolved.discord.auth.activityInstanceVerification, "react.strictMode": resolved.react.strictMode, "websocket.enabled": resolved.websocket.enabled, "websocket.compression": resolved.websocket.compression, "websocket.reconnect.enabled": resolved.websocket.reconnect.enabled })) {
|
|
61
74
|
if (typeof value !== "boolean")
|
|
62
75
|
fail(field);
|
|
63
76
|
}
|
|
64
77
|
return resolved;
|
|
65
78
|
}
|
|
66
|
-
//# sourceMappingURL=defaults.js.map
|
package/dist/config/index.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
1
|
export { defineConfig } from "./types.js";
|
|
2
2
|
export type { LudicordActivityConfig, LudicordAuthConfig, LudicordConfig, LudicordDiscordConfig, LudicordReconnectConfig, LudicordServerConfig, LudicordServerLimits, LudicordWebSocketConfig, OutsideDiscordBehavior, ResolvedLudicordConfig, } from "./types.js";
|
|
3
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/config/index.js
CHANGED
package/dist/config/loader.d.ts
CHANGED
package/dist/config/loader.js
CHANGED
package/dist/config/types.d.ts
CHANGED
|
@@ -14,6 +14,14 @@ export interface LudicordActivityConfig {
|
|
|
14
14
|
defaultEmbed?: string;
|
|
15
15
|
outsideDiscord?: OutsideDiscordBehavior;
|
|
16
16
|
}
|
|
17
|
+
export interface LudicordReactConfig {
|
|
18
|
+
/** Enables React development checks without changing production behavior. */
|
|
19
|
+
strictMode?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export interface LudicordBuildConfig {
|
|
22
|
+
/** Warn when an individual emitted client asset exceeds this many bytes. */
|
|
23
|
+
clientAssetWarningLimit?: number;
|
|
24
|
+
}
|
|
17
25
|
export interface LudicordServerLimits {
|
|
18
26
|
body?: string;
|
|
19
27
|
}
|
|
@@ -22,6 +30,10 @@ export interface LudicordServerConfig {
|
|
|
22
30
|
host?: string;
|
|
23
31
|
allowedHosts?: true | readonly string[];
|
|
24
32
|
limits?: LudicordServerLimits;
|
|
33
|
+
/** Maximum duration of an API route handler in milliseconds. */
|
|
34
|
+
requestTimeout?: number;
|
|
35
|
+
/** Maximum graceful-shutdown duration before remaining connections close. */
|
|
36
|
+
shutdownTimeout?: number;
|
|
25
37
|
}
|
|
26
38
|
export interface LudicordReconnectConfig {
|
|
27
39
|
enabled?: boolean;
|
|
@@ -34,11 +46,15 @@ export interface LudicordWebSocketConfig {
|
|
|
34
46
|
heartbeatInterval?: number;
|
|
35
47
|
maxPayload?: number;
|
|
36
48
|
compression?: boolean;
|
|
49
|
+
/** Per-client inbound event limit for each one-second window. */
|
|
50
|
+
maxMessagesPerSecond?: number;
|
|
37
51
|
reconnect?: LudicordReconnectConfig;
|
|
38
52
|
}
|
|
39
53
|
export interface LudicordConfig {
|
|
40
54
|
discord?: LudicordDiscordConfig;
|
|
41
55
|
activity?: LudicordActivityConfig;
|
|
56
|
+
react?: LudicordReactConfig;
|
|
57
|
+
build?: LudicordBuildConfig;
|
|
42
58
|
server?: LudicordServerConfig;
|
|
43
59
|
websocket?: LudicordWebSocketConfig;
|
|
44
60
|
}
|
|
@@ -57,6 +73,12 @@ export interface ResolvedLudicordConfig {
|
|
|
57
73
|
defaultEmbed: string;
|
|
58
74
|
outsideDiscord: OutsideDiscordBehavior;
|
|
59
75
|
};
|
|
76
|
+
react: {
|
|
77
|
+
strictMode: boolean;
|
|
78
|
+
};
|
|
79
|
+
build: {
|
|
80
|
+
clientAssetWarningLimit: number;
|
|
81
|
+
};
|
|
60
82
|
server: {
|
|
61
83
|
port: number;
|
|
62
84
|
host: string;
|
|
@@ -64,12 +86,15 @@ export interface ResolvedLudicordConfig {
|
|
|
64
86
|
limits: {
|
|
65
87
|
body: string;
|
|
66
88
|
};
|
|
89
|
+
requestTimeout: number;
|
|
90
|
+
shutdownTimeout: number;
|
|
67
91
|
};
|
|
68
92
|
websocket: {
|
|
69
93
|
enabled: boolean;
|
|
70
94
|
heartbeatInterval: number;
|
|
71
95
|
maxPayload: number;
|
|
72
96
|
compression: boolean;
|
|
97
|
+
maxMessagesPerSecond: number;
|
|
73
98
|
reconnect: {
|
|
74
99
|
enabled: boolean;
|
|
75
100
|
attempts: number;
|
|
@@ -79,4 +104,3 @@ export interface ResolvedLudicordConfig {
|
|
|
79
104
|
};
|
|
80
105
|
}
|
|
81
106
|
export declare function defineConfig<const TConfig extends LudicordConfig>(config: TConfig): TConfig;
|
|
82
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/config/types.js
CHANGED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function discordUserAvatarUrl(userId: string, avatar: string | null | undefined): string | null;
|
|
2
|
+
export declare function discordUserBannerUrl(userId: string, banner: string | null | undefined): string | null;
|
|
3
|
+
export declare function discordGuildIconUrl(guildId: string, icon: string | null | undefined): string | null;
|
|
4
|
+
export declare function discordGuildMemberAvatarUrl(guildId: string, userId: string, avatar: string | null | undefined): string | null;
|
|
5
|
+
export declare function discordAvatarDecorationUrl(asset: string | null | undefined): string | null;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
function cdnAsset(path, hash) {
|
|
2
|
+
if (hash === null || hash === undefined || hash === "")
|
|
3
|
+
return null;
|
|
4
|
+
if (/^https?:\/\//i.test(hash))
|
|
5
|
+
return hash;
|
|
6
|
+
const extension = hash.startsWith("a_") ? "gif" : "png";
|
|
7
|
+
return `https://cdn.discordapp.com/${path}/${encodeURIComponent(hash)}.${extension}`;
|
|
8
|
+
}
|
|
9
|
+
export function discordUserAvatarUrl(userId, avatar) {
|
|
10
|
+
return cdnAsset(`avatars/${encodeURIComponent(userId)}`, avatar);
|
|
11
|
+
}
|
|
12
|
+
export function discordUserBannerUrl(userId, banner) {
|
|
13
|
+
return cdnAsset(`banners/${encodeURIComponent(userId)}`, banner);
|
|
14
|
+
}
|
|
15
|
+
export function discordGuildIconUrl(guildId, icon) {
|
|
16
|
+
return cdnAsset(`icons/${encodeURIComponent(guildId)}`, icon);
|
|
17
|
+
}
|
|
18
|
+
export function discordGuildMemberAvatarUrl(guildId, userId, avatar) {
|
|
19
|
+
if (guildId === "")
|
|
20
|
+
return null;
|
|
21
|
+
return cdnAsset(`guilds/${encodeURIComponent(guildId)}/users/${encodeURIComponent(userId)}/avatars`, avatar);
|
|
22
|
+
}
|
|
23
|
+
export function discordAvatarDecorationUrl(asset) {
|
|
24
|
+
if (asset === null || asset === undefined || asset === "")
|
|
25
|
+
return null;
|
|
26
|
+
if (/^https?:\/\//i.test(asset))
|
|
27
|
+
return asset;
|
|
28
|
+
return `https://cdn.discordapp.com/avatar-decoration-presets/${encodeURIComponent(asset)}.png`;
|
|
29
|
+
}
|
package/dist/discord/data.d.ts
CHANGED
|
@@ -1,21 +1,57 @@
|
|
|
1
1
|
import type { DiscordSDK } from "@discord/embedded-app-sdk";
|
|
2
|
-
import type { LudicordGuildMember } from "./events.js";
|
|
2
|
+
import type { LudicordDiscordChannel, LudicordGuildMember } from "./events.js";
|
|
3
|
+
export interface LudicordDiscordRole {
|
|
4
|
+
readonly id: string;
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly permissions?: string;
|
|
7
|
+
readonly position?: number;
|
|
8
|
+
readonly color?: number;
|
|
9
|
+
readonly managed?: boolean;
|
|
10
|
+
readonly raw: Readonly<Record<string, unknown>>;
|
|
11
|
+
}
|
|
12
|
+
export type LudicordDiscordResourceStatus = "idle" | "loading" | "ready" | "error";
|
|
13
|
+
export interface LudicordDiscordResource<T> {
|
|
14
|
+
readonly data: T;
|
|
15
|
+
readonly status: LudicordDiscordResourceStatus;
|
|
16
|
+
readonly error: Error | null;
|
|
17
|
+
readonly updatedAt: number | null;
|
|
18
|
+
refresh(): void;
|
|
19
|
+
}
|
|
20
|
+
export declare function refreshDiscordData(): Promise<void>;
|
|
3
21
|
type Commands = DiscordSDK["commands"];
|
|
4
22
|
export type LudicordDiscordCommand = Exclude<keyof Commands, "authorize" | "authenticate">;
|
|
5
23
|
export declare function useDiscordCommands(): {
|
|
6
24
|
supports(name: LudicordDiscordCommand): boolean;
|
|
7
25
|
call<T extends LudicordDiscordCommand>(name: T, ...args: Parameters<Commands[T]>): Promise<Awaited<ReturnType<Commands[T]>>>;
|
|
8
26
|
};
|
|
27
|
+
export declare function useDiscordGuildChannels(options?: {
|
|
28
|
+
readonly enabled?: boolean;
|
|
29
|
+
}): LudicordDiscordResource<readonly LudicordDiscordChannel[]>;
|
|
30
|
+
export declare function useDiscordGuildRoles(options?: {
|
|
31
|
+
readonly enabled?: boolean;
|
|
32
|
+
}): LudicordDiscordResource<readonly LudicordDiscordRole[]>;
|
|
33
|
+
export declare function useDiscordGuildMember(userId?: string, options?: {
|
|
34
|
+
readonly enabled?: boolean;
|
|
35
|
+
}): LudicordDiscordResource<LudicordGuildMember | null>;
|
|
36
|
+
export declare function useDiscordPermissions(): {
|
|
37
|
+
readonly guild: string | null;
|
|
38
|
+
readonly channel: string | null;
|
|
39
|
+
readonly member: string | null;
|
|
40
|
+
readonly hasGuild: (permission: bigint | number | string) => boolean;
|
|
41
|
+
readonly hasChannel: (permission: bigint | number | string) => boolean;
|
|
42
|
+
readonly hasMember: (permission: bigint | number | string) => boolean;
|
|
43
|
+
};
|
|
9
44
|
export declare function useDiscordGuildMembers(options?: {
|
|
10
45
|
readonly limit?: number;
|
|
11
46
|
readonly enabled?: boolean;
|
|
12
47
|
}): {
|
|
48
|
+
readonly data: readonly LudicordGuildMember[];
|
|
13
49
|
readonly members: readonly LudicordGuildMember[];
|
|
14
50
|
readonly status: "idle" | "ready" | "error" | "loading";
|
|
15
51
|
readonly error: Error | null;
|
|
52
|
+
readonly updatedAt: number | null;
|
|
16
53
|
readonly hasMore: boolean;
|
|
17
54
|
readonly refresh: () => void;
|
|
18
55
|
readonly loadMore: () => void;
|
|
19
56
|
};
|
|
20
57
|
export {};
|
|
21
|
-
//# sourceMappingURL=data.d.ts.map
|
package/dist/discord/data.js
CHANGED
|
@@ -1,7 +1,49 @@
|
|
|
1
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
2
2
|
import { useDiscord } from "./runtime.js";
|
|
3
|
-
import { addDiscordDiagnostic, useDiscordGuild } from "./events.js";
|
|
4
|
-
import { useAuthStatus } from "../auth/client.js";
|
|
3
|
+
import { addDiscordDiagnostic, channelTypeName, enrichDiscordContext, useDiscordData, useDiscordGuild } from "./events.js";
|
|
4
|
+
import { useAuthStatus, useLudicordSession } from "../auth/client.js";
|
|
5
|
+
import { discordGuildMemberAvatarUrl, discordUserAvatarUrl } from "./assets.js";
|
|
6
|
+
const refreshListeners = new Set();
|
|
7
|
+
let refreshRevision = 0;
|
|
8
|
+
const subscribeRefresh = (listener) => { refreshListeners.add(listener); return () => refreshListeners.delete(listener); };
|
|
9
|
+
const getRefreshRevision = () => refreshRevision;
|
|
10
|
+
const notifyRefresh = () => { refreshRevision += 1; for (const listener of refreshListeners)
|
|
11
|
+
listener(); };
|
|
12
|
+
export async function refreshDiscordData() {
|
|
13
|
+
let failure = null;
|
|
14
|
+
try {
|
|
15
|
+
const response = await fetch("/_ludicord/auth/context", { credentials: "include", cache: "no-store" });
|
|
16
|
+
const body = await response.json();
|
|
17
|
+
if (!response.ok)
|
|
18
|
+
throw new Error(body.message ?? "Extended Discord context is unavailable.");
|
|
19
|
+
enrichDiscordContext(body);
|
|
20
|
+
}
|
|
21
|
+
catch (cause) {
|
|
22
|
+
failure = cause instanceof Error ? cause : new Error("Extended Discord context is unavailable.");
|
|
23
|
+
addDiscordDiagnostic(failure.message);
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
notifyRefresh();
|
|
27
|
+
}
|
|
28
|
+
if (failure !== null)
|
|
29
|
+
throw failure;
|
|
30
|
+
}
|
|
31
|
+
function normalizeMember(member, guildId) {
|
|
32
|
+
return {
|
|
33
|
+
user: {
|
|
34
|
+
id: member.user.id,
|
|
35
|
+
username: member.user.username,
|
|
36
|
+
displayName: member.user.global_name ?? member.user.username,
|
|
37
|
+
avatar: discordUserAvatarUrl(member.user.id, member.user.avatar),
|
|
38
|
+
raw: { ...member.user },
|
|
39
|
+
},
|
|
40
|
+
nickname: member.nick ?? null,
|
|
41
|
+
avatar: discordGuildMemberAvatarUrl(guildId, member.user.id, typeof member.avatar === "string" ? member.avatar : null),
|
|
42
|
+
roles: member.roles ?? [],
|
|
43
|
+
...(member.joined_at === undefined ? {} : { joinedAt: member.joined_at }),
|
|
44
|
+
raw: { ...member },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
5
47
|
export function useDiscordCommands() {
|
|
6
48
|
const discord = useDiscord();
|
|
7
49
|
return useMemo(() => ({
|
|
@@ -24,6 +66,132 @@ export function useDiscordCommands() {
|
|
|
24
66
|
},
|
|
25
67
|
}), [discord.sdk, discord.status]);
|
|
26
68
|
}
|
|
69
|
+
function useDiscordServerResource(options) {
|
|
70
|
+
const globalRevision = useSyncExternalStore(subscribeRefresh, getRefreshRevision, getRefreshRevision);
|
|
71
|
+
const [localRevision, setLocalRevision] = useState(0);
|
|
72
|
+
const [data, setData] = useState(options.initialData);
|
|
73
|
+
const [status, setStatus] = useState("idle");
|
|
74
|
+
const [error, setError] = useState(null);
|
|
75
|
+
const [updatedAt, setUpdatedAt] = useState(null);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (options.url === null) {
|
|
78
|
+
setData(options.initialData);
|
|
79
|
+
setStatus("idle");
|
|
80
|
+
setError(null);
|
|
81
|
+
setUpdatedAt(null);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
const controller = new AbortController();
|
|
85
|
+
setStatus("loading");
|
|
86
|
+
setError(null);
|
|
87
|
+
void fetch(options.url, { credentials: "include", cache: "no-store", signal: controller.signal })
|
|
88
|
+
.then(async (response) => {
|
|
89
|
+
const body = await response.json();
|
|
90
|
+
if (!response.ok)
|
|
91
|
+
throw new Error(body.message ?? "Discord server data is unavailable.");
|
|
92
|
+
if (controller.signal.aborted)
|
|
93
|
+
return;
|
|
94
|
+
setData(options.select(body));
|
|
95
|
+
setUpdatedAt(Date.now());
|
|
96
|
+
setStatus("ready");
|
|
97
|
+
})
|
|
98
|
+
.catch((cause) => {
|
|
99
|
+
if (controller.signal.aborted)
|
|
100
|
+
return;
|
|
101
|
+
const failure = cause instanceof Error ? cause : new Error("Discord server data is unavailable.");
|
|
102
|
+
setError(failure);
|
|
103
|
+
setStatus("error");
|
|
104
|
+
addDiscordDiagnostic(failure.message);
|
|
105
|
+
});
|
|
106
|
+
return () => controller.abort();
|
|
107
|
+
}, [globalRevision, localRevision, options.initialData, options.select, options.url]);
|
|
108
|
+
const refresh = useCallback(() => setLocalRevision((value) => value + 1), []);
|
|
109
|
+
return { data, status, error, updatedAt, refresh };
|
|
110
|
+
}
|
|
111
|
+
const EMPTY_CHANNELS = [];
|
|
112
|
+
const EMPTY_ROLES = [];
|
|
113
|
+
export function useDiscordGuildChannels(options = {}) {
|
|
114
|
+
const guild = useDiscordGuild();
|
|
115
|
+
const authenticated = useAuthStatus() === "authenticated";
|
|
116
|
+
const enabled = options.enabled !== false && authenticated && guild !== null;
|
|
117
|
+
const select = useCallback((body) => (body.channels ?? []).map((channel) => {
|
|
118
|
+
const guildId = channel.guild_id ?? guild?.id;
|
|
119
|
+
return {
|
|
120
|
+
id: channel.id,
|
|
121
|
+
type: channel.type,
|
|
122
|
+
kind: channelTypeName(channel.type),
|
|
123
|
+
isVoice: channel.type === 2 || channel.type === 13,
|
|
124
|
+
...(channel.name === undefined ? {} : { name: channel.name }),
|
|
125
|
+
...(channel.topic == null ? {} : { topic: channel.topic }),
|
|
126
|
+
...(guildId === undefined ? {} : { guildId }),
|
|
127
|
+
raw: { ...channel },
|
|
128
|
+
};
|
|
129
|
+
}), [guild?.id]);
|
|
130
|
+
return useDiscordServerResource({
|
|
131
|
+
url: enabled ? "/_ludicord/auth/guild-channels" : null,
|
|
132
|
+
initialData: EMPTY_CHANNELS,
|
|
133
|
+
select,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
export function useDiscordGuildRoles(options = {}) {
|
|
137
|
+
const guild = useDiscordGuild();
|
|
138
|
+
const authenticated = useAuthStatus() === "authenticated";
|
|
139
|
+
const enabled = options.enabled !== false && authenticated && guild !== null;
|
|
140
|
+
const select = useCallback((body) => (body.roles ?? []).map((role) => ({
|
|
141
|
+
id: role.id,
|
|
142
|
+
name: role.name,
|
|
143
|
+
...(role.permissions === undefined ? {} : { permissions: role.permissions }),
|
|
144
|
+
...(role.position === undefined ? {} : { position: role.position }),
|
|
145
|
+
...(role.color === undefined ? {} : { color: role.color }),
|
|
146
|
+
...(role.managed === undefined ? {} : { managed: role.managed }),
|
|
147
|
+
raw: { ...role },
|
|
148
|
+
})), []);
|
|
149
|
+
return useDiscordServerResource({
|
|
150
|
+
url: enabled ? "/_ludicord/auth/guild-roles" : null,
|
|
151
|
+
initialData: EMPTY_ROLES,
|
|
152
|
+
select,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
export function useDiscordGuildMember(userId, options = {}) {
|
|
156
|
+
const guild = useDiscordGuild();
|
|
157
|
+
const session = useLudicordSession();
|
|
158
|
+
const selectedUserId = userId ?? session?.user.id;
|
|
159
|
+
const authenticated = useAuthStatus() === "authenticated";
|
|
160
|
+
const enabled = options.enabled !== false && authenticated && guild !== null && selectedUserId !== undefined;
|
|
161
|
+
const select = useCallback((body) => body.member === undefined || guild === null ? null : normalizeMember(body.member, guild.id), [guild]);
|
|
162
|
+
return useDiscordServerResource({
|
|
163
|
+
url: enabled ? `/_ludicord/auth/guild-member/${encodeURIComponent(selectedUserId)}` : null,
|
|
164
|
+
initialData: null,
|
|
165
|
+
select,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
function hasPermission(value, permission) {
|
|
169
|
+
if (value === null)
|
|
170
|
+
return false;
|
|
171
|
+
try {
|
|
172
|
+
const required = BigInt(permission);
|
|
173
|
+
return (BigInt(value) & required) === required;
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
export function useDiscordPermissions() {
|
|
180
|
+
const permissions = useDiscordData().permissions;
|
|
181
|
+
return useMemo(() => {
|
|
182
|
+
const guildValue = permissions.guild;
|
|
183
|
+
const channelValue = permissions.channel;
|
|
184
|
+
const memberValue = permissions.member;
|
|
185
|
+
return {
|
|
186
|
+
guild: guildValue,
|
|
187
|
+
channel: channelValue,
|
|
188
|
+
member: memberValue,
|
|
189
|
+
hasGuild: (permission) => hasPermission(guildValue, permission),
|
|
190
|
+
hasChannel: (permission) => hasPermission(channelValue, permission),
|
|
191
|
+
hasMember: (permission) => hasPermission(memberValue, permission),
|
|
192
|
+
};
|
|
193
|
+
}, [permissions]);
|
|
194
|
+
}
|
|
27
195
|
export function useDiscordGuildMembers(options = {}) {
|
|
28
196
|
const guild = useDiscordGuild();
|
|
29
197
|
const auth = useAuthStatus();
|
|
@@ -33,13 +201,16 @@ export function useDiscordGuildMembers(options = {}) {
|
|
|
33
201
|
const [members, setMembers] = useState([]);
|
|
34
202
|
const [status, setStatus] = useState("idle");
|
|
35
203
|
const [error, setError] = useState(null);
|
|
204
|
+
const [updatedAt, setUpdatedAt] = useState(null);
|
|
36
205
|
const [next, setNext] = useState(null);
|
|
37
206
|
const [request, setRequest] = useState({ revision: 0, after: "" });
|
|
207
|
+
const globalRevision = useSyncExternalStore(subscribeRefresh, getRefreshRevision, getRefreshRevision);
|
|
38
208
|
useEffect(() => {
|
|
39
209
|
if (!enabled) {
|
|
40
210
|
setMembers([]);
|
|
41
211
|
setError(null);
|
|
42
212
|
setStatus("idle");
|
|
213
|
+
setUpdatedAt(null);
|
|
43
214
|
setNext(null);
|
|
44
215
|
previousGuild.current = undefined;
|
|
45
216
|
return;
|
|
@@ -65,13 +236,10 @@ export function useDiscordGuildMembers(options = {}) {
|
|
|
65
236
|
throw new Error(body.message ?? "Discord guild members are unavailable.");
|
|
66
237
|
if (abort.signal.aborted)
|
|
67
238
|
return;
|
|
68
|
-
const page = (body.members ?? []).map((member) => (
|
|
69
|
-
user: { id: member.user.id, username: member.user.username, displayName: member.user.global_name ?? member.user.username,
|
|
70
|
-
avatar: member.user.avatar ? `https://cdn.discordapp.com/avatars/${member.user.id}/${member.user.avatar}.png` : null },
|
|
71
|
-
nickname: member.nick ?? null, roles: member.roles ?? [], ...(member.joined_at === undefined ? {} : { joinedAt: member.joined_at }),
|
|
72
|
-
}));
|
|
239
|
+
const page = (body.members ?? []).map((member) => normalizeMember(member, guild?.id ?? ""));
|
|
73
240
|
setMembers((current) => after === "" ? page : [...current.filter((item) => !page.some((entry) => entry.user.id === item.user.id)), ...page]);
|
|
74
241
|
setNext(body.next ?? null);
|
|
242
|
+
setUpdatedAt(Date.now());
|
|
75
243
|
setStatus("ready");
|
|
76
244
|
}).catch((cause) => {
|
|
77
245
|
if (abort.signal.aborted)
|
|
@@ -82,10 +250,9 @@ export function useDiscordGuildMembers(options = {}) {
|
|
|
82
250
|
addDiscordDiagnostic(failure.message);
|
|
83
251
|
});
|
|
84
252
|
return () => abort.abort();
|
|
85
|
-
}, [enabled, guild?.id, limit, request]);
|
|
253
|
+
}, [enabled, globalRevision, guild?.id, limit, request]);
|
|
86
254
|
const refresh = useCallback(() => setRequest((current) => ({ revision: current.revision + 1, after: "" })), []);
|
|
87
255
|
const loadMore = useCallback(() => { if (next !== null && status !== "loading")
|
|
88
256
|
setRequest((current) => ({ revision: current.revision + 1, after: next })); }, [next, status]);
|
|
89
|
-
return { members, status, error, hasMore: next !== null, refresh, loadMore };
|
|
257
|
+
return { data: members, members, status, error, updatedAt, hasMore: next !== null, refresh, loadMore };
|
|
90
258
|
}
|
|
91
|
-
//# sourceMappingURL=data.js.map
|
package/dist/discord/events.d.ts
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import type { LudicordSessionUser } from "../auth/types.js";
|
|
2
2
|
import type { DiscordRuntimeSnapshot, DiscordSdkLike } from "./runtime.js";
|
|
3
|
+
import type { DiscordRawPayload } from "./payload.js";
|
|
4
|
+
export type LudicordDiscordEventName = "VOICE_STATE_UPDATE" | "SPEAKING_START" | "SPEAKING_STOP" | "ACTIVITY_LAYOUT_MODE_UPDATE" | "ORIENTATION_UPDATE" | "CURRENT_USER_UPDATE" | "CURRENT_GUILD_MEMBER_UPDATE" | "THERMAL_STATE_UPDATE" | "ACTIVITY_INSTANCE_PARTICIPANTS_UPDATE" | "ENTITLEMENT_CREATE";
|
|
3
5
|
export interface LudicordDiscordUser {
|
|
4
6
|
readonly id: string;
|
|
5
7
|
readonly username: string;
|
|
6
8
|
readonly displayName: string;
|
|
7
9
|
readonly avatar: string | null;
|
|
8
10
|
readonly locale?: string;
|
|
11
|
+
readonly raw: DiscordRawPayload;
|
|
9
12
|
}
|
|
10
13
|
export interface LudicordDiscordChannel {
|
|
11
14
|
readonly id: string;
|
|
@@ -15,6 +18,7 @@ export interface LudicordDiscordChannel {
|
|
|
15
18
|
readonly guildId?: string;
|
|
16
19
|
readonly kind?: string;
|
|
17
20
|
readonly isVoice?: boolean;
|
|
21
|
+
readonly raw: DiscordRawPayload;
|
|
18
22
|
}
|
|
19
23
|
export interface LudicordDiscordGuild {
|
|
20
24
|
readonly id: string;
|
|
@@ -23,6 +27,7 @@ export interface LudicordDiscordGuild {
|
|
|
23
27
|
readonly description?: string | null;
|
|
24
28
|
readonly memberCount?: number;
|
|
25
29
|
readonly features?: readonly string[];
|
|
30
|
+
readonly raw: DiscordRawPayload;
|
|
26
31
|
}
|
|
27
32
|
export interface LudicordGuildMember {
|
|
28
33
|
readonly user: LudicordDiscordUser;
|
|
@@ -30,12 +35,14 @@ export interface LudicordGuildMember {
|
|
|
30
35
|
readonly avatar?: string | null;
|
|
31
36
|
readonly roles: readonly string[];
|
|
32
37
|
readonly joinedAt?: string;
|
|
38
|
+
readonly raw: DiscordRawPayload;
|
|
33
39
|
}
|
|
34
40
|
export interface LudicordParticipant {
|
|
35
41
|
readonly id: string;
|
|
36
42
|
readonly username?: string;
|
|
37
43
|
readonly displayName?: string;
|
|
38
44
|
readonly avatar?: string | null;
|
|
45
|
+
readonly raw: DiscordRawPayload;
|
|
39
46
|
}
|
|
40
47
|
export interface LudicordVoiceState {
|
|
41
48
|
readonly userId: string;
|
|
@@ -43,6 +50,7 @@ export interface LudicordVoiceState {
|
|
|
43
50
|
readonly deaf?: boolean;
|
|
44
51
|
readonly selfMute?: boolean;
|
|
45
52
|
readonly selfDeaf?: boolean;
|
|
53
|
+
readonly raw: DiscordRawPayload;
|
|
46
54
|
}
|
|
47
55
|
export interface LudicordDiscordEventSnapshot {
|
|
48
56
|
readonly user: LudicordDiscordUser | null;
|
|
@@ -60,6 +68,12 @@ export interface LudicordDiscordEventSnapshot {
|
|
|
60
68
|
readonly channelMembers: readonly LudicordDiscordUser[];
|
|
61
69
|
readonly currentGuildMember: LudicordGuildMember | null;
|
|
62
70
|
readonly entitlements: readonly Readonly<Record<string, unknown>>[];
|
|
71
|
+
readonly permissions: {
|
|
72
|
+
readonly guild: string | null;
|
|
73
|
+
readonly channel: string | null;
|
|
74
|
+
readonly member: string | null;
|
|
75
|
+
};
|
|
76
|
+
readonly rawEvents: Readonly<Partial<Record<LudicordDiscordEventName, unknown>>>;
|
|
63
77
|
}
|
|
64
78
|
export declare function channelTypeName(type: number): string;
|
|
65
79
|
export declare function addDiscordDiagnostic(message: string): void;
|
|
@@ -74,6 +88,7 @@ export declare function enrichDiscordContext(context: {
|
|
|
74
88
|
description?: string | null;
|
|
75
89
|
approximate_member_count?: number;
|
|
76
90
|
features?: readonly string[];
|
|
91
|
+
readonly [key: string]: unknown;
|
|
77
92
|
};
|
|
78
93
|
channel?: {
|
|
79
94
|
id: string;
|
|
@@ -81,13 +96,25 @@ export declare function enrichDiscordContext(context: {
|
|
|
81
96
|
type: number;
|
|
82
97
|
guild_id?: string;
|
|
83
98
|
topic?: string | null;
|
|
99
|
+
readonly [key: string]: unknown;
|
|
100
|
+
};
|
|
101
|
+
member?: Readonly<Record<string, unknown>>;
|
|
102
|
+
permissions?: {
|
|
103
|
+
readonly guild?: string | null;
|
|
104
|
+
readonly channel?: string | null;
|
|
105
|
+
readonly member?: string | null;
|
|
84
106
|
};
|
|
85
107
|
}): void;
|
|
86
|
-
export declare function setDiscordGuildFromAuth(guildId: string | undefined, guildName: string | undefined, guildIcon?: string | null): void;
|
|
108
|
+
export declare function setDiscordGuildFromAuth(guildId: string | undefined, guildName: string | undefined, guildIcon?: string | null, raw?: DiscordRawPayload): void;
|
|
87
109
|
export declare function diagnoseDiscordScope(scope: string, feature: string): void;
|
|
88
110
|
export declare function useDiscordUser(): LudicordDiscordUser | null;
|
|
89
111
|
export declare function useDiscordChannel(): LudicordDiscordChannel | null;
|
|
90
112
|
export declare function useDiscordGuild(): LudicordDiscordGuild | null;
|
|
113
|
+
export declare function useDiscordData(): LudicordDiscordEventSnapshot;
|
|
114
|
+
export declare function useDiscordRawEvent(event: LudicordDiscordEventName): unknown;
|
|
115
|
+
export declare function useDiscordEvent<TPayload = unknown>(event: LudicordDiscordEventName, listener: (payload: TPayload) => void, options?: {
|
|
116
|
+
readonly replayLatest?: boolean;
|
|
117
|
+
}): void;
|
|
91
118
|
export declare function useParticipants(): readonly LudicordParticipant[];
|
|
92
119
|
export declare function useChannelMembers(): readonly LudicordDiscordUser[];
|
|
93
120
|
export declare function useCurrentGuildMember(): LudicordGuildMember | null;
|
|
@@ -103,4 +130,3 @@ export declare function useDiscordLocale(): string | null;
|
|
|
103
130
|
export declare function useDiscordDiagnostics(): readonly string[];
|
|
104
131
|
export declare function getDiscordEventSnapshot(): LudicordDiscordEventSnapshot;
|
|
105
132
|
export declare function resetDiscordEventStoreForTesting(): Promise<void>;
|
|
106
|
-
//# sourceMappingURL=events.d.ts.map
|