revbot.js 0.0.8 → 0.0.11
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/dist/index.d.mts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +175 -17
- package/dist/index.mjs +175 -17
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -124,6 +124,7 @@ declare class BitField {
|
|
|
124
124
|
|
|
125
125
|
declare class RestClient {
|
|
126
126
|
private readonly client;
|
|
127
|
+
private rateLimitQueue;
|
|
127
128
|
constructor(client: BaseClient);
|
|
128
129
|
/**
|
|
129
130
|
* Helper function to handle API requests.
|
|
@@ -134,6 +135,7 @@ declare class RestClient {
|
|
|
134
135
|
* @returns The API response.
|
|
135
136
|
*/
|
|
136
137
|
private request;
|
|
138
|
+
private retryRequest;
|
|
137
139
|
/**
|
|
138
140
|
* GET request.
|
|
139
141
|
* @param url The URL for the request.
|
|
@@ -176,6 +178,7 @@ declare class RestClient {
|
|
|
176
178
|
|
|
177
179
|
declare class CDNClient {
|
|
178
180
|
private readonly client;
|
|
181
|
+
private rateLimitQueue;
|
|
179
182
|
constructor(client: BaseClient);
|
|
180
183
|
/**
|
|
181
184
|
* Helper function to handle API requests.
|
|
@@ -186,6 +189,7 @@ declare class CDNClient {
|
|
|
186
189
|
* @returns The API response.
|
|
187
190
|
*/
|
|
188
191
|
private request;
|
|
192
|
+
private retryRequest;
|
|
189
193
|
/**
|
|
190
194
|
* POST request.
|
|
191
195
|
* @param url The URL for the request.
|
package/dist/index.d.ts
CHANGED
|
@@ -124,6 +124,7 @@ declare class BitField {
|
|
|
124
124
|
|
|
125
125
|
declare class RestClient {
|
|
126
126
|
private readonly client;
|
|
127
|
+
private rateLimitQueue;
|
|
127
128
|
constructor(client: BaseClient);
|
|
128
129
|
/**
|
|
129
130
|
* Helper function to handle API requests.
|
|
@@ -134,6 +135,7 @@ declare class RestClient {
|
|
|
134
135
|
* @returns The API response.
|
|
135
136
|
*/
|
|
136
137
|
private request;
|
|
138
|
+
private retryRequest;
|
|
137
139
|
/**
|
|
138
140
|
* GET request.
|
|
139
141
|
* @param url The URL for the request.
|
|
@@ -176,6 +178,7 @@ declare class RestClient {
|
|
|
176
178
|
|
|
177
179
|
declare class CDNClient {
|
|
178
180
|
private readonly client;
|
|
181
|
+
private rateLimitQueue;
|
|
179
182
|
constructor(client: BaseClient);
|
|
180
183
|
/**
|
|
181
184
|
* Helper function to handle API requests.
|
|
@@ -186,6 +189,7 @@ declare class CDNClient {
|
|
|
186
189
|
* @returns The API response.
|
|
187
190
|
*/
|
|
188
191
|
private request;
|
|
192
|
+
private retryRequest;
|
|
189
193
|
/**
|
|
190
194
|
* POST request.
|
|
191
195
|
* @param url The URL for the request.
|
package/dist/index.js
CHANGED
|
@@ -3232,15 +3232,96 @@ var ServerMemberManager = class extends BaseManager {
|
|
|
3232
3232
|
var import_node_events = require("events");
|
|
3233
3233
|
|
|
3234
3234
|
// src/rest/restClient.ts
|
|
3235
|
-
var
|
|
3235
|
+
var import_axios4 = require("axios");
|
|
3236
3236
|
|
|
3237
3237
|
// package.json
|
|
3238
|
-
var version = "0.0.
|
|
3238
|
+
var version = "0.0.11";
|
|
3239
|
+
|
|
3240
|
+
// src/rest/restUtils/rateLimitQueue.ts
|
|
3241
|
+
var import_axios3 = __toESM(require("axios"));
|
|
3242
|
+
var RateLimitQueue = class {
|
|
3243
|
+
constructor() {
|
|
3244
|
+
this.bucketMap = /* @__PURE__ */ new Map();
|
|
3245
|
+
this.pathToBucket = /* @__PURE__ */ new Map();
|
|
3246
|
+
}
|
|
3247
|
+
request(config) {
|
|
3248
|
+
return __async(this, null, function* () {
|
|
3249
|
+
const path = config.url;
|
|
3250
|
+
const bucketId = this.pathToBucket.get(path);
|
|
3251
|
+
let bucket = bucketId ? this.bucketMap.get(bucketId) : void 0;
|
|
3252
|
+
if (bucket && bucket.remaining <= 0 && Date.now() < bucket.resetAfter) {
|
|
3253
|
+
return new Promise((resolve, reject) => {
|
|
3254
|
+
bucket.queue.push(() => __async(this, null, function* () {
|
|
3255
|
+
try {
|
|
3256
|
+
const res = yield this._doRequest(config, path);
|
|
3257
|
+
resolve(res);
|
|
3258
|
+
} catch (e) {
|
|
3259
|
+
reject(e);
|
|
3260
|
+
}
|
|
3261
|
+
}));
|
|
3262
|
+
});
|
|
3263
|
+
} else {
|
|
3264
|
+
return this._doRequest(config, path);
|
|
3265
|
+
}
|
|
3266
|
+
});
|
|
3267
|
+
}
|
|
3268
|
+
_doRequest(config, path) {
|
|
3269
|
+
return __async(this, null, function* () {
|
|
3270
|
+
const response = yield (0, import_axios3.default)(config);
|
|
3271
|
+
this._updateRateLimit(path, response);
|
|
3272
|
+
return response;
|
|
3273
|
+
});
|
|
3274
|
+
}
|
|
3275
|
+
_updateRateLimit(path, response) {
|
|
3276
|
+
const headers = response.headers;
|
|
3277
|
+
const limit = parseInt(headers["x-ratelimit-limit"]);
|
|
3278
|
+
const remaining = parseInt(headers["x-ratelimit-remaining"]);
|
|
3279
|
+
const resetAfter = parseFloat(headers["x-ratelimit-reset-after"]) * 1e3 + Date.now();
|
|
3280
|
+
const bucket = headers["x-ratelimit-bucket"];
|
|
3281
|
+
const resetIn = parseInt(headers["x-ratelimit-reset-after"]);
|
|
3282
|
+
if (!bucket) return;
|
|
3283
|
+
this.pathToBucket.set(path, bucket);
|
|
3284
|
+
let state = this.bucketMap.get(bucket);
|
|
3285
|
+
if (!state) {
|
|
3286
|
+
state = {
|
|
3287
|
+
limit,
|
|
3288
|
+
remaining,
|
|
3289
|
+
resetAfter,
|
|
3290
|
+
bucket,
|
|
3291
|
+
resetIn,
|
|
3292
|
+
queue: [],
|
|
3293
|
+
lastPath: path
|
|
3294
|
+
};
|
|
3295
|
+
this.bucketMap.set(bucket, state);
|
|
3296
|
+
} else {
|
|
3297
|
+
state.limit = limit;
|
|
3298
|
+
state.remaining = remaining;
|
|
3299
|
+
state.resetAfter = resetAfter;
|
|
3300
|
+
state.lastPath = path;
|
|
3301
|
+
}
|
|
3302
|
+
if (remaining <= 0) {
|
|
3303
|
+
if (state.resetTimeout) clearTimeout(state.resetTimeout);
|
|
3304
|
+
const delay = resetIn;
|
|
3305
|
+
state.resetTimeout = setTimeout(() => {
|
|
3306
|
+
state.remaining = state.limit;
|
|
3307
|
+
state.resetTimeout = void 0;
|
|
3308
|
+
while (state.queue.length > 0 && state.remaining > 0) {
|
|
3309
|
+
const fn = state.queue.shift();
|
|
3310
|
+
if (fn) {
|
|
3311
|
+
state.remaining--;
|
|
3312
|
+
fn();
|
|
3313
|
+
}
|
|
3314
|
+
}
|
|
3315
|
+
}, delay);
|
|
3316
|
+
}
|
|
3317
|
+
}
|
|
3318
|
+
};
|
|
3239
3319
|
|
|
3240
3320
|
// src/rest/restClient.ts
|
|
3241
3321
|
var RestClient = class {
|
|
3242
3322
|
constructor(client3) {
|
|
3243
3323
|
this.client = client3;
|
|
3324
|
+
this.rateLimitQueue = new RateLimitQueue();
|
|
3244
3325
|
}
|
|
3245
3326
|
/**
|
|
3246
3327
|
* Helper function to handle API requests.
|
|
@@ -3250,12 +3331,13 @@ var RestClient = class {
|
|
|
3250
3331
|
* @param query Query parameters (if applicable).
|
|
3251
3332
|
* @returns The API response.
|
|
3252
3333
|
*/
|
|
3253
|
-
request(method, url, body, query) {
|
|
3334
|
+
request(method, url, body, query, retry) {
|
|
3254
3335
|
return __async(this, null, function* () {
|
|
3336
|
+
var _a;
|
|
3255
3337
|
try {
|
|
3256
3338
|
if (!this.client.token) throw new Error("Token is required");
|
|
3257
3339
|
const authHeader = this.client.bot ? "X-Bot-Token" : "X-Session-Token";
|
|
3258
|
-
const config = {
|
|
3340
|
+
const config = __spreadProps(__spreadValues({}, {
|
|
3259
3341
|
method,
|
|
3260
3342
|
url: `${apiUrl}${url}`,
|
|
3261
3343
|
params: query,
|
|
@@ -3264,12 +3346,49 @@ var RestClient = class {
|
|
|
3264
3346
|
[authHeader]: this.client.token,
|
|
3265
3347
|
"User-Agent": `RevBot.js/${version}`
|
|
3266
3348
|
}
|
|
3267
|
-
}
|
|
3268
|
-
|
|
3349
|
+
}), {
|
|
3350
|
+
url: `${apiUrl}${url}`
|
|
3351
|
+
});
|
|
3352
|
+
const response = yield this.rateLimitQueue.request(config);
|
|
3269
3353
|
return response.data;
|
|
3270
3354
|
} catch (error) {
|
|
3271
|
-
|
|
3272
|
-
|
|
3355
|
+
if (retry) throw typeof error;
|
|
3356
|
+
if (error instanceof import_axios4.AxiosError) {
|
|
3357
|
+
if (error.status && (error.status === 429 || error.status >= 500)) {
|
|
3358
|
+
return this.retryRequest(0, method, url, body, query);
|
|
3359
|
+
}
|
|
3360
|
+
if (error.status) {
|
|
3361
|
+
throw new Error(
|
|
3362
|
+
`API call failed with status ${error.status}: ${(_a = error.response) == null ? void 0 : _a.statusText}`
|
|
3363
|
+
);
|
|
3364
|
+
}
|
|
3365
|
+
}
|
|
3366
|
+
throw new Error(
|
|
3367
|
+
`API call failed: ${error instanceof Error ? error.message : error}`
|
|
3368
|
+
);
|
|
3369
|
+
}
|
|
3370
|
+
});
|
|
3371
|
+
}
|
|
3372
|
+
retryRequest(attempt = 0, method, url, body, query) {
|
|
3373
|
+
return __async(this, null, function* () {
|
|
3374
|
+
var _a, _b;
|
|
3375
|
+
if (attempt >= ((_b = (_a = this.client.options.rest) == null ? void 0 : _a.retries) != null ? _b : 3)) {
|
|
3376
|
+
throw new Error("Max retries reached");
|
|
3377
|
+
}
|
|
3378
|
+
try {
|
|
3379
|
+
return yield this.request(method, url, body, query, true);
|
|
3380
|
+
} catch (error) {
|
|
3381
|
+
console.warn(`Attempt ${attempt + 1} failed:`, error);
|
|
3382
|
+
yield new Promise(
|
|
3383
|
+
(resolve) => {
|
|
3384
|
+
var _a2, _b2, _c;
|
|
3385
|
+
return setTimeout(
|
|
3386
|
+
resolve,
|
|
3387
|
+
(_c = (_a2 = this.client.options.rest) == null ? void 0 : _a2.timeout) != null ? _c : (_b2 = DEFAULT_CLIENT_OPTIONS.rest) == null ? void 0 : _b2.timeout
|
|
3388
|
+
);
|
|
3389
|
+
}
|
|
3390
|
+
);
|
|
3391
|
+
return this.retryRequest(attempt + 1, method, url, body, query);
|
|
3273
3392
|
}
|
|
3274
3393
|
});
|
|
3275
3394
|
}
|
|
@@ -3334,10 +3453,11 @@ var RestClient = class {
|
|
|
3334
3453
|
};
|
|
3335
3454
|
|
|
3336
3455
|
// src/rest/CDNClient.ts
|
|
3337
|
-
var
|
|
3456
|
+
var import_axios5 = require("axios");
|
|
3338
3457
|
var CDNClient = class {
|
|
3339
3458
|
constructor(client3) {
|
|
3340
3459
|
this.client = client3;
|
|
3460
|
+
this.rateLimitQueue = new RateLimitQueue();
|
|
3341
3461
|
}
|
|
3342
3462
|
/**
|
|
3343
3463
|
* Helper function to handle API requests.
|
|
@@ -3347,12 +3467,12 @@ var CDNClient = class {
|
|
|
3347
3467
|
* @param query Query parameters (if applicable).
|
|
3348
3468
|
* @returns The API response.
|
|
3349
3469
|
*/
|
|
3350
|
-
request(method, url, data, query) {
|
|
3470
|
+
request(method, url, data, query, retry) {
|
|
3351
3471
|
return __async(this, null, function* () {
|
|
3352
3472
|
try {
|
|
3353
3473
|
if (!this.client.token) throw new Error("Token is required");
|
|
3354
3474
|
const authHeader = this.client.bot ? "X-Bot-Token" : "X-Session-Token";
|
|
3355
|
-
const config = {
|
|
3475
|
+
const config = __spreadProps(__spreadValues({}, {
|
|
3356
3476
|
method,
|
|
3357
3477
|
url: `${cdnUrl}${url}`,
|
|
3358
3478
|
params: query,
|
|
@@ -3363,12 +3483,50 @@ var CDNClient = class {
|
|
|
3363
3483
|
"Content-Type": "multipart/form-data",
|
|
3364
3484
|
"User-Agent": `RevBot.js/${version}`
|
|
3365
3485
|
}, data.getHeaders())
|
|
3366
|
-
}
|
|
3367
|
-
|
|
3486
|
+
}), {
|
|
3487
|
+
url: `${cdnUrl}${url}`
|
|
3488
|
+
});
|
|
3489
|
+
const response = yield this.rateLimitQueue.request(config);
|
|
3368
3490
|
return response.data;
|
|
3369
3491
|
} catch (error) {
|
|
3370
|
-
|
|
3371
|
-
|
|
3492
|
+
if (retry) throw typeof error;
|
|
3493
|
+
if (error instanceof import_axios5.AxiosError) {
|
|
3494
|
+
if (error.status && (error.status === 429 || error.status >= 500)) {
|
|
3495
|
+
return this.retryRequest(0, method, url, data, query);
|
|
3496
|
+
}
|
|
3497
|
+
if (error.status) {
|
|
3498
|
+
console.error(`API call failed with status ${error.status}:`, error);
|
|
3499
|
+
throw new Error(
|
|
3500
|
+
`API call failed with status ${error.status}: ${error.message}`
|
|
3501
|
+
);
|
|
3502
|
+
}
|
|
3503
|
+
}
|
|
3504
|
+
throw new Error(
|
|
3505
|
+
`API call failed: ${error instanceof Error ? error.message : error}`
|
|
3506
|
+
);
|
|
3507
|
+
}
|
|
3508
|
+
});
|
|
3509
|
+
}
|
|
3510
|
+
retryRequest(attempt = 0, method, url, body, query) {
|
|
3511
|
+
return __async(this, null, function* () {
|
|
3512
|
+
var _a, _b;
|
|
3513
|
+
if (attempt >= ((_b = (_a = this.client.options.rest) == null ? void 0 : _a.retries) != null ? _b : 3)) {
|
|
3514
|
+
throw new Error("Max retries reached");
|
|
3515
|
+
}
|
|
3516
|
+
try {
|
|
3517
|
+
return yield this.request(method, url, body, query, true);
|
|
3518
|
+
} catch (error) {
|
|
3519
|
+
console.warn(`Attempt ${attempt + 1} failed:`, error);
|
|
3520
|
+
yield new Promise(
|
|
3521
|
+
(resolve) => {
|
|
3522
|
+
var _a2, _b2, _c;
|
|
3523
|
+
return setTimeout(
|
|
3524
|
+
resolve,
|
|
3525
|
+
(_c = (_a2 = this.client.options.rest) == null ? void 0 : _a2.timeout) != null ? _c : (_b2 = DEFAULT_CLIENT_OPTIONS.rest) == null ? void 0 : _b2.timeout
|
|
3526
|
+
);
|
|
3527
|
+
}
|
|
3528
|
+
);
|
|
3529
|
+
return this.retryRequest(attempt + 1, method, url, body, query);
|
|
3372
3530
|
}
|
|
3373
3531
|
});
|
|
3374
3532
|
}
|
|
@@ -3401,7 +3559,7 @@ var BaseClient = class extends import_node_events.EventEmitter {
|
|
|
3401
3559
|
__privateAdd(this, _token, null);
|
|
3402
3560
|
/** Whether the client is a bot. */
|
|
3403
3561
|
this.bot = true;
|
|
3404
|
-
this.options = __spreadValues({}, options);
|
|
3562
|
+
this.options = __spreadValues(__spreadValues({}, DEFAULT_CLIENT_OPTIONS), options);
|
|
3405
3563
|
this.bot = (_a = this.options.isBot) != null ? _a : true;
|
|
3406
3564
|
this.api = new RestClient(this);
|
|
3407
3565
|
this.cdn = new CDNClient(this);
|
|
@@ -3637,7 +3795,7 @@ var Message6 = class extends Event {
|
|
|
3637
3795
|
const channel = this.client.channels.cache.get(data.channel);
|
|
3638
3796
|
if (channel == null ? void 0 : channel.isText()) {
|
|
3639
3797
|
const message = channel.messages._add(data);
|
|
3640
|
-
if (data.author !== SYSTEM_USER_ID) {
|
|
3798
|
+
if (data.author !== SYSTEM_USER_ID && !data.webhook) {
|
|
3641
3799
|
yield this.client.users.fetch(data.author, { force: false });
|
|
3642
3800
|
}
|
|
3643
3801
|
this.client.emit("message" /* MESSAGE */, message);
|
package/dist/index.mjs
CHANGED
|
@@ -3158,15 +3158,96 @@ var ServerMemberManager = class extends BaseManager {
|
|
|
3158
3158
|
import { EventEmitter } from "node:events";
|
|
3159
3159
|
|
|
3160
3160
|
// src/rest/restClient.ts
|
|
3161
|
-
import
|
|
3161
|
+
import { AxiosError } from "axios";
|
|
3162
3162
|
|
|
3163
3163
|
// package.json
|
|
3164
|
-
var version = "0.0.
|
|
3164
|
+
var version = "0.0.11";
|
|
3165
|
+
|
|
3166
|
+
// src/rest/restUtils/rateLimitQueue.ts
|
|
3167
|
+
import axios3 from "axios";
|
|
3168
|
+
var RateLimitQueue = class {
|
|
3169
|
+
constructor() {
|
|
3170
|
+
this.bucketMap = /* @__PURE__ */ new Map();
|
|
3171
|
+
this.pathToBucket = /* @__PURE__ */ new Map();
|
|
3172
|
+
}
|
|
3173
|
+
request(config) {
|
|
3174
|
+
return __async(this, null, function* () {
|
|
3175
|
+
const path = config.url;
|
|
3176
|
+
const bucketId = this.pathToBucket.get(path);
|
|
3177
|
+
let bucket = bucketId ? this.bucketMap.get(bucketId) : void 0;
|
|
3178
|
+
if (bucket && bucket.remaining <= 0 && Date.now() < bucket.resetAfter) {
|
|
3179
|
+
return new Promise((resolve, reject) => {
|
|
3180
|
+
bucket.queue.push(() => __async(this, null, function* () {
|
|
3181
|
+
try {
|
|
3182
|
+
const res = yield this._doRequest(config, path);
|
|
3183
|
+
resolve(res);
|
|
3184
|
+
} catch (e) {
|
|
3185
|
+
reject(e);
|
|
3186
|
+
}
|
|
3187
|
+
}));
|
|
3188
|
+
});
|
|
3189
|
+
} else {
|
|
3190
|
+
return this._doRequest(config, path);
|
|
3191
|
+
}
|
|
3192
|
+
});
|
|
3193
|
+
}
|
|
3194
|
+
_doRequest(config, path) {
|
|
3195
|
+
return __async(this, null, function* () {
|
|
3196
|
+
const response = yield axios3(config);
|
|
3197
|
+
this._updateRateLimit(path, response);
|
|
3198
|
+
return response;
|
|
3199
|
+
});
|
|
3200
|
+
}
|
|
3201
|
+
_updateRateLimit(path, response) {
|
|
3202
|
+
const headers = response.headers;
|
|
3203
|
+
const limit = parseInt(headers["x-ratelimit-limit"]);
|
|
3204
|
+
const remaining = parseInt(headers["x-ratelimit-remaining"]);
|
|
3205
|
+
const resetAfter = parseFloat(headers["x-ratelimit-reset-after"]) * 1e3 + Date.now();
|
|
3206
|
+
const bucket = headers["x-ratelimit-bucket"];
|
|
3207
|
+
const resetIn = parseInt(headers["x-ratelimit-reset-after"]);
|
|
3208
|
+
if (!bucket) return;
|
|
3209
|
+
this.pathToBucket.set(path, bucket);
|
|
3210
|
+
let state = this.bucketMap.get(bucket);
|
|
3211
|
+
if (!state) {
|
|
3212
|
+
state = {
|
|
3213
|
+
limit,
|
|
3214
|
+
remaining,
|
|
3215
|
+
resetAfter,
|
|
3216
|
+
bucket,
|
|
3217
|
+
resetIn,
|
|
3218
|
+
queue: [],
|
|
3219
|
+
lastPath: path
|
|
3220
|
+
};
|
|
3221
|
+
this.bucketMap.set(bucket, state);
|
|
3222
|
+
} else {
|
|
3223
|
+
state.limit = limit;
|
|
3224
|
+
state.remaining = remaining;
|
|
3225
|
+
state.resetAfter = resetAfter;
|
|
3226
|
+
state.lastPath = path;
|
|
3227
|
+
}
|
|
3228
|
+
if (remaining <= 0) {
|
|
3229
|
+
if (state.resetTimeout) clearTimeout(state.resetTimeout);
|
|
3230
|
+
const delay = resetIn;
|
|
3231
|
+
state.resetTimeout = setTimeout(() => {
|
|
3232
|
+
state.remaining = state.limit;
|
|
3233
|
+
state.resetTimeout = void 0;
|
|
3234
|
+
while (state.queue.length > 0 && state.remaining > 0) {
|
|
3235
|
+
const fn = state.queue.shift();
|
|
3236
|
+
if (fn) {
|
|
3237
|
+
state.remaining--;
|
|
3238
|
+
fn();
|
|
3239
|
+
}
|
|
3240
|
+
}
|
|
3241
|
+
}, delay);
|
|
3242
|
+
}
|
|
3243
|
+
}
|
|
3244
|
+
};
|
|
3165
3245
|
|
|
3166
3246
|
// src/rest/restClient.ts
|
|
3167
3247
|
var RestClient = class {
|
|
3168
3248
|
constructor(client3) {
|
|
3169
3249
|
this.client = client3;
|
|
3250
|
+
this.rateLimitQueue = new RateLimitQueue();
|
|
3170
3251
|
}
|
|
3171
3252
|
/**
|
|
3172
3253
|
* Helper function to handle API requests.
|
|
@@ -3176,12 +3257,13 @@ var RestClient = class {
|
|
|
3176
3257
|
* @param query Query parameters (if applicable).
|
|
3177
3258
|
* @returns The API response.
|
|
3178
3259
|
*/
|
|
3179
|
-
request(method, url, body, query) {
|
|
3260
|
+
request(method, url, body, query, retry) {
|
|
3180
3261
|
return __async(this, null, function* () {
|
|
3262
|
+
var _a;
|
|
3181
3263
|
try {
|
|
3182
3264
|
if (!this.client.token) throw new Error("Token is required");
|
|
3183
3265
|
const authHeader = this.client.bot ? "X-Bot-Token" : "X-Session-Token";
|
|
3184
|
-
const config = {
|
|
3266
|
+
const config = __spreadProps(__spreadValues({}, {
|
|
3185
3267
|
method,
|
|
3186
3268
|
url: `${apiUrl}${url}`,
|
|
3187
3269
|
params: query,
|
|
@@ -3190,12 +3272,49 @@ var RestClient = class {
|
|
|
3190
3272
|
[authHeader]: this.client.token,
|
|
3191
3273
|
"User-Agent": `RevBot.js/${version}`
|
|
3192
3274
|
}
|
|
3193
|
-
}
|
|
3194
|
-
|
|
3275
|
+
}), {
|
|
3276
|
+
url: `${apiUrl}${url}`
|
|
3277
|
+
});
|
|
3278
|
+
const response = yield this.rateLimitQueue.request(config);
|
|
3195
3279
|
return response.data;
|
|
3196
3280
|
} catch (error) {
|
|
3197
|
-
|
|
3198
|
-
|
|
3281
|
+
if (retry) throw typeof error;
|
|
3282
|
+
if (error instanceof AxiosError) {
|
|
3283
|
+
if (error.status && (error.status === 429 || error.status >= 500)) {
|
|
3284
|
+
return this.retryRequest(0, method, url, body, query);
|
|
3285
|
+
}
|
|
3286
|
+
if (error.status) {
|
|
3287
|
+
throw new Error(
|
|
3288
|
+
`API call failed with status ${error.status}: ${(_a = error.response) == null ? void 0 : _a.statusText}`
|
|
3289
|
+
);
|
|
3290
|
+
}
|
|
3291
|
+
}
|
|
3292
|
+
throw new Error(
|
|
3293
|
+
`API call failed: ${error instanceof Error ? error.message : error}`
|
|
3294
|
+
);
|
|
3295
|
+
}
|
|
3296
|
+
});
|
|
3297
|
+
}
|
|
3298
|
+
retryRequest(attempt = 0, method, url, body, query) {
|
|
3299
|
+
return __async(this, null, function* () {
|
|
3300
|
+
var _a, _b;
|
|
3301
|
+
if (attempt >= ((_b = (_a = this.client.options.rest) == null ? void 0 : _a.retries) != null ? _b : 3)) {
|
|
3302
|
+
throw new Error("Max retries reached");
|
|
3303
|
+
}
|
|
3304
|
+
try {
|
|
3305
|
+
return yield this.request(method, url, body, query, true);
|
|
3306
|
+
} catch (error) {
|
|
3307
|
+
console.warn(`Attempt ${attempt + 1} failed:`, error);
|
|
3308
|
+
yield new Promise(
|
|
3309
|
+
(resolve) => {
|
|
3310
|
+
var _a2, _b2, _c;
|
|
3311
|
+
return setTimeout(
|
|
3312
|
+
resolve,
|
|
3313
|
+
(_c = (_a2 = this.client.options.rest) == null ? void 0 : _a2.timeout) != null ? _c : (_b2 = DEFAULT_CLIENT_OPTIONS.rest) == null ? void 0 : _b2.timeout
|
|
3314
|
+
);
|
|
3315
|
+
}
|
|
3316
|
+
);
|
|
3317
|
+
return this.retryRequest(attempt + 1, method, url, body, query);
|
|
3199
3318
|
}
|
|
3200
3319
|
});
|
|
3201
3320
|
}
|
|
@@ -3260,10 +3379,11 @@ var RestClient = class {
|
|
|
3260
3379
|
};
|
|
3261
3380
|
|
|
3262
3381
|
// src/rest/CDNClient.ts
|
|
3263
|
-
import
|
|
3382
|
+
import { AxiosError as AxiosError2 } from "axios";
|
|
3264
3383
|
var CDNClient = class {
|
|
3265
3384
|
constructor(client3) {
|
|
3266
3385
|
this.client = client3;
|
|
3386
|
+
this.rateLimitQueue = new RateLimitQueue();
|
|
3267
3387
|
}
|
|
3268
3388
|
/**
|
|
3269
3389
|
* Helper function to handle API requests.
|
|
@@ -3273,12 +3393,12 @@ var CDNClient = class {
|
|
|
3273
3393
|
* @param query Query parameters (if applicable).
|
|
3274
3394
|
* @returns The API response.
|
|
3275
3395
|
*/
|
|
3276
|
-
request(method, url, data, query) {
|
|
3396
|
+
request(method, url, data, query, retry) {
|
|
3277
3397
|
return __async(this, null, function* () {
|
|
3278
3398
|
try {
|
|
3279
3399
|
if (!this.client.token) throw new Error("Token is required");
|
|
3280
3400
|
const authHeader = this.client.bot ? "X-Bot-Token" : "X-Session-Token";
|
|
3281
|
-
const config = {
|
|
3401
|
+
const config = __spreadProps(__spreadValues({}, {
|
|
3282
3402
|
method,
|
|
3283
3403
|
url: `${cdnUrl}${url}`,
|
|
3284
3404
|
params: query,
|
|
@@ -3289,12 +3409,50 @@ var CDNClient = class {
|
|
|
3289
3409
|
"Content-Type": "multipart/form-data",
|
|
3290
3410
|
"User-Agent": `RevBot.js/${version}`
|
|
3291
3411
|
}, data.getHeaders())
|
|
3292
|
-
}
|
|
3293
|
-
|
|
3412
|
+
}), {
|
|
3413
|
+
url: `${cdnUrl}${url}`
|
|
3414
|
+
});
|
|
3415
|
+
const response = yield this.rateLimitQueue.request(config);
|
|
3294
3416
|
return response.data;
|
|
3295
3417
|
} catch (error) {
|
|
3296
|
-
|
|
3297
|
-
|
|
3418
|
+
if (retry) throw typeof error;
|
|
3419
|
+
if (error instanceof AxiosError2) {
|
|
3420
|
+
if (error.status && (error.status === 429 || error.status >= 500)) {
|
|
3421
|
+
return this.retryRequest(0, method, url, data, query);
|
|
3422
|
+
}
|
|
3423
|
+
if (error.status) {
|
|
3424
|
+
console.error(`API call failed with status ${error.status}:`, error);
|
|
3425
|
+
throw new Error(
|
|
3426
|
+
`API call failed with status ${error.status}: ${error.message}`
|
|
3427
|
+
);
|
|
3428
|
+
}
|
|
3429
|
+
}
|
|
3430
|
+
throw new Error(
|
|
3431
|
+
`API call failed: ${error instanceof Error ? error.message : error}`
|
|
3432
|
+
);
|
|
3433
|
+
}
|
|
3434
|
+
});
|
|
3435
|
+
}
|
|
3436
|
+
retryRequest(attempt = 0, method, url, body, query) {
|
|
3437
|
+
return __async(this, null, function* () {
|
|
3438
|
+
var _a, _b;
|
|
3439
|
+
if (attempt >= ((_b = (_a = this.client.options.rest) == null ? void 0 : _a.retries) != null ? _b : 3)) {
|
|
3440
|
+
throw new Error("Max retries reached");
|
|
3441
|
+
}
|
|
3442
|
+
try {
|
|
3443
|
+
return yield this.request(method, url, body, query, true);
|
|
3444
|
+
} catch (error) {
|
|
3445
|
+
console.warn(`Attempt ${attempt + 1} failed:`, error);
|
|
3446
|
+
yield new Promise(
|
|
3447
|
+
(resolve) => {
|
|
3448
|
+
var _a2, _b2, _c;
|
|
3449
|
+
return setTimeout(
|
|
3450
|
+
resolve,
|
|
3451
|
+
(_c = (_a2 = this.client.options.rest) == null ? void 0 : _a2.timeout) != null ? _c : (_b2 = DEFAULT_CLIENT_OPTIONS.rest) == null ? void 0 : _b2.timeout
|
|
3452
|
+
);
|
|
3453
|
+
}
|
|
3454
|
+
);
|
|
3455
|
+
return this.retryRequest(attempt + 1, method, url, body, query);
|
|
3298
3456
|
}
|
|
3299
3457
|
});
|
|
3300
3458
|
}
|
|
@@ -3327,7 +3485,7 @@ var BaseClient = class extends EventEmitter {
|
|
|
3327
3485
|
__privateAdd(this, _token, null);
|
|
3328
3486
|
/** Whether the client is a bot. */
|
|
3329
3487
|
this.bot = true;
|
|
3330
|
-
this.options = __spreadValues({}, options);
|
|
3488
|
+
this.options = __spreadValues(__spreadValues({}, DEFAULT_CLIENT_OPTIONS), options);
|
|
3331
3489
|
this.bot = (_a = this.options.isBot) != null ? _a : true;
|
|
3332
3490
|
this.api = new RestClient(this);
|
|
3333
3491
|
this.cdn = new CDNClient(this);
|
|
@@ -3563,7 +3721,7 @@ var Message6 = class extends Event {
|
|
|
3563
3721
|
const channel = this.client.channels.cache.get(data.channel);
|
|
3564
3722
|
if (channel == null ? void 0 : channel.isText()) {
|
|
3565
3723
|
const message = channel.messages._add(data);
|
|
3566
|
-
if (data.author !== SYSTEM_USER_ID) {
|
|
3724
|
+
if (data.author !== SYSTEM_USER_ID && !data.webhook) {
|
|
3567
3725
|
yield this.client.users.fetch(data.author, { force: false });
|
|
3568
3726
|
}
|
|
3569
3727
|
this.client.emit("message" /* MESSAGE */, message);
|
package/package.json
CHANGED