@discordeno/rest 19.0.0-next.782a778 → 19.0.0-next.7b5d99e

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.
@@ -1 +1 @@
1
- {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAmEA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAKrH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CAmsDhF"}
1
+ {"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../src/manager.ts"],"names":[],"mappings":"AAiEA,OAAO,KAAK,EAA4B,wBAAwB,EAAE,WAAW,EAAsB,MAAM,YAAY,CAAA;AAKrH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,wBAAwB,GAAG,WAAW,CA2pDhF"}
package/dist/manager.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/restrict-template-expressions */ /* eslint-disable no-const-assign */ import { InteractionResponseTypes } from '@discordeno/types';
2
- import { calculateBits, camelize, camelToSnakeCase, delay, encode, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
2
+ import { calculateBits, camelize, camelToSnakeCase, delay, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
3
3
  import fetch from 'node-fetch';
4
4
  import { createInvalidRequestBucket } from './invalidBucket.js';
5
5
  import { Queue } from './queue.js';
@@ -754,7 +754,7 @@ export function createRestManager(options) {
754
754
  }
755
755
  return parts.join('/');
756
756
  },
757
- processRequest (request) {
757
+ async processRequest (request) {
758
758
  const route = request.url.substring(request.url.indexOf('api/'));
759
759
  const parts = route.split('/');
760
760
  // Remove the api/
@@ -764,6 +764,10 @@ export function createRestManager(options) {
764
764
  // Set the full url to discord api in case it was recieved in a proxy rest
765
765
  request.url = `${rest.baseUrl}/v${rest.version}/${parts.join('/')}`;
766
766
  const url = rest.simplifyUrl(request.url, request.method);
767
+ if (request.runThroughQueue === false) {
768
+ await rest.sendRequest(request);
769
+ return;
770
+ }
767
771
  const queue = rest.queues.get(url);
768
772
  if (queue !== undefined) {
769
773
  queue.makeRequest(request);
@@ -781,34 +785,7 @@ export function createRestManager(options) {
781
785
  },
782
786
  async makeRequest (method, url, options) {
783
787
  if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') {
784
- // Special handling for sending blobs across http to proxy
785
- // TODO: fix this hacky handling
786
- if (!(options?.body instanceof FormData) && !Array.isArray(options?.body) && options?.body?.file) {
787
- if (!Array.isArray(options.body.file)) {
788
- options.body.file = [
789
- options.body.file
790
- ];
791
- }
792
- // convert blobs to string before sending to proxy
793
- options.body.file = await Promise.all(options.body.file.map(async (f)=>{
794
- const url = encode(await f.blob.arrayBuffer());
795
- return {
796
- name: f.name,
797
- blob: `data:${f.blob.type};base64,${url}`
798
- };
799
- }));
800
- }
801
- const headers = {
802
- Authorization: rest.authorization ?? ''
803
- };
804
- if (options?.body) {
805
- headers['Content-Type'] = 'application/json';
806
- }
807
- const result = await fetch(`${rest.baseUrl}${url}`, {
808
- body: options?.body ? JSON.stringify(options.body) : undefined,
809
- headers,
810
- method
811
- });
788
+ const result = await fetch(`${rest.baseUrl}${url}`, rest.createRequestBody(method, options));
812
789
  if (!result.ok) {
813
790
  const err = await result.json().catch(()=>{});
814
791
  // Legacy Handling to not break old code or when body is missing
@@ -817,21 +794,22 @@ export function createRestManager(options) {
817
794
  }
818
795
  return result.status !== 204 ? await result.json() : undefined;
819
796
  }
820
- return await new Promise((resolve, reject)=>{
797
+ return await new Promise(async (resolve, reject)=>{
821
798
  const payload = {
822
799
  url,
823
800
  method,
824
801
  requestBodyOptions: options,
825
802
  retryCount: 0,
826
803
  retryRequest: async function(payload) {
827
- rest.processRequest(payload);
804
+ await rest.processRequest(payload);
828
805
  },
829
806
  resolve: (data)=>{
830
807
  resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
831
808
  },
832
- reject
809
+ reject,
810
+ runThroughQueue: options?.runThroughQueue
833
811
  };
834
- rest.processRequest(payload);
812
+ await rest.processRequest(payload);
835
813
  });
836
814
  },
837
815
  async get (url, options) {
@@ -872,19 +850,22 @@ export function createRestManager(options) {
872
850
  async addThreadMember (channelId, userId) {
873
851
  await rest.put(rest.routes.channels.threads.user(channelId, userId));
874
852
  },
875
- async createAutomodRule (guildId, body) {
853
+ async createAutomodRule (guildId, body, reason) {
876
854
  return await rest.post(rest.routes.guilds.automod.rules(guildId), {
877
- body
855
+ body,
856
+ reason
878
857
  });
879
858
  },
880
- async createChannel (guildId, body) {
859
+ async createChannel (guildId, body, reason) {
881
860
  return await rest.post(rest.routes.guilds.channels(guildId), {
882
- body
861
+ body,
862
+ reason
883
863
  });
884
864
  },
885
- async createEmoji (guildId, body) {
865
+ async createEmoji (guildId, body, reason) {
886
866
  return await rest.post(rest.routes.guilds.emojis(guildId), {
887
- body
867
+ body,
868
+ reason
888
869
  });
889
870
  },
890
871
  async createGlobalApplicationCommand (body) {
@@ -910,14 +891,15 @@ export function createRestManager(options) {
910
891
  body
911
892
  });
912
893
  },
913
- async createGuildSticker (guildId, options) {
894
+ async createGuildSticker (guildId, options, reason) {
914
895
  const form = new FormData();
915
896
  form.append('file', options.file.blob, options.file.name);
916
897
  form.append('name', options.name);
917
898
  form.append('description', options.description);
918
899
  form.append('tags', options.tags);
919
900
  return await rest.post(rest.routes.guilds.stickers(guildId), {
920
- body: form
901
+ body: form,
902
+ reason
921
903
  });
922
904
  },
923
905
  async createGuildTemplate (guildId, body) {
@@ -925,15 +907,17 @@ export function createRestManager(options) {
925
907
  body
926
908
  });
927
909
  },
928
- async createForumThread (channelId, body) {
910
+ async createForumThread (channelId, body, reason) {
929
911
  return await rest.post(rest.routes.channels.forum(channelId), {
930
912
  body,
931
- files: body.files
913
+ files: body.files,
914
+ reason
932
915
  });
933
916
  },
934
- async createInvite (channelId, body = {}) {
917
+ async createInvite (channelId, body = {}, reason) {
935
918
  return await rest.post(rest.routes.channels.invites(channelId), {
936
- body
919
+ body,
920
+ reason
937
921
  });
938
922
  },
939
923
  async createRole (guildId, body, reason) {
@@ -942,14 +926,16 @@ export function createRestManager(options) {
942
926
  reason
943
927
  });
944
928
  },
945
- async createScheduledEvent (guildId, body) {
929
+ async createScheduledEvent (guildId, body, reason) {
946
930
  return await rest.post(rest.routes.guilds.events.events(guildId), {
947
- body
931
+ body,
932
+ reason
948
933
  });
949
934
  },
950
- async createStageInstance (body) {
935
+ async createStageInstance (body, reason) {
951
936
  return await rest.post(rest.routes.channels.stages(), {
952
- body
937
+ body,
938
+ reason
953
939
  });
954
940
  },
955
941
  async createWebhook (channelId, options, reason) {
@@ -982,7 +968,9 @@ export function createRestManager(options) {
982
968
  });
983
969
  },
984
970
  async deleteFollowupMessage (token, messageId) {
985
- await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
971
+ await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
972
+ unauthorized: true
973
+ });
986
974
  },
987
975
  async deleteGlobalApplicationCommand (commandId) {
988
976
  await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
@@ -1001,8 +989,10 @@ export function createRestManager(options) {
1001
989
  async deleteGuildTemplate (guildId, templateCode) {
1002
990
  await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
1003
991
  },
1004
- async deleteIntegration (guildId, integrationId) {
1005
- await rest.delete(rest.routes.guilds.integration(guildId, integrationId));
992
+ async deleteIntegration (guildId, integrationId, reason) {
993
+ await rest.delete(rest.routes.guilds.integration(guildId, integrationId), {
994
+ reason
995
+ });
1006
996
  },
1007
997
  async deleteInvite (inviteCode, reason) {
1008
998
  await rest.delete(rest.routes.guilds.invite(inviteCode), {
@@ -1023,7 +1013,9 @@ export function createRestManager(options) {
1023
1013
  });
1024
1014
  },
1025
1015
  async deleteOriginalInteractionResponse (token) {
1026
- await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token));
1016
+ await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token), {
1017
+ unauthorized: true
1018
+ });
1027
1019
  },
1028
1020
  async deleteOwnReaction (channelId, messageId, reaction) {
1029
1021
  reaction = processReactionString(reaction);
@@ -1036,8 +1028,10 @@ export function createRestManager(options) {
1036
1028
  reaction = processReactionString(reaction);
1037
1029
  await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
1038
1030
  },
1039
- async deleteRole (guildId, roleId) {
1040
- await rest.delete(rest.routes.guilds.roles.one(guildId, roleId));
1031
+ async deleteRole (guildId, roleId, reason) {
1032
+ await rest.delete(rest.routes.guilds.roles.one(guildId, roleId), {
1033
+ reason
1034
+ });
1041
1035
  },
1042
1036
  async deleteScheduledEvent (guildId, eventId) {
1043
1037
  await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
@@ -1072,9 +1066,10 @@ export function createRestManager(options) {
1072
1066
  }
1073
1067
  });
1074
1068
  },
1075
- async editAutomodRule (guildId, ruleId, body) {
1069
+ async editAutomodRule (guildId, ruleId, body, reason) {
1076
1070
  return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
1077
- body
1071
+ body,
1072
+ reason
1078
1073
  });
1079
1074
  },
1080
1075
  async editBotProfile (options) {
@@ -1086,14 +1081,16 @@ export function createRestManager(options) {
1086
1081
  }
1087
1082
  });
1088
1083
  },
1089
- async editChannel (channelId, body) {
1084
+ async editChannel (channelId, body, reason) {
1090
1085
  return await rest.patch(rest.routes.channels.channel(channelId), {
1091
- body
1086
+ body,
1087
+ reason
1092
1088
  });
1093
1089
  },
1094
- async editChannelPermissionOverrides (channelId, body) {
1090
+ async editChannelPermissionOverrides (channelId, body, reason) {
1095
1091
  await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
1096
- body
1092
+ body,
1093
+ reason
1097
1094
  });
1098
1095
  },
1099
1096
  async editChannelPositions (guildId, body) {
@@ -1101,15 +1098,17 @@ export function createRestManager(options) {
1101
1098
  body
1102
1099
  });
1103
1100
  },
1104
- async editEmoji (guildId, id, body) {
1101
+ async editEmoji (guildId, id, body, reason) {
1105
1102
  return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
1106
- body
1103
+ body,
1104
+ reason
1107
1105
  });
1108
1106
  },
1109
1107
  async editFollowupMessage (token, messageId, body) {
1110
1108
  return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1111
1109
  body,
1112
- files: body.files
1110
+ files: body.files,
1111
+ unauthorized: true
1113
1112
  });
1114
1113
  },
1115
1114
  async editGlobalApplicationCommand (commandId, body) {
@@ -1117,9 +1116,10 @@ export function createRestManager(options) {
1117
1116
  body
1118
1117
  });
1119
1118
  },
1120
- async editGuild (guildId, body) {
1119
+ async editGuild (guildId, body, reason) {
1121
1120
  return await rest.patch(rest.routes.guilds.guild(guildId), {
1122
- body
1121
+ body,
1122
+ reason
1123
1123
  });
1124
1124
  },
1125
1125
  async editGuildApplicationCommand (commandId, guildId, body) {
@@ -1135,9 +1135,10 @@ export function createRestManager(options) {
1135
1135
  reason
1136
1136
  });
1137
1137
  },
1138
- async editGuildSticker (guildId, stickerId, body) {
1138
+ async editGuildSticker (guildId, stickerId, body, reason) {
1139
1139
  return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
1140
- body
1140
+ body,
1141
+ reason
1141
1142
  });
1142
1143
  },
1143
1144
  async editGuildTemplate (guildId, templateCode, body) {
@@ -1162,7 +1163,8 @@ export function createRestManager(options) {
1162
1163
  type: InteractionResponseTypes.UpdateMessage,
1163
1164
  data: options
1164
1165
  },
1165
- files: options.files
1166
+ files: options.files,
1167
+ unauthorized: true
1166
1168
  });
1167
1169
  },
