@discordeno/rest 19.0.0-next.65f60af → 19.0.0-next.66869ce

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,CA2rDhF"}
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,CAwqDhF"}
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';
@@ -545,46 +545,47 @@ export function createRestManager(options) {
545
545
  if (typeof obj === 'bigint') return obj.toString();
546
546
  return obj;
547
547
  },
548
- createRequest (options) {
548
+ createRequestBody (method, options) {
549
549
  const headers = {
550
550
  'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`
551
551
  };
552
- if (!options.unauthorized) headers.authorization = `Bot ${rest.token}`;
552
+ if (!options?.unauthorized) headers.authorization = `Bot ${rest.token}`;
553
553
  // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
554
- if (options.reason !== undefined) {
555
- headers['x-audit-log-reason'] = encodeURIComponent(options.reason);
554
+ if (options?.reason !== undefined) {
555
+ headers['x-audit-log-reason'] = encodeURIComponent(options?.reason);
556
556
  }
557
557
  let body;
558
558
  // TODO: check if we need to add specific check for GET method
559
559
  // Since GET does not allow bodies
560
560
  // Have to check for attachments first, since body then has to be send in a different way.
561
- if (options.attachments !== undefined) {
561
+ if (options?.files !== undefined) {
562
562
  const form = new FormData();
563
- for(let i = 0; i < options.attachments.length; ++i){
564
- form.append(`file${i}`, options.attachments[i].blob, options.attachments[i].name);
563
+ for(let i = 0; i < options.files.length; ++i){
564
+ form.append(`file${i}`, options.files[i].blob, options.files[i].name);
565
565
  }
566
- form.append('payload_json', JSON.stringify(options.body));
566
+ form.append('payload_json', JSON.stringify({
567
+ ...options.body,
568
+ files: undefined
569
+ }));
567
570
  body = form;
568
- // TODO: boundary?
569
- // `multipart/form-data; boundary=${form.getBoundary()}`
570
- headers['content-type'] = `multipart/form-data`;
571
- } else if (options.body !== undefined) {
571
+ // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
572
+ } else if (options?.body !== undefined) {
572
573
  if (options.body instanceof FormData) {
573
574
  body = options.body;
574
- headers['content-type'] = `multipart/form-data`;
575
+ // No need to set the `content-type` header since `fetch` does that automatically for us when we use a `FormData` object.
575
576
  } else {
576
- body = JSON.stringify(options.body);
577
+ body = JSON.stringify(rest.changeToDiscordFormat(options.body));
577
578
  headers['content-type'] = `application/json`;
578
579
  }
579
580
  }
580
581
  // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
581
- if (options.headers) {
582
+ if (options?.headers) {
582
583
  Object.assign(headers, options.headers);
583
584
  }
584
585
  return {
585
586
  body,
586
587
  headers,
587
- method: options.method
588
+ method
588
589
  };
589
590
  },
590
591
  processRateLimitedPaths () {
@@ -679,12 +680,7 @@ export function createRestManager(options) {
679
680
  },
680
681
  async sendRequest (options) {
681
682
  const url = options.url.startsWith('https://') ? options.url : `${rest.baseUrl}/v${rest.version}${options.url}`;
682
- const payload = rest.createRequest({
683
- method: options.method,
684
- url: options.url,
685
- body: options.options?.body,
686
- ...options.options
687
- });
683
+ const payload = rest.createRequestBody(options.method, options.requestBodyOptions);
688
684
  logger.debug(`sending request to ${url}`, 'with payload:', {
689
685
  ...payload,
690
686
  headers: {
@@ -785,34 +781,7 @@ export function createRestManager(options) {
785
781
  },
786
782
  async makeRequest (method, url, options) {
787
783
  if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') {
788
- // Special handling for sending blobs across http to proxy
789
- // TODO: fix this hacky handling
790
- if (!(options?.body instanceof FormData) && !Array.isArray(options?.body) && options?.body?.file) {
791
- if (!Array.isArray(options.body.file)) {
792
- options.body.file = [
793
- options.body.file
794
- ];
795
- }
796
- // convert blobs to string before sending to proxy
797
- options.body.file = await Promise.all(options.body.file.map(async (f)=>{
798
- const url = encode(await f.blob.arrayBuffer());
799
- return {
800
- name: f.name,
801
- blob: `data:${f.blob.type};base64,${url}`
802
- };
803
- }));
804
- }
805
- const headers = {
806
- Authorization: rest.authorization ?? ''
807
- };
808
- if (options?.body) {
809
- headers['Content-Type'] = 'application/json';
810
- }
811
- const result = await fetch(`${rest.baseUrl}${url}`, {
812
- body: options?.body ? JSON.stringify(options.body) : undefined,
813
- headers,
814
- method
815
- });
784
+ const result = await fetch(`${rest.baseUrl}${url}`, rest.createRequestBody(method, options));
816
785
  if (!result.ok) {
817
786
  const err = await result.json().catch(()=>{});
818
787
  // Legacy Handling to not break old code or when body is missing
@@ -825,7 +794,7 @@ export function createRestManager(options) {
825
794
  const payload = {
826
795
  url,
827
796
  method,
828
- options,
797
+ requestBodyOptions: options,
829
798
  retryCount: 0,
830
799
  retryRequest: async function(payload) {
831
800
  rest.processRequest(payload);
@@ -838,30 +807,20 @@ export function createRestManager(options) {
838
807
  rest.processRequest(payload);
839
808
  });
840
809
  },
841
- async get (url, body) {
842
- return camelize(await rest.makeRequest('GET', url, {
843
- body
844
- }));
810
+ async get (url, options) {
811
+ return camelize(await rest.makeRequest('GET', url, options));
845
812
  },
846
- async post (url, body) {
847
- return camelize(await rest.makeRequest('POST', url, {
848
- body
849
- }));
813
+ async post (url, options) {
814
+ return camelize(await rest.makeRequest('POST', url, options));
850
815
  },
851
- async delete (url, body) {
852
- camelize(await rest.makeRequest('DELETE', url, {
853
- body
854
- }));
816
+ async delete (url, options) {
817
+ camelize(await rest.makeRequest('DELETE', url, options));
855
818
  },
856
- async patch (url, body) {
857
- return camelize(await rest.makeRequest('PATCH', url, {
858
- body
859
- }));
819
+ async patch (url, options) {
820
+ return camelize(await rest.makeRequest('PATCH', url, options));
860
821
  },
861
- async put (url, body) {
862
- return camelize(await rest.makeRequest('PUT', url, {
863
- body
864
- }));
822
+ async put (url, options) {
823
+ return camelize(await rest.makeRequest('PUT', url, options));
865
824
  },
866
825
  async addReaction (channelId, messageId, reaction) {
867
826
  reaction = processReactionString(reaction);
@@ -886,19 +845,22 @@ export function createRestManager(options) {
886
845
  async addThreadMember (channelId, userId) {
887
846
  await rest.put(rest.routes.channels.threads.user(channelId, userId));
888
847
  },
889
- async createAutomodRule (guildId, body) {
848
+ async createAutomodRule (guildId, body, reason) {
890
849
  return await rest.post(rest.routes.guilds.automod.rules(guildId), {
891
- body
850
+ body,
851
+ reason
892
852
  });
893
853
  },
894
- async createChannel (guildId, body) {
854
+ async createChannel (guildId, body, reason) {
895
855
  return await rest.post(rest.routes.guilds.channels(guildId), {
896
- body
856
+ body,
857
+ reason
897
858
  });
898
859
  },
899
- async createEmoji (guildId, body) {
860
+ async createEmoji (guildId, body, reason) {
900
861
  return await rest.post(rest.routes.guilds.emojis(guildId), {
901
- body
862
+ body,
863
+ reason
902
864
  });
903
865
  },
904
866
  async createGlobalApplicationCommand (body) {
@@ -924,14 +886,15 @@ export function createRestManager(options) {
924
886
  body
925
887
  });
926
888
  },
927
- async createGuildSticker (guildId, options) {
889
+ async createGuildSticker (guildId, options, reason) {
928
890
  const form = new FormData();
929
891
  form.append('file', options.file.blob, options.file.name);
930
892
  form.append('name', options.name);
931
893
  form.append('description', options.description);
932
894
  form.append('tags', options.tags);
933
895
  return await rest.post(rest.routes.guilds.stickers(guildId), {
934
- body: form
896
+ body: form,
897
+ reason
935
898
  });
936
899
  },
937
900
  async createGuildTemplate (guildId, body) {
@@ -939,14 +902,17 @@ export function createRestManager(options) {
939
902
  body
940
903
  });
941
904
  },
942
- async createForumThread (channelId, body) {
905
+ async createForumThread (channelId, body, reason) {
943
906
  return await rest.post(rest.routes.channels.forum(channelId), {
944
- body
907
+ body,
908
+ files: body.files,
909
+ reason
945
910
  });
946
911
  },
947
- async createInvite (channelId, body = {}) {
912
+ async createInvite (channelId, body = {}, reason) {
948
913
  return await rest.post(rest.routes.channels.invites(channelId), {
949
- body
914
+ body,
915
+ reason
950
916
  });
951
917
  },
952
918
  async createRole (guildId, body, reason) {
@@ -955,14 +921,16 @@ export function createRestManager(options) {
955
921
  reason
956
922
  });
957
923
  },
958
- async createScheduledEvent (guildId, body) {
924
+ async createScheduledEvent (guildId, body, reason) {
959
925
  return await rest.post(rest.routes.guilds.events.events(guildId), {
960
- body
926
+ body,
927
+ reason
961
928
  });
962
929
  },
963
- async createStageInstance (body) {
930
+ async createStageInstance (body, reason) {
964
931
  return await rest.post(rest.routes.channels.stages(), {
965
- body
932
+ body,
933
+ reason
966
934
  });
967
935
  },
968
936
  async createWebhook (channelId, options, reason) {
@@ -1014,8 +982,10 @@ export function createRestManager(options) {
1014
982
  async deleteGuildTemplate (guildId, templateCode) {
1015
983
  await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
1016
984
  },
1017
- async deleteIntegration (guildId, integrationId) {
1018
- await rest.delete(rest.routes.guilds.integration(guildId, integrationId));
985
+ async deleteIntegration (guildId, integrationId, reason) {
986
+ await rest.delete(rest.routes.guilds.integration(guildId, integrationId), {
987
+ reason
988
+ });
1019
989
  },
1020
990
  async deleteInvite (inviteCode, reason) {
1021
991
  await rest.delete(rest.routes.guilds.invite(inviteCode), {
@@ -1049,8 +1019,10 @@ export function createRestManager(options) {
1049
1019
  reaction = processReactionString(reaction);
1050
1020
  await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
1051
1021
  },
1052
- async deleteRole (guildId, roleId) {
1053
- await rest.delete(rest.routes.guilds.roles.one(guildId, roleId));
1022
+ async deleteRole (guildId, roleId, reason) {
1023
+ await rest.delete(rest.routes.guilds.roles.one(guildId, roleId), {
1024
+ reason
1025
+ });
1054
1026
  },
1055
1027
  async deleteScheduledEvent (guildId, eventId) {
1056
1028
  await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
@@ -1085,9 +1057,10 @@ export function createRestManager(options) {
1085
1057
  }
1086
1058
  });
1087
1059
  },
1088
- async editAutomodRule (guildId, ruleId, body) {
1060
+ async editAutomodRule (guildId, ruleId, body, reason) {
1089
1061
  return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
1090
- body
1062
+ body,
1063
+ reason
1091
1064
  });
1092
1065
  },
1093
1066
  async editBotProfile (options) {
@@ -1099,14 +1072,16 @@ export function createRestManager(options) {
1099
1072
  }
1100
1073
  });
1101
1074
  },
1102
- async editChannel (channelId, body) {
1075
+ async editChannel (channelId, body, reason) {
1103
1076
  return await rest.patch(rest.routes.channels.channel(channelId), {
1104
- body
1077
+ body,
1078
+ reason
1105
1079
  });
1106
1080
  },
1107
- async editChannelPermissionOverrides (channelId, body) {
1081
+ async editChannelPermissionOverrides (channelId, body, reason) {
1108
1082
  await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
1109
- body
1083
+ body,
1084
+ reason
1110
1085
  });
1111
1086
  },
1112
1087
  async editChannelPositions (guildId, body) {
@@ -1114,14 +1089,16 @@ export function createRestManager(options) {
1114
1089
  body
1115
1090
  });
1116
1091
  },
1117
- async editEmoji (guildId, id, body) {
1092
+ async editEmoji (guildId, id, body, reason) {
1118
1093
  return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
1119
- body
1094
+ body,
1095
+ reason
1120
1096
  });
1121
1097
  },
1122
1098
  async editFollowupMessage (token, messageId, body) {
1123
1099
  return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1124
- body
1100
+ body,
1101
+ files: body.files
1125
1102
  });
1126
1103
  },
1127
1104
  async editGlobalApplicationCommand (commandId, body) {
@@ -1129,9 +1106,10 @@ export function createRestManager(options) {
1129
1106
  body
1130
1107
  });
1131
1108
  },
1132
- async editGuild (guildId, body) {
1109
+ async editGuild (guildId, body, reason) {
1133
1110
  return await rest.patch(rest.routes.guilds.guild(guildId), {
1134
- body
1111
+ body,
1112
+ reason
1135
1113
  });
1136
1114
  },
1137
1115
  async editGuildApplicationCommand (commandId, guildId, body) {
@@ -1147,9 +1125,10 @@ export function createRestManager(options) {
1147
1125
  reason
1148
1126
  });
1149
1127
  },
1150
- async editGuildSticker (guildId, stickerId, body) {
1128
+ async editGuildSticker (guildId, stickerId, body, reason) {
1151
1129
  return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
1152
- body
1130
+ body,
1131
+ reason
1153
1132
  });
1154
1133
  },
1155
1134
  async editGuildTemplate (guildId, templateCode, body) {
@@ -1164,7 +1143,8 @@ export function createRestManager(options) {
1164
1143
  },
1165
1144
  async editOriginalInteractionResponse (token, body) {
1166
1145
  return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), {
1167
- body
1146
+ body,
1147
+ files: body.files
1168
1148
  });
1169
1149
  },
1170
1150
  async editOriginalWebhookMessage (webhookId, token, options) {
@@ -1172,7 +1152,8 @@ export function createRestManager(options) {
1172
1152
  body: {
1173
1153
  type: InteractionResponseTypes.UpdateMessage,
1174
1154
  data: options
1175
- }
1155
+ },
1156
+ files: options.files
1176
1157
  });
1177
1158
  },
1178
1159
  async editOwnVoiceState (guildId, options) {
@@ -1183,19 +1164,22 @@ export function createRestManager(options) {
1183
1164
  }
1184
1165
  });
1185
1166
  },
1186
- async editScheduledEvent (guildId, eventId, body) {
1167
+ async editScheduledEvent (guildId, eventId, body, reason) {
1187
1168
  return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
1188
- body
1169
+ body,
1170
+ reason
1189
1171
  });
1190
1172
  },
1191
- async editRole (guildId, roleId, body) {
1173
+ async editRole (guildId, roleId, body, reason) {
1192
1174
  return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
1193
- body
1175
+ body,
1176
+ reason
1194
1177
  });
1195
1178
  },
1196
- async editRolePositions (guildId, body) {
1179
+ async editRolePositions (guildId, body, reason) {
1197
1180
  return await rest.patch(rest.routes.guilds.roles.all(guildId), {
1198
- body
1181
+ body,
1182
+ reason
1199
1183
  });
1200
1184
  },
1201
1185
  async editStageInstance (channelId, topic, reason) {
@@ -1211,14 +1195,16 @@ export function createRestManager(options) {
1211
1195
  body: options
1212
1196
  });
1213
1197
  },
1214
- async editWebhook (webhookId, body) {
1198
+ async editWebhook (webhookId, body, reason) {
1215
1199
  return await rest.patch(rest.routes.webhooks.id(webhookId), {
1216
- body
1200
+ body,
1201
+ reason
1217
1202
  });
1218
1203
  },
1219
1204
  async editWebhookMessage (webhookId, token, messageId, options) {
1220
1205
  return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), {
1221
- body: options
1206
+ body: options,
1207
+ files: options.files
1222
1208
  });
1223
1209
  },
1224
1210
  async editWebhookWithToken (webhookId, token, body) {
@@ -1226,14 +1212,16 @@ export function createRestManager(options) {
1226
1212
  body
1227
1213
  });
1228
1214
  },
1229
- async editWelcomeScreen (guildId, body) {
1215
+ async editWelcomeScreen (guildId, body, reason) {
1230
1216
  return await rest.patch(rest.routes.guilds.welcome(guildId), {
1231
- body
1217
+ body,
1218
+ reason
1232
1219
  });
1233
1220
  },
1234
- async editWidgetSettings (guildId, body) {
1221
+ async editWidgetSettings (guildId, body, reason) {
1235
1222
  return await rest.patch(rest.routes.guilds.widget(guildId), {
1236
- body
1223
+ body,
1224
+ reason
1237
1225
  });
1238
1226
  },
1239
1227
  async executeWebhook (webhookId, token, options) {
@@ -1463,8 +1451,9 @@ export function createRestManager(options) {
1463
1451
  rest.sendRequest({
1464
1452
  url: rest.routes.webhooks.webhook(rest.applicationId, token),
1465
1453
  method: 'POST',
1466
- options: {
1467
- body: options
1454
+ requestBodyOptions: {
1455
+ body: options,
1456
+ files: options.files
1468
1457
  },
1469
1458
  retryCount: 0,
1470
1459
  retryRequest: async function(options) {
@@ -1484,7 +1473,7 @@ export function createRestManager(options) {
1484
1473
  rest.sendRequest({
1485
1474
  url: rest.routes.interactions.responses.callback(interactionId, token),
1486
1475
  method: 'POST',
1487
- options: {
1476
+ requestBodyOptions: {
1488
1477
  body: options
1489
1478
  },
1490
1479
  retryCount: 0,
@@ -1501,35 +1490,41 @@ export function createRestManager(options) {
1501
1490
  },
1502
1491
  async sendMessage (channelId, body) {
1503
1492
  return await rest.post(rest.routes.channels.messages(channelId), {
1504
- body
1493
+ body,
1494
+ files: body.files
1505
1495
  });
1506
1496
  },
1507
- async startThreadWithMessage (channelId, messageId, body) {
1497
+ async startThreadWithMessage (channelId, messageId, body, reason) {
1508
1498
  return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1509
- body
1499
+ body,
1500
+ reason
1510
1501
  });
1511
1502
  },
1512
- async startThreadWithoutMessage (channelId, body) {
1503
+ async startThreadWithoutMessage (channelId, body, reason) {
1513
1504
  return await rest.post(rest.routes.channels.threads.all(channelId), {
1514
- body
1505
+ body,
1506
+ reason
1515
1507
  });
1516
1508
  },
1517
1509
  async syncGuildTemplate (guildId) {
1518
1510
  return await rest.put(rest.routes.guilds.templates.all(guildId));
1519
1511
  },
1520
- async banMember (guildId, userId, body) {
1512
+ async banMember (guildId, userId, body, reason) {
1521
1513
  await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1522
- body
1514
+ body,
1515
+ reason
1523
1516
  });
1524
1517
  },
1525
- async editBotMember (guildId, body) {
1518
+ async editBotMember (guildId, body, reason) {
1526
1519
  return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1527
- body
1520
+ body,
1521
+ reason
1528
1522
  });
1529
1523
  },
1530
- async editMember (guildId, userId, body) {
1524
+ async editMember (guildId, userId, body, reason) {
1531
1525
  return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1532
- body
1526
+ body,
1527
+ reason
1533
1528
  });
1534
1529
  },
1535
1530
  async getMember (guildId, userId) {
@@ -1548,16 +1543,19 @@ export function createRestManager(options) {
1548
1543
  reason
1549
1544
  });
1550
1545
  },
1551
- async pruneMembers (guildId, body) {
1546
+ async pruneMembers (guildId, body, reason) {
1552
1547
  return await rest.post(rest.routes.guilds.members.prune(guildId), {
1553
- body
1548
+ body,
1549
+ reason
1554
1550
  });
1555
1551
  },
1556
1552
  async searchMembers (guildId, query, options) {
1557
1553
  return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1558
1554
  },
1559
- async unbanMember (guildId, userId) {
1560
- await rest.delete(rest.routes.guilds.members.ban(guildId, userId));
1555
+ async unbanMember (guildId, userId, reason) {
1556
+ await rest.delete(rest.routes.guilds.members.ban(guildId, userId), {
1557
+ reason
1558
+ });
1561
1559
  },
1562
1560
  async unpinMessage (channelId, messageId, reason) {
1563
1561
  await rest.delete(rest.routes.channels.pin(channelId, messageId), {