revbot.js 0.2.1 → 0.2.3

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 CHANGED
@@ -3,7 +3,7 @@
3
3
  # Note: this package is currently in the early stages of development and testing
4
4
 
5
5
  `revbot.js` is a Node.js library for building bots on the Revolt platform. It provides an easy-to-use interface for interacting with Revolt's API, managing events, and handling various bot functionalities. NOTE: node 21 is required to run this
6
- [revolt server](https://rvlt.gg/2NcCgYZ0) [docs](https://jade3375.github.io/revbot.js/)
6
+ [revolt server](https://stt.gg/cFSwBt4R) [docs](https://jade3375.github.io/revbot.js/)
7
7
 
8
8
  ## Installation
9
9
 
package/dist/index.d.mts CHANGED
@@ -740,9 +740,9 @@ declare class MessageStruct extends Base {
740
740
  /**
741
741
  * Retrieves the author of the message.
742
742
  *
743
- * @returns {User | null} The user who authored the message, or `null` if not found.
743
+ * @returns {User | ServerMember | null} The user who authored the message, or `null` if not found.
744
744
  */
745
- get author(): User | null;
745
+ get author(): User | ServerMember | null;
746
746
  /**
747
747
  * Retrieves the channel where the message was sent.
748
748
  *
@@ -1850,6 +1850,39 @@ declare class ServerMember extends Base {
1850
1850
  * ```
1851
1851
  */
1852
1852
  leave(): Promise<void>;
1853
+ /**
1854
+ * Gets the effective permissions for this server member based on their roles.
1855
+ *
1856
+ * The permissions are calculated by:
1857
+ * 1. Starting with a base FullPermissions with no permissions
1858
+ * 2. For each role the member has, applying the role's allow permissions
1859
+ * 3. For each role the member has, removing the role's deny permissions
1860
+ *
1861
+ * @returns {FullPermissions} The effective permissions for this member
1862
+ *
1863
+ * @example
1864
+ * ```typescript
1865
+ * const permissions = member.getPermissions();
1866
+ * console.log(permissions.has('MANAGE_MESSAGES')); // true or false
1867
+ * ```
1868
+ */
1869
+ permissions(): FullPermissions;
1870
+ /**
1871
+ * Checks if this server member has a specific permission.
1872
+ *
1873
+ * @param {string | number | FullPermissions} permission - The permission to check for
1874
+ * @returns {boolean} Whether the member has the permission
1875
+ *
1876
+ * @example
1877
+ * ```typescript
1878
+ * if (member.hasPermission('MANAGE_MESSAGES')) {
1879
+ * // Member can manage messages
1880
+ * }
1881
+ * ```
1882
+ *
1883
+ * note this works on the same basis as stoats permissions checking
1884
+ */
1885
+ hasPermission(permission: string | number | FullPermissions): boolean;
1853
1886
  /**
1854
1887
  * Retrieves the user associated with this server member.
1855
1888
  *
@@ -2068,7 +2101,7 @@ declare class UserManager extends BaseManager<User, User$1> {
2068
2101
  * @param resolvable The user to resolve
2069
2102
  * @returns The user or null if it cannot be resolved
2070
2103
  */
2071
- resolve(resolvable: MessageStruct | User): User;
2104
+ resolve(resolvable: MessageStruct | User): User | null;
2072
2105
  resolve(resolvable: string | User$1): User | null;
2073
2106
  /**
2074
2107
  * get a user id form cache
@@ -2190,6 +2223,8 @@ declare class WebSocketClient {
2190
2223
  reconnecting: Promise<unknown> | null;
2191
2224
  /** Whether the WebSocket client is ready. */
2192
2225
  ready: boolean;
2226
+ /** The number of reconnection attempts made. */
2227
+ retryCount: number;
2193
2228
  /**
2194
2229
  * Creates a new WebSocketClient instance.
2195
2230
  *
@@ -2272,7 +2307,7 @@ declare class WebSocketClient {
2272
2307
  *
2273
2308
  * @returns {Promise<void>} A promise that resolves when the connection is destroyed.
2274
2309
  */
2275
- destroy(): Promise<void>;
2310
+ destroy(isUserInitiated?: boolean): Promise<void>;
2276
2311
  }
2277
2312
 
2278
2313
  /**
package/dist/index.d.ts CHANGED
@@ -740,9 +740,9 @@ declare class MessageStruct extends Base {
740
740
  /**
741
741
  * Retrieves the author of the message.
742
742
  *
743
- * @returns {User | null} The user who authored the message, or `null` if not found.
743
+ * @returns {User | ServerMember | null} The user who authored the message, or `null` if not found.
744
744
  */
745
- get author(): User | null;
745
+ get author(): User | ServerMember | null;
746
746
  /**
747
747
  * Retrieves the channel where the message was sent.
748
748
  *
@@ -1850,6 +1850,39 @@ declare class ServerMember extends Base {
1850
1850
  * ```
1851
1851
  */
1852
1852
  leave(): Promise<void>;
1853
+ /**
1854
+ * Gets the effective permissions for this server member based on their roles.
1855
+ *
1856
+ * The permissions are calculated by:
1857
+ * 1. Starting with a base FullPermissions with no permissions
1858
+ * 2. For each role the member has, applying the role's allow permissions
1859
+ * 3. For each role the member has, removing the role's deny permissions
1860
+ *
1861
+ * @returns {FullPermissions} The effective permissions for this member
1862
+ *
1863
+ * @example
1864
+ * ```typescript
1865
+ * const permissions = member.getPermissions();
1866
+ * console.log(permissions.has('MANAGE_MESSAGES')); // true or false
1867
+ * ```
1868
+ */
1869
+ permissions(): FullPermissions;
1870
+ /**
1871
+ * Checks if this server member has a specific permission.
1872
+ *
1873
+ * @param {string | number | FullPermissions} permission - The permission to check for
1874
+ * @returns {boolean} Whether the member has the permission
1875
+ *
1876
+ * @example
1877
+ * ```typescript
1878
+ * if (member.hasPermission('MANAGE_MESSAGES')) {
1879
+ * // Member can manage messages
1880
+ * }
1881
+ * ```
1882
+ *
1883
+ * note this works on the same basis as stoats permissions checking
1884
+ */
1885
+ hasPermission(permission: string | number | FullPermissions): boolean;
1853
1886
  /**
1854
1887
  * Retrieves the user associated with this server member.
1855
1888
  *
@@ -2068,7 +2101,7 @@ declare class UserManager extends BaseManager<User, User$1> {
2068
2101
  * @param resolvable The user to resolve
2069
2102
  * @returns The user or null if it cannot be resolved
2070
2103
  */
2071
- resolve(resolvable: MessageStruct | User): User;
2104
+ resolve(resolvable: MessageStruct | User): User | null;
2072
2105
  resolve(resolvable: string | User$1): User | null;
2073
2106
  /**
2074
2107
  * get a user id form cache
@@ -2190,6 +2223,8 @@ declare class WebSocketClient {
2190
2223
  reconnecting: Promise<unknown> | null;
2191
2224
  /** Whether the WebSocket client is ready. */
2192
2225
  ready: boolean;
2226
+ /** The number of reconnection attempts made. */
2227
+ retryCount: number;
2193
2228
  /**
2194
2229
  * Creates a new WebSocketClient instance.
2195
2230
  *
@@ -2272,7 +2307,7 @@ declare class WebSocketClient {
2272
2307
  *
2273
2308
  * @returns {Promise<void>} A promise that resolves when the connection is destroyed.
2274
2309
  */
2275
- destroy(): Promise<void>;
2310
+ destroy(isUserInitiated?: boolean): Promise<void>;
2276
2311
  }
2277
2312
 
2278
2313
  /**
package/dist/index.js CHANGED
@@ -940,11 +940,14 @@ var MessageStruct = class extends Base {
940
940
  /**
941
941
  * Retrieves the author of the message.
942
942
  *
943
- * @returns {User | null} The user who authored the message, or `null` if not found.
943
+ * @returns {User | ServerMember | null} The user who authored the message, or `null` if not found.
944
944
  */
945
945
  get author() {
946
- var _a;
947
- return (_a = this.client.users.cache.get(this.authorId)) != null ? _a : null;
946
+ var _a, _b, _c, _d;
947
+ if (this.inServer()) {
948
+ return (_c = (_b = (_a = this.server) == null ? void 0 : _a.members.cache.get(this.authorId)) != null ? _b : this.client.users.cache.get(this.authorId)) != null ? _c : null;
949
+ }
950
+ return (_d = this.client.users.cache.get(this.authorId)) != null ? _d : null;
948
951
  }
949
952
  /**
950
953
  * Retrieves the channel where the message was sent.
@@ -2352,6 +2355,58 @@ var ServerMember3 = class extends Base {
2352
2355
  leave() {
2353
2356
  return this.client.servers.delete(this.serverId);
2354
2357
  }
2358
+ /**
2359
+ * Gets the effective permissions for this server member based on their roles.
2360
+ *
2361
+ * The permissions are calculated by:
2362
+ * 1. Starting with a base FullPermissions with no permissions
2363
+ * 2. For each role the member has, applying the role's allow permissions
2364
+ * 3. For each role the member has, removing the role's deny permissions
2365
+ *
2366
+ * @returns {FullPermissions} The effective permissions for this member
2367
+ *
2368
+ * @example
2369
+ * ```typescript
2370
+ * const permissions = member.getPermissions();
2371
+ * console.log(permissions.has('MANAGE_MESSAGES')); // true or false
2372
+ * ```
2373
+ */
2374
+ permissions() {
2375
+ var _a, _b;
2376
+ let permissions = new FullPermissions();
2377
+ for (const role of this.roles) {
2378
+ if ((_a = role.overwrite) == null ? void 0 : _a.allow) {
2379
+ permissions = permissions.add(role.overwrite.allow);
2380
+ }
2381
+ }
2382
+ for (const role of this.roles) {
2383
+ if ((_b = role.overwrite) == null ? void 0 : _b.deny) {
2384
+ permissions = permissions.remove(role.overwrite.deny);
2385
+ }
2386
+ }
2387
+ return permissions;
2388
+ }
2389
+ /**
2390
+ * Checks if this server member has a specific permission.
2391
+ *
2392
+ * @param {string | number | FullPermissions} permission - The permission to check for
2393
+ * @returns {boolean} Whether the member has the permission
2394
+ *
2395
+ * @example
2396
+ * ```typescript
2397
+ * if (member.hasPermission('MANAGE_MESSAGES')) {
2398
+ * // Member can manage messages
2399
+ * }
2400
+ * ```
2401
+ *
2402
+ * note this works on the same basis as stoats permissions checking
2403
+ */
2404
+ hasPermission(permission) {
2405
+ var _a;
2406
+ if (((_a = this.client.servers.cache.get(this.serverId)) == null ? void 0 : _a.ownerId) === this.id)
2407
+ return true;
2408
+ return this.permissions().has(permission);
2409
+ }
2355
2410
  // async displayAvatarURL(options?: { size: number }): Promise<string> {
2356
2411
  // return await this.user.displayAvatarURL(options);
2357
2412
  // }
@@ -2767,7 +2822,8 @@ var UserManager = class extends BaseManager {
2767
2822
  });