1168
1170
  async editOwnVoiceState (guildId, options) {
@@ -1173,19 +1175,22 @@ export function createRestManager(options) {
1173
1175
  }
1174
1176
  });
1175
1177
  },
1176
- async editScheduledEvent (guildId, eventId, body) {
1178
+ async editScheduledEvent (guildId, eventId, body, reason) {
1177
1179
  return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
1178
- body
1180
+ body,
1181
+ reason
1179
1182
  });
1180
1183
  },
1181
- async editRole (guildId, roleId, body) {
1184
+ async editRole (guildId, roleId, body, reason) {
1182
1185
  return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
1183
- body
1186
+ body,
1187
+ reason
1184
1188
  });
1185
1189
  },
1186
- async editRolePositions (guildId, body) {
1190
+ async editRolePositions (guildId, body, reason) {
1187
1191
  return await rest.patch(rest.routes.guilds.roles.all(guildId), {
1188
- body
1192
+ body,
1193
+ reason
1189
1194
  });
1190
1195
  },
1191
1196
  async editStageInstance (channelId, topic, reason) {
@@ -1201,9 +1206,10 @@ export function createRestManager(options) {
1201
1206
  body: options
1202
1207
  });
1203
1208
  },
1204
- async editWebhook (webhookId, body) {
1209
+ async editWebhook (webhookId, body, reason) {
1205
1210
  return await rest.patch(rest.routes.webhooks.id(webhookId), {
1206
- body
1211
+ body,
1212
+ reason
1207
1213
  });
1208
1214
  },
1209
1215
  async editWebhookMessage (webhookId, token, messageId, options) {
@@ -1217,14 +1223,16 @@ export function createRestManager(options) {
1217
1223
  body
1218
1224
  });
1219
1225
  },
