wumbly 0.1.0 → 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/README.md +49 -0
- package/dist/client.cjs +416 -31
- package/dist/client.cjs.map +1 -1
- package/dist/client.d.cts +2 -1
- package/dist/client.d.ts +2 -1
- package/dist/client.js +416 -31
- package/dist/client.js.map +1 -1
- package/dist/commands.d.cts +2 -1
- package/dist/commands.d.ts +2 -1
- package/dist/events.d.cts +2 -1
- package/dist/events.d.ts +2 -1
- package/dist/{index-BaTK8C7O.d.ts → index-0P9mmVy0.d.cts} +72 -33
- package/dist/{index-BaTK8C7O.d.cts → index-CR7RtVXn.d.ts} +72 -33
- package/dist/index.cjs +445 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +26 -3
- package/dist/index.d.ts +26 -3
- package/dist/index.js +433 -31
- package/dist/index.js.map +1 -1
- package/dist/loader.d.cts +2 -1
- package/dist/loader.d.ts +2 -1
- 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 +2 -1
- package/dist/voice.cjs.map +1 -1
- package/dist/voice.d.cts +2 -1
- package/dist/voice.d.ts +2 -1
- package/dist/voice.js +2 -1
- package/dist/voice.js.map +1 -1
- package/package.json +10 -1
package/README.md
CHANGED
|
@@ -178,6 +178,55 @@ client.register(command, event);
|
|
|
178
178
|
|
|
179
179
|
`client.commands.list()`, `client.commands.get('name')`, `client.reload()` and `client.stop()` are available on the client.
|
|
180
180
|
|
|
181
|
+
## Permissions
|
|
182
|
+
|
|
183
|
+
Guild members expose computed permissions. They are recalculated on access, so role changes show up immediately.
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
import { PermissionFlags } from 'wumbly';
|
|
187
|
+
|
|
188
|
+
client.on('messageCreate', async (message) => {
|
|
189
|
+
const member = message.member;
|
|
190
|
+
if (!member) return;
|
|
191
|
+
|
|
192
|
+
if (member.has(PermissionFlags.ManageChannels)) {
|
|
193
|
+
// guild-level permission
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const allowed = member.permissionsIn(message.channel).has(PermissionFlags.SendMessages);
|
|
197
|
+
if (!allowed) return message.reply('I cannot send messages here.');
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Guild owners and administrators get every permission, and channel checks apply the @everyone, role and member overwrites in that order.
|
|
202
|
+
|
|
203
|
+
Overwrites are edited through REST:
|
|
204
|
+
|
|
205
|
+
```ts
|
|
206
|
+
import { OverwriteType, PermissionFlags } from 'wumbly';
|
|
207
|
+
|
|
208
|
+
await channel.permissionOverwrites.edit(channel.guildId, {
|
|
209
|
+
type: OverwriteType.Role,
|
|
210
|
+
deny: PermissionFlags.SendMessages,
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
await channel.permissionOverwrites.delete(channel.guildId);
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
`edit` replaces the whole allow/deny pair, it is not a partial update. The manager also exposes `cache`, `get`, `has`, `values` and `toArray`.
|
|
217
|
+
|
|
218
|
+
Guilds are normalized when cached: `guild.ownerId`, `guild.roles` and `guild.members.me` are filled from the gateway payload, and members received with the guild land in `guild.members.cache`. When `GUILD_CREATE` omits `owner_id`, wumbly fetches `GET /guilds/:id` once before emitting `guildCreate`.
|
|
219
|
+
|
|
220
|
+
## Logging
|
|
221
|
+
|
|
222
|
+
`PlatformClient` accepts `logLevel` (`debug`, `info`, `warn`, `error`). The default is `info`. `debug` prints gateway and voice payloads.
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
const client = new Client({
|
|
226
|
+
client: { logLevel: 'debug' },
|
|
227
|
+
});
|
|
228
|
+
```
|
|
229
|
+
|
|
181
230
|
## Voice
|
|
182
231
|
|
|
183
232
|
Voice is optional and never bundled. Native voice stacks ship platform-specific binaries, which are a problem on Termux/Android, especially on 32-bit builds. Wumbly has no native dependencies.
|
package/dist/client.cjs
CHANGED
|
@@ -187,21 +187,237 @@ var Events = {
|
|
|
187
187
|
VoiceStateUpdate: "voiceStateUpdate",
|
|
188
188
|
VoiceServerUpdate: "voiceServerUpdate",
|
|
189
189
|
VoiceStateAck: "voiceStateAck",
|
|
190
|
-
VoiceStatesSync: "voiceStatesSync"
|
|
190
|
+
VoiceStatesSync: "voiceStatesSync",
|
|
191
|
+
GuildDelete: "guildDelete"
|
|
191
192
|
};
|
|
192
193
|
|
|
194
|
+
// src/platform/permissions.ts
|
|
195
|
+
var PermissionFlags = {
|
|
196
|
+
CreateInstantInvite: 1n << 0n,
|
|
197
|
+
KickMembers: 1n << 1n,
|
|
198
|
+
BanMembers: 1n << 2n,
|
|
199
|
+
Administrator: 1n << 3n,
|
|
200
|
+
ManageChannels: 1n << 4n,
|
|
201
|
+
ManageGuild: 1n << 5n,
|
|
202
|
+
AddReactions: 1n << 6n,
|
|
203
|
+
ViewAuditLog: 1n << 7n,
|
|
204
|
+
PrioritySpeaker: 1n << 8n,
|
|
205
|
+
Stream: 1n << 9n,
|
|
206
|
+
ViewChannel: 1n << 10n,
|
|
207
|
+
SendMessages: 1n << 11n,
|
|
208
|
+
SendTtsMessages: 1n << 12n,
|
|
209
|
+
ManageMessages: 1n << 13n,
|
|
210
|
+
EmbedLinks: 1n << 14n,
|
|
211
|
+
AttachFiles: 1n << 15n,
|
|
212
|
+
ReadMessageHistory: 1n << 16n,
|
|
213
|
+
MentionEveryone: 1n << 17n,
|
|
214
|
+
UseExternalEmojis: 1n << 18n,
|
|
215
|
+
ViewGuildInsights: 1n << 19n,
|
|
216
|
+
Connect: 1n << 20n,
|
|
217
|
+
Speak: 1n << 21n,
|
|
218
|
+
MuteMembers: 1n << 22n,
|
|
219
|
+
DeafenMembers: 1n << 23n,
|
|
220
|
+
MoveMembers: 1n << 24n,
|
|
221
|
+
UseVad: 1n << 25n,
|
|
222
|
+
ChangeNickname: 1n << 26n,
|
|
223
|
+
ManageNicknames: 1n << 27n,
|
|
224
|
+
ManageRoles: 1n << 28n,
|
|
225
|
+
ManageWebhooks: 1n << 29n,
|
|
226
|
+
ManageGuildExpressions: 1n << 30n,
|
|
227
|
+
UseApplicationCommands: 1n << 31n,
|
|
228
|
+
RequestToSpeak: 1n << 32n,
|
|
229
|
+
ManageEvents: 1n << 33n,
|
|
230
|
+
ManageThreads: 1n << 34n,
|
|
231
|
+
CreatePublicThreads: 1n << 35n,
|
|
232
|
+
CreatePrivateThreads: 1n << 36n,
|
|
233
|
+
UseExternalStickers: 1n << 37n,
|
|
234
|
+
SendMessagesInThreads: 1n << 38n,
|
|
235
|
+
UseEmbeddedActivities: 1n << 39n,
|
|
236
|
+
ModerateMembers: 1n << 40n
|
|
237
|
+
};
|
|
238
|
+
var ALL_PERMISSIONS = (1n << 64n) - 1n;
|
|
239
|
+
var OverwriteType = {
|
|
240
|
+
Role: 0,
|
|
241
|
+
Member: 1
|
|
242
|
+
};
|
|
243
|
+
function resolvePermission(value) {
|
|
244
|
+
if (typeof value === "bigint") return value;
|
|
245
|
+
if (typeof value === "number") {
|
|
246
|
+
return Number.isFinite(value) ? BigInt(Math.trunc(value)) : 0n;
|
|
247
|
+
}
|
|
248
|
+
if (typeof value === "string") {
|
|
249
|
+
const trimmed = value.trim();
|
|
250
|
+
if (!trimmed) return 0n;
|
|
251
|
+
const named = PermissionFlags[trimmed];
|
|
252
|
+
if (named !== void 0) return named;
|
|
253
|
+
try {
|
|
254
|
+
return BigInt(trimmed);
|
|
255
|
+
} catch {
|
|
256
|
+
return 0n;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (value instanceof Permissions) return value.bitfield;
|
|
260
|
+
return 0n;
|
|
261
|
+
}
|
|
262
|
+
function hasPermission(bitfield, ...flags) {
|
|
263
|
+
const bits = resolvePermission(bitfield);
|
|
264
|
+
return flags.every((flag) => {
|
|
265
|
+
const required = resolvePermission(flag);
|
|
266
|
+
return (bits & required) === required;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
function hasAnyPermission(bitfield, ...flags) {
|
|
270
|
+
const bits = resolvePermission(bitfield);
|
|
271
|
+
return flags.some((flag) => (bits & resolvePermission(flag)) !== 0n);
|
|
272
|
+
}
|
|
273
|
+
var Permissions = class {
|
|
274
|
+
bitfield;
|
|
275
|
+
constructor(value = 0n) {
|
|
276
|
+
this.bitfield = resolvePermission(value);
|
|
277
|
+
Object.freeze(this);
|
|
278
|
+
}
|
|
279
|
+
/** `true` when every given flag is present. */
|
|
280
|
+
has(...flags) {
|
|
281
|
+
return hasPermission(this.bitfield, ...flags);
|
|
282
|
+
}
|
|
283
|
+
/** `true` when at least one of the given flags is present. */
|
|
284
|
+
any(...flags) {
|
|
285
|
+
return hasAnyPermission(this.bitfield, ...flags);
|
|
286
|
+
}
|
|
287
|
+
/** Names of the given flags that are not present. */
|
|
288
|
+
missing(...flags) {
|
|
289
|
+
const missing = [];
|
|
290
|
+
for (const [name, bit] of Object.entries(PermissionFlags)) {
|
|
291
|
+
const requested = flags.some((flag) => resolvePermission(flag) === bit);
|
|
292
|
+
if (requested && (this.bitfield & bit) === 0n) missing.push(name);
|
|
293
|
+
}
|
|
294
|
+
return missing;
|
|
295
|
+
}
|
|
296
|
+
/** Names of every single-bit permission that is present. */
|
|
297
|
+
toArray() {
|
|
298
|
+
const names = [];
|
|
299
|
+
for (const [name, bit] of Object.entries(PermissionFlags)) {
|
|
300
|
+
if ((this.bitfield & bit) === bit) names.push(name);
|
|
301
|
+
}
|
|
302
|
+
return names;
|
|
303
|
+
}
|
|
304
|
+
equals(other) {
|
|
305
|
+
return this.bitfield === resolvePermission(other);
|
|
306
|
+
}
|
|
307
|
+
toString() {
|
|
308
|
+
return this.bitfield.toString();
|
|
309
|
+
}
|
|
310
|
+
toJSON() {
|
|
311
|
+
return this.toString();
|
|
312
|
+
}
|
|
313
|
+
valueOf() {
|
|
314
|
+
return this.bitfield;
|
|
315
|
+
}
|
|
316
|
+
[Symbol.toPrimitive]() {
|
|
317
|
+
return this.bitfield;
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
var normalizeOverwrite = (raw) => ({
|
|
321
|
+
id: String(raw?.id ?? ""),
|
|
322
|
+
type: Number(raw?.type ?? OverwriteType.Role),
|
|
323
|
+
allow: resolvePermission(raw?.allow ?? 0n),
|
|
324
|
+
deny: resolvePermission(raw?.deny ?? 0n)
|
|
325
|
+
});
|
|
326
|
+
function readOverwrites(channel) {
|
|
327
|
+
if (!channel) return [];
|
|
328
|
+
const manager = channel.permissionOverwrites;
|
|
329
|
+
if (manager instanceof Map) {
|
|
330
|
+
return [...manager.values()].map(normalizeOverwrite);
|
|
331
|
+
}
|
|
332
|
+
if (manager?.cache instanceof Map) {
|
|
333
|
+
return [...manager.cache.values()].map(normalizeOverwrite);
|
|
334
|
+
}
|
|
335
|
+
if (typeof manager?.values === "function") {
|
|
336
|
+
return [...manager.values()].map(normalizeOverwrite);
|
|
337
|
+
}
|
|
338
|
+
const raw = Array.isArray(channel.permission_overwrites) ? channel.permission_overwrites : Array.isArray(manager) ? manager : [];
|
|
339
|
+
return raw.map(normalizeOverwrite);
|
|
340
|
+
}
|
|
341
|
+
var memberRoleIds = (member) => {
|
|
342
|
+
const roles = member?.roles;
|
|
343
|
+
if (!Array.isArray(roles)) return [];
|
|
344
|
+
return roles.map((role) => typeof role === "string" ? role : role?.id).filter((id) => typeof id === "string" && id.length > 0);
|
|
345
|
+
};
|
|
346
|
+
function computePermissions(guild, member, channel = null) {
|
|
347
|
+
const guildId = String(guild?.id ?? "");
|
|
348
|
+
const memberId = String(member?.id ?? member?.user?.id ?? "");
|
|
349
|
+
const ownerId = String(guild?.ownerId ?? guild?.owner_id ?? "");
|
|
350
|
+
if (ownerId && ownerId === memberId) return new Permissions(ALL_PERMISSIONS);
|
|
351
|
+
const roles = Array.isArray(guild?.roles) ? guild.roles : [];
|
|
352
|
+
const roleIds = new Set(memberRoleIds(member));
|
|
353
|
+
let bitfield = 0n;
|
|
354
|
+
for (const role of roles) {
|
|
355
|
+
const roleId = String(role?.id ?? "");
|
|
356
|
+
if (!roleId) continue;
|
|
357
|
+
if (roleId === guildId || roleIds.has(roleId)) {
|
|
358
|
+
bitfield |= resolvePermission(role.permissions ?? 0n);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if ((bitfield & PermissionFlags.Administrator) === PermissionFlags.Administrator) {
|
|
362
|
+
return new Permissions(ALL_PERMISSIONS);
|
|
363
|
+
}
|
|
364
|
+
if (!channel) return new Permissions(bitfield);
|
|
365
|
+
const overwrites = readOverwrites(channel);
|
|
366
|
+
const everyone = overwrites.find(
|
|
367
|
+
(overwrite) => overwrite.id === guildId && overwrite.type === OverwriteType.Role
|
|
368
|
+
);
|
|
369
|
+
if (everyone) bitfield = bitfield & ~everyone.deny | everyone.allow;
|
|
370
|
+
let roleAllow = 0n;
|
|
371
|
+
let roleDeny = 0n;
|
|
372
|
+
for (const overwrite of overwrites) {
|
|
373
|
+
if (overwrite.type !== OverwriteType.Role) continue;
|
|
374
|
+
if (overwrite.id === guildId) continue;
|
|
375
|
+
if (!roleIds.has(overwrite.id)) continue;
|
|
376
|
+
roleAllow |= overwrite.allow;
|
|
377
|
+
roleDeny |= overwrite.deny;
|
|
378
|
+
}
|
|
379
|
+
bitfield = bitfield & ~roleDeny | roleAllow;
|
|
380
|
+
const memberOverwrite = overwrites.find(
|
|
381
|
+
(overwrite) => overwrite.id === memberId && overwrite.type === OverwriteType.Member
|
|
382
|
+
);
|
|
383
|
+
if (memberOverwrite) {
|
|
384
|
+
bitfield = bitfield & ~memberOverwrite.deny | memberOverwrite.allow;
|
|
385
|
+
}
|
|
386
|
+
return new Permissions(bitfield);
|
|
387
|
+
}
|
|
388
|
+
|
|
193
389
|
// src/platform/client.ts
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
390
|
+
var LOG_LEVELS = {
|
|
391
|
+
debug: 10,
|
|
392
|
+
info: 20,
|
|
393
|
+
warn: 30,
|
|
394
|
+
error: 40
|
|
395
|
+
};
|
|
396
|
+
function makeLogger(level = "info") {
|
|
397
|
+
const threshold = LOG_LEVELS[level] ?? LOG_LEVELS.info;
|
|
398
|
+
const wrap = (name) => (msg, data) => {
|
|
399
|
+
if (LOG_LEVELS[name] < threshold) return;
|
|
400
|
+
let line = msg;
|
|
401
|
+
if (data !== void 0) {
|
|
402
|
+
try {
|
|
403
|
+
line = `${msg} ${JSON.stringify(data)}`;
|
|
404
|
+
} catch {
|
|
405
|
+
line = `${msg} [unserializable]`;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
if (name === "error") console.error(line);
|
|
409
|
+
else if (name === "warn") console.warn(line);
|
|
410
|
+
else console.log(line);
|
|
198
411
|
};
|
|
199
|
-
return {
|
|
412
|
+
return { debug: wrap("debug"), info: wrap("info"), warn: wrap("warn"), error: wrap("error") };
|
|
200
413
|
}
|
|
201
414
|
function makeMessage(client, raw) {
|
|
202
415
|
const author = raw.author ?? { id: raw.author_id };
|
|
203
416
|
const channelId = raw.channel_id;
|
|
204
|
-
const
|
|
417
|
+
const cached = client.channels.get(channelId);
|
|
418
|
+
const guildId = raw.guild_id ?? cached?.guildId ?? null;
|
|
419
|
+
const guild = guildId ? client.guilds.get(guildId) ?? null : null;
|
|
420
|
+
const channel = cached ?? client.wrapChannel({ id: channelId, guildId, type: 0 }, guild);
|
|
205
421
|
const message = {
|
|
206
422
|
...raw,
|
|
207
423
|
id: raw.id,
|
|
@@ -209,15 +425,9 @@ function makeMessage(client, raw) {
|
|
|
209
425
|
author,
|
|
210
426
|
guildId,
|
|
211
427
|
channelId,
|
|
212
|
-
channel
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
guild: guildId ? client.guilds.get(guildId) ?? null : null,
|
|
216
|
-
send: (options) => client.sendMessage(channelId, options),
|
|
217
|
-
isVoice: () => false
|
|
218
|
-
},
|
|
219
|
-
guild: guildId ? client.guilds.get(guildId) ?? null : null,
|
|
220
|
-
member: raw.member ? { ...raw.member, user: author } : null,
|
|
428
|
+
channel,
|
|
429
|
+
guild,
|
|
430
|
+
member: raw.member ? guild ? client.wrapMember(raw.member, guild) : { ...raw.member, user: author } : null,
|
|
221
431
|
reply: async (options) => {
|
|
222
432
|
const payload = typeof options === "string" ? { content: options } : { ...options };
|
|
223
433
|
payload.message_reference = { message_id: raw.id, channel_id: channelId, ...guildId ? { guild_id: guildId } : {} };
|
|
@@ -228,25 +438,163 @@ function makeMessage(client, raw) {
|
|
|
228
438
|
}
|
|
229
439
|
var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
230
440
|
rest = new RestClient();
|
|
231
|
-
logger
|
|
441
|
+
logger;
|
|
232
442
|
guilds = /* @__PURE__ */ new Map();
|
|
233
443
|
channels = /* @__PURE__ */ new Map();
|
|
234
444
|
users = /* @__PURE__ */ new Map();
|
|
235
445
|
user = null;
|
|
236
446
|
ws = null;
|
|
237
447
|
options;
|
|
448
|
+
readyAt = null;
|
|
238
449
|
readyResolve = null;
|
|
239
450
|
constructor(options = {}) {
|
|
240
451
|
super();
|
|
241
452
|
this.options = options;
|
|
453
|
+
this.logger = makeLogger(options.logLevel ?? "info");
|
|
242
454
|
if (options.apiBase) this.rest.baseUrl = options.apiBase;
|
|
455
|
+
this.channels.fetch = async (id) => {
|
|
456
|
+
const cached = this.channels.get(id);
|
|
457
|
+
if (cached) return cached;
|
|
458
|
+
const raw = await this.rest.get(`/channels/${id}`);
|
|
459
|
+
const ch = this.wrapChannel(raw);
|
|
460
|
+
this.channels.set(ch.id, ch);
|
|
461
|
+
return ch;
|
|
462
|
+
};
|
|
463
|
+
this.guilds.fetch = async (id) => {
|
|
464
|
+
const cached = this.guilds.get(id);
|
|
465
|
+
if (cached) return cached;
|
|
466
|
+
const raw = await this.rest.get(`/guilds/${id}`);
|
|
467
|
+
const g = this.wrapGuild(raw);
|
|
468
|
+
this.guilds.set(g.id, g);
|
|
469
|
+
return g;
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
get uptime() {
|
|
473
|
+
return this.readyAt ? Date.now() - this.readyAt : null;
|
|
474
|
+
}
|
|
475
|
+
/**
|
|
476
|
+
* Normalize a guild payload. Gateway payloads often omit `owner_id` and
|
|
477
|
+
* deliver members as an array, so both are handled here.
|
|
478
|
+
*/
|
|
479
|
+
wrapGuild(guild) {
|
|
480
|
+
if (!guild) return guild;
|
|
481
|
+
const g = { ...guild };
|
|
482
|
+
g.shardId = g.shardId ?? 0;
|
|
483
|
+
g.ownerId = g.ownerId ?? g.owner_id ?? null;
|
|
484
|
+
g.roles = Array.isArray(g.roles) ? g.roles : [];
|
|
485
|
+
g.iconURL = (opts) => g.icon ? `https://fluxerusercontent.com/icons/${g.id}/${g.icon}${opts?.extension ? "." + opts.extension : ""}` : null;
|
|
486
|
+
const rawMembers = g.members;
|
|
487
|
+
const cache = /* @__PURE__ */ new Map();
|
|
488
|
+
const members = { cache, me: null };
|
|
489
|
+
members.get = (id) => cache.get(String(id)) ?? null;
|
|
490
|
+
members.fetch = async (id) => {
|
|
491
|
+
const cached = cache.get(String(id));
|
|
492
|
+
if (cached) return cached;
|
|
493
|
+
const raw = await this.rest.get(`/guilds/${g.id}/members/${id}`);
|
|
494
|
+
const member = this.wrapMember(raw, g);
|
|
495
|
+
if (member?.id) cache.set(member.id, member);
|
|
496
|
+
return member;
|
|
497
|
+
};
|
|
498
|
+
members.fetchMe = async () => {
|
|
499
|
+
const raw = await this.rest.get(`/guilds/${g.id}/members/@me`);
|
|
500
|
+
const member = this.wrapMember(raw, g);
|
|
501
|
+
if (member?.id) cache.set(member.id, member);
|
|
502
|
+
members.me = member;
|
|
503
|
+
return member;
|
|
504
|
+
};
|
|
505
|
+
if (Array.isArray(rawMembers)) {
|
|
506
|
+
for (const raw of rawMembers) {
|
|
507
|
+
const member = this.wrapMember(raw, g);
|
|
508
|
+
if (member?.id) cache.set(member.id, member);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
const meId = String(this.user?.id ?? "");
|
|
512
|
+
members.me = meId ? cache.get(meId) ?? null : null;
|
|
513
|
+
g.members = members;
|
|
514
|
+
g.channels = g.channels && !Array.isArray(g.channels) ? { ...g.channels } : {};
|
|
515
|
+
g.channels.cache = this.channels;
|
|
516
|
+
g.channels.get = (id) => this.channels.get(id);
|
|
517
|
+
g.channels.fetch = (id) => this.channels.fetch(id);
|
|
518
|
+
return g;
|
|
519
|
+
}
|
|
520
|
+
/** Attach permission helpers to a raw member payload. */
|
|
521
|
+
wrapMember(raw, guild) {
|
|
522
|
+
if (!raw) return raw;
|
|
523
|
+
const member = { ...raw };
|
|
524
|
+
member.id = String(member.id ?? member.user?.id ?? "");
|
|
525
|
+
if (!member.user && member.id) member.user = { id: member.id };
|
|
526
|
+
Object.defineProperty(member, "permissions", {
|
|
527
|
+
enumerable: true,
|
|
528
|
+
configurable: true,
|
|
529
|
+
get: () => computePermissions(guild, member)
|
|
530
|
+
});
|
|
531
|
+
member.permissionsIn = (channel) => computePermissions(guild, member, channel);
|
|
532
|
+
member.has = (...flags) => member.permissions.has(...flags);
|
|
533
|
+
return member;
|
|
534
|
+
}
|
|
535
|
+
wrapChannel(ch, guild) {
|
|
536
|
+
if (!ch) return ch;
|
|
537
|
+
const c = { ...ch };
|
|
538
|
+
c.guildId = c.guildId ?? c.guild_id ?? guild?.id;
|
|
539
|
+
c.guild = guild ?? (c.guildId ? this.guilds.get(c.guildId) ?? null : c.guild ?? null);
|
|
540
|
+
c.isVoice = () => [2, 13].includes(c.type);
|
|
541
|
+
c.isTextBased = () => [0, 1, 5, 10, 11, 12].includes(c.type);
|
|
542
|
+
c.send = (options) => this.sendMessage(c.id, options);
|
|
543
|
+
const rawOverwrites = Array.isArray(ch.permission_overwrites) ? ch.permission_overwrites : [];
|
|
544
|
+
const manager = this.createOverwriteManager(c.id, rawOverwrites);
|
|
545
|
+
c.permissionOverwrites = manager;
|
|
546
|
+
Object.defineProperty(c, "permission_overwrites", {
|
|
547
|
+
enumerable: true,
|
|
548
|
+
configurable: true,
|
|
549
|
+
get: () => manager.toArray()
|
|
550
|
+
});
|
|
551
|
+
return c;
|
|
552
|
+
}
|
|
553
|
+
/** Overwrite manager backed by `PUT/DELETE /channels/:id/permissions/:id`. */
|
|
554
|
+
createOverwriteManager(channelId, raw) {
|
|
555
|
+
const cache = /* @__PURE__ */ new Map();
|
|
556
|
+
for (const item of raw) {
|
|
557
|
+
const overwrite = normalizeOverwrite(item);
|
|
558
|
+
if (overwrite.id) cache.set(overwrite.id, overwrite);
|
|
559
|
+
}
|
|
560
|
+
const write = async (id, options = {}) => {
|
|
561
|
+
const overwriteId = String(id);
|
|
562
|
+
const existing = cache.get(overwriteId);
|
|
563
|
+
const type = options.type ?? existing?.type ?? OverwriteType.Role;
|
|
564
|
+
const allow = resolvePermission(options.allow ?? 0n);
|
|
565
|
+
const deny = resolvePermission(options.deny ?? 0n);
|
|
566
|
+
await this.rest.put(`/channels/${channelId}/permissions/${overwriteId}`, {
|
|
567
|
+
type,
|
|
568
|
+
allow: allow.toString(),
|
|
569
|
+
deny: deny.toString()
|
|
570
|
+
});
|
|
571
|
+
const overwrite = { id: overwriteId, type, allow, deny };
|
|
572
|
+
cache.set(overwriteId, overwrite);
|
|
573
|
+
return overwrite;
|
|
574
|
+
};
|
|
575
|
+
return {
|
|
576
|
+
cache,
|
|
577
|
+
get: (id) => cache.get(String(id)) ?? null,
|
|
578
|
+
has: (id) => cache.has(String(id)),
|
|
579
|
+
values: () => cache.values(),
|
|
580
|
+
toArray: () => [...cache.values()],
|
|
581
|
+
edit: write,
|
|
582
|
+
create: write,
|
|
583
|
+
delete: async (id) => {
|
|
584
|
+
const overwriteId = String(id);
|
|
585
|
+
await this.rest.delete(`/channels/${channelId}/permissions/${overwriteId}`);
|
|
586
|
+
cache.delete(overwriteId);
|
|
587
|
+
}
|
|
588
|
+
};
|
|
243
589
|
}
|
|
244
590
|
async login(token, _options) {
|
|
245
591
|
this.rest.setToken(token);
|
|
246
|
-
|
|
247
|
-
this.ws
|
|
248
|
-
this.ws.
|
|
249
|
-
|
|
592
|
+
const gw = new Gateway({ token, url: this.options.gatewayUrl, intents: this.options.intents });
|
|
593
|
+
this.ws = gw;
|
|
594
|
+
this.ws.ping = -1;
|
|
595
|
+
gw.on("dispatch", (t, d) => this.handleDispatch(t, d));
|
|
596
|
+
gw.on("error", (e) => this.emit("error", e));
|
|
597
|
+
await gw.connect();
|
|
250
598
|
await new Promise((resolve3) => {
|
|
251
599
|
this.readyResolve = resolve3;
|
|
252
600
|
});
|
|
@@ -259,18 +607,20 @@ var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
|
259
607
|
this.ws = null;
|
|
260
608
|
}
|
|
261
609
|
sendToGateway(_shardId, payload) {
|
|
262
|
-
this.logger.debug
|
|
263
|
-
console.log("[gateway send]", JSON.stringify(payload));
|
|
610
|
+
this.logger.debug("[gateway send]", payload);
|
|
264
611
|
this.ws?.send(payload);
|
|
265
612
|
}
|
|
266
613
|
handleDispatch(t, d) {
|
|
267
614
|
this.emit("raw", { t, d });
|
|
268
|
-
if (t.includes("VOICE"))
|
|
615
|
+
if (t.includes("VOICE")) this.logger.debug("[voice dispatch]", { t, d });
|
|
269
616
|
switch (t) {
|
|
270
617
|
case "READY": {
|
|
271
618
|
this.user = d?.user ?? null;
|
|
619
|
+
if (this.user) this.user.setPresence = async (_p) => {
|
|
620
|
+
};
|
|
272
621
|
if (this.user) this.users.set(this.user.id, this.user);
|
|
273
|
-
for (const g of d?.guilds ?? []) this.guilds.set(g.id, { ...g, shardId: 0 });
|
|
622
|
+
for (const g of d?.guilds ?? []) this.guilds.set(g.id, this.wrapGuild({ ...g, shardId: 0 }));
|
|
623
|
+
this.readyAt = Date.now();
|
|
274
624
|
this.emit(Events.Ready);
|
|
275
625
|
this.readyResolve?.();
|
|
276
626
|
this.readyResolve = null;
|
|
@@ -291,16 +641,20 @@ var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
|
291
641
|
return;
|
|
292
642
|
}
|
|
293
643
|
case "GUILD_CREATE": {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
644
|
+
void this.handleGuildCreate(d).catch((error) => {
|
|
645
|
+
this.logger.warn("Could not load guild", { guildId: d?.id, error: String(error) });
|
|
646
|
+
});
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
case "GUILD_DELETE": {
|
|
650
|
+
this.guilds.delete(d?.id);
|
|
651
|
+
this.emit(Events.GuildDelete ?? "guildDelete", d);
|
|
299
652
|
return;
|
|
300
653
|
}
|
|
301
654
|
case "CHANNEL_CREATE":
|
|
302
655
|
case "CHANNEL_UPDATE": {
|
|
303
|
-
const
|
|
656
|
+
const guild = d.guild_id ? this.guilds.get(d.guild_id) ?? null : null;
|
|
657
|
+
const ch = this.wrapChannel({ ...d, guildId: d.guild_id, guild }, guild);
|
|
304
658
|
this.channels.set(ch.id, ch);
|
|
305
659
|
return;
|
|
306
660
|
}
|
|
@@ -317,6 +671,37 @@ var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
|
317
671
|
this.emit(t.toLowerCase().replace(/_([a-z])/g, (_, c) => c.toUpperCase()), d);
|
|
318
672
|
}
|
|
319
673
|
}
|
|
674
|
+
/**
|
|
675
|
+
* Cache a gateway guild. Gateway payloads can include `roles` but omit
|
|
676
|
+
* `owner_id`, so the REST guild is fetched once and merged before the
|
|
677
|
+
* `guildCreate` event is emitted.
|
|
678
|
+
*/
|
|
679
|
+
async handleGuildCreate(raw) {
|
|
680
|
+
if (!raw?.id) return;
|
|
681
|
+
let data = raw;
|
|
682
|
+
if (!raw.owner_id && !raw.ownerId) {
|
|
683
|
+
const fetched = await this.rest.get(`/guilds/${raw.id}`).catch(() => null);
|
|
684
|
+
if (fetched) {
|
|
685
|
+
data = {
|
|
686
|
+
...raw,
|
|
687
|
+
...fetched,
|
|
688
|
+
channels: raw.channels ?? fetched.channels,
|
|
689
|
+
members: raw.members ?? fetched.members
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
const guild = this.wrapGuild({ ...data, shardId: 0 });
|
|
694
|
+
this.guilds.set(guild.id, guild);
|
|
695
|
+
for (const ch of data?.channels ?? []) {
|
|
696
|
+
const channel = this.wrapChannel({ ...ch, guildId: guild.id }, guild);
|
|
697
|
+
this.channels.set(channel.id, channel);
|
|
698
|
+
}
|
|
699
|
+
this.emit(Events.GuildCreate, guild);
|
|
700
|
+
this.emit(Events.VoiceStatesSync, {
|
|
701
|
+
guildId: guild.id,
|
|
702
|
+
voiceStates: data?.voice_states ?? []
|
|
703
|
+
});
|
|
704
|
+
}
|
|
320
705
|
async sendMessage(channelId, options) {
|
|
321
706
|
const payload = typeof options === "string" ? { content: options } : options;
|
|
322
707
|
const raw = await this.rest.post(`/channels/${channelId}/messages`, payload);
|