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.js
CHANGED
|
@@ -393,7 +393,8 @@ var Events = {
|
|
|
393
393
|
VoiceStateUpdate: "voiceStateUpdate",
|
|
394
394
|
VoiceServerUpdate: "voiceServerUpdate",
|
|
395
395
|
VoiceStateAck: "voiceStateAck",
|
|
396
|
-
VoiceStatesSync: "voiceStatesSync"
|
|
396
|
+
VoiceStatesSync: "voiceStatesSync",
|
|
397
|
+
GuildDelete: "guildDelete"
|
|
397
398
|
};
|
|
398
399
|
|
|
399
400
|
// src/voice/manager.ts
|
|
@@ -615,19 +616,239 @@ var Gateway = class extends EventEmitter {
|
|
|
615
616
|
}
|
|
616
617
|
};
|
|
617
618
|
|
|
619
|
+
// src/platform/permissions.ts
|
|
620
|
+
var PermissionFlags = {
|
|
621
|
+
CreateInstantInvite: 1n << 0n,
|
|
622
|
+
KickMembers: 1n << 1n,
|
|
623
|
+
BanMembers: 1n << 2n,
|
|
624
|
+
Administrator: 1n << 3n,
|
|
625
|
+
ManageChannels: 1n << 4n,
|
|
626
|
+
ManageGuild: 1n << 5n,
|
|
627
|
+
AddReactions: 1n << 6n,
|
|
628
|
+
ViewAuditLog: 1n << 7n,
|
|
629
|
+
PrioritySpeaker: 1n << 8n,
|
|
630
|
+
Stream: 1n << 9n,
|
|
631
|
+
ViewChannel: 1n << 10n,
|
|
632
|
+
SendMessages: 1n << 11n,
|
|
633
|
+
SendTtsMessages: 1n << 12n,
|
|
634
|
+
ManageMessages: 1n << 13n,
|
|
635
|
+
EmbedLinks: 1n << 14n,
|
|
636
|
+
AttachFiles: 1n << 15n,
|
|
637
|
+
ReadMessageHistory: 1n << 16n,
|
|
638
|
+
MentionEveryone: 1n << 17n,
|
|
639
|
+
UseExternalEmojis: 1n << 18n,
|
|
640
|
+
ViewGuildInsights: 1n << 19n,
|
|
641
|
+
Connect: 1n << 20n,
|
|
642
|
+
Speak: 1n << 21n,
|
|
643
|
+
MuteMembers: 1n << 22n,
|
|
644
|
+
DeafenMembers: 1n << 23n,
|
|
645
|
+
MoveMembers: 1n << 24n,
|
|
646
|
+
UseVad: 1n << 25n,
|
|
647
|
+
ChangeNickname: 1n << 26n,
|
|
648
|
+
ManageNicknames: 1n << 27n,
|
|
649
|
+
ManageRoles: 1n << 28n,
|
|
650
|
+
ManageWebhooks: 1n << 29n,
|
|
651
|
+
ManageGuildExpressions: 1n << 30n,
|
|
652
|
+
UseApplicationCommands: 1n << 31n,
|
|
653
|
+
RequestToSpeak: 1n << 32n,
|
|
654
|
+
ManageEvents: 1n << 33n,
|
|
655
|
+
ManageThreads: 1n << 34n,
|
|
656
|
+
CreatePublicThreads: 1n << 35n,
|
|
657
|
+
CreatePrivateThreads: 1n << 36n,
|
|
658
|
+
UseExternalStickers: 1n << 37n,
|
|
659
|
+
SendMessagesInThreads: 1n << 38n,
|
|
660
|
+
UseEmbeddedActivities: 1n << 39n,
|
|
661
|
+
ModerateMembers: 1n << 40n
|
|
662
|
+
};
|
|
663
|
+
var ALL_PERMISSIONS = (1n << 64n) - 1n;
|
|
664
|
+
var OverwriteType = {
|
|
665
|
+
Role: 0,
|
|
666
|
+
Member: 1
|
|
667
|
+
};
|
|
668
|
+
function resolvePermission(value) {
|
|
669
|
+
if (typeof value === "bigint") return value;
|
|
670
|
+
if (typeof value === "number") {
|
|
671
|
+
return Number.isFinite(value) ? BigInt(Math.trunc(value)) : 0n;
|
|
672
|
+
}
|
|
673
|
+
if (typeof value === "string") {
|
|
674
|
+
const trimmed = value.trim();
|
|
675
|
+
if (!trimmed) return 0n;
|
|
676
|
+
const named = PermissionFlags[trimmed];
|
|
677
|
+
if (named !== void 0) return named;
|
|
678
|
+
try {
|
|
679
|
+
return BigInt(trimmed);
|
|
680
|
+
} catch {
|
|
681
|
+
return 0n;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
if (value instanceof Permissions) return value.bitfield;
|
|
685
|
+
return 0n;
|
|
686
|
+
}
|
|
687
|
+
function resolvePermissions(...values) {
|
|
688
|
+
let bitfield = 0n;
|
|
689
|
+
for (const value of values.flat()) bitfield |= resolvePermission(value);
|
|
690
|
+
return bitfield;
|
|
691
|
+
}
|
|
692
|
+
function hasPermission(bitfield, ...flags) {
|
|
693
|
+
const bits = resolvePermission(bitfield);
|
|
694
|
+
return flags.every((flag) => {
|
|
695
|
+
const required = resolvePermission(flag);
|
|
696
|
+
return (bits & required) === required;
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
function hasAnyPermission(bitfield, ...flags) {
|
|
700
|
+
const bits = resolvePermission(bitfield);
|
|
701
|
+
return flags.some((flag) => (bits & resolvePermission(flag)) !== 0n);
|
|
702
|
+
}
|
|
703
|
+
var Permissions = class {
|
|
704
|
+
bitfield;
|
|
705
|
+
constructor(value = 0n) {
|
|
706
|
+
this.bitfield = resolvePermission(value);
|
|
707
|
+
Object.freeze(this);
|
|
708
|
+
}
|
|
709
|
+
/** `true` when every given flag is present. */
|
|
710
|
+
has(...flags) {
|
|
711
|
+
return hasPermission(this.bitfield, ...flags);
|
|
712
|
+
}
|
|
713
|
+
/** `true` when at least one of the given flags is present. */
|
|
714
|
+
any(...flags) {
|
|
715
|
+
return hasAnyPermission(this.bitfield, ...flags);
|
|
716
|
+
}
|
|
717
|
+
/** Names of the given flags that are not present. */
|
|
718
|
+
missing(...flags) {
|
|
719
|
+
const missing = [];
|
|
720
|
+
for (const [name, bit] of Object.entries(PermissionFlags)) {
|
|
721
|
+
const requested = flags.some((flag) => resolvePermission(flag) === bit);
|
|
722
|
+
if (requested && (this.bitfield & bit) === 0n) missing.push(name);
|
|
723
|
+
}
|
|
724
|
+
return missing;
|
|
725
|
+
}
|
|
726
|
+
/** Names of every single-bit permission that is present. */
|
|
727
|
+
toArray() {
|
|
728
|
+
const names = [];
|
|
729
|
+
for (const [name, bit] of Object.entries(PermissionFlags)) {
|
|
730
|
+
if ((this.bitfield & bit) === bit) names.push(name);
|
|
731
|
+
}
|
|
732
|
+
return names;
|
|
733
|
+
}
|
|
734
|
+
equals(other) {
|
|
735
|
+
return this.bitfield === resolvePermission(other);
|
|
736
|
+
}
|
|
737
|
+
toString() {
|
|
738
|
+
return this.bitfield.toString();
|
|
739
|
+
}
|
|
740
|
+
toJSON() {
|
|
741
|
+
return this.toString();
|
|
742
|
+
}
|
|
743
|
+
valueOf() {
|
|
744
|
+
return this.bitfield;
|
|
745
|
+
}
|
|
746
|
+
[Symbol.toPrimitive]() {
|
|
747
|
+
return this.bitfield;
|
|
748
|
+
}
|
|
749
|
+
};
|
|
750
|
+
var normalizeOverwrite = (raw) => ({
|
|
751
|
+
id: String(raw?.id ?? ""),
|
|
752
|
+
type: Number(raw?.type ?? OverwriteType.Role),
|
|
753
|
+
allow: resolvePermission(raw?.allow ?? 0n),
|
|
754
|
+
deny: resolvePermission(raw?.deny ?? 0n)
|
|
755
|
+
});
|
|
756
|
+
function readOverwrites(channel) {
|
|
757
|
+
if (!channel) return [];
|
|
758
|
+
const manager = channel.permissionOverwrites;
|
|
759
|
+
if (manager instanceof Map) {
|
|
760
|
+
return [...manager.values()].map(normalizeOverwrite);
|
|
761
|
+
}
|
|
762
|
+
if (manager?.cache instanceof Map) {
|
|
763
|
+
return [...manager.cache.values()].map(normalizeOverwrite);
|
|
764
|
+
}
|
|
765
|
+
if (typeof manager?.values === "function") {
|
|
766
|
+
return [...manager.values()].map(normalizeOverwrite);
|
|
767
|
+
}
|
|
768
|
+
const raw = Array.isArray(channel.permission_overwrites) ? channel.permission_overwrites : Array.isArray(manager) ? manager : [];
|
|
769
|
+
return raw.map(normalizeOverwrite);
|
|
770
|
+
}
|
|
771
|
+
var memberRoleIds = (member) => {
|
|
772
|
+
const roles = member?.roles;
|
|
773
|
+
if (!Array.isArray(roles)) return [];
|
|
774
|
+
return roles.map((role) => typeof role === "string" ? role : role?.id).filter((id) => typeof id === "string" && id.length > 0);
|
|
775
|
+
};
|
|
776
|
+
function computePermissions(guild, member, channel = null) {
|
|
777
|
+
const guildId = String(guild?.id ?? "");
|
|
778
|
+
const memberId = String(member?.id ?? member?.user?.id ?? "");
|
|
779
|
+
const ownerId = String(guild?.ownerId ?? guild?.owner_id ?? "");
|
|
780
|
+
if (ownerId && ownerId === memberId) return new Permissions(ALL_PERMISSIONS);
|
|
781
|
+
const roles = Array.isArray(guild?.roles) ? guild.roles : [];
|
|
782
|
+
const roleIds = new Set(memberRoleIds(member));
|
|
783
|
+
let bitfield = 0n;
|
|
784
|
+
for (const role of roles) {
|
|
785
|
+
const roleId = String(role?.id ?? "");
|
|
786
|
+
if (!roleId) continue;
|
|
787
|
+
if (roleId === guildId || roleIds.has(roleId)) {
|
|
788
|
+
bitfield |= resolvePermission(role.permissions ?? 0n);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
if ((bitfield & PermissionFlags.Administrator) === PermissionFlags.Administrator) {
|
|
792
|
+
return new Permissions(ALL_PERMISSIONS);
|
|
793
|
+
}
|
|
794
|
+
if (!channel) return new Permissions(bitfield);
|
|
795
|
+
const overwrites = readOverwrites(channel);
|
|
796
|
+
const everyone = overwrites.find(
|
|
797
|
+
(overwrite) => overwrite.id === guildId && overwrite.type === OverwriteType.Role
|
|
798
|
+
);
|
|
799
|
+
if (everyone) bitfield = bitfield & ~everyone.deny | everyone.allow;
|
|
800
|
+
let roleAllow = 0n;
|
|
801
|
+
let roleDeny = 0n;
|
|
802
|
+
for (const overwrite of overwrites) {
|
|
803
|
+
if (overwrite.type !== OverwriteType.Role) continue;
|
|
804
|
+
if (overwrite.id === guildId) continue;
|
|
805
|
+
if (!roleIds.has(overwrite.id)) continue;
|
|
806
|
+
roleAllow |= overwrite.allow;
|
|
807
|
+
roleDeny |= overwrite.deny;
|
|
808
|
+
}
|
|
809
|
+
bitfield = bitfield & ~roleDeny | roleAllow;
|
|
810
|
+
const memberOverwrite = overwrites.find(
|
|
811
|
+
(overwrite) => overwrite.id === memberId && overwrite.type === OverwriteType.Member
|
|
812
|
+
);
|
|
813
|
+
if (memberOverwrite) {
|
|
814
|
+
bitfield = bitfield & ~memberOverwrite.deny | memberOverwrite.allow;
|
|
815
|
+
}
|
|
816
|
+
return new Permissions(bitfield);
|
|
817
|
+
}
|
|
818
|
+
|
|
618
819
|
// src/platform/client.ts
|
|
619
820
|
import { EventEmitter as EventEmitter2 } from "events";
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
821
|
+
var LOG_LEVELS = {
|
|
822
|
+
debug: 10,
|
|
823
|
+
info: 20,
|
|
824
|
+
warn: 30,
|
|
825
|
+
error: 40
|
|
826
|
+
};
|
|
827
|
+
function makeLogger(level = "info") {
|
|
828
|
+
const threshold = LOG_LEVELS[level] ?? LOG_LEVELS.info;
|
|
829
|
+
const wrap = (name) => (msg, data) => {
|
|
830
|
+
if (LOG_LEVELS[name] < threshold) return;
|
|
831
|
+
let line = msg;
|
|
832
|
+
if (data !== void 0) {
|
|
833
|
+
try {
|
|
834
|
+
line = `${msg} ${JSON.stringify(data)}`;
|
|
835
|
+
} catch {
|
|
836
|
+
line = `${msg} [unserializable]`;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
if (name === "error") console.error(line);
|
|
840
|
+
else if (name === "warn") console.warn(line);
|
|
841
|
+
else console.log(line);
|
|
624
842
|
};
|
|
625
|
-
return {
|
|
843
|
+
return { debug: wrap("debug"), info: wrap("info"), warn: wrap("warn"), error: wrap("error") };
|
|
626
844
|
}
|
|
627
845
|
function makeMessage(client, raw) {
|
|
628
846
|
const author = raw.author ?? { id: raw.author_id };
|
|
629
847
|
const channelId = raw.channel_id;
|
|
630
|
-
const
|
|
848
|
+
const cached = client.channels.get(channelId);
|
|
849
|
+
const guildId = raw.guild_id ?? cached?.guildId ?? null;
|
|
850
|
+
const guild = guildId ? client.guilds.get(guildId) ?? null : null;
|
|
851
|
+
const channel = cached ?? client.wrapChannel({ id: channelId, guildId, type: 0 }, guild);
|
|
631
852
|
const message = {
|
|
632
853
|
...raw,
|
|
633
854
|
id: raw.id,
|
|
@@ -635,15 +856,9 @@ function makeMessage(client, raw) {
|
|
|
635
856
|
author,
|
|
636
857
|
guildId,
|
|
637
858
|
channelId,
|
|
638
|
-
channel
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
guild: guildId ? client.guilds.get(guildId) ?? null : null,
|
|
642
|
-
send: (options) => client.sendMessage(channelId, options),
|
|
643
|
-
isVoice: () => false
|
|
644
|
-
},
|
|
645
|
-
guild: guildId ? client.guilds.get(guildId) ?? null : null,
|
|
646
|
-
member: raw.member ? { ...raw.member, user: author } : null,
|
|
859
|
+
channel,
|
|
860
|
+
guild,
|
|
861
|
+
member: raw.member ? guild ? client.wrapMember(raw.member, guild) : { ...raw.member, user: author } : null,
|
|
647
862
|
reply: async (options) => {
|
|
648
863
|
const payload = typeof options === "string" ? { content: options } : { ...options };
|
|
649
864
|
payload.message_reference = { message_id: raw.id, channel_id: channelId, ...guildId ? { guild_id: guildId } : {} };
|
|
@@ -654,25 +869,163 @@ function makeMessage(client, raw) {
|
|
|
654
869
|
}
|
|
655
870
|
var PlatformClient = class extends EventEmitter2 {
|
|
656
871
|
rest = new RestClient();
|
|
657
|
-
logger
|
|
872
|
+
logger;
|
|
658
873
|
guilds = /* @__PURE__ */ new Map();
|
|
659
874
|
channels = /* @__PURE__ */ new Map();
|
|
660
875
|
users = /* @__PURE__ */ new Map();
|
|
661
876
|
user = null;
|
|
662
877
|
ws = null;
|
|
663
878
|
options;
|
|
879
|
+
readyAt = null;
|
|
664
880
|
readyResolve = null;
|
|
665
881
|
constructor(options = {}) {
|
|
666
882
|
super();
|
|
667
883
|
this.options = options;
|
|
884
|
+
this.logger = makeLogger(options.logLevel ?? "info");
|
|
668
885
|
if (options.apiBase) this.rest.baseUrl = options.apiBase;
|
|
886
|
+
this.channels.fetch = async (id) => {
|
|
887
|
+
const cached = this.channels.get(id);
|
|
888
|
+
if (cached) return cached;
|
|
889
|
+
const raw = await this.rest.get(`/channels/${id}`);
|
|
890
|
+
const ch = this.wrapChannel(raw);
|
|
891
|
+
this.channels.set(ch.id, ch);
|
|
892
|
+
return ch;
|
|
893
|
+
};
|
|
894
|
+
this.guilds.fetch = async (id) => {
|
|
895
|
+
const cached = this.guilds.get(id);
|
|
896
|
+
if (cached) return cached;
|
|
897
|
+
const raw = await this.rest.get(`/guilds/${id}`);
|
|
898
|
+
const g = this.wrapGuild(raw);
|
|
899
|
+
this.guilds.set(g.id, g);
|
|
900
|
+
return g;
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
get uptime() {
|
|
904
|
+
return this.readyAt ? Date.now() - this.readyAt : null;
|
|
905
|
+
}
|
|
906
|
+
/**
|
|
907
|
+
* Normalize a guild payload. Gateway payloads often omit `owner_id` and
|
|
908
|
+
* deliver members as an array, so both are handled here.
|
|
909
|
+
*/
|
|
910
|
+
wrapGuild(guild) {
|
|
911
|
+
if (!guild) return guild;
|
|
912
|
+
const g = { ...guild };
|
|
913
|
+
g.shardId = g.shardId ?? 0;
|
|
914
|
+
g.ownerId = g.ownerId ?? g.owner_id ?? null;
|
|
915
|
+
g.roles = Array.isArray(g.roles) ? g.roles : [];
|
|
916
|
+
g.iconURL = (opts) => g.icon ? `https://fluxerusercontent.com/icons/${g.id}/${g.icon}${opts?.extension ? "." + opts.extension : ""}` : null;
|
|
917
|
+
const rawMembers = g.members;
|
|
918
|
+
const cache = /* @__PURE__ */ new Map();
|
|
919
|
+
const members = { cache, me: null };
|
|
920
|
+
members.get = (id) => cache.get(String(id)) ?? null;
|
|
921
|
+
members.fetch = async (id) => {
|
|
922
|
+
const cached = cache.get(String(id));
|
|
923
|
+
if (cached) return cached;
|
|
924
|
+
const raw = await this.rest.get(`/guilds/${g.id}/members/${id}`);
|
|
925
|
+
const member = this.wrapMember(raw, g);
|
|
926
|
+
if (member?.id) cache.set(member.id, member);
|
|
927
|
+
return member;
|
|
928
|
+
};
|
|
929
|
+
members.fetchMe = async () => {
|
|
930
|
+
const raw = await this.rest.get(`/guilds/${g.id}/members/@me`);
|
|
931
|
+
const member = this.wrapMember(raw, g);
|
|
932
|
+
if (member?.id) cache.set(member.id, member);
|
|
933
|
+
members.me = member;
|
|
934
|
+
return member;
|
|
935
|
+
};
|
|
936
|
+
if (Array.isArray(rawMembers)) {
|
|
937
|
+
for (const raw of rawMembers) {
|
|
938
|
+
const member = this.wrapMember(raw, g);
|
|
939
|
+
if (member?.id) cache.set(member.id, member);
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
const meId = String(this.user?.id ?? "");
|
|
943
|
+
members.me = meId ? cache.get(meId) ?? null : null;
|
|
944
|
+
g.members = members;
|
|
945
|
+
g.channels = g.channels && !Array.isArray(g.channels) ? { ...g.channels } : {};
|
|
946
|
+
g.channels.cache = this.channels;
|
|
947
|
+
g.channels.get = (id) => this.channels.get(id);
|
|
948
|
+
g.channels.fetch = (id) => this.channels.fetch(id);
|
|
949
|
+
return g;
|
|
950
|
+
}
|
|
951
|
+
/** Attach permission helpers to a raw member payload. */
|
|
952
|
+
wrapMember(raw, guild) {
|
|
953
|
+
if (!raw) return raw;
|
|
954
|
+
const member = { ...raw };
|
|
955
|
+
member.id = String(member.id ?? member.user?.id ?? "");
|
|
956
|
+
if (!member.user && member.id) member.user = { id: member.id };
|
|
957
|
+
Object.defineProperty(member, "permissions", {
|
|
958
|
+
enumerable: true,
|
|
959
|
+
configurable: true,
|
|
960
|
+
get: () => computePermissions(guild, member)
|
|
961
|
+
});
|
|
962
|
+
member.permissionsIn = (channel) => computePermissions(guild, member, channel);
|
|
963
|
+
member.has = (...flags) => member.permissions.has(...flags);
|
|
964
|
+
return member;
|
|
965
|
+
}
|
|
966
|
+
wrapChannel(ch, guild) {
|
|
967
|
+
if (!ch) return ch;
|
|
968
|
+
const c = { ...ch };
|
|
969
|
+
c.guildId = c.guildId ?? c.guild_id ?? guild?.id;
|
|
970
|
+
c.guild = guild ?? (c.guildId ? this.guilds.get(c.guildId) ?? null : c.guild ?? null);
|
|
971
|
+
c.isVoice = () => [2, 13].includes(c.type);
|
|
972
|
+
c.isTextBased = () => [0, 1, 5, 10, 11, 12].includes(c.type);
|
|
973
|
+
c.send = (options) => this.sendMessage(c.id, options);
|
|
974
|
+
const rawOverwrites = Array.isArray(ch.permission_overwrites) ? ch.permission_overwrites : [];
|
|
975
|
+
const manager = this.createOverwriteManager(c.id, rawOverwrites);
|
|
976
|
+
c.permissionOverwrites = manager;
|
|
977
|
+
Object.defineProperty(c, "permission_overwrites", {
|
|
978
|
+
enumerable: true,
|
|
979
|
+
configurable: true,
|
|
980
|
+
get: () => manager.toArray()
|
|
981
|
+
});
|
|
982
|
+
return c;
|
|
983
|
+
}
|
|
984
|
+
/** Overwrite manager backed by `PUT/DELETE /channels/:id/permissions/:id`. */
|
|
985
|
+
createOverwriteManager(channelId, raw) {
|
|
986
|
+
const cache = /* @__PURE__ */ new Map();
|
|
987
|
+
for (const item of raw) {
|
|
988
|
+
const overwrite = normalizeOverwrite(item);
|
|
989
|
+
if (overwrite.id) cache.set(overwrite.id, overwrite);
|
|
990
|
+
}
|
|
991
|
+
const write = async (id, options = {}) => {
|
|
992
|
+
const overwriteId = String(id);
|
|
993
|
+
const existing = cache.get(overwriteId);
|
|
994
|
+
const type = options.type ?? existing?.type ?? OverwriteType.Role;
|
|
995
|
+
const allow = resolvePermission(options.allow ?? 0n);
|
|
996
|
+
const deny = resolvePermission(options.deny ?? 0n);
|
|
997
|
+
await this.rest.put(`/channels/${channelId}/permissions/${overwriteId}`, {
|
|
998
|
+
type,
|
|
999
|
+
allow: allow.toString(),
|
|
1000
|
+
deny: deny.toString()
|
|
1001
|
+
});
|
|
1002
|
+
const overwrite = { id: overwriteId, type, allow, deny };
|
|
1003
|
+
cache.set(overwriteId, overwrite);
|
|
1004
|
+
return overwrite;
|
|
1005
|
+
};
|
|
1006
|
+
return {
|
|
1007
|
+
cache,
|
|
1008
|
+
get: (id) => cache.get(String(id)) ?? null,
|
|
1009
|
+
has: (id) => cache.has(String(id)),
|
|
1010
|
+
values: () => cache.values(),
|
|
1011
|
+
toArray: () => [...cache.values()],
|
|
1012
|
+
edit: write,
|
|
1013
|
+
create: write,
|
|
1014
|
+
delete: async (id) => {
|
|
1015
|
+
const overwriteId = String(id);
|
|
1016
|
+
await this.rest.delete(`/channels/${channelId}/permissions/${overwriteId}`);
|
|
1017
|
+
cache.delete(overwriteId);
|
|
1018
|
+
}
|
|
1019
|
+
};
|
|
669
1020
|
}
|
|
670
1021
|
async login(token, _options) {
|
|
671
1022
|
this.rest.setToken(token);
|
|
672
|
-
|
|
673
|
-
this.ws
|
|
674
|
-
this.ws.
|
|
675
|
-
|
|
1023
|
+
const gw = new Gateway({ token, url: this.options.gatewayUrl, intents: this.options.intents });
|
|
1024
|
+
this.ws = gw;
|
|
1025
|
+
this.ws.ping = -1;
|
|
1026
|
+
gw.on("dispatch", (t, d) => this.handleDispatch(t, d));
|
|
1027
|
+
gw.on("error", (e) => this.emit("error", e));
|
|
1028
|
+
await gw.connect();
|
|
676
1029
|
await new Promise((resolve3) => {
|
|
677
1030
|
this.readyResolve = resolve3;
|
|
678
1031
|
});
|
|
@@ -685,18 +1038,20 @@ var PlatformClient = class extends EventEmitter2 {
|
|
|
685
1038
|
this.ws = null;
|
|
686
1039
|
}
|
|
687
1040
|
sendToGateway(_shardId, payload) {
|
|
688
|
-
this.logger.debug
|
|
689
|
-
console.log("[gateway send]", JSON.stringify(payload));
|
|
1041
|
+
this.logger.debug("[gateway send]", payload);
|
|
690
1042
|
this.ws?.send(payload);
|
|
691
1043
|
}
|
|
692
1044
|
handleDispatch(t, d) {
|
|
693
1045
|
this.emit("raw", { t, d });
|
|
694
|
-
if (t.includes("VOICE"))
|
|
1046
|
+
if (t.includes("VOICE")) this.logger.debug("[voice dispatch]", { t, d });
|
|
695
1047
|
switch (t) {
|
|
696
1048
|
case "READY": {
|
|
697
1049
|
this.user = d?.user ?? null;
|
|
1050
|
+
if (this.user) this.user.setPresence = async (_p) => {
|
|
1051
|
+
};
|
|
698
1052
|
if (this.user) this.users.set(this.user.id, this.user);
|
|
699
|
-
for (const g of d?.guilds ?? []) this.guilds.set(g.id, { ...g, shardId: 0 });
|
|
1053
|
+
for (const g of d?.guilds ?? []) this.guilds.set(g.id, this.wrapGuild({ ...g, shardId: 0 }));
|
|
1054
|
+
this.readyAt = Date.now();
|
|
700
1055
|
this.emit(Events.Ready);
|
|
701
1056
|
this.readyResolve?.();
|
|
702
1057
|
this.readyResolve = null;
|
|
@@ -717,16 +1072,20 @@ var PlatformClient = class extends EventEmitter2 {
|
|
|
717
1072
|
return;
|
|
718
1073
|
}
|
|
719
1074
|
case "GUILD_CREATE": {
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
1075
|
+
void this.handleGuildCreate(d).catch((error) => {
|
|
1076
|
+
this.logger.warn("Could not load guild", { guildId: d?.id, error: String(error) });
|
|
1077
|
+
});
|
|
1078
|
+
return;
|
|
1079
|
+
}
|
|
1080
|
+
case "GUILD_DELETE": {
|
|
1081
|
+
this.guilds.delete(d?.id);
|
|
1082
|
+
this.emit(Events.GuildDelete ?? "guildDelete", d);
|
|
725
1083
|
return;
|
|
726
1084
|
}
|
|
727
1085
|
case "CHANNEL_CREATE":
|
|
728
1086
|
case "CHANNEL_UPDATE": {
|
|
729
|
-
const
|
|
1087
|
+
const guild = d.guild_id ? this.guilds.get(d.guild_id) ?? null : null;
|
|
1088
|
+
const ch = this.wrapChannel({ ...d, guildId: d.guild_id, guild }, guild);
|
|
730
1089
|
this.channels.set(ch.id, ch);
|
|
731
1090
|
return;
|
|
732
1091
|
}
|
|
@@ -743,6 +1102,37 @@ var PlatformClient = class extends EventEmitter2 {
|
|
|
743
1102
|
this.emit(t.toLowerCase().replace(/_([a-z])/g, (_, c) => c.toUpperCase()), d);
|
|
744
1103
|
}
|
|
745
1104
|
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Cache a gateway guild. Gateway payloads can include `roles` but omit
|
|
1107
|
+
* `owner_id`, so the REST guild is fetched once and merged before the
|
|
1108
|
+
* `guildCreate` event is emitted.
|
|
1109
|
+
*/
|
|
1110
|
+
async handleGuildCreate(raw) {
|
|
1111
|
+
if (!raw?.id) return;
|
|
1112
|
+
let data = raw;
|
|
1113
|
+
if (!raw.owner_id && !raw.ownerId) {
|
|
1114
|
+
const fetched = await this.rest.get(`/guilds/${raw.id}`).catch(() => null);
|
|
1115
|
+
if (fetched) {
|
|
1116
|
+
data = {
|
|
1117
|
+
...raw,
|
|
1118
|
+
...fetched,
|
|
1119
|
+
channels: raw.channels ?? fetched.channels,
|
|
1120
|
+
members: raw.members ?? fetched.members
|
|
1121
|
+
};
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
const guild = this.wrapGuild({ ...data, shardId: 0 });
|
|
1125
|
+
this.guilds.set(guild.id, guild);
|
|
1126
|
+
for (const ch of data?.channels ?? []) {
|
|
1127
|
+
const channel = this.wrapChannel({ ...ch, guildId: guild.id }, guild);
|
|
1128
|
+
this.channels.set(channel.id, channel);
|
|
1129
|
+
}
|
|
1130
|
+
this.emit(Events.GuildCreate, guild);
|
|
1131
|
+
this.emit(Events.VoiceStatesSync, {
|
|
1132
|
+
guildId: guild.id,
|
|
1133
|
+
voiceStates: data?.voice_states ?? []
|
|
1134
|
+
});
|
|
1135
|
+
}
|
|
746
1136
|
async sendMessage(channelId, options) {
|
|
747
1137
|
const payload = typeof options === "string" ? { content: options } : options;
|
|
748
1138
|
const raw = await this.rest.post(`/channels/${channelId}/messages`, payload);
|
|
@@ -1052,6 +1442,7 @@ var Client = class extends PlatformClient {
|
|
|
1052
1442
|
}
|
|
1053
1443
|
};
|
|
1054
1444
|
export {
|
|
1445
|
+
ALL_PERMISSIONS,
|
|
1055
1446
|
COMMAND_BRAND,
|
|
1056
1447
|
Client,
|
|
1057
1448
|
Command,
|
|
@@ -1068,11 +1459,15 @@ export {
|
|
|
1068
1459
|
ListenerStore,
|
|
1069
1460
|
Middleware,
|
|
1070
1461
|
ModuleLoadError,
|
|
1462
|
+
OverwriteType,
|
|
1463
|
+
PermissionFlags,
|
|
1464
|
+
Permissions,
|
|
1071
1465
|
PlatformClient,
|
|
1072
1466
|
RestClient,
|
|
1073
1467
|
VoiceManager,
|
|
1074
1468
|
VoiceUnavailableError,
|
|
1075
1469
|
collectFiles,
|
|
1470
|
+
computePermissions,
|
|
1076
1471
|
createCommand,
|
|
1077
1472
|
createCommandContext,
|
|
1078
1473
|
createEvent,
|
|
@@ -1080,12 +1475,19 @@ export {
|
|
|
1080
1475
|
createMiddleware,
|
|
1081
1476
|
defaultExtensions,
|
|
1082
1477
|
formatDuration,
|
|
1478
|
+
hasAnyPermission,
|
|
1479
|
+
hasPermission,
|
|
1083
1480
|
isBun,
|
|
1084
1481
|
isCommand,
|
|
1085
1482
|
isEvent,
|
|
1086
1483
|
isListener,
|
|
1087
1484
|
loadDirectory,
|
|
1088
1485
|
loadFile,
|
|
1486
|
+
memberRoleIds,
|
|
1487
|
+
normalizeOverwrite,
|
|
1488
|
+
readOverwrites,
|
|
1489
|
+
resolvePermission,
|
|
1490
|
+
resolvePermissions,
|
|
1089
1491
|
runtimeName,
|
|
1090
1492
|
tokenize
|
|
1091
1493
|
};
|