1220
- async editWelcomeScreen (guildId, body) {
1226
+ async editWelcomeScreen (guildId, body, reason) {
1221
1227
  return await rest.patch(rest.routes.guilds.welcome(guildId), {
1222
- body
1228
+ body,
1229
+ reason
1223
1230
  });
1224
1231
  },
1225
- async editWidgetSettings (guildId, body) {
1232
+ async editWidgetSettings (guildId, body, reason) {
1226
1233
  return await rest.patch(rest.routes.guilds.widget(guildId), {
1227
- body
1234
+ body,
1235
+ reason
1228
1236
  });
1229
1237
  },
1230
1238
  async executeWebhook (webhookId, token, options) {
@@ -1295,7 +1303,9 @@ export function createRestManager(options) {
1295
1303
  return await rest.get(rest.routes.guilds.emojis(guildId));
1296
1304
  },
1297
1305
  async getFollowupMessage (token, messageId) {
1298
- return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
1306
+ return await rest.get(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1307
+ unauthorized: true
1308
+ });
1299
1309
  },
1300
1310
  async getGatewayBot () {
1301
1311
  return await rest.get(rest.routes.gatewayBot());
@@ -1348,7 +1358,9 @@ export function createRestManager(options) {
1348
1358
  return await rest.get(rest.routes.nitroStickerPacks());
1349
1359
  },
1350
1360
  async getOriginalInteractionResponse (token) {
1351
- return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token));
1361
+ return await rest.get(rest.routes.interactions.responses.original(rest.applicationId, token), {
1362
+ unauthorized: true
1363
+ });
1352
1364
  },
1353
1365
  async getPinnedMessages (channelId) {
1354
1366
  return await rest.get(rest.routes.channels.pins(channelId));
@@ -1448,47 +1460,19 @@ export function createRestManager(options) {
1448
1460
  async removeThreadMember (channelId, userId) {
1449
1461
  await rest.delete(rest.routes.channels.threads.user(channelId, userId));
1450
1462
  },
1451
- // TODO: why that
1452
1463
  async sendFollowupMessage (token, options) {
1453
- return await new Promise((resolve, reject)=>{
1454
- rest.sendRequest({
1455
- url: rest.routes.webhooks.webhook(rest.applicationId, token),
1456
- method: 'POST',
1457
- requestBodyOptions: {
1458
- body: options,
1459
- files: options.files
1460
- },
1461
- retryCount: 0,
1462
- retryRequest: async function(options) {
1463
- // TODO: should change to reprocess queue item
1464
- await rest.sendRequest(options);
1465
- },
1466
- resolve: (data)=>{
1467
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1468
- },
1469
- reject
1470
- });
1464
+ return await rest.post(rest.routes.webhooks.webhook(rest.applicationId, token), {
1465
+ body: options,
1466
+ files: options.files,
1467
+ unauthorized: true
1471
1468
  });
1472
1469
  },
1473
- // TODO: why that
1474
1470
  async sendInteractionResponse (interactionId, token, options) {
1475
- await new Promise((resolve, reject)=>{
1476
- rest.sendRequest({
1477
- url: rest.routes.interactions.responses.callback(interactionId, token),
1478
- method: 'POST',
1479
- requestBodyOptions: {
1480
- body: options
1481
- },
1482
- retryCount: 0,
1483
- retryRequest: async function(options) {
1484
- // TODO: should change to reprocess queue item
1485
- await rest.sendRequest(options);
1486
- },
1487
- resolve: (data)=>{
1488
- resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1489
- },
1490
- reject
1491
- });
1471
+ return await rest.post(rest.routes.interactions.responses.callback(interactionId, token), {
1472
+ body: options,
1473
+ files: options.data?.files,
1474
+ runThroughQueue: false,
1475
+ unauthorized: true
1492
1476
  });
1493
1477
  },
1494
1478
  async sendMessage (channelId, body) {
@@ -1497,32 +1481,37 @@ export function createRestManager(options) {
1497
1481
  files: body.files
1498
1482
  });
1499
1483
  },
1500
- async startThreadWithMessage (channelId, messageId, body) {
1484
+ async startThreadWithMessage (channelId, messageId, body, reason) {
1501
1485
  return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1502
- body
1486
+ body,
1487
+ reason
1503
1488
  });
1504
1489
  },
1505
- async startThreadWithoutMessage (channelId, body) {
1490
+ async startThreadWithoutMessage (channelId, body, reason) {
1506
1491
  return await rest.post(rest.routes.channels.threads.all(channelId), {
1507
- body
1492
+ body,
1493
+ reason
1508
1494
  });
1509
1495
  },
1510
1496
  async syncGuildTemplate (guildId) {
1511
1497
  return await rest.put(rest.routes.guilds.templates.all(guildId));
1512
1498
  },
1513
- async banMember (guildId, userId, body) {
1499
+ async banMember (guildId, userId, body, reason) {
1514
1500
  await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1515
- body
1501
+ body,
1502
+ reason
1516
1503
  });
1517
1504
  },
1518
- async editBotMember (guildId, body) {
1505
+ async editBotMember (guildId, body, reason) {
1519
1506
  return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1520
- body
1507
+ body,
1508
+ reason
1521
1509
  });
1522
1510
  },
1523
- async editMember (guildId, userId, body) {
1511
+ async editMember (guildId, userId, body, reason) {
1524
1512
  return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1525
- body
1513
+ body,
1514
+ reason
1526
1515
  });
1527
1516
  },
1528
1517
  async getMember (guildId, userId) {
@@ -1541,16 +1530,19 @@ export function createRestManager(options) {
1541
1530
  reason
1542
1531
  });
1543
1532
  },
1544
- async pruneMembers (guildId, body) {
1533
+ async pruneMembers (guildId, body, reason) {
1545
1534
  return await rest.post(rest.routes.guilds.members.prune(guildId), {
1546
- body
1535
+ body,
1536
+ reason
1547
1537
  });
1548
1538
  },
1549
1539
  async searchMembers (guildId, query, options) {
1550
1540
  return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1551
1541
  },
1552
- async unbanMember (guildId, userId) {
1553
- await rest.delete(rest.routes.guilds.members.ban(guildId, userId));
1542
+ async unbanMember (guildId, userId, reason) {
1543
+ await rest.delete(rest.routes.guilds.members.ban(guildId, userId), {
1544
+ reason
1545
+ });
1554
1546
  },
1555
1547
  async unpinMessage (channelId, messageId, reason) {
1556
1548
  await rest.delete(rest.routes.channels.pin(channelId, messageId), {