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/index.cjs
CHANGED
|
@@ -30,6 +30,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
|
+
ALL_PERMISSIONS: () => ALL_PERMISSIONS,
|
|
33
34
|
COMMAND_BRAND: () => COMMAND_BRAND,
|
|
34
35
|
Client: () => Client,
|
|
35
36
|
Command: () => Command,
|
|
@@ -46,11 +47,15 @@ __export(src_exports, {
|
|
|
46
47
|
ListenerStore: () => ListenerStore,
|
|
47
48
|
Middleware: () => Middleware,
|
|
48
49
|
ModuleLoadError: () => ModuleLoadError,
|
|
50
|
+
OverwriteType: () => OverwriteType,
|
|
51
|
+
PermissionFlags: () => PermissionFlags,
|
|
52
|
+
Permissions: () => Permissions,
|
|
49
53
|
PlatformClient: () => PlatformClient,
|
|
50
54
|
RestClient: () => RestClient,
|
|
51
55
|
VoiceManager: () => VoiceManager,
|
|
52
56
|
VoiceUnavailableError: () => VoiceUnavailableError,
|
|
53
57
|
collectFiles: () => collectFiles,
|
|
58
|
+
computePermissions: () => computePermissions,
|
|
54
59
|
createCommand: () => createCommand,
|
|
55
60
|
createCommandContext: () => createCommandContext,
|
|
56
61
|
createEvent: () => createEvent,
|
|
@@ -58,12 +63,19 @@ __export(src_exports, {
|
|
|
58
63
|
createMiddleware: () => createMiddleware,
|
|
59
64
|
defaultExtensions: () => defaultExtensions,
|
|
60
65
|
formatDuration: () => formatDuration,
|
|
66
|
+
hasAnyPermission: () => hasAnyPermission,
|
|
67
|
+
hasPermission: () => hasPermission,
|
|
61
68
|
isBun: () => isBun,
|
|
62
69
|
isCommand: () => isCommand,
|
|
63
70
|
isEvent: () => isEvent,
|
|
64
71
|
isListener: () => isListener,
|
|
65
72
|
loadDirectory: () => loadDirectory,
|
|
66
73
|
loadFile: () => loadFile,
|
|
74
|
+
memberRoleIds: () => memberRoleIds,
|
|
75
|
+
normalizeOverwrite: () => normalizeOverwrite,
|
|
76
|
+
readOverwrites: () => readOverwrites,
|
|
77
|
+
resolvePermission: () => resolvePermission,
|
|
78
|
+
resolvePermissions: () => resolvePermissions,
|
|
67
79
|
runtimeName: () => runtimeName,
|
|
68
80
|
tokenize: () => tokenize
|
|
69
81
|
});
|
|
@@ -464,7 +476,8 @@ var Events = {
|
|
|
464
476
|
VoiceStateUpdate: "voiceStateUpdate",
|
|
465
477
|
VoiceServerUpdate: "voiceServerUpdate",
|
|
466
478
|
VoiceStateAck: "voiceStateAck",
|
|
467
|
-
VoiceStatesSync: "voiceStatesSync"
|
|
479
|
+
VoiceStatesSync: "voiceStatesSync",
|
|
480
|
+
GuildDelete: "guildDelete"
|
|
468
481
|
};
|
|
469
482
|
|
|
470
483
|
// src/voice/manager.ts
|
|
@@ -686,19 +699,239 @@ var Gateway = class extends import_node_events.EventEmitter {
|
|
|
686
699
|
}
|
|
687
700
|
};
|
|
688
701
|
|
|
702
|
+
// src/platform/permissions.ts
|
|
703
|
+
var PermissionFlags = {
|
|
704
|
+
CreateInstantInvite: 1n << 0n,
|
|
705
|
+
KickMembers: 1n << 1n,
|
|
706
|
+
BanMembers: 1n << 2n,
|
|
707
|
+
Administrator: 1n << 3n,
|
|
708
|
+
ManageChannels: 1n << 4n,
|
|
709
|
+
ManageGuild: 1n << 5n,
|
|
710
|
+
AddReactions: 1n << 6n,
|
|
711
|
+
ViewAuditLog: 1n << 7n,
|
|
712
|
+
PrioritySpeaker: 1n << 8n,
|
|
713
|
+
Stream: 1n << 9n,
|
|
714
|
+
ViewChannel: 1n << 10n,
|
|
715
|
+
SendMessages: 1n << 11n,
|
|
716
|
+
SendTtsMessages: 1n << 12n,
|
|
717
|
+
ManageMessages: 1n << 13n,
|
|
718
|
+
EmbedLinks: 1n << 14n,
|
|
719
|
+
AttachFiles: 1n << 15n,
|
|
720
|
+
ReadMessageHistory: 1n << 16n,
|
|
721
|
+
MentionEveryone: 1n << 17n,
|
|
722
|
+
UseExternalEmojis: 1n << 18n,
|
|
723
|
+
ViewGuildInsights: 1n << 19n,
|
|
724
|
+
Connect: 1n << 20n,
|
|
725
|
+
Speak: 1n << 21n,
|
|
726
|
+
MuteMembers: 1n << 22n,
|
|
727
|
+
DeafenMembers: 1n << 23n,
|
|
728
|
+
MoveMembers: 1n << 24n,
|
|
729
|
+
UseVad: 1n << 25n,
|
|
730
|
+
ChangeNickname: 1n << 26n,
|
|
731
|
+
ManageNicknames: 1n << 27n,
|
|
732
|
+
ManageRoles: 1n << 28n,
|
|
733
|
+
ManageWebhooks: 1n << 29n,
|
|
734
|
+
ManageGuildExpressions: 1n << 30n,
|
|
735
|
+
UseApplicationCommands: 1n << 31n,
|
|
736
|
+
RequestToSpeak: 1n << 32n,
|
|
737
|
+
ManageEvents: 1n << 33n,
|
|
738
|
+
ManageThreads: 1n << 34n,
|
|
739
|
+
CreatePublicThreads: 1n << 35n,
|
|
740
|
+
CreatePrivateThreads: 1n << 36n,
|
|
741
|
+
UseExternalStickers: 1n << 37n,
|
|
742
|
+
SendMessagesInThreads: 1n << 38n,
|
|
743
|
+
UseEmbeddedActivities: 1n << 39n,
|
|
744
|
+
ModerateMembers: 1n << 40n
|
|
745
|
+
};
|
|
746
|
+
var ALL_PERMISSIONS = (1n << 64n) - 1n;
|
|
747
|
+
var OverwriteType = {
|
|
748
|
+
Role: 0,
|
|
749
|
+
Member: 1
|
|
750
|
+
};
|
|
751
|
+
function resolvePermission(value) {
|
|
752
|
+
if (typeof value === "bigint") return value;
|
|
753
|
+
if (typeof value === "number") {
|
|
754
|
+
return Number.isFinite(value) ? BigInt(Math.trunc(value)) : 0n;
|
|
755
|
+
}
|
|
756
|
+
if (typeof value === "string") {
|
|
757
|
+
const trimmed = value.trim();
|
|
758
|
+
if (!trimmed) return 0n;
|
|
759
|
+
const named = PermissionFlags[trimmed];
|
|
760
|
+
if (named !== void 0) return named;
|
|
761
|
+
try {
|
|
762
|
+
return BigInt(trimmed);
|
|
763
|
+
} catch {
|
|
764
|
+
return 0n;
|
|
765
|
+
}
|
|
766
|
+
}
|
|
767
|
+
if (value instanceof Permissions) return value.bitfield;
|
|
768
|
+
return 0n;
|
|
769
|
+
}
|
|
770
|
+
function resolvePermissions(...values) {
|
|
771
|
+
let bitfield = 0n;
|
|
772
|
+
for (const value of values.flat()) bitfield |= resolvePermission(value);
|
|
773
|
+
return bitfield;
|
|
774
|
+
}
|
|
775
|
+
function hasPermission(bitfield, ...flags) {
|
|
776
|
+
const bits = resolvePermission(bitfield);
|
|
777
|
+
return flags.every((flag) => {
|
|
778
|
+
const required = resolvePermission(flag);
|
|
779
|
+
return (bits & required) === required;
|
|
780
|
+
});
|
|
781
|
+
}
|
|
782
|
+
function hasAnyPermission(bitfield, ...flags) {
|
|
783
|
+
const bits = resolvePermission(bitfield);
|
|
784
|
+
return flags.some((flag) => (bits & resolvePermission(flag)) !== 0n);
|
|
785
|
+
}
|
|
786
|
+
var Permissions = class {
|
|
787
|
+
bitfield;
|
|
788
|
+
constructor(value = 0n) {
|
|
789
|
+
this.bitfield = resolvePermission(value);
|
|
790
|
+
Object.freeze(this);
|
|
791
|
+
}
|
|
792
|
+
/** `true` when every given flag is present. */
|
|
793
|
+
has(...flags) {
|
|
794
|
+
return hasPermission(this.bitfield, ...flags);
|
|
795
|
+
}
|
|
796
|
+
/** `true` when at least one of the given flags is present. */
|
|
797
|
+
any(...flags) {
|
|
798
|
+
return hasAnyPermission(this.bitfield, ...flags);
|
|
799
|
+
}
|
|
800
|
+
/** Names of the given flags that are not present. */
|
|
801
|
+
missing(...flags) {
|
|
802
|
+
const missing = [];
|
|
803
|
+
for (const [name, bit] of Object.entries(PermissionFlags)) {
|
|
804
|
+
const requested = flags.some((flag) => resolvePermission(flag) === bit);
|
|
805
|
+
if (requested && (this.bitfield & bit) === 0n) missing.push(name);
|
|
806
|
+
}
|
|
807
|
+
return missing;
|
|
808
|
+
}
|
|
809
|
+
/** Names of every single-bit permission that is present. */
|
|
810
|
+
toArray() {
|
|
811
|
+
const names = [];
|
|
812
|
+
for (const [name, bit] of Object.entries(PermissionFlags)) {
|
|
813
|
+
if ((this.bitfield & bit) === bit) names.push(name);
|
|
814
|
+
}
|
|
815
|
+
return names;
|
|
816
|
+
}
|
|
817
|
+
equals(other) {
|
|
818
|
+
return this.bitfield === resolvePermission(other);
|
|
819
|
+
}
|
|
820
|
+
toString() {
|
|
821
|
+
return this.bitfield.toString();
|
|
822
|
+
}
|
|
823
|
+
toJSON() {
|
|
824
|
+
return this.toString();
|
|
825
|
+
}
|
|
826
|
+
valueOf() {
|
|
827
|
+
return this.bitfield;
|
|
828
|
+
}
|
|
829
|
+
[Symbol.toPrimitive]() {
|
|
830
|
+
return this.bitfield;
|
|
831
|
+
}
|
|
832
|
+
};
|
|
833
|
+
var normalizeOverwrite = (raw) => ({
|
|
834
|
+
id: String(raw?.id ?? ""),
|
|
835
|
+
type: Number(raw?.type ?? OverwriteType.Role),
|
|
836
|
+
allow: resolvePermission(raw?.allow ?? 0n),
|
|
837
|
+
deny: resolvePermission(raw?.deny ?? 0n)
|
|
838
|
+
});
|
|
839
|
+
function readOverwrites(channel) {
|
|
840
|
+
if (!channel) return [];
|
|
841
|
+
const manager = channel.permissionOverwrites;
|
|
842
|
+
if (manager instanceof Map) {
|
|
843
|
+
return [...manager.values()].map(normalizeOverwrite);
|
|
844
|
+
}
|
|
845
|
+
if (manager?.cache instanceof Map) {
|
|
846
|
+
return [...manager.cache.values()].map(normalizeOverwrite);
|
|
847
|
+
}
|
|
848
|
+
if (typeof manager?.values === "function") {
|
|
849
|
+
return [...manager.values()].map(normalizeOverwrite);
|
|
850
|
+
}
|
|
851
|
+
const raw = Array.isArray(channel.permission_overwrites) ? channel.permission_overwrites : Array.isArray(manager) ? manager : [];
|
|
852
|
+
return raw.map(normalizeOverwrite);
|
|
853
|
+
}
|
|
854
|
+
var memberRoleIds = (member) => {
|
|
855
|
+
const roles = member?.roles;
|
|
856
|
+
if (!Array.isArray(roles)) return [];
|
|
857
|
+
return roles.map((role) => typeof role === "string" ? role : role?.id).filter((id) => typeof id === "string" && id.length > 0);
|
|
858
|
+
};
|
|
859
|
+
function computePermissions(guild, member, channel = null) {
|
|
860
|
+
const guildId = String(guild?.id ?? "");
|
|
861
|
+
const memberId = String(member?.id ?? member?.user?.id ?? "");
|
|
862
|
+
const ownerId = String(guild?.ownerId ?? guild?.owner_id ?? "");
|
|
863
|
+
if (ownerId && ownerId === memberId) return new Permissions(ALL_PERMISSIONS);
|
|
864
|
+
const roles = Array.isArray(guild?.roles) ? guild.roles : [];
|
|
865
|
+
const roleIds = new Set(memberRoleIds(member));
|
|
866
|
+
let bitfield = 0n;
|
|
867
|
+
for (const role of roles) {
|
|
868
|
+
const roleId = String(role?.id ?? "");
|
|
869
|
+
if (!roleId) continue;
|
|
870
|
+
if (roleId === guildId || roleIds.has(roleId)) {
|
|
871
|
+
bitfield |= resolvePermission(role.permissions ?? 0n);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
if ((bitfield & PermissionFlags.Administrator) === PermissionFlags.Administrator) {
|
|
875
|
+
return new Permissions(ALL_PERMISSIONS);
|
|
876
|
+
}
|
|
877
|
+
if (!channel) return new Permissions(bitfield);
|
|
878
|
+
const overwrites = readOverwrites(channel);
|
|
879
|
+
const everyone = overwrites.find(
|
|
880
|
+
(overwrite) => overwrite.id === guildId && overwrite.type === OverwriteType.Role
|
|
881
|
+
);
|
|
882
|
+
if (everyone) bitfield = bitfield & ~everyone.deny | everyone.allow;
|
|
883
|
+
let roleAllow = 0n;
|
|
884
|
+
let roleDeny = 0n;
|
|
885
|
+
for (const overwrite of overwrites) {
|
|
886
|
+
if (overwrite.type !== OverwriteType.Role) continue;
|
|
887
|
+
if (overwrite.id === guildId) continue;
|
|
888
|
+
if (!roleIds.has(overwrite.id)) continue;
|
|
889
|
+
roleAllow |= overwrite.allow;
|
|
890
|
+
roleDeny |= overwrite.deny;
|
|
891
|
+
}
|
|
892
|
+
bitfield = bitfield & ~roleDeny | roleAllow;
|
|
893
|
+
const memberOverwrite = overwrites.find(
|
|
894
|
+
(overwrite) => overwrite.id === memberId && overwrite.type === OverwriteType.Member
|
|
895
|
+
);
|
|
896
|
+
if (memberOverwrite) {
|
|
897
|
+
bitfield = bitfield & ~memberOverwrite.deny | memberOverwrite.allow;
|
|
898
|
+
}
|
|
899
|
+
return new Permissions(bitfield);
|
|
900
|
+
}
|
|
901
|
+
|
|
689
902
|
// src/platform/client.ts
|
|
690
903
|
var import_node_events2 = require("events");
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
904
|
+
var LOG_LEVELS = {
|
|
905
|
+
debug: 10,
|
|
906
|
+
info: 20,
|
|
907
|
+
warn: 30,
|
|
908
|
+
error: 40
|
|
909
|
+
};
|
|
910
|
+
function makeLogger(level = "info") {
|
|
911
|
+
const threshold = LOG_LEVELS[level] ?? LOG_LEVELS.info;
|
|
912
|
+
const wrap = (name) => (msg, data) => {
|
|
913
|
+
if (LOG_LEVELS[name] < threshold) return;
|
|
914
|
+
let line = msg;
|
|
915
|
+
if (data !== void 0) {
|
|
916
|
+
try {
|
|
917
|
+
line = `${msg} ${JSON.stringify(data)}`;
|
|
918
|
+
} catch {
|
|
919
|
+
line = `${msg} [unserializable]`;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
if (name === "error") console.error(line);
|
|
923
|
+
else if (name === "warn") console.warn(line);
|
|
924
|
+
else console.log(line);
|
|
695
925
|
};
|
|
696
|
-
return {
|
|
926
|
+
return { debug: wrap("debug"), info: wrap("info"), warn: wrap("warn"), error: wrap("error") };
|
|
697
927
|
}
|
|
698
928
|
function makeMessage(client, raw) {
|
|
699
929
|
const author = raw.author ?? { id: raw.author_id };
|
|
700
930
|
const channelId = raw.channel_id;
|
|
701
|
-
const
|
|
931
|
+
const cached = client.channels.get(channelId);
|
|
932
|
+
const guildId = raw.guild_id ?? cached?.guildId ?? null;
|
|
933
|
+
const guild = guildId ? client.guilds.get(guildId) ?? null : null;
|
|
934
|
+
const channel = cached ?? client.wrapChannel({ id: channelId, guildId, type: 0 }, guild);
|
|
702
935
|
const message = {
|
|
703
936
|
...raw,
|
|
704
937
|
id: raw.id,
|
|
@@ -706,15 +939,9 @@ function makeMessage(client, raw) {
|
|
|
706
939
|
author,
|
|
707
940
|
guildId,
|
|
708
941
|
channelId,
|
|
709
|
-
channel
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
guild: guildId ? client.guilds.get(guildId) ?? null : null,
|
|
713
|
-
send: (options) => client.sendMessage(channelId, options),
|
|
714
|
-
isVoice: () => false
|
|
715
|
-
},
|
|
716
|
-
guild: guildId ? client.guilds.get(guildId) ?? null : null,
|
|
717
|
-
member: raw.member ? { ...raw.member, user: author } : null,
|
|
942
|
+
channel,
|
|
943
|
+
guild,
|
|
944
|
+
member: raw.member ? guild ? client.wrapMember(raw.member, guild) : { ...raw.member, user: author } : null,
|
|
718
945
|
reply: async (options) => {
|
|
719
946
|
const payload = typeof options === "string" ? { content: options } : { ...options };
|
|
720
947
|
payload.message_reference = { message_id: raw.id, channel_id: channelId, ...guildId ? { guild_id: guildId } : {} };
|
|
@@ -725,25 +952,163 @@ function makeMessage(client, raw) {
|
|
|
725
952
|
}
|
|
726
953
|
var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
727
954
|
rest = new RestClient();
|
|
728
|
-
logger
|
|
955
|
+
logger;
|
|
729
956
|
guilds = /* @__PURE__ */ new Map();
|
|
730
957
|
channels = /* @__PURE__ */ new Map();
|
|
731
958
|
users = /* @__PURE__ */ new Map();
|
|
732
959
|
user = null;
|
|
733
960
|
ws = null;
|
|
734
961
|
options;
|
|
962
|
+
readyAt = null;
|
|
735
963
|
readyResolve = null;
|
|
736
964
|
constructor(options = {}) {
|
|
737
965
|
super();
|
|
738
966
|
this.options = options;
|
|
967
|
+
this.logger = makeLogger(options.logLevel ?? "info");
|
|
739
968
|
if (options.apiBase) this.rest.baseUrl = options.apiBase;
|
|
969
|
+
this.channels.fetch = async (id) => {
|
|
970
|
+
const cached = this.channels.get(id);
|
|
971
|
+
if (cached) return cached;
|
|
972
|
+
const raw = await this.rest.get(`/channels/${id}`);
|
|
973
|
+
const ch = this.wrapChannel(raw);
|
|
974
|
+
this.channels.set(ch.id, ch);
|
|
975
|
+
return ch;
|
|
976
|
+
};
|
|
977
|
+
this.guilds.fetch = async (id) => {
|
|
978
|
+
const cached = this.guilds.get(id);
|
|
979
|
+
if (cached) return cached;
|
|
980
|
+
const raw = await this.rest.get(`/guilds/${id}`);
|
|
981
|
+
const g = this.wrapGuild(raw);
|
|
982
|
+
this.guilds.set(g.id, g);
|
|
983
|
+
return g;
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
get uptime() {
|
|
987
|
+
return this.readyAt ? Date.now() - this.readyAt : null;
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* Normalize a guild payload. Gateway payloads often omit `owner_id` and
|
|
991
|
+
* deliver members as an array, so both are handled here.
|
|
992
|
+
*/
|
|
993
|
+
wrapGuild(guild) {
|
|
994
|
+
if (!guild) return guild;
|
|
995
|
+
const g = { ...guild };
|
|
996
|
+
g.shardId = g.shardId ?? 0;
|
|
997
|
+
g.ownerId = g.ownerId ?? g.owner_id ?? null;
|
|
998
|
+
g.roles = Array.isArray(g.roles) ? g.roles : [];
|
|
999
|
+
g.iconURL = (opts) => g.icon ? `https://fluxerusercontent.com/icons/${g.id}/${g.icon}${opts?.extension ? "." + opts.extension : ""}` : null;
|
|
1000
|
+
const rawMembers = g.members;
|
|
1001
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1002
|
+
const members = { cache, me: null };
|
|
1003
|
+
members.get = (id) => cache.get(String(id)) ?? null;
|
|
1004
|
+
members.fetch = async (id) => {
|
|
1005
|
+
const cached = cache.get(String(id));
|
|
1006
|
+
if (cached) return cached;
|
|
1007
|
+
const raw = await this.rest.get(`/guilds/${g.id}/members/${id}`);
|
|
1008
|
+
const member = this.wrapMember(raw, g);
|
|
1009
|
+
if (member?.id) cache.set(member.id, member);
|
|
1010
|
+
return member;
|
|
1011
|
+
};
|
|
1012
|
+
members.fetchMe = async () => {
|
|
1013
|
+
const raw = await this.rest.get(`/guilds/${g.id}/members/@me`);
|
|
1014
|
+
const member = this.wrapMember(raw, g);
|
|
1015
|
+
if (member?.id) cache.set(member.id, member);
|
|
1016
|
+
members.me = member;
|
|
1017
|
+
return member;
|
|
1018
|
+
};
|
|
1019
|
+
if (Array.isArray(rawMembers)) {
|
|
1020
|
+
for (const raw of rawMembers) {
|
|
1021
|
+
const member = this.wrapMember(raw, g);
|
|
1022
|
+
if (member?.id) cache.set(member.id, member);
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
const meId = String(this.user?.id ?? "");
|
|
1026
|
+
members.me = meId ? cache.get(meId) ?? null : null;
|
|
1027
|
+
g.members = members;
|
|
1028
|
+
g.channels = g.channels && !Array.isArray(g.channels) ? { ...g.channels } : {};
|
|
1029
|
+
g.channels.cache = this.channels;
|
|
1030
|
+
g.channels.get = (id) => this.channels.get(id);
|
|
1031
|
+
g.channels.fetch = (id) => this.channels.fetch(id);
|
|
1032
|
+
return g;
|
|
1033
|
+
}
|
|
1034
|
+
/** Attach permission helpers to a raw member payload. */
|
|
1035
|
+
wrapMember(raw, guild) {
|
|
1036
|
+
if (!raw) return raw;
|
|
1037
|
+
const member = { ...raw };
|
|
1038
|
+
member.id = String(member.id ?? member.user?.id ?? "");
|
|
1039
|
+
if (!member.user && member.id) member.user = { id: member.id };
|
|
1040
|
+
Object.defineProperty(member, "permissions", {
|
|
1041
|
+
enumerable: true,
|
|
1042
|
+
configurable: true,
|
|
1043
|
+
get: () => computePermissions(guild, member)
|
|
1044
|
+
});
|
|
1045
|
+
member.permissionsIn = (channel) => computePermissions(guild, member, channel);
|
|
1046
|
+
member.has = (...flags) => member.permissions.has(...flags);
|
|
1047
|
+
return member;
|
|
1048
|
+
}
|
|
1049
|
+
wrapChannel(ch, guild) {
|
|
1050
|
+
if (!ch) return ch;
|
|
1051
|
+
const c = { ...ch };
|
|
1052
|
+
c.guildId = c.guildId ?? c.guild_id ?? guild?.id;
|
|
1053
|
+
c.guild = guild ?? (c.guildId ? this.guilds.get(c.guildId) ?? null : c.guild ?? null);
|
|
1054
|
+
c.isVoice = () => [2, 13].includes(c.type);
|
|
1055
|
+
c.isTextBased = () => [0, 1, 5, 10, 11, 12].includes(c.type);
|
|
1056
|
+
c.send = (options) => this.sendMessage(c.id, options);
|
|
1057
|
+
const rawOverwrites = Array.isArray(ch.permission_overwrites) ? ch.permission_overwrites : [];
|
|
1058
|
+
const manager = this.createOverwriteManager(c.id, rawOverwrites);
|
|
1059
|
+
c.permissionOverwrites = manager;
|
|
1060
|
+
Object.defineProperty(c, "permission_overwrites", {
|
|
1061
|
+
enumerable: true,
|
|
1062
|
+
configurable: true,
|
|
1063
|
+
get: () => manager.toArray()
|
|
1064
|
+
});
|
|
1065
|
+
return c;
|
|
1066
|
+
}
|
|
1067
|
+
/** Overwrite manager backed by `PUT/DELETE /channels/:id/permissions/:id`. */
|
|
1068
|
+
createOverwriteManager(channelId, raw) {
|
|
1069
|
+
const cache = /* @__PURE__ */ new Map();
|
|
1070
|
+
for (const item of raw) {
|
|
1071
|
+
const overwrite = normalizeOverwrite(item);
|
|
1072
|
+
if (overwrite.id) cache.set(overwrite.id, overwrite);
|
|
1073
|
+
}
|
|
1074
|
+
const write = async (id, options = {}) => {
|
|
1075
|
+
const overwriteId = String(id);
|
|
1076
|
+
const existing = cache.get(overwriteId);
|
|
1077
|
+
const type = options.type ?? existing?.type ?? OverwriteType.Role;
|
|
1078
|
+
const allow = resolvePermission(options.allow ?? 0n);
|
|
1079
|
+
const deny = resolvePermission(options.deny ?? 0n);
|
|
1080
|
+
await this.rest.put(`/channels/${channelId}/permissions/${overwriteId}`, {
|
|
1081
|
+
type,
|
|
1082
|
+
allow: allow.toString(),
|
|
1083
|
+
deny: deny.toString()
|
|
1084
|
+
});
|
|
1085
|
+
const overwrite = { id: overwriteId, type, allow, deny };
|
|
1086
|
+
cache.set(overwriteId, overwrite);
|
|
1087
|
+
return overwrite;
|
|
1088
|
+
};
|
|
1089
|
+
return {
|
|
1090
|
+
cache,
|
|
1091
|
+
get: (id) => cache.get(String(id)) ?? null,
|
|
1092
|
+
has: (id) => cache.has(String(id)),
|
|
1093
|
+
values: () => cache.values(),
|
|
1094
|
+
toArray: () => [...cache.values()],
|
|
1095
|
+
edit: write,
|
|
1096
|
+
create: write,
|
|
1097
|
+
delete: async (id) => {
|
|
1098
|
+
const overwriteId = String(id);
|
|
1099
|
+
await this.rest.delete(`/channels/${channelId}/permissions/${overwriteId}`);
|
|
1100
|
+
cache.delete(overwriteId);
|
|
1101
|
+
}
|
|
1102
|
+
};
|
|
740
1103
|
}
|
|
741
1104
|
async login(token, _options) {
|
|
742
1105
|
this.rest.setToken(token);
|
|
743
|
-
|
|
744
|
-
this.ws
|
|
745
|
-
this.ws.
|
|
746
|
-
|
|
1106
|
+
const gw = new Gateway({ token, url: this.options.gatewayUrl, intents: this.options.intents });
|
|
1107
|
+
this.ws = gw;
|
|
1108
|
+
this.ws.ping = -1;
|
|
1109
|
+
gw.on("dispatch", (t, d) => this.handleDispatch(t, d));
|
|
1110
|
+
gw.on("error", (e) => this.emit("error", e));
|
|
1111
|
+
await gw.connect();
|
|
747
1112
|
await new Promise((resolve3) => {
|
|
748
1113
|
this.readyResolve = resolve3;
|
|
749
1114
|
});
|
|
@@ -756,18 +1121,20 @@ var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
|
756
1121
|
this.ws = null;
|
|
757
1122
|
}
|
|
758
1123
|
sendToGateway(_shardId, payload) {
|
|
759
|
-
this.logger.debug
|
|
760
|
-
console.log("[gateway send]", JSON.stringify(payload));
|
|
1124
|
+
this.logger.debug("[gateway send]", payload);
|
|
761
1125
|
this.ws?.send(payload);
|
|
762
1126
|
}
|
|
763
1127
|
handleDispatch(t, d) {
|
|
764
1128
|
this.emit("raw", { t, d });
|
|
765
|
-
if (t.includes("VOICE"))
|
|
1129
|
+
if (t.includes("VOICE")) this.logger.debug("[voice dispatch]", { t, d });
|
|
766
1130
|
switch (t) {
|
|
767
1131
|
case "READY": {
|
|
768
1132
|
this.user = d?.user ?? null;
|
|
1133
|
+
if (this.user) this.user.setPresence = async (_p) => {
|
|
1134
|
+
};
|
|
769
1135
|
if (this.user) this.users.set(this.user.id, this.user);
|
|
770
|
-
for (const g of d?.guilds ?? []) this.guilds.set(g.id, { ...g, shardId: 0 });
|
|
1136
|
+
for (const g of d?.guilds ?? []) this.guilds.set(g.id, this.wrapGuild({ ...g, shardId: 0 }));
|
|
1137
|
+
this.readyAt = Date.now();
|
|
771
1138
|
this.emit(Events.Ready);
|
|
772
1139
|
this.readyResolve?.();
|
|
773
1140
|
this.readyResolve = null;
|
|
@@ -788,16 +1155,20 @@ var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
|
788
1155
|
return;
|
|
789
1156
|
}
|
|
790
1157
|
case "GUILD_CREATE": {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
1158
|
+
void this.handleGuildCreate(d).catch((error) => {
|
|
1159
|
+
this.logger.warn("Could not load guild", { guildId: d?.id, error: String(error) });
|
|
1160
|
+
});
|
|
1161
|
+
return;
|
|
1162
|
+
}
|
|
1163
|
+
case "GUILD_DELETE": {
|
|
1164
|
+
this.guilds.delete(d?.id);
|
|
1165
|
+
this.emit(Events.GuildDelete ?? "guildDelete", d);
|
|
796
1166
|
return;
|
|
797
1167
|
}
|
|
798
1168
|
case "CHANNEL_CREATE":
|
|
799
1169
|
case "CHANNEL_UPDATE": {
|
|
800
|
-
const
|
|
1170
|
+
const guild = d.guild_id ? this.guilds.get(d.guild_id) ?? null : null;
|
|
1171
|
+
const ch = this.wrapChannel({ ...d, guildId: d.guild_id, guild }, guild);
|
|
801
1172
|
this.channels.set(ch.id, ch);
|
|
802
1173
|
return;
|
|
803
1174
|
}
|
|
@@ -814,6 +1185,37 @@ var PlatformClient = class extends import_node_events2.EventEmitter {
|
|
|
814
1185
|
this.emit(t.toLowerCase().replace(/_([a-z])/g, (_, c) => c.toUpperCase()), d);
|
|
815
1186
|
}
|
|
816
1187
|
}
|
|
1188
|
+
/**
|
|
1189
|
+
* Cache a gateway guild. Gateway payloads can include `roles` but omit
|
|
1190
|
+
* `owner_id`, so the REST guild is fetched once and merged before the
|
|
1191
|
+
* `guildCreate` event is emitted.
|
|
1192
|
+
*/
|
|
1193
|
+
async handleGuildCreate(raw) {
|
|
1194
|
+
if (!raw?.id) return;
|
|
1195
|
+
let data = raw;
|
|
1196
|
+
if (!raw.owner_id && !raw.ownerId) {
|
|
1197
|
+
const fetched = await this.rest.get(`/guilds/${raw.id}`).catch(() => null);
|
|
1198
|
+
if (fetched) {
|
|
1199
|
+
data = {
|
|
1200
|
+
...raw,
|
|
1201
|
+
...fetched,
|
|
1202
|
+
channels: raw.channels ?? fetched.channels,
|
|
1203
|
+
members: raw.members ?? fetched.members
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
const guild = this.wrapGuild({ ...data, shardId: 0 });
|
|
1208
|
+
this.guilds.set(guild.id, guild);
|
|
1209
|
+
for (const ch of data?.channels ?? []) {
|
|
1210
|
+
const channel = this.wrapChannel({ ...ch, guildId: guild.id }, guild);
|
|
1211
|
+
this.channels.set(channel.id, channel);
|
|
1212
|
+
}
|
|
1213
|
+
this.emit(Events.GuildCreate, guild);
|
|
1214
|
+
this.emit(Events.VoiceStatesSync, {
|
|
1215
|
+
guildId: guild.id,
|
|
1216
|
+
voiceStates: data?.voice_states ?? []
|
|
1217
|
+
});
|
|
1218
|
+
}
|
|
817
1219
|
async sendMessage(channelId, options) {
|
|
818
1220
|
const payload = typeof options === "string" ? { content: options } : options;
|
|
819
1221
|
const raw = await this.rest.post(`/channels/${channelId}/messages`, payload);
|
|
@@ -1124,6 +1526,7 @@ var Client = class extends PlatformClient {
|
|
|
1124
1526
|
};
|
|
1125
1527
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1126
1528
|
0 && (module.exports = {
|
|
1529
|
+
ALL_PERMISSIONS,
|
|
1127
1530
|
COMMAND_BRAND,
|
|
1128
1531
|
Client,
|
|
1129
1532
|
Command,
|
|
@@ -1140,11 +1543,15 @@ var Client = class extends PlatformClient {
|
|
|
1140
1543
|
ListenerStore,
|
|
1141
1544
|
Middleware,
|
|
1142
1545
|
ModuleLoadError,
|
|
1546
|
+
OverwriteType,
|
|
1547
|
+
PermissionFlags,
|
|
1548
|
+
Permissions,
|
|
1143
1549
|
PlatformClient,
|
|
1144
1550
|
RestClient,
|
|
1145
1551
|
VoiceManager,
|
|
1146
1552
|
VoiceUnavailableError,
|
|
1147
1553
|
collectFiles,
|
|
1554
|
+
computePermissions,
|
|
1148
1555
|
createCommand,
|
|
1149
1556
|
createCommandContext,
|
|
1150
1557
|
createEvent,
|
|
@@ -1152,12 +1559,19 @@ var Client = class extends PlatformClient {
|
|
|
1152
1559
|
createMiddleware,
|
|
1153
1560
|
defaultExtensions,
|
|
1154
1561
|
formatDuration,
|
|
1562
|
+
hasAnyPermission,
|
|
1563
|
+
hasPermission,
|
|
1155
1564
|
isBun,
|
|
1156
1565
|
isCommand,
|
|
1157
1566
|
isEvent,
|
|
1158
1567
|
isListener,
|
|
1159
1568
|
loadDirectory,
|
|
1160
1569
|
loadFile,
|
|
1570
|
+
memberRoleIds,
|
|
1571
|
+
normalizeOverwrite,
|
|
1572
|
+
readOverwrites,
|
|
1573
|
+
resolvePermission,
|
|
1574
|
+
resolvePermissions,
|
|
1161
1575
|
runtimeName,
|
|
1162
1576
|
tokenize
|
|
1163
1577
|
});
|