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