privage.js 0.21.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.
Files changed (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +503 -0
  3. package/dist/Client.d.ts +192 -0
  4. package/dist/Client.js +904 -0
  5. package/dist/attachments.d.ts +41 -0
  6. package/dist/attachments.js +63 -0
  7. package/dist/collectors.d.ts +41 -0
  8. package/dist/collectors.js +52 -0
  9. package/dist/commands.d.ts +108 -0
  10. package/dist/commands.js +216 -0
  11. package/dist/compat.d.ts +72 -0
  12. package/dist/compat.js +75 -0
  13. package/dist/components.d.ts +137 -0
  14. package/dist/components.js +226 -0
  15. package/dist/embeds.d.ts +97 -0
  16. package/dist/embeds.js +125 -0
  17. package/dist/errors.d.ts +32 -0
  18. package/dist/errors.js +48 -0
  19. package/dist/formatters.d.ts +16 -0
  20. package/dist/formatters.js +43 -0
  21. package/dist/index.d.ts +33 -0
  22. package/dist/index.js +101 -0
  23. package/dist/intents.d.ts +28 -0
  24. package/dist/intents.js +33 -0
  25. package/dist/modals.d.ts +96 -0
  26. package/dist/modals.js +162 -0
  27. package/dist/rest.d.ts +208 -0
  28. package/dist/rest.js +356 -0
  29. package/dist/structures/Channel.d.ts +25 -0
  30. package/dist/structures/Channel.js +34 -0
  31. package/dist/structures/Interaction.d.ts +149 -0
  32. package/dist/structures/Interaction.js +170 -0
  33. package/dist/structures/Member.d.ts +35 -0
  34. package/dist/structures/Member.js +55 -0
  35. package/dist/structures/Message.d.ts +181 -0
  36. package/dist/structures/Message.js +191 -0
  37. package/dist/structures/Role.d.ts +17 -0
  38. package/dist/structures/Role.js +20 -0
  39. package/dist/structures/Server.d.ts +29 -0
  40. package/dist/structures/Server.js +36 -0
  41. package/dist/structures/ServerChannels.d.ts +24 -0
  42. package/dist/structures/ServerChannels.js +50 -0
  43. package/dist/types.d.ts +195 -0
  44. package/dist/types.js +2 -0
  45. package/package.json +49 -0
@@ -0,0 +1,192 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Rest } from './rest';
3
+ import type { KickBanOptions, TimeoutOptions, FetchMessagesOptions, FetchMembersOptions, PresenceStatus, Activity, SendMessageOptions, EditMessageOptions, CreatePollOptions, CreateWebhookOptions, EditWebhookOptions } from './rest';
4
+ import { CommandsManager } from './commands';
5
+ import { MessageCollector, type MessageCollectorOptions } from './collectors';
6
+ import { Message } from './structures/Message';
7
+ import { Member } from './structures/Member';
8
+ import { Server } from './structures/Server';
9
+ import { Channel } from './structures/Channel';
10
+ import { Role } from './structures/Role';
11
+ import type { ClientOptions, ClientEvents, BotUser } from './types';
12
+ /**
13
+ * A Privage bot connection. Owns the full lifecycle — auth, ticket, socket,
14
+ * topic joins, `session.open`/`session.resume`, heartbeat, reconnect with
15
+ * backoff, dedupe, and transparent resume + REST catchup — and surfaces
16
+ * friendly, camelCase events. The wire protocol never leaks into the public
17
+ * API (see the protocol spec §12).
18
+ */
19
+ export interface Client {
20
+ on<K extends keyof ClientEvents>(event: K, listener: (...args: ClientEvents[K]) => void): this;
21
+ once<K extends keyof ClientEvents>(event: K, listener: (...args: ClientEvents[K]) => void): this;
22
+ off<K extends keyof ClientEvents>(event: K, listener: (...args: ClientEvents[K]) => void): this;
23
+ emit<K extends keyof ClientEvents>(event: K, ...args: ClientEvents[K]): boolean;
24
+ }
25
+ export declare class Client extends EventEmitter {
26
+ /** REST helper, available after `login()`. */
27
+ rest: Rest;
28
+ /** The bot's own identity, populated once connected. */
29
+ user: BotUser | null;
30
+ /**
31
+ * Slash-command registry — `client.commands.set([...])` bulk-overwrites,
32
+ * `client.commands.fetch()` lists. Use after `login()`. Invocations arrive
33
+ * as `interaction` events with `kind: 'command'`.
34
+ */
35
+ readonly commands: CommandsManager;
36
+ /**
37
+ * Server name cache — hydrated from `GET /servers` on connect, kept fresh
38
+ * by `server-meta-updated`/`server-deleted` (requires `Intents.Servers`).
39
+ * Capped at {@link SERVER_CACHE_CAP}, oldest entries evict first.
40
+ */
41
+ readonly servers: ReadonlyMap<string, Server>;
42
+ /**
43
+ * Channel name cache — hydrated per-server in the background after connect,
44
+ * kept fresh by `channel-created`/`channel-updated`/`channel-deleted`
45
+ * (requires `Intents.Servers`), and lazily filled on cache miss (see
46
+ * `ensureChannel`). Capped at {@link CHANNEL_CACHE_CAP}, oldest entries evict first.
47
+ */
48
+ readonly channels: ReadonlyMap<string, Channel>;
49
+ private readonly pendingChannelFetches;
50
+ private readonly pendingServerFetches;
51
+ private token;
52
+ private readonly api;
53
+ private readonly wsUrl;
54
+ private readonly intents;
55
+ private readonly debugMode;
56
+ private readonly streamId;
57
+ private socket;
58
+ private reconnectAttempts;
59
+ private reconnectPending;
60
+ private destroyed;
61
+ private connectedOnce;
62
+ private readyAtMs;
63
+ private loginResolve?;
64
+ private loginReject?;
65
+ private streamToken;
66
+ private resumeCursor;
67
+ private readonly topicCursors;
68
+ private readonly seen;
69
+ private readonly seenSet;
70
+ private readonly channelByMessage;
71
+ private readonly channelByMessageKeys;
72
+ constructor(options: ClientOptions);
73
+ /** Authenticate and connect. Resolves once the session is open (`ready`); rejects on a fatal auth error. */
74
+ login(token: string): Promise<void>;
75
+ /**
76
+ * True once `ready` has fired. Stays true across reconnects — it means
77
+ * "ready happened", not "currently connected" (discord.js semantics).
78
+ */
79
+ isReady(): boolean;
80
+ /** When `ready` fired, or `null` before login completes. */
81
+ get readyAt(): Date | null;
82
+ /** Epoch-ms twin of {@link readyAt}. */
83
+ get readyTimestamp(): number | null;
84
+ /** Milliseconds since `ready`, or `null` before login completes. */
85
+ get uptime(): number | null;
86
+ /** Disconnect and stop reconnecting. */
87
+ destroy(): void;
88
+ /** Grant a role to a member. Requires the bot to have MANAGE_ROLES and the role to sit below the bot's highest. */
89
+ addRole(serverId: string, userId: string, roleId: string): Promise<any>;
90
+ /** Remove a role from a member. */
91
+ removeRole(serverId: string, userId: string, roleId: string): Promise<any>;
92
+ /** Kick a member (needs KICK_MEMBERS). */
93
+ kick(serverId: string, userId: string, opts?: KickBanOptions): Promise<any>;
94
+ /** Ban a member (needs BAN_MEMBERS). */
95
+ ban(serverId: string, userId: string, opts?: KickBanOptions): Promise<any>;
96
+ /** Lift a ban. */
97
+ unban(serverId: string, userId: string): Promise<any>;
98
+ /** Time out a member (needs TIMEOUT_MEMBERS). */
99
+ timeout(serverId: string, userId: string, opts: TimeoutOptions): Promise<any>;
100
+ /** Remove a member's timeout. */
101
+ removeTimeout(serverId: string, userId: string): Promise<any>;
102
+ /** Delete a message (needs MANAGE_MESSAGES, or the bot's own message). */
103
+ deleteMessage(messageId: string): Promise<any>;
104
+ /** Send a message to a channel by id — no server id needed. */
105
+ send(channelId: string, content: string | Omit<SendMessageOptions, 'replyTo'>): Promise<any>;
106
+ /** React to a message. */
107
+ react(messageId: string, emoji: string): Promise<any>;
108
+ /** Remove the bot's own reaction from a message. */
109
+ removeReaction(messageId: string, emoji: string): Promise<any>;
110
+ /** Create a poll in a channel (needs send permission there). Limits: question ≤500, 2–6 unique options ≤200, deadline ≤7 days. */
111
+ createPoll(channelId: string, opts: CreatePollOptions): Promise<any>;
112
+ /** Close a poll early (creator or MANAGE_MESSAGES). A `pollClose` event follows. */
113
+ closePoll(pollId: string): Promise<any>;
114
+ /** Create a webhook on a text/announcement channel (needs MANAGE_WEBHOOKS). Response carries the webhook token. */
115
+ createWebhook(channelId: string, opts?: CreateWebhookOptions): Promise<any>;
116
+ /** List a channel's webhooks. */
117
+ fetchChannelWebhooks(channelId: string): Promise<any>;
118
+ /** List a server's webhooks. */
119
+ fetchServerWebhooks(serverId: string): Promise<any>;
120
+ /** Rename a webhook / change its avatar (`avatarUrl: null` clears). */
121
+ editWebhook(webhookId: string, opts: EditWebhookOptions): Promise<any>;
122
+ /** Delete a webhook. */
123
+ deleteWebhook(webhookId: string): Promise<any>;
124
+ /** Rotate a webhook's token — the old one stops working immediately. */
125
+ rotateWebhookToken(webhookId: string): Promise<any>;
126
+ /** Collect messages matching a filter until a count/time limit — see MessageCollector. */
127
+ createMessageCollector(options?: MessageCollectorOptions): MessageCollector;
128
+ /** Promise form of a collector: resolves with collected messages (default max 1, 60s). */
129
+ awaitMessages(options?: MessageCollectorOptions): Promise<Message[]>;
130
+ /** Set the bot's presence status ('online' | 'idle' | 'dnd' | 'offline'). */
131
+ setStatus(status: PresenceStatus): Promise<any>;
132
+ /** Set the bot's rich-presence activity (shows on the profile/member list). */
133
+ setActivity(activity: Activity): Promise<any>;
134
+ /** Clear the bot's rich-presence activity. */
135
+ clearActivity(): Promise<any>;
136
+ /** Edit a message's content. Own messages only (403 otherwise). */
137
+ editMessage(messageId: string, content: string | EditMessageOptions): Promise<any>;
138
+ /** Pin a message. */
139
+ pinMessage(messageId: string): Promise<any>;
140
+ /** Unpin a message. */
141
+ unpinMessage(messageId: string): Promise<any>;
142
+ /** Fetch a channel's message history. */
143
+ fetchMessages(channelId: string, opts?: FetchMessagesOptions): Promise<Message[]>;
144
+ /** List a server's members. */
145
+ fetchMembers(serverId: string, opts?: FetchMembersOptions): Promise<Member[]>;
146
+ /** List a server's roles. */
147
+ fetchRoles(serverId: string): Promise<Role[]>;
148
+ /** Set (or clear, with `null`) a member's nickname. Others need MANAGE_NICKNAMES. */
149
+ setNickname(serverId: string, userId: string, nickname: string | null): Promise<any>;
150
+ private debug;
151
+ private warn;
152
+ private isFatal;
153
+ private scheduleReconnect;
154
+ private connect;
155
+ private handshake;
156
+ /** `GET /servers` → populate the server cache. Best-effort — never fatal. */
157
+ private hydrateServers;
158
+ /**
159
+ * Per-server channel list, concurrency-limited and run in the background
160
+ * (never awaited by `handshake`). Best-effort — a slow or failing server's
161
+ * channel list never breaks the connection or delays other servers.
162
+ */
163
+ private hydrateChannels;
164
+ /**
165
+ * Lazily resolve an uncached channel over REST (deduped per id, fire-and-forget).
166
+ * Called automatically on a cache-miss message so the *next* message in that
167
+ * channel resolves — useful for brand-new channels or bots that didn't
168
+ * declare `Intents.Servers`. Safe to call redundantly; a no-op if already
169
+ * cached or already in flight.
170
+ */
171
+ ensureChannel(channelId: string, serverId?: string | null): void;
172
+ /** Best-effort fill for a server discovered via a lazily-resolved channel. */
173
+ private ensureServer;
174
+ /** Cache-maintenance for structure events. Cache-only — never emits new SDK events. */
175
+ private updateCaches;
176
+ /** Insert, evicting the oldest entry (by insertion order) once at capacity. */
177
+ private cacheSet;
178
+ private join;
179
+ private sessionOpen;
180
+ private sessionResume;
181
+ /** Send a `session.*` RPC and resolve its response `d` (rejects on `error`/timeout). */
182
+ private rpc;
183
+ /** Backfill per-topic gaps the resume buffer couldn't cover. Best-effort — never fatal. */
184
+ private restCatchup;
185
+ private handleEvent;
186
+ private advanceCursors;
187
+ private topicOf;
188
+ private dispatchMessage;
189
+ private rememberChannel;
190
+ private safeEmit;
191
+ private remember;
192
+ }