lunibee 0.2.0 → 0.2.1

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.
@@ -109,6 +109,11 @@ class Channel extends BaseStructure {
109
109
  editTopic(topic) {
110
110
  return this.edit({ topic });
111
111
  }
112
+ editPermissionOverwrite(targetId, changes, options) {
113
+ if (!this.#context?.editPermissionOverwrite)
114
+ throw new Error("This channel is not attached to a client.");
115
+ return this.#context.editPermissionOverwrite(this.id, targetId, changes, options);
116
+ }
112
117
  editParent(parentId) {
113
118
  return this.edit({ parent_id: parentId });
114
119
  }
@@ -243,6 +248,7 @@ class Guild extends BaseStructure {
243
248
  return this.name;
244
249
  }
245
250
  }
251
+
246
252
  // packages/core/src/permissions.ts
247
253
  var Permission = {
248
254
  createInstantInvite: 1n << 0n,
@@ -635,6 +641,11 @@ class PermissionSet {
635
641
  toArray() {
636
642
  return Object.keys(Permission).filter((permission) => this.has(permission));
637
643
  }
644
+ static #renamed = {
645
+ ManageEmojisAndStickers: "ManageGuildExpressions",
646
+ ManageEmojis: "ManageGuildExpressions",
647
+ UseSlashCommands: "UseApplicationCommands"
648
+ };
638
649
  #resolve(permission) {
639
650
  if (typeof permission === "bigint")
640
651
  return permission;
@@ -649,6 +660,9 @@ class PermissionSet {
649
660
  return bit;
650
661
  }
651
662
  if (typeof permission === "string") {
663
+ const renamed = PermissionSet.#renamed[permission];
664
+ if (renamed)
665
+ return this.#resolve(renamed);
652
666
  if (permission in Permission)
653
667
  return Permission[permission];
654
668
  if (permission in Permissions)
@@ -737,7 +751,7 @@ var InteractionResponseType = {
737
751
  // packages/rest/package.json
738
752
  var package_default = {
739
753
  name: "@lunibee/rest",
740
- version: "0.2.0",
754
+ version: "0.2.1",
741
755
  type: "module",
742
756
  module: "./src/index.ts",
743
757
  types: "./src/index.ts",
@@ -808,13 +822,15 @@ class GuildMember {
808
822
  roleIds;
809
823
  pending;
810
824
  joinedAt;
811
- permissions;
812
825
  avatarHash;
813
826
  premiumSince;
814
827
  timedOutUntil;
815
828
  flags;
816
- constructor(data) {
829
+ #suppliedPermissions;
830
+ #context;
831
+ constructor(data, context) {
817
832
  this.user = new User(data.user);
833
+ this.#context = context;
818
834
  if (!/^\d{1,20}$/.test(data.guild_id))
819
835
  throw new TypeError("Member guild_id must be a valid snowflake.");
820
836
  this.guildId = data.guild_id;
@@ -824,12 +840,53 @@ class GuildMember {
824
840
  this.joinedAt = data.joined_at ? new Date(data.joined_at) : undefined;
825
841
  if (this.joinedAt?.toString() === "Invalid Date")
826
842
  throw new RangeError("Member joined_at must be a valid date.");
827
- this.permissions = new PermissionsBitField(data.permissions ?? 0n);
843
+ if (data.permissions !== undefined)
844
+ this.#suppliedPermissions = new PermissionsBitField(data.permissions);
828
845
  this.avatarHash = data.avatar ?? null;
829
846
  this.premiumSince = toDateOrNull(data.premium_since);
830
847
  this.timedOutUntil = toDateOrNull(data.communication_disabled_until);
831
848
  this.flags = data.flags ?? 0;
832
849
  }
850
+ get permissions() {
851
+ if (this.#suppliedPermissions)
852
+ return this.#suppliedPermissions;
853
+ return new PermissionsBitField(this.#context?.memberPermissions?.(this.guildId, this.user.id, this.roleIds) ?? 0n);
854
+ }
855
+ permissionsIn(channelId) {
856
+ const computed = this.#context?.memberPermissions?.(this.guildId, this.user.id, this.roleIds, channelId);
857
+ return computed ? new PermissionsBitField(computed) : null;
858
+ }
859
+ #action(name) {
860
+ const action = this.#context?.[name];
861
+ if (!action)
862
+ throw new Error("This member is not attached to a client.");
863
+ return action;
864
+ }
865
+ kick(reason) {
866
+ return this.#action("kickMember")(this.guildId, this.user.id, reason);
867
+ }
868
+ ban(options = {}) {
869
+ return this.#action("banMember")(this.guildId, this.user.id, options);
870
+ }
871
+ edit(options, reason) {
872
+ return this.#action("editMember")(this.guildId, this.user.id, options, reason);
873
+ }
874
+ timeout(milliseconds, reason) {
875
+ if (milliseconds !== null && !(milliseconds > 0))
876
+ throw new RangeError("Timeout length must be a positive number of milliseconds.");
877
+ return this.edit({
878
+ communication_disabled_until: milliseconds === null ? null : new Date(Date.now() + milliseconds).toISOString()
879
+ }, reason);
880
+ }
881
+ setNickname(nickname, reason) {
882
+ return this.edit({ nick: nickname }, reason);
883
+ }
884
+ addRole(roleId, reason) {
885
+ return this.#action("addMemberRole")(this.guildId, this.user.id, roleId, reason);
886
+ }
887
+ removeRole(roleId, reason) {
888
+ return this.#action("removeMemberRole")(this.guildId, this.user.id, roleId, reason);
889
+ }
833
890
  get displayName() {
834
891
  return this.nickname ?? this.user.displayName;
835
892
  }
@@ -1363,6 +1420,17 @@ class CommandOptions {
1363
1420
  throw new TypeError(`Resolved mentionable for option "${name}" is missing.`);
1364
1421
  return resolved;
1365
1422
  }
1423
+ getMentionableType(name) {
1424
+ const opt = this.#get(name);
1425
+ if (!opt || opt.type !== OptionType.Mentionable)
1426
+ return null;
1427
+ const id = String(opt.value);
1428
+ if (this.#resolved?.users?.[id])
1429
+ return "user";
1430
+ if (this.#resolved?.roles?.[id])
1431
+ return "role";
1432
+ return null;
1433
+ }
1366
1434
  getAttachment(name, required = false) {
1367
1435
  const opt = this.#get(name);
1368
1436
  if (!opt || opt.type !== OptionType.Attachment) {
@@ -1450,6 +1518,20 @@ class Interaction {
1450
1518
  isChatInputCommand() {
1451
1519
  return this.type === InteractionType.ApplicationCommand;
1452
1520
  }
1521
+ awaitModalSubmit(options) {
1522
+ const collect = this.#client.resourceContext?.collectInteractions;
1523
+ if (!collect)
1524
+ return Promise.reject(new Error("This interaction is not attached to a client."));
1525
+ if (!(options.time > 0))
1526
+ return Promise.reject(new RangeError("awaitModalSubmit needs a positive time."));
1527
+ const userId = this.user?.id;
1528
+ const collector = collect({
1529
+ time: options.time,
1530
+ max: 1,
1531
+ filter: async (interaction) => interaction.isModalSubmit() && interaction.user?.id === userId && (options.customId === undefined || interaction.customId === options.customId) && (options.filter ? await options.filter(interaction) : true)
1532
+ });
1533
+ return collector.next();
1534
+ }
1453
1535
  isMessageComponent() {
1454
1536
  return this.type === InteractionType.MessageComponent;
1455
1537
  }
@@ -1509,7 +1591,7 @@ class Interaction {
1509
1591
  return new GuildMember({
1510
1592
  ...raw,
1511
1593
  guild_id: this.guildId
1512
- });
1594
+ }, this.#client.resourceContext);
1513
1595
  }
1514
1596
  get createdTimestamp() {
1515
1597
  return Number((BigInt(this.id) >> 22n) + DISCORD_EPOCH);
@@ -1545,7 +1627,8 @@ class Interaction {
1545
1627
  async reply(options) {
1546
1628
  return this.acknowledge(InteractionResponse.message(toMessageData(options)), "replied");
1547
1629
  }
1548
- async deferReply(ephemeral = false) {
1630
+ async deferReply(options = false) {
1631
+ const ephemeral = typeof options === "boolean" ? options : options.ephemeral === true;
1549
1632
  await this.acknowledge(InteractionResponse.defer(ephemeral), "deferred");
1550
1633
  }
1551
1634
  editReply(options) {
@@ -1563,7 +1646,8 @@ class Interaction {
1563
1646
  return this.acknowledge(new InteractionResponse(InteractionResponseType.MessageUpdate, toMessageData(options)), "replied");
1564
1647
  }
1565
1648
  async showModal(modal) {
1566
- return this.acknowledge(new InteractionResponse(InteractionResponseType.Modal, modal), "replied");
1649
+ const data = "toJSON" in modal && typeof modal.toJSON === "function" ? modal.toJSON() : modal;
1650
+ return this.acknowledge(new InteractionResponse(InteractionResponseType.Modal, data), "replied");
1567
1651
  }
1568
1652
  }
1569
1653
 
@@ -1584,6 +1668,9 @@ class ComponentInteraction extends Interaction {
1584
1668
  get customId() {
1585
1669
  return this.data?.data?.custom_id ?? "";
1586
1670
  }
1671
+ get messageId() {
1672
+ return this.data?.message?.id;
1673
+ }
1587
1674
  get componentType() {
1588
1675
  return this.data?.data?.component_type ?? 0;
1589
1676
  }
@@ -1834,6 +1921,7 @@ class AuditLog {
1834
1921
  }
1835
1922
 
1836
1923
  // packages/structures/src/index.ts
1924
+ var COMPONENT_COLLECTOR_DEFAULT_TIME = 15 * 60000;
1837
1925
  var NON_SYSTEM_MESSAGE_TYPES = new Set([0, 19, 20, 23]);
1838
1926
 
1839
1927
  class Message extends BaseStructure {
@@ -1854,6 +1942,7 @@ class Message extends BaseStructure {
1854
1942
  reference;
1855
1943
  components;
1856
1944
  referencedMessage;
1945
+ member;
1857
1946
  #context;
1858
1947
  constructor(data, context) {
1859
1948
  super(data.id);
@@ -1876,6 +1965,11 @@ class Message extends BaseStructure {
1876
1965
  this.reference = data.message_reference;
1877
1966
  this.referencedMessage = data.referenced_message ? new Message(data.referenced_message, context) : data.referenced_message === null ? null : undefined;
1878
1967
  this.#context = context;
1968
+ this.member = data.member && data.guild_id ? new GuildMember({
1969
+ ...data.member,
1970
+ user: data.member.user ?? data.author,
1971
+ guild_id: data.guild_id
1972
+ }, context) : null;
1879
1973
  this.channel = new Channel({ id: data.channel_id, type: 0, guild_id: data.guild_id }, context);
1880
1974
  }
1881
1975
  get embedsSuppressed() {
@@ -1916,6 +2010,20 @@ class Message extends BaseStructure {
1916
2010
  throw new Error("This message is not attached to a client.");
1917
2011
  return this.#context.crosspostMessage(this.channelId, this.id);
1918
2012
  }
2013
+ createComponentCollector(options = {}) {
2014
+ const collect = this.#context?.collectInteractions;
2015
+ if (!collect)
2016
+ throw new Error("This message is not attached to a client.");
2017
+ const { componentType, filter, ...rest } = options;
2018
+ return collect({
2019
+ ...rest,
2020
+ time: rest.time ?? (rest.idle ? undefined : COMPONENT_COLLECTOR_DEFAULT_TIME),
2021
+ filter: async (interaction) => interaction.isMessageComponent() && interaction.messageId === this.id && (componentType === undefined || interaction.componentType === componentType) && (filter ? await filter(interaction) : true)
2022
+ });
2023
+ }
2024
+ awaitComponent(options = {}) {
2025
+ return this.createComponentCollector({ ...options, max: 1 }).next();
2026
+ }
1919
2027
  react(emoji) {
1920
2028
  if (!this.#context?.addReaction)
1921
2029
  throw new Error("This message is not attached to a client.");
@@ -2031,4 +2139,4 @@ export {
2031
2139
  createInteraction
2032
2140
  };
2033
2141
 
2034
- //# debugId=39460C3E91D6CD3564756E2164756E21
2142
+ //# debugId=56391DD25556C30C64756E2164756E21