2768
2823
  }
2769
2824
  resolve(resolvable) {
2770
- if (resolvable instanceof MessageStruct) return resolvable.author;
2825
+ if (resolvable instanceof MessageStruct)
2826
+ return resolvable.author;
2771
2827
  return super.resolve(resolvable);
2772
2828
  }
2773
2829
  /**
@@ -3304,7 +3360,7 @@ var import_node_events = require("events");
3304
3360
  var import_axios4 = __toESM(require("axios"));
3305
3361
 
3306
3362
  // package.json
3307
- var version = "0.2.0";
3363
+ var version = "0.2.3";
3308
3364
 
3309
3365
  // src/rest/restUtils/rateLimitQueue.ts
3310
3366
  var import_axios3 = __toESM(require("axios"));
@@ -4070,13 +4126,33 @@ var ServerRoleUpdate = class extends Event {
4070
4126
  * @returns {void}
4071
4127
  */
4072
4128
  handle(data) {
4073
- const server = this.client.servers.cache.get(data.id);
4074
- if (!server) return;
4075
- const role = server.roles.cache.get(data.role_id);
4076
- const oldRole = role == null ? void 0 : role._update(data.data, data.clear);
4077
- if (role && oldRole && !role.equals(oldRole)) {
4078
- this.client.emit("roleUpdate" /* ROLE_UPDATE */, oldRole, role);
4079
- }
4129
+ return __async(this, null, function* () {
4130
+ const server = this.client.servers.cache.get(data.id);
4131
+ if (!server) return;
4132
+ let role = server.roles.cache.get(data.role_id);
4133
+ const oldRole = role == null ? void 0 : role._update(data.data, data.clear);
4134
+ if (!oldRole && !role) {
4135
+ yield Promise.all(
4136
+ Object.values(data.data).map((raw) => __async(this, null, function* () {
4137
+ if (raw && typeof raw === "object") {
4138
+ server.roles._add(__spreadProps(__spreadValues({
4139
+ name: "",
4140
+ permissions: {
4141
+ a: 0,
4142
+ d: 0
4143
+ }
4144
+ }, raw), {
4145
+ id: data.role_id
4146
+ }));
4147
+ }
4148
+ }))
4149
+ );
4150
+ role = server.roles.cache.get(data.role_id);
4151
+ }
4152
+ if (role && oldRole && !role.equals(oldRole)) {
4153
+ this.client.emit("roleUpdate" /* ROLE_UPDATE */, oldRole, role);
4154
+ }
4155
+ });
4080
4156
  }
