micup.ts 1.0.0 → 1.1.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 +93 -38
- package/README.tr.md +137 -0
- package/dist/bot/client.d.ts +79 -0
- package/dist/bot/client.js +299 -0
- package/dist/events/background.d.ts +7 -0
- package/dist/events/background.js +23 -0
- package/dist/events/catalog.d.ts +243 -0
- package/dist/events/catalog.js +1 -0
- package/dist/events/guild-tag.d.ts +9 -0
- package/dist/events/guild-tag.js +38 -0
- package/dist/events/mux.d.ts +20 -5
- package/dist/events/names.d.ts +1 -0
- package/dist/events/names.js +71 -0
- package/dist/events/safe.d.ts +9 -0
- package/dist/events/safe.js +27 -0
- package/dist/gateway/compression.d.ts +9 -0
- package/dist/gateway/compression.js +119 -0
- package/dist/gateway/connection.js +42 -17
- package/dist/gateway/protocol.d.ts +4 -1
- package/dist/gateway/protocol.js +9 -3
- package/dist/gateway/subscription.d.ts +10 -0
- package/dist/gateway/subscription.js +26 -0
- package/dist/index.d.ts +48 -4
- package/dist/index.js +19 -3
- package/dist/model/components.d.ts +82 -0
- package/dist/model/components.js +74 -0
- package/dist/model/constants.d.ts +330 -0
- package/dist/model/constants.js +330 -0
- package/dist/model/embed.d.ts +59 -0
- package/dist/model/embed.js +40 -0
- package/dist/model/entities.d.ts +125 -0
- package/dist/model/entities.js +12 -0
- package/dist/model/format.d.ts +32 -0
- package/dist/model/format.js +61 -0
- package/dist/model/guild.d.ts +114 -1
- package/dist/model/helpers.d.ts +88 -0
- package/dist/model/helpers.js +185 -0
- package/dist/model/interaction.d.ts +1 -1
- package/dist/model/locale.d.ts +15 -0
- package/dist/model/locale.js +55 -0
- package/dist/model/media.d.ts +23 -0
- package/dist/model/media.js +65 -0
- package/dist/model/permissions.d.ts +5 -0
- package/dist/model/permissions.js +31 -0
- package/dist/rest/attachments.d.ts +26 -0
- package/dist/rest/attachments.js +82 -0
- package/dist/rest/channels.d.ts +87 -3
- package/dist/rest/channels.js +181 -2
- package/dist/rest/commands.d.ts +1 -0
- package/dist/rest/commands.js +17 -0
- package/dist/rest/discovery.d.ts +35 -0
- package/dist/rest/discovery.js +33 -0
- package/dist/rest/errors.d.ts +38 -0
- package/dist/rest/errors.js +46 -0
- package/dist/rest/expressions.d.ts +65 -0
- package/dist/rest/expressions.js +89 -0
- package/dist/rest/guilds.d.ts +102 -1
- package/dist/rest/guilds.js +127 -0
- package/dist/rest/identity.d.ts +1 -0
- package/dist/rest/identity.js +15 -0
- package/dist/rest/interactions.d.ts +1 -1
- package/dist/rest/messages.d.ts +54 -30
- package/dist/rest/messages.js +32 -12
- package/dist/rest/rooms.d.ts +142 -0
- package/dist/rest/rooms.js +133 -0
- package/dist/rest/sounds.d.ts +44 -0
- package/dist/rest/sounds.js +89 -0
- package/dist/rest/transport.d.ts +8 -2
- package/dist/rest/transport.js +38 -5
- package/dist/rest/users.d.ts +65 -0
- package/dist/rest/users.js +63 -0
- package/dist/rest/webhooks.d.ts +56 -0
- package/dist/rest/webhooks.js +94 -0
- package/dist/session/inventory.d.ts +22 -0
- package/dist/session/inventory.js +159 -0
- package/dist/session/session.d.ts +21 -2
- package/dist/session/session.js +61 -4
- package/dist/session/voice.d.ts +1 -0
- package/dist/session/voice.js +25 -17
- package/dist/shard/manager.d.ts +38 -1
- package/dist/shard/manager.js +87 -0
- package/dist/state/cache.d.ts +6 -1
- package/dist/state/cache.js +45 -1
- package/docs/README.md +13 -0
- package/docs/commands-and-interactions.md +120 -0
- package/docs/errors-and-limits.md +79 -0
- package/docs/gateway-and-sharding.md +102 -0
- package/docs/getting-started.md +79 -0
- package/docs/migrating.md +44 -0
- package/docs/rest-reference.md +101 -0
- package/docs/voice.md +95 -0
- package/package.json +25 -7
- package/dist/rest/client.d.ts +0 -48
- package/dist/rest/client.js +0 -151
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { snowflake } from './messages.js';
|
|
2
|
+
export class MicupUsers {
|
|
3
|
+
rest;
|
|
4
|
+
constructor(rest) {
|
|
5
|
+
this.rest = rest;
|
|
6
|
+
}
|
|
7
|
+
get(userId, options) {
|
|
8
|
+
return this.rest.request('GET', '/users/' + snowflake(userId, 'User ID'), options);
|
|
9
|
+
}
|
|
10
|
+
profile(userId, { guildId, withMutualFriends, withMutualGuilds, signal } = {}) {
|
|
11
|
+
const query = new URLSearchParams();
|
|
12
|
+
if (guildId !== undefined)
|
|
13
|
+
query.set('guild_id', snowflake(guildId, 'Guild ID'));
|
|
14
|
+
if (withMutualFriends)
|
|
15
|
+
query.set('with_mutual_friends', 'true');
|
|
16
|
+
if (withMutualGuilds)
|
|
17
|
+
query.set('with_mutual_guilds', 'true');
|
|
18
|
+
return this.rest.request('GET', '/users/' + snowflake(userId, 'User ID') + '/profile', { query, signal });
|
|
19
|
+
}
|
|
20
|
+
createDM(userId, options) {
|
|
21
|
+
return this.rest.request('POST', '/users/@me/channels', { ...options, body: { recipient_id: snowflake(userId, 'User ID') } });
|
|
22
|
+
}
|
|
23
|
+
application(options) {
|
|
24
|
+
return this.rest.request('GET', '/applications/@me', options);
|
|
25
|
+
}
|
|
26
|
+
editCurrentUser(edit, options) {
|
|
27
|
+
if (edit.username !== undefined && !/^[A-Za-z0-9_]{1,32}$/.test(edit.username)) {
|
|
28
|
+
throw new TypeError('A bot username takes 1 to 32 ASCII letters, digits or underscores');
|
|
29
|
+
}
|
|
30
|
+
return this.rest.request('PATCH', '/users/@me', { ...options, body: edit });
|
|
31
|
+
}
|
|
32
|
+
publicApplication(applicationId, options) {
|
|
33
|
+
return this.rest.request('GET', '/oauth2/applications/' + snowflake(applicationId, 'Application ID') + '/public', { ...options, anonymous: true });
|
|
34
|
+
}
|
|
35
|
+
guilds({ before, after, limit, signal } = {}) {
|
|
36
|
+
const query = new URLSearchParams();
|
|
37
|
+
if (limit !== undefined) {
|
|
38
|
+
if (!Number.isSafeInteger(limit) || limit < 1 || limit > 200)
|
|
39
|
+
throw new RangeError('Guild list limit must be between 1 and 200');
|
|
40
|
+
query.set('limit', String(limit));
|
|
41
|
+
}
|
|
42
|
+
if (before !== undefined)
|
|
43
|
+
query.set('before', snowflake(before, 'Guild cursor'));
|
|
44
|
+
if (after !== undefined)
|
|
45
|
+
query.set('after', snowflake(after, 'Guild cursor'));
|
|
46
|
+
return this.rest.request('GET', '/users/@me/guilds', { query, signal });
|
|
47
|
+
}
|
|
48
|
+
async *iterateGuilds({ signal } = {}) {
|
|
49
|
+
let after;
|
|
50
|
+
while (true) {
|
|
51
|
+
const guilds = await this.guilds({ limit: 200, after, signal });
|
|
52
|
+
for (const guild of guilds)
|
|
53
|
+
yield guild;
|
|
54
|
+
const next = guilds.at(-1)?.id;
|
|
55
|
+
if (guilds.length < 200 || !next || next === after)
|
|
56
|
+
return;
|
|
57
|
+
after = next;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
leaveGuild(guildId, options) {
|
|
61
|
+
return this.rest.request('DELETE', '/users/@me/guilds/' + snowflake(guildId, 'Guild ID'), options);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { MicupMessage, MicupUser } from '../model/entities.js';
|
|
2
|
+
import type { AuditOptions } from './guilds.js';
|
|
3
|
+
import { type MessageInput } from './messages.js';
|
|
4
|
+
import type { MicupRequestOptions, RestTransport } from './transport.js';
|
|
5
|
+
export interface MicupWebhook {
|
|
6
|
+
id: string;
|
|
7
|
+
channel_id: string;
|
|
8
|
+
guild_id?: string;
|
|
9
|
+
type: number;
|
|
10
|
+
name: string;
|
|
11
|
+
avatar?: string | null;
|
|
12
|
+
user?: MicupUser;
|
|
13
|
+
token?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface WebhookEdit {
|
|
16
|
+
name?: string;
|
|
17
|
+
avatar?: string | null;
|
|
18
|
+
channel_id?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface WebhookExecuteOptions extends MicupRequestOptions {
|
|
21
|
+
wait?: boolean;
|
|
22
|
+
threadId?: string;
|
|
23
|
+
threadName?: string;
|
|
24
|
+
appliedTags?: readonly string[];
|
|
25
|
+
username?: string;
|
|
26
|
+
avatarURL?: string;
|
|
27
|
+
}
|
|
28
|
+
interface WebhookMessageOptions extends MicupRequestOptions {
|
|
29
|
+
threadId?: string;
|
|
30
|
+
}
|
|
31
|
+
export declare class MicupWebhooks {
|
|
32
|
+
private readonly rest;
|
|
33
|
+
constructor(rest: RestTransport);
|
|
34
|
+
channel(channelId: string, options?: MicupRequestOptions): Promise<MicupWebhook[]>;
|
|
35
|
+
guild(guildId: string, options?: MicupRequestOptions): Promise<MicupWebhook[]>;
|
|
36
|
+
create(channelId: string, name: string, { avatar, ...options }?: AuditOptions & {
|
|
37
|
+
avatar?: string;
|
|
38
|
+
}): Promise<MicupWebhook>;
|
|
39
|
+
get(webhookId: string, { token, signal }?: MicupRequestOptions & {
|
|
40
|
+
token?: string;
|
|
41
|
+
}): Promise<MicupWebhook>;
|
|
42
|
+
edit(webhookId: string, edit: WebhookEdit, { token, ...options }?: AuditOptions & {
|
|
43
|
+
token?: string;
|
|
44
|
+
}): Promise<MicupWebhook>;
|
|
45
|
+
delete(webhookId: string, { token, ...options }?: AuditOptions & {
|
|
46
|
+
token?: string;
|
|
47
|
+
}): Promise<void>;
|
|
48
|
+
execute(webhookId: string, token: string, input: string | MessageInput, options: WebhookExecuteOptions & {
|
|
49
|
+
wait: true;
|
|
50
|
+
}): Promise<MicupMessage>;
|
|
51
|
+
execute(webhookId: string, token: string, input: string | MessageInput, options?: WebhookExecuteOptions): Promise<MicupMessage | undefined>;
|
|
52
|
+
message(id: string, token: string, messageId: string, { threadId, signal }?: WebhookMessageOptions): Promise<MicupMessage>;
|
|
53
|
+
editMessage(id: string, token: string, messageId: string, edit: Pick<MessageInput, 'content' | 'embeds' | 'flags'>, { threadId, signal }?: WebhookMessageOptions): Promise<MicupMessage>;
|
|
54
|
+
deleteMessage(id: string, token: string, messageId: string, { threadId, signal }?: WebhookMessageOptions): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
export {};
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { messageBody, snowflake } from './messages.js';
|
|
2
|
+
function webhookPath(id, token) {
|
|
3
|
+
const base = '/webhooks/' + snowflake(id, 'Webhook ID');
|
|
4
|
+
if (token === undefined)
|
|
5
|
+
return base;
|
|
6
|
+
if (typeof token !== 'string' || !token.length || token === '.' || token === '..')
|
|
7
|
+
throw new TypeError('A webhook token is required');
|
|
8
|
+
return base + '/' + encodeURIComponent(token);
|
|
9
|
+
}
|
|
10
|
+
function threadQuery(threadId) {
|
|
11
|
+
const query = new URLSearchParams();
|
|
12
|
+
if (threadId !== undefined)
|
|
13
|
+
query.set('thread_id', snowflake(threadId, 'Thread ID'));
|
|
14
|
+
return query;
|
|
15
|
+
}
|
|
16
|
+
function webhookEdit(edit) {
|
|
17
|
+
if (edit.name !== undefined && (typeof edit.name !== 'string' || [...edit.name].length < 1 || [...edit.name].length > 80)) {
|
|
18
|
+
throw new TypeError('A webhook name must contain 1 to 80 characters');
|
|
19
|
+
}
|
|
20
|
+
if (edit.channel_id !== undefined)
|
|
21
|
+
snowflake(edit.channel_id, 'Channel ID');
|
|
22
|
+
if (edit.name === undefined && edit.avatar === undefined && edit.channel_id === undefined)
|
|
23
|
+
throw new TypeError('Webhook edits need a change');
|
|
24
|
+
return edit;
|
|
25
|
+
}
|
|
26
|
+
export class MicupWebhooks {
|
|
27
|
+
rest;
|
|
28
|
+
constructor(rest) {
|
|
29
|
+
this.rest = rest;
|
|
30
|
+
}
|
|
31
|
+
channel(channelId, options) {
|
|
32
|
+
return this.rest.request('GET', '/channels/' + snowflake(channelId, 'Channel ID') + '/webhooks', options);
|
|
33
|
+
}
|
|
34
|
+
guild(guildId, options) {
|
|
35
|
+
return this.rest.request('GET', '/guilds/' + snowflake(guildId, 'Guild ID') + '/webhooks', options);
|
|
36
|
+
}
|
|
37
|
+
create(channelId, name, { avatar, ...options } = {}) {
|
|
38
|
+
return this.rest.request('POST', '/channels/' + snowflake(channelId, 'Channel ID') + '/webhooks', {
|
|
39
|
+
...options, body: webhookEdit({ name, avatar }),
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
get(webhookId, { token, signal } = {}) {
|
|
43
|
+
return this.rest.request('GET', webhookPath(webhookId, token), { signal, anonymous: token !== undefined });
|
|
44
|
+
}
|
|
45
|
+
edit(webhookId, edit, { token, ...options } = {}) {
|
|
46
|
+
if (token !== undefined && edit.channel_id !== undefined)
|
|
47
|
+
throw new TypeError('An authenticated bot must move the webhook');
|
|
48
|
+
return this.rest.request('PATCH', webhookPath(webhookId, token), {
|
|
49
|
+
...options, body: webhookEdit(edit), anonymous: token !== undefined,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
delete(webhookId, { token, ...options } = {}) {
|
|
53
|
+
return this.rest.request('DELETE', webhookPath(webhookId, token), { ...options, anonymous: token !== undefined });
|
|
54
|
+
}
|
|
55
|
+
execute(webhookId, token, input, options = {}) {
|
|
56
|
+
const { wait, threadId, threadName, appliedTags, username, avatarURL, signal } = options;
|
|
57
|
+
const message = typeof input === 'string' ? { content: input } : input;
|
|
58
|
+
if (message.components?.length)
|
|
59
|
+
throw new TypeError('Webhook messages cannot carry components');
|
|
60
|
+
if (!message.content && !message.embeds?.length && !message.files?.length && !message.uploads?.length && !message.sticker_ids?.length) {
|
|
61
|
+
throw new TypeError('A webhook message needs content, an embed, an attachment or a sticker');
|
|
62
|
+
}
|
|
63
|
+
if (threadName !== undefined && (!threadName || [...threadName].length > 100 || threadId !== undefined)) {
|
|
64
|
+
throw new TypeError('Choose either an existing thread or a new forum post name');
|
|
65
|
+
}
|
|
66
|
+
if (appliedTags?.length && (!threadName || appliedTags.length > 5))
|
|
67
|
+
throw new TypeError('A forum post accepts up to five tags');
|
|
68
|
+
const query = threadQuery(threadId);
|
|
69
|
+
if (wait)
|
|
70
|
+
query.set('wait', 'true');
|
|
71
|
+
return this.rest.request('POST', webhookPath(webhookId, token), {
|
|
72
|
+
body: messageBody(message, undefined, { username, avatar_url: avatarURL, thread_name: threadName, applied_tags: appliedTags }),
|
|
73
|
+
query, signal, anonymous: true, idempotent: Boolean(message.nonce),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
message(id, token, messageId, { threadId, signal } = {}) {
|
|
77
|
+
return this.rest.request('GET', webhookPath(id, token) + '/messages/' + snowflake(messageId, 'Message ID'), {
|
|
78
|
+
query: threadQuery(threadId), signal, anonymous: true,
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
editMessage(id, token, messageId, edit, { threadId, signal } = {}) {
|
|
82
|
+
const { content, embeds, flags } = edit;
|
|
83
|
+
if (content === undefined && embeds === undefined && flags === undefined)
|
|
84
|
+
throw new TypeError('Webhook message edits need a change');
|
|
85
|
+
return this.rest.request('PATCH', webhookPath(id, token) + '/messages/' + snowflake(messageId, 'Message ID'), {
|
|
86
|
+
query: threadQuery(threadId), signal, anonymous: true, body: { content, embeds, flags },
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
deleteMessage(id, token, messageId, { threadId, signal } = {}) {
|
|
90
|
+
return this.rest.request('DELETE', webhookPath(id, token) + '/messages/' + snowflake(messageId, 'Message ID'), {
|
|
91
|
+
query: threadQuery(threadId), signal, anonymous: true,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { GatewayDispatch } from '../gateway/protocol.js';
|
|
2
|
+
import type { MicupMember } from '../model/guild.js';
|
|
3
|
+
export type MemberPageLookup = (guildId: string, options: {
|
|
4
|
+
limit: number;
|
|
5
|
+
after?: string;
|
|
6
|
+
signal: AbortSignal;
|
|
7
|
+
}) => Promise<readonly MicupMember[]>;
|
|
8
|
+
export interface GuildMemberInventory {
|
|
9
|
+
guildId: string;
|
|
10
|
+
members: Map<string, boolean>;
|
|
11
|
+
complete: boolean;
|
|
12
|
+
available: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare class MemberInventory {
|
|
15
|
+
#private;
|
|
16
|
+
private readonly page;
|
|
17
|
+
constructor(page: MemberPageLookup);
|
|
18
|
+
clear(): void;
|
|
19
|
+
snapshots(): GuildMemberInventory[];
|
|
20
|
+
observe(event: GatewayDispatch): void;
|
|
21
|
+
run(ready: () => boolean, signal: AbortSignal): Promise<void>;
|
|
22
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
2
|
+
import { compareSnowflakes } from '../model/format.js';
|
|
3
|
+
const pageSize = 1000;
|
|
4
|
+
function abortable(signal, work) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const abort = () => reject(signal.reason);
|
|
7
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
8
|
+
void work.then(resolve, reject).finally(() => signal.removeEventListener('abort', abort));
|
|
9
|
+
if (signal.aborted) {
|
|
10
|
+
signal.removeEventListener('abort', abort);
|
|
11
|
+
abort();
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export class MemberInventory {
|
|
16
|
+
page;
|
|
17
|
+
#guilds = new Map();
|
|
18
|
+
constructor(page) {
|
|
19
|
+
this.page = page;
|
|
20
|
+
}
|
|
21
|
+
clear() {
|
|
22
|
+
this.#guilds.clear();
|
|
23
|
+
}
|
|
24
|
+
snapshots() {
|
|
25
|
+
return [...this.#guilds].sort(([a], [b]) => a.localeCompare(b)).map(([guildId, entry]) => ({
|
|
26
|
+
guildId, complete: entry.complete, available: entry.available, members: new Map(entry.members),
|
|
27
|
+
}));
|
|
28
|
+
}
|
|
29
|
+
observe(event) {
|
|
30
|
+
const { name, data } = event;
|
|
31
|
+
if (!data || typeof data !== 'object')
|
|
32
|
+
return;
|
|
33
|
+
switch (name) {
|
|
34
|
+
case 'READY':
|
|
35
|
+
this.clear();
|
|
36
|
+
for (const guild of data.guilds ?? [])
|
|
37
|
+
this.#guild(guild);
|
|
38
|
+
break;
|
|
39
|
+
case 'GUILD_CREATE':
|
|
40
|
+
case 'GUILD_SYNC':
|
|
41
|
+
this.#guild(data);
|
|
42
|
+
break;
|
|
43
|
+
case 'GUILD_DELETE': {
|
|
44
|
+
const guild = data;
|
|
45
|
+
if (guild.unavailable) {
|
|
46
|
+
const entry = this.#guilds.get(guild.id);
|
|
47
|
+
if (entry)
|
|
48
|
+
entry.available = false;
|
|
49
|
+
}
|
|
50
|
+
else
|
|
51
|
+
this.#guilds.delete(guild.id);
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case 'GUILD_MEMBER_ADD':
|
|
55
|
+
case 'GUILD_MEMBER_UPDATE':
|
|
56
|
+
case 'GUILD_MEMBER_REMOVE': {
|
|
57
|
+
const { guild_id, user } = data;
|
|
58
|
+
if (user?.id)
|
|
59
|
+
this.#write(guild_id, user, name === 'GUILD_MEMBER_REMOVE');
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
case 'VOICE_STATE_UPDATE': {
|
|
63
|
+
const { guild_id, member } = data;
|
|
64
|
+
if (guild_id && member?.user)
|
|
65
|
+
this.#write(guild_id, member.user);
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async run(ready, signal) {
|
|
71
|
+
try {
|
|
72
|
+
while (!signal.aborted) {
|
|
73
|
+
await delay(1000, undefined, { signal, ref: false });
|
|
74
|
+
while (!signal.aborted && ready()) {
|
|
75
|
+
const current = [...this.#guilds].find(([, entry]) => entry.available && !entry.complete && performance.now() >= entry.retryAt);
|
|
76
|
+
if (!current)
|
|
77
|
+
break;
|
|
78
|
+
await this.#sync(current[0], current[1], AbortSignal.any([signal, AbortSignal.timeout(30_000)]));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
if (!signal.aborted)
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
#guild(guild) {
|
|
88
|
+
if (!guild?.id)
|
|
89
|
+
return;
|
|
90
|
+
const existing = this.#guilds.get(guild.id);
|
|
91
|
+
if (guild.unavailable && existing) {
|
|
92
|
+
existing.available = false;
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const entry = {
|
|
96
|
+
available: !guild.unavailable, complete: false, members: new Map(), retryAt: 0,
|
|
97
|
+
};
|
|
98
|
+
this.#guilds.set(guild.id, entry);
|
|
99
|
+
for (const member of guild.members ?? []) {
|
|
100
|
+
if (member.user)
|
|
101
|
+
this.#write(guild.id, member.user);
|
|
102
|
+
}
|
|
103
|
+
for (const voice of guild.voice_states ?? []) {
|
|
104
|
+
if (voice.member?.user)
|
|
105
|
+
this.#write(guild.id, voice.member.user);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
#write(guildId, user, removed = false) {
|
|
109
|
+
const entry = this.#guilds.get(guildId);
|
|
110
|
+
if (!entry || !user.id)
|
|
111
|
+
return;
|
|
112
|
+
const bot = removed ? undefined : Boolean(user.bot);
|
|
113
|
+
if (bot === undefined)
|
|
114
|
+
entry.members.delete(user.id);
|
|
115
|
+
else
|
|
116
|
+
entry.members.set(user.id, bot);
|
|
117
|
+
entry.changes?.set(user.id, bot);
|
|
118
|
+
}
|
|
119
|
+
async #sync(guildId, entry, signal) {
|
|
120
|
+
const members = new Map();
|
|
121
|
+
entry.changes = new Map();
|
|
122
|
+
let cursor;
|
|
123
|
+
try {
|
|
124
|
+
while (this.#guilds.get(guildId) === entry && entry.available) {
|
|
125
|
+
signal.throwIfAborted();
|
|
126
|
+
const page = await abortable(signal, this.page(guildId, { limit: pageSize, after: cursor, signal }));
|
|
127
|
+
signal.throwIfAborted();
|
|
128
|
+
for (const member of page) {
|
|
129
|
+
if (!member.user?.id)
|
|
130
|
+
throw new Error('Micup returned an invalid inventory member');
|
|
131
|
+
members.set(member.user.id, Boolean(member.user.bot));
|
|
132
|
+
}
|
|
133
|
+
if (page.length < pageSize) {
|
|
134
|
+
if (this.#guilds.get(guildId) !== entry || !entry.available)
|
|
135
|
+
return;
|
|
136
|
+
for (const [id, bot] of entry.changes) {
|
|
137
|
+
if (bot === undefined)
|
|
138
|
+
members.delete(id);
|
|
139
|
+
else
|
|
140
|
+
members.set(id, bot);
|
|
141
|
+
}
|
|
142
|
+
entry.members = members;
|
|
143
|
+
entry.complete = true;
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
const next = page.at(-1)?.user?.id;
|
|
147
|
+
if (!next || cursor !== undefined && compareSnowflakes(next, cursor) <= 0)
|
|
148
|
+
throw new Error('Micup member pagination did not advance');
|
|
149
|
+
cursor = next;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
entry.retryAt = performance.now() + 10_000;
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
entry.changes = undefined;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { type GatewayDispatch } from '../gateway/protocol.js';
|
|
2
2
|
import { type VoiceJoinOptions, type VoiceServerUpdate, type VoiceSessionOptions } from './voice.js';
|
|
3
3
|
import { GatewayState } from '../state/cache.js';
|
|
4
|
-
import type { MicupMember } from '../model/guild.js';
|
|
4
|
+
import type { MicupChannel, MicupMember } from '../model/guild.js';
|
|
5
|
+
import { type GatewaySubscription } from '../gateway/subscription.js';
|
|
5
6
|
import { type CustomStatus, type Presence, type PresenceStatus } from '../model/presence.js';
|
|
6
7
|
import { type ChannelMemberCount, type GuildCount, type GuildMembersRequest } from './requests.js';
|
|
8
|
+
import { type GuildMemberInventory, type MemberPageLookup } from './inventory.js';
|
|
9
|
+
export type ChannelLookup = (channelId: string, options?: {
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}) => Promise<MicupChannel>;
|
|
7
12
|
export interface GatewaySessionConfig {
|
|
8
13
|
readonly endpoint: string;
|
|
9
14
|
readonly token: string;
|
|
10
15
|
readonly botId?: string;
|
|
16
|
+
readonly lookupChannel?: ChannelLookup;
|
|
17
|
+
readonly lookupMembers?: MemberPageLookup;
|
|
11
18
|
readonly shardId?: number;
|
|
12
19
|
readonly shardCount?: number;
|
|
13
20
|
}
|
|
@@ -15,6 +22,8 @@ export interface GatewaySessionOptions extends VoiceSessionOptions {
|
|
|
15
22
|
readonly handler: (event: GatewayDispatch, signal: AbortSignal) => void | Promise<void>;
|
|
16
23
|
readonly ignoredEvents?: readonly string[];
|
|
17
24
|
readonly flags?: number;
|
|
25
|
+
readonly compress?: boolean;
|
|
26
|
+
readonly trackMembers?: boolean;
|
|
18
27
|
readonly presence?: Presence;
|
|
19
28
|
readonly minBackoffMs?: number;
|
|
20
29
|
readonly maxBackoffMs?: number;
|
|
@@ -57,7 +66,17 @@ export declare class GatewaySession {
|
|
|
57
66
|
signal?: AbortSignal;
|
|
58
67
|
}): Promise<void>;
|
|
59
68
|
updateVoiceState(guildId: string, options: VoiceJoinOptions): Promise<void>;
|
|
60
|
-
|
|
69
|
+
resolveGuild(channelId: string, { signal }?: {
|
|
70
|
+
signal?: AbortSignal;
|
|
71
|
+
}): Promise<string | undefined>;
|
|
72
|
+
subscribe(subscriptions: Readonly<Record<string, GatewaySubscription>>, options?: {
|
|
73
|
+
signal?: AbortSignal;
|
|
74
|
+
}): Promise<void>;
|
|
75
|
+
memberInventories(): GuildMemberInventory[];
|
|
76
|
+
run(options?: {
|
|
77
|
+
signal?: AbortSignal;
|
|
78
|
+
}): Promise<void>;
|
|
79
|
+
runOnce(options?: {
|
|
61
80
|
signal?: AbortSignal;
|
|
62
81
|
}): Promise<void>;
|
|
63
82
|
}
|
package/dist/session/session.js
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
2
2
|
import { GatewayConnection } from '../gateway/connection.js';
|
|
3
|
+
import { assertZstdAvailable } from '../gateway/compression.js';
|
|
3
4
|
import { GatewayCloseError, gatewayEndpoint } from '../gateway/protocol.js';
|
|
4
5
|
import { SessionVoice } from './voice.js';
|
|
5
6
|
import { Permission, hasPermissions } from '../model/permissions.js';
|
|
6
7
|
import { GatewayState } from '../state/cache.js';
|
|
7
8
|
import { GatewayOpcode } from '../gateway/protocol.js';
|
|
9
|
+
import { validateSubscriptions } from '../gateway/subscription.js';
|
|
8
10
|
import { activePresence, normalizePresence } from '../model/presence.js';
|
|
9
11
|
import { SessionRequests } from './requests.js';
|
|
12
|
+
import { MemberInventory } from './inventory.js';
|
|
10
13
|
export class GatewaySession {
|
|
11
14
|
#resume = { sequence: null };
|
|
12
15
|
#endpoint;
|
|
@@ -18,6 +21,8 @@ export class GatewaySession {
|
|
|
18
21
|
#voice;
|
|
19
22
|
state = new GatewayState();
|
|
20
23
|
#botId;
|
|
24
|
+
#lookup;
|
|
25
|
+
#inventory;
|
|
21
26
|
#presence;
|
|
22
27
|
#requests;
|
|
23
28
|
#minBackoff;
|
|
@@ -26,11 +31,19 @@ export class GatewaySession {
|
|
|
26
31
|
#ready = false;
|
|
27
32
|
#connection;
|
|
28
33
|
constructor(config, options) {
|
|
29
|
-
|
|
34
|
+
if (options?.compress)
|
|
35
|
+
assertZstdAvailable();
|
|
36
|
+
this.#endpoint = gatewayEndpoint(config.endpoint, options?.compress);
|
|
30
37
|
if (!/^[\x21-\x7E]+$/.test(config.token ?? ''))
|
|
31
38
|
throw new TypeError('Gateway token is required');
|
|
32
39
|
this.#token = config.token;
|
|
33
40
|
this.#botId = config.botId;
|
|
41
|
+
this.#lookup = config.lookupChannel;
|
|
42
|
+
if (options?.trackMembers) {
|
|
43
|
+
if (!config.lookupMembers)
|
|
44
|
+
throw new TypeError('Member tracking requires a member page lookup');
|
|
45
|
+
this.#inventory = new MemberInventory(config.lookupMembers);
|
|
46
|
+
}
|
|
34
47
|
const id = config.shardId ?? 0;
|
|
35
48
|
const count = config.shardCount ?? 1;
|
|
36
49
|
if (!Number.isSafeInteger(id) || !Number.isSafeInteger(count) || id < 0 || id >= count) {
|
|
@@ -114,10 +127,50 @@ export class GatewaySession {
|
|
|
114
127
|
updateVoiceState(guildId, options) {
|
|
115
128
|
return this.#voice.update(guildId, options);
|
|
116
129
|
}
|
|
117
|
-
async
|
|
130
|
+
async resolveGuild(channelId, { signal } = {}) {
|
|
131
|
+
const cached = this.state.guildForChannel(channelId);
|
|
132
|
+
if (cached)
|
|
133
|
+
return cached;
|
|
134
|
+
if (!this.#lookup)
|
|
135
|
+
throw new Error('No channel lookup is configured');
|
|
136
|
+
const channel = await this.#lookup(channelId, { signal });
|
|
137
|
+
if (channel.id !== channelId)
|
|
138
|
+
throw new Error('Micup returned a different channel');
|
|
139
|
+
this.state.rememberChannel(channel);
|
|
140
|
+
return channel.guild_id;
|
|
141
|
+
}
|
|
142
|
+
subscribe(subscriptions, options) {
|
|
143
|
+
validateSubscriptions(subscriptions);
|
|
144
|
+
return this.send(GatewayOpcode.LazyRequest, { subscriptions }, options);
|
|
145
|
+
}
|
|
146
|
+
memberInventories() {
|
|
147
|
+
return this.#inventory?.snapshots() ?? [];
|
|
148
|
+
}
|
|
149
|
+
#applyState(event) {
|
|
150
|
+
const voice = event.name === 'VOICE_STATE_UPDATE' ? event.data : null;
|
|
151
|
+
const guild = voice?.guild_id;
|
|
152
|
+
const user = voice?.user_id;
|
|
153
|
+
const previous = guild && user ? this.state.voiceChannel(guild, user) : undefined;
|
|
154
|
+
this.state.apply(event);
|
|
155
|
+
this.#inventory?.observe(event);
|
|
156
|
+
if (guild && user && previous && user !== this.#botId && previous !== voice?.channel_id &&
|
|
157
|
+
this.#botId && this.state.voiceChannel(guild, this.#botId) === previous &&
|
|
158
|
+
this.state.voiceMembers(guild, previous).every((id) => id === this.#botId)) {
|
|
159
|
+
this.#options.onVoiceEmpty?.(guild, previous);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
run(options) {
|
|
163
|
+
return this.#run(true, options);
|
|
164
|
+
}
|
|
165
|
+
runOnce(options) {
|
|
166
|
+
return this.#run(false, options);
|
|
167
|
+
}
|
|
168
|
+
async #run(reconnect, { signal = new AbortController().signal } = {}) {
|
|
118
169
|
if (this.#running)
|
|
119
170
|
throw new Error('Micup Gateway session is already running');
|
|
120
171
|
this.#running = true;
|
|
172
|
+
const stopInventory = new AbortController();
|
|
173
|
+
const inventory = this.#inventory?.run(() => this.ready, AbortSignal.any([stopInventory.signal, signal]));
|
|
121
174
|
let backoff = this.#minBackoff;
|
|
122
175
|
try {
|
|
123
176
|
while (!signal.aborted) {
|
|
@@ -135,7 +188,7 @@ export class GatewaySession {
|
|
|
135
188
|
if (event.name === 'READY') {
|
|
136
189
|
this.#botId = event.data?.user?.id ?? this.#botId;
|
|
137
190
|
}
|
|
138
|
-
this
|
|
191
|
+
this.#applyState(event);
|
|
139
192
|
await this.#options.handler(event, lifetime);
|
|
140
193
|
},
|
|
141
194
|
receive: (event) => {
|
|
@@ -151,6 +204,7 @@ export class GatewaySession {
|
|
|
151
204
|
this.#voice.invalidate();
|
|
152
205
|
this.#requests.invalidate();
|
|
153
206
|
this.state.clear();
|
|
207
|
+
this.#inventory?.clear();
|
|
154
208
|
},
|
|
155
209
|
onReady: (resumed) => {
|
|
156
210
|
this.#ready = true;
|
|
@@ -168,11 +222,12 @@ export class GatewaySession {
|
|
|
168
222
|
this.#voice.invalidate();
|
|
169
223
|
this.#requests.invalidate();
|
|
170
224
|
this.state.clear();
|
|
225
|
+
this.#inventory?.clear();
|
|
171
226
|
this.#resume.sessionId = undefined;
|
|
172
227
|
this.#resume.sequence = null;
|
|
173
228
|
}
|
|
174
229
|
this.#options.onDisconnect?.(error);
|
|
175
|
-
if (error instanceof GatewayCloseError && error.fatal)
|
|
230
|
+
if (!reconnect || error instanceof GatewayCloseError && error.fatal)
|
|
176
231
|
throw error;
|
|
177
232
|
await delay(backoff, undefined, { signal }).catch((cause) => {
|
|
178
233
|
if (!signal.aborted)
|
|
@@ -183,6 +238,8 @@ export class GatewaySession {
|
|
|
183
238
|
}
|
|
184
239
|
}
|
|
185
240
|
finally {
|
|
241
|
+
stopInventory.abort();
|
|
242
|
+
await inventory;
|
|
186
243
|
this.#ready = false;
|
|
187
244
|
this.#connection = undefined;
|
|
188
245
|
this.#running = false;
|
package/dist/session/voice.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export interface VoiceJoinOptions {
|
|
|
15
15
|
}
|
|
16
16
|
export interface VoiceSessionOptions {
|
|
17
17
|
onVoiceLost?: (guildId: string, connectionId: string) => void;
|
|
18
|
+
onVoiceEmpty?: (guildId: string, channelId: string) => void;
|
|
18
19
|
onVoiceGrant?: (grant: VoiceServerUpdate) => void;
|
|
19
20
|
}
|
|
20
21
|
export declare class VoiceRefusedError extends Error {
|