4081
4157
  };
4082
4158
 
@@ -4306,6 +4382,8 @@ var WebSocketClient = class {
4306
4382
  this.reconnecting = null;
4307
4383
  /** Whether the WebSocket client is ready. */
4308
4384
  this.ready = false;
4385
+ /** The number of reconnection attempts made. */
4386
+ this.retryCount = 0;
4309
4387
  }
4310
4388
  /**
4311
4389
  * Logs a debug message.
@@ -4445,6 +4523,8 @@ var WebSocketClient = class {
4445
4523
  break;
4446
4524
  case "Authenticated" /* AUTHENTICATED */:
4447
4525
  this.connected = true;
4526
+ this.retryCount = 0;
4527
+ this.debug(`Successfully authenticated.`);
4448
4528
  break;
4449
4529
  case "Pong" /* PONG */:
4450
4530
  this.debug(`Received a heartbeat.`);
@@ -4504,6 +4584,15 @@ var WebSocketClient = class {
4504
4584
  * @returns {Promise<this>} A promise that resolves when the connection is established.
4505
4585
  */
4506
4586
  connect() {
4587
+ this.retryCount = this.retryCount + 1;
4588
+ if (this.retryCount > 10) {
4589
+ this.debug("Max retry attempts reached");
4590
+ return Promise.reject(
4591
+ new Error(
4592
+ "Max retry attempts reached on WS connection, try again later."
4593
+ )
4594
+ );
4595
+ }
4507
4596
  return new Promise((resolve) => __async(this, null, function* () {
4508
4597
  var _a, _b, _c, _d;
4509
4598
  if (((_a = this.socket) == null ? void 0 : _a.readyState) === WebSocket.OPEN && this.ready) {
@@ -4531,7 +4620,7 @@ var WebSocketClient = class {
4531
4620
  *
4532
4621
  * @returns {Promise<void>} A promise that resolves when the connection is destroyed.
4533
4622
  */
4534
- destroy() {
4623
+ destroy(isUserInitiated) {
4535
4624
  return new Promise((resolve) => {
4536
4625
  var _a;
4537
4626
  this.setHeartbeatTimer(-1);
@@ -4540,11 +4629,20 @@ var WebSocketClient = class {
4540
4629
  if (((_a = this.socket) == null ? void 0 : _a.readyState) === WebSocket.OPEN) {
4541
4630
  this.socket.addEventListener("close", () => {
4542
4631
  this.socket = null;
4632
+ if (!isUserInitiated) {
4633
+ setTimeout(() => this.connect(), 1e3);
4634
+ }
4543
4635
  resolve();
4544
4636
  });
4545
4637
  this.socket.close();
4638
+ if (!isUserInitiated) {
4639
+ setTimeout(() => this.connect(), 1e3);
4640
+ }
4546
4641
  } else {
4547
4642
  this.socket = null;
4643
+ if (!isUserInitiated) {
4644
+ setTimeout(() => this.connect(), 1e3);
4645
+ }
4548
4646
  resolve();
4549
4647
  }
4550
4648
  });
@@ -4636,7 +4734,7 @@ var client2 = class extends BaseClient {
4636
4734
  this.token = null;
4637
4735
  this.user = null;
4638
4736
  this.readyAt = null;
4639
- yield this.ws.destroy();
4737
+ yield this.ws.destroy(true);
4640
4738
  });
4641
4739
  }
4642
4740
  /**