@discordeno/rest 19.0.0-next.fc13bdf → 19.0.0-next.fdf0d53

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/manager.js CHANGED
@@ -1,9 +1,11 @@
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, findFiles, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
2
+ import { calculateBits, camelize, camelToSnakeCase, delay, encode, getBotIdFromToken, isGetMessagesAfter, isGetMessagesAround, isGetMessagesBefore, isGetMessagesLimit, logger, processReactionString, urlToBase64 } from '@discordeno/utils';
3
+ import fetch from 'node-fetch';
3
4
  import { createInvalidRequestBucket } from './invalidBucket.js';
4
5
  import { Queue } from './queue.js';
5
6
  // TODO: make dynamic based on package.json file
6
7
  const version = '19.0.0-alpha.1';
8
+ const URL_PARTS_REGEX = /([a-z]+)\/(?:[0-9]{17,}(\/@me)?)/g;
7
9
  export function createRestManager(options) {
8
10
  // Falsy token string check
9
11
  if (!options.token) throw new Error('You must provide a valid token.');
@@ -11,7 +13,7 @@ export function createRestManager(options) {
11
13
  token: options.token,
12
14
  applicationId: options.applicationId ? BigInt(options.applicationId) : getBotIdFromToken(options.token),
13
15
  version: options.version ?? 10,
14
- baseUrl: options.baseUrl ?? 'https://discord.com/api',
16
+ baseUrl: options.proxy?.baseUrl ?? 'https://discord.com/api',
15
17
  maxRetryCount: Infinity,
16
18
  globallyRateLimited: false,
17
19
  processingRateLimitedPaths: false,
@@ -19,6 +21,7 @@ export function createRestManager(options) {
19
21
  queues: new Map(),
20
22
  rateLimitedPaths: new Map(),
21
23
  invalidBucket: createInvalidRequestBucket({}),
24
+ authorization: options.proxy?.authorization,
22
25
  routes: {
23
26
  webhooks: {
24
27
  id: (webhookId)=>{
@@ -216,6 +219,8 @@ export function createRestManager(options) {
216
219
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
217
220
  if (options.before) url += `&before=${options.before}`;
218
221
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
222
+ if (options.after) url += `&after=${options.after}`;
223
+ // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
219
224
  if (options.limit) url += `&limit=${options.limit}`;
220
225
  // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
221
226
  if (options.userId) url += `&user_id=${options.userId}`;
@@ -546,50 +551,40 @@ export function createRestManager(options) {
546
551
  'user-agent': `DiscordBot (https://github.com/discordeno/discordeno, v${version})`
547
552
  };
548
553
  if (!options.unauthorized) headers.authorization = `Bot ${rest.token}`;
549
- // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
550
- if (options.headers) {
551
- for(const key in options.headers){
552
- headers[key.toLowerCase()] = options.headers[key];
553
- }
554
- }
555
- // GET METHODS SHOULD NOT HAVE A BODY
556
- if (options.method === 'GET') {
557
- options.body = undefined;
558
- }
559
554
  // IF A REASON IS PROVIDED ENCODE IT IN HEADERS
560
- if (options.body?.reason) {
561
- headers['X-Audit-Log-Reason'] = encodeURIComponent(options.body.reason);
562
- options.body.reason = undefined;
555
+ if (options.reason !== undefined) {
556
+ headers['x-audit-log-reason'] = encodeURIComponent(options.reason);
563
557
  }
564
- if (options.body) {
565
- const { file } = options.body;
566
- if (file) {
567
- const files = findFiles(file);
568
- const form = new FormData();
569
- // WHEN CREATING A STICKER, DISCORD WANTS FORM DATA ONLY
570
- if (options.url?.endsWith('/stickers') && options.method === 'POST') {
571
- form.append('file', files[0].blob, files[0].name);
572
- form.append('name', options.body.name);
573
- form.append('description', options.body.description);
574
- form.append('tags', options.body.tags);
575
- } else {
576
- for(let i = 0; i < files.length; i++){
577
- form.append(`file${i}`, files[i].blob, files[i].name);
578
- }
579
- if (file) options.body.file = undefined;
580
- form.append('payload_json', JSON.stringify(rest.changeToDiscordFormat(options.body)));
581
- }
582
- options.body.file = form;
583
- } else if (options.body && ![
584
- 'GET',
585
- 'DELETE'
586
- ].includes(options.method)) {
587
- headers['Content-Type'] = 'application/json';
558
+ let body;
559
+ // TODO: check if we need to add specific check for GET method
560
+ // Since GET does not allow bodies
561
+ // Have to check for attachments first, since body then has to be send in a different way.
562
+ if (options.attachments !== undefined) {
563
+ const form = new FormData();
564
+ for(let i = 0; i < options.attachments.length; ++i){
565
+ form.append(`file${i}`, options.attachments[i].blob, options.attachments[i].name);
588
566
  }
567
+ form.append('payload_json', JSON.stringify(options.body));
568
+ body = form;
569
+ // TODO: boundary?
570
+ // `multipart/form-data; boundary=${form.getBoundary()}`
571
+ headers['content-type'] = `multipart/form-data`;
572
+ } else if (options.body !== undefined) {
573
+ if (options.body instanceof FormData) {
574
+ body = options.body;
575
+ headers['content-type'] = `multipart/form-data`;
576
+ } else {
577
+ body = JSON.stringify(options.body);
578
+ headers['content-type'] = `application/json`;
579
+ }
580
+ }
581
+ // SOMETIMES SPECIAL HEADERS (E.G. CUSTOM AUTHORIZATION) NEED TO BE USED
582
+ if (options.headers) {
583
+ Object.assign(headers, options.headers);
589
584
  }
590
585
  return {
586
+ body,
591
587
  headers,
592
- body: options.body?.file ?? JSON.stringify(rest.changeToDiscordFormat(options.body)),
593
588
  method: options.method
594
589
  };
595
590
  },
@@ -655,8 +650,8 @@ export function createRestManager(options) {
655
650
  }
656
651
  // IF THERE IS NO REMAINING GLOBAL LIMIT, MARK IT RATE LIMITED GLOBALLY
657
652
  if (global) {
658
- const retryAfter1 = headers.get('retry-after');
659
- const globalReset = Date.now() + Number(retryAfter1) * 1000;
653
+ const retryAfter = headers.get('retry-after');
654
+ const globalReset = Date.now() + Number(retryAfter) * 1000;
660
655
  // rest.debug(
661
656
  // `[REST = Globally Rate Limited] URL: ${url} | Global Rest: ${globalReset}`
662
657
  // )
@@ -688,7 +683,7 @@ export function createRestManager(options) {
688
683
  const payload = rest.createRequest({
689
684
  method: options.method,
690
685
  url: options.url,
691
- body: options.body,
686
+ body: options.options?.body,
692
687
  ...options.options
693
688
  });
694
689
  logger.debug(`sending request to ${url}`, 'with payload:', {
@@ -712,11 +707,12 @@ export function createRestManager(options) {
712
707
  logger.debug(`Request to ${url} exceeded the maximum allowed retries.`, 'with payload:', payload);
713
708
  // rest.debug(`[REST - RetriesMaxed] ${JSON.stringify(options)}`)
714
709
  // Remove item from queue to prevent retry
715
- return options.reject({
710
+ options.reject({
716
711
  ok: false,
717
712
  status: response.status,
718
713
  error: 'The options was rate limited and it maxed out the retries limit.'
719
714
  });
715
+ return;
720
716
  }
721
717
  // Rate limited, add back to queue
722
718
  rest.invalidBucket.handleCompletedRequest(response.status, response.headers.get('X-RateLimit-Scope') === 'shared');
@@ -726,16 +722,17 @@ export function createRestManager(options) {
726
722
  await response.json();
727
723
  return await options.retryRequest?.(options);
728
724
  }
729
- return options.reject({
725
+ options.reject({
730
726
  ok: false,
731
727
  status: response.status,
732
728
  body: JSON.stringify(await response.json())
733
729
  });
730
+ return;
734
731
  }
735
732
  const is204 = response.status === 204;
736
733
  const json = is204 ? undefined : await response.json();
737
734
  // Discord sometimes sends no response with 204 code
738
- return options.resolve({
735
+ options.resolve({
739
736
  ok: true,
740
737
  status: response.status,
741
738
  body: JSON.stringify(json)
@@ -744,19 +741,23 @@ export function createRestManager(options) {
744
741
  // Credits: github.com/abalabahaha/eris lib/rest/RequestHandler.js#L397
745
742
  // Modified for our use-case
746
743
  simplifyUrl (url, method) {
747
- let route = url.replace(/\/([a-z-]+)\/(?:[0-9]{17,19})/g, function(match, p) {
748
- return [
749
- 'channels',
750
- 'guilds'
751
- ].includes(p) ? match : `/${p}/x`;
752
- }).replace(/\/reactions\/[^/]+/g, '/reactions/x');
753
- // GENERAL /reactions and /reactions/emoji/@me share the buckets
744
+ let route = url.replace(URL_PARTS_REGEX, function(match, pattern) {
745
+ if (pattern.startsWith('channels') || pattern.startsWith('guilds')) {
746
+ return match;
747
+ }
748
+ // GENERAL /reactions and /reactions/emoji/@me share the buckets
749
+ if (pattern.startsWith('reactions')) {
750
+ return 'reactions';
751
+ }
752
+ return `${pattern}/x`;
753
+ });
754
754
  if (route.includes('/reactions')) {
755
- route = route.substring(0, route.indexOf('/reactions') + '/reactions'.length);
755
+ // 10 is the length of `/reactions`
756
+ route = route.substring(0, route.indexOf('/reactions') + 10);
756
757
  }
757
758
  // Delete Message endpoint has its own rate limit
758
759
  if (method === 'DELETE' && route.endsWith('/messages/x')) {
759
- route = method + route;
760
+ route = 'D' + route;
760
761
  }
761
762
  return route;
762
763
  },
@@ -785,45 +786,95 @@ export function createRestManager(options) {
785
786
  rest.queues.set(url, bucketQueue);
786
787
  }
787
788
  },
788
- async makeRequest (method, url, body, options) {
789
+ async makeRequest (method, url, options) {
790
+ if (!rest.baseUrl.startsWith('https://discord.com') && url[0] === '/') {
791
+ // Special handling for sending blobs across http to proxy
792
+ // TODO: fix this hacky handling
793
+ if (!(options?.body instanceof FormData) && !Array.isArray(options?.body) && options?.body?.file) {
794
+ if (!Array.isArray(options.body.file)) {
795
+ options.body.file = [
796
+ options.body.file
797
+ ];
798
+ }
799
+ // convert blobs to string before sending to proxy
800
+ options.body.file = await Promise.all(options.body.file.map(async (f)=>{
801
+ const url = encode(await f.blob.arrayBuffer());
802
+ return {
803
+ name: f.name,
804
+ blob: `data:${f.blob.type};base64,${url}`
805
+ };
806
+ }));
807
+ }
808
+ const headers = {
809
+ Authorization: rest.authorization ?? ''
810
+ };
811
+ if (options?.body) {
812
+ headers['Content-Type'] = 'application/json';
813
+ }
814
+ const result = await fetch(`${rest.baseUrl}${url}`, {
815
+ body: options?.body ? JSON.stringify(options.body) : undefined,
816
+ headers,
817
+ method
818
+ });
819
+ if (!result.ok) {
820
+ const err = await result.json().catch(()=>{});
821
+ // Legacy Handling to not break old code or when body is missing
822
+ if (!err?.body) throw new Error(`Error: ${err.message ?? result.statusText}`);
823
+ throw new Error(JSON.stringify(err));
824
+ }
825
+ return result.status !== 204 ? await result.json() : undefined;
826
+ }
789
827
  return await new Promise((resolve, reject)=>{
790
828
  const payload = {
791
829
  url,
792
830
  method,
793
- body,
831
+ options,
794
832
  retryCount: 0,
795
- retryRequest: async function(options) {
833
+ retryRequest: async function(payload) {
796
834
  rest.processRequest(payload);
797
835
  },
798
- resolve: (data)=>resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined),
799
- reject,
800
- options
836
+ resolve: (data)=>{
837
+ resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
838
+ },
839
+ reject
801
840
  };
802
841
  rest.processRequest(payload);
803
842
  });
804
843
  },
805
- async get (url) {
806
- return camelize(await rest.makeRequest('GET', url));
844
+ async get (url, body) {
845
+ return camelize(await rest.makeRequest('GET', url, {
846
+ body
847
+ }));
807
848
  },
808
849
  async post (url, body) {
809
- return camelize(await rest.makeRequest('POST', url, body));
850
+ return camelize(await rest.makeRequest('POST', url, {
851
+ body
852
+ }));
810
853
  },
811
854
  async delete (url, body) {
812
- return camelize(await rest.makeRequest('DELETE', url, body));
855
+ camelize(await rest.makeRequest('DELETE', url, {
856
+ body
857
+ }));
813
858
  },
814
859
  async patch (url, body) {
815
- return camelize(await rest.makeRequest('PATCH', url, body));
860
+ return camelize(await rest.makeRequest('PATCH', url, {
861
+ body
862
+ }));
816
863
  },
817
- async put (url, body, options) {
818
- return camelize(await rest.makeRequest('PUT', url, body, options));
864
+ async put (url, body) {
865
+ return camelize(await rest.makeRequest('PUT', url, {
866
+ body
867
+ }));
819
868
  },
820
869
  async addReaction (channelId, messageId, reaction) {
821
870
  reaction = processReactionString(reaction);
822
- return await rest.put(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
871
+ await rest.put(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
823
872
  },
824
873
  async addReactions (channelId, messageId, reactions, ordered = false) {
825
874
  if (!ordered) {
826
- await Promise.all(reactions.map(async (reaction)=>await rest.addReaction(channelId, messageId, reaction)));
875
+ await Promise.all(reactions.map(async (reaction)=>{
876
+ await rest.addReaction(channelId, messageId, reaction);
877
+ }));
827
878
  return;
828
879
  }
829
880
  for (const reaction of reactions){
@@ -831,283 +882,373 @@ export function createRestManager(options) {
831
882
  }
832
883
  },
833
884
  async addRole (guildId, userId, roleId, reason) {
834
- return await rest.put(rest.routes.guilds.roles.member(guildId, userId, roleId), {
885
+ await rest.put(rest.routes.guilds.roles.member(guildId, userId, roleId), {
835
886
  reason
836
887
  });
837
888
  },
838
889
  async addThreadMember (channelId, userId) {
839
- return await rest.put(rest.routes.channels.threads.user(channelId, userId));
890
+ await rest.put(rest.routes.channels.threads.user(channelId, userId));
840
891
  },
841
- async createAutomodRule (guildId, options) {
842
- return await rest.post(rest.routes.guilds.automod.rules(guildId), options);
892
+ async createAutomodRule (guildId, body) {
893
+ return await rest.post(rest.routes.guilds.automod.rules(guildId), {
894
+ body
895
+ });
843
896
  },
844
- async createChannel (guildId, options) {
845
- return await rest.post(rest.routes.guilds.channels(guildId), options);
897
+ async createChannel (guildId, body) {
898
+ return await rest.post(rest.routes.guilds.channels(guildId), {
899
+ body
900
+ });
846
901
  },
847
- async createEmoji (guildId, options) {
848
- return await rest.post(rest.routes.guilds.emojis(guildId), options);
902
+ async createEmoji (guildId, body) {
903
+ return await rest.post(rest.routes.guilds.emojis(guildId), {
904
+ body
905
+ });
849
906
  },
850
- async createGlobalApplicationCommand (command) {
851
- return await rest.post(rest.routes.interactions.commands.commands(rest.applicationId), command);
907
+ async createGlobalApplicationCommand (body) {
908
+ return await rest.post(rest.routes.interactions.commands.commands(rest.applicationId), {
909
+ body
910
+ });
852
911
  },
853
- async createGuild (options) {
854
- return await rest.post(rest.routes.guilds.all(), options);
912
+ async createGuild (body) {
913
+ return await rest.post(rest.routes.guilds.all(), {
914
+ body
915
+ });
855
916
  },
856
- async createGuildApplicationCommand (command, guildId) {
857
- return await rest.post(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), command);
917
+ async createGuildApplicationCommand (body, guildId) {
918
+ return await rest.post(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
919
+ body
920
+ });
858
921
  },
859
- async createGuildFromTemplate (templateCode, options) {
860
- if (options.icon) {
861
- options.icon = await urlToBase64(options.icon);
922
+ async createGuildFromTemplate (templateCode, body) {
923
+ if (body.icon) {
924
+ body.icon = await urlToBase64(body.icon);
862
925
  }
863
- return await rest.post(rest.routes.guilds.templates.code(templateCode), options);
926
+ return await rest.post(rest.routes.guilds.templates.code(templateCode), {
927
+ body
928
+ });
864
929
  },
865
930
  async createGuildSticker (guildId, options) {
866
- return await rest.post(rest.routes.guilds.stickers(guildId), options);
931
+ const form = new FormData();
932
+ form.append('file', options.file.blob, options.file.name);
933
+ form.append('name', options.name);
934
+ form.append('description', options.description);
935
+ form.append('tags', options.tags);
936
+ return await rest.post(rest.routes.guilds.stickers(guildId), {
937
+ body: form
938
+ });
867
939
  },
868
- async createGuildTemplate (guildId, options) {
869
- return await rest.post(rest.routes.guilds.templates.all(guildId), options);
940
+ async createGuildTemplate (guildId, body) {
941
+ return await rest.post(rest.routes.guilds.templates.all(guildId), {
942
+ body
943
+ });
870
944
  },
871
- async createForumThread (channelId, options) {
872
- return await rest.post(rest.routes.channels.forum(channelId), options);
945
+ async createForumThread (channelId, body) {
946
+ return await rest.post(rest.routes.channels.forum(channelId), {
947
+ body
948
+ });
873
949
  },
874
- async createInvite (channelId, options = {}) {
875
- return await rest.post(rest.routes.channels.invites(channelId), options);
950
+ async createInvite (channelId, body = {}) {
951
+ return await rest.post(rest.routes.channels.invites(channelId), {
952
+ body
953
+ });
876
954
  },
877
- async createRole (guildId, options, reason) {
955
+ async createRole (guildId, body, reason) {
878
956
  return await rest.post(rest.routes.guilds.roles.all(guildId), {
879
- ...options,
957
+ body,
880
958
  reason
881
959
  });
882
960
  },
883
- async createScheduledEvent (guildId, options) {
884
- return await rest.post(rest.routes.guilds.events.events(guildId), options);
961
+ async createScheduledEvent (guildId, body) {
962
+ return await rest.post(rest.routes.guilds.events.events(guildId), {
963
+ body
964
+ });
885
965
  },
886
- async createStageInstance (options) {
887
- return await rest.post(rest.routes.channels.stages(), options);
966
+ async createStageInstance (body) {
967
+ return await rest.post(rest.routes.channels.stages(), {
968
+ body
969
+ });
888
970
  },
889
- async createWebhook (channelId, options) {
971
+ async createWebhook (channelId, options, reason) {
890
972
  return await rest.post(rest.routes.channels.webhooks(channelId), {
891
- name: options.name,
892
- avatar: options.avatar ? await urlToBase64(options.avatar) : undefined,
893
- reason: options.reason
973
+ body: {
974
+ name: options.name,
975
+ avatar: options.avatar ? await urlToBase64(options.avatar) : undefined
976
+ },
977
+ reason
894
978
  });
895
979
  },
896
980
  async deleteAutomodRule (guildId, ruleId, reason) {
897
- return await rest.delete(rest.routes.guilds.automod.rule(guildId, ruleId), {
981
+ await rest.delete(rest.routes.guilds.automod.rule(guildId, ruleId), {
898
982
  reason
899
983
  });
900
984
  },
901
985
  async deleteChannel (channelId, reason) {
902
- return await rest.delete(rest.routes.channels.channel(channelId), {
986
+ await rest.delete(rest.routes.channels.channel(channelId), {
903
987
  reason
904
988
  });
905
989
  },
906
990
  async deleteChannelPermissionOverride (channelId, overwriteId, reason) {
907
- return await rest.delete(rest.routes.channels.overwrite(channelId, overwriteId), reason ? {
991
+ await rest.delete(rest.routes.channels.overwrite(channelId, overwriteId), {
908
992
  reason
909
- } : undefined);
993
+ });
910
994
  },
911
995
  async deleteEmoji (guildId, id, reason) {
912
- return await rest.delete(rest.routes.guilds.emoji(guildId, id), {
996
+ await rest.delete(rest.routes.guilds.emoji(guildId, id), {
913
997
  reason
914
998
  });
915
999
  },
916
1000
  async deleteFollowupMessage (token, messageId) {
917
- return await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
1001
+ await rest.delete(rest.routes.interactions.responses.message(rest.applicationId, token, messageId));
918
1002
  },
919
1003
  async deleteGlobalApplicationCommand (commandId) {
920
- return await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
1004
+ await rest.delete(rest.routes.interactions.commands.command(rest.applicationId, commandId));
921
1005
  },
922
1006
  async deleteGuild (guildId) {
923
- return await rest.delete(rest.routes.guilds.guild(guildId));
1007
+ await rest.delete(rest.routes.guilds.guild(guildId));
924
1008
  },
925
1009
  async deleteGuildApplicationCommand (commandId, guildId) {
926
- return await rest.delete(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
1010
+ await rest.delete(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId));
927
1011
  },
928
1012
  async deleteGuildSticker (guildId, stickerId, reason) {
929
- return await rest.delete(rest.routes.guilds.sticker(guildId, stickerId), reason ? {
1013
+ await rest.delete(rest.routes.guilds.sticker(guildId, stickerId), {
930
1014
  reason
931
- } : undefined);
1015
+ });
932
1016
  },
933
1017
  async deleteGuildTemplate (guildId, templateCode) {
934
- return await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
1018
+ await rest.delete(rest.routes.guilds.templates.guild(guildId, templateCode));
935
1019
  },
936
1020
  async deleteIntegration (guildId, integrationId) {
937
- return await rest.delete(rest.routes.guilds.integration(guildId, integrationId));
1021
+ await rest.delete(rest.routes.guilds.integration(guildId, integrationId));
938
1022
  },
939
1023
  async deleteInvite (inviteCode, reason) {
940
- return await rest.delete(rest.routes.guilds.invite(inviteCode), reason ? {
1024
+ await rest.delete(rest.routes.guilds.invite(inviteCode), {
941
1025
  reason
942
- } : undefined);
1026
+ });
943
1027
  },
944
1028
  async deleteMessage (channelId, messageId, reason) {
945
- return await rest.delete(rest.routes.channels.message(channelId, messageId), {
1029
+ await rest.delete(rest.routes.channels.message(channelId, messageId), {
946
1030
  reason
947
1031
  });
948
1032
  },
949
1033
  async deleteMessages (channelId, messageIds, reason) {
950
- return await rest.post(rest.routes.channels.bulk(channelId), {
951
- messages: messageIds.slice(0, 100).map((id)=>id.toString()),
1034
+ await rest.post(rest.routes.channels.bulk(channelId), {
1035
+ body: {
1036
+ messages: messageIds.slice(0, 100).map((id)=>id.toString())
1037
+ },
952
1038
  reason
953
1039
  });
954
1040
  },
955
1041
  async deleteOriginalInteractionResponse (token) {
956
- return await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token));
1042
+ await rest.delete(rest.routes.interactions.responses.original(rest.applicationId, token));
957
1043
  },
958
1044
  async deleteOwnReaction (channelId, messageId, reaction) {
959
1045
  reaction = processReactionString(reaction);
960
- return await rest.delete(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
1046
+ await rest.delete(rest.routes.channels.reactions.bot(channelId, messageId, reaction));
961
1047
  },
962
1048
  async deleteReactionsAll (channelId, messageId) {
963
- return await rest.delete(rest.routes.channels.reactions.all(channelId, messageId));
1049
+ await rest.delete(rest.routes.channels.reactions.all(channelId, messageId));
964
1050
  },
965
1051
  async deleteReactionsEmoji (channelId, messageId, reaction) {
966
1052
  reaction = processReactionString(reaction);
967
- return await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
1053
+ await rest.delete(rest.routes.channels.reactions.emoji(channelId, messageId, reaction));
968
1054
  },
969
1055
  async deleteRole (guildId, roleId) {
970
- return await rest.delete(rest.routes.guilds.roles.one(guildId, roleId));
1056
+ await rest.delete(rest.routes.guilds.roles.one(guildId, roleId));
971
1057
  },
972
1058
  async deleteScheduledEvent (guildId, eventId) {
973
- return await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
1059
+ await rest.delete(rest.routes.guilds.events.event(guildId, eventId));
974
1060
  },
975
1061
  async deleteStageInstance (channelId, reason) {
976
- return await rest.delete(rest.routes.channels.stage(channelId), reason ? {
1062
+ await rest.delete(rest.routes.channels.stage(channelId), {
977
1063
  reason
978
- } : undefined);
1064
+ });
979
1065
  },
980
1066
  async deleteUserReaction (channelId, messageId, userId, reaction) {
981
1067
  reaction = processReactionString(reaction);
982
- return await rest.delete(rest.routes.channels.reactions.user(channelId, messageId, reaction, userId));
1068
+ await rest.delete(rest.routes.channels.reactions.user(channelId, messageId, reaction, userId));
983
1069
  },
984
1070
  async deleteWebhook (webhookId, reason) {
985
- return await rest.delete(rest.routes.webhooks.id(webhookId), {
1071
+ await rest.delete(rest.routes.webhooks.id(webhookId), {
986
1072
  reason
987
1073
  });
988
1074
  },
989
1075
  async deleteWebhookMessage (webhookId, token, messageId, options) {
990
- return await rest.delete(rest.routes.webhooks.message(webhookId, token, messageId, options));
1076
+ await rest.delete(rest.routes.webhooks.message(webhookId, token, messageId, options));
991
1077
  },
992
1078
  async deleteWebhookWithToken (webhookId, token) {
993
- return await rest.delete(rest.routes.webhooks.webhook(webhookId, token));
1079
+ await rest.delete(rest.routes.webhooks.webhook(webhookId, token));
994
1080
  },
995
- async editApplicationCommandPermissions (guildId, commandId, bearerToken, options) {
1081
+ async editApplicationCommandPermissions (guildId, commandId, bearerToken, permissions) {
996
1082
  return await rest.put(rest.routes.interactions.commands.permission(rest.applicationId, guildId, commandId), {
997
- permissions: options
998
- }, {
1083
+ body: {
1084
+ permissions
1085
+ },
999
1086
  headers: {
1000
1087
  authorization: `Bearer ${bearerToken}`
1001
1088
  }
1002
1089
  });
1003
1090
  },
1004
- async editAutomodRule (guildId, ruleId, options) {
1005
- return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), options);
1091
+ async editAutomodRule (guildId, ruleId, body) {
1092
+ return await rest.patch(rest.routes.guilds.automod.rule(guildId, ruleId), {
1093
+ body
1094
+ });
1006
1095
  },
1007
1096
  async editBotProfile (options) {
1008
1097
  const avatar = options?.botAvatarURL ? await urlToBase64(options?.botAvatarURL) : options?.botAvatarURL;
1009
1098
  return await rest.patch(rest.routes.userBot(), {
1010
- username: options.username?.trim(),
1011
- avatar
1099
+ body: {
1100
+ username: options.username?.trim(),
1101
+ avatar
1102
+ }
1012
1103
  });
1013
1104
  },
1014
- async editChannel (channelId, options) {
1015
- return await rest.patch(rest.routes.channels.channel(channelId), options);
1105
+ async editChannel (channelId, body) {
1106
+ return await rest.patch(rest.routes.channels.channel(channelId), {
1107
+ body
1108
+ });
1016
1109
  },
1017
- async editChannelPermissionOverrides (channelId, options) {
1018
- return await rest.put(rest.routes.channels.overwrite(channelId, options.id), options);
1110
+ async editChannelPermissionOverrides (channelId, body) {
1111
+ await rest.put(rest.routes.channels.overwrite(channelId, body.id), {
1112
+ body
1113
+ });
1019
1114
  },
1020
- async editChannelPositions (guildId, channelPositions) {
1021
- return await rest.patch(rest.routes.guilds.channels(guildId), channelPositions);
1115
+ async editChannelPositions (guildId, body) {
1116
+ await rest.patch(rest.routes.guilds.channels(guildId), {
1117
+ body
1118
+ });
1022
1119
  },
1023
- async editEmoji (guildId, id, options) {
1024
- return await rest.patch(rest.routes.guilds.emoji(guildId, id), options);
1120
+ async editEmoji (guildId, id, body) {
1121
+ return await rest.patch(rest.routes.guilds.emoji(guildId, id), {
1122
+ body
1123
+ });
1025
1124
  },
1026
- async editFollowupMessage (token, messageId, options) {
1027
- return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), options);
1125
+ async editFollowupMessage (token, messageId, body) {
1126
+ return await rest.patch(rest.routes.interactions.responses.message(rest.applicationId, token, messageId), {
1127
+ body
1128
+ });
1028
1129
  },
1029
- async editGlobalApplicationCommand (commandId, options) {
1030
- return await rest.patch(rest.routes.interactions.commands.command(rest.applicationId, commandId), options);
1130
+ async editGlobalApplicationCommand (commandId, body) {
1131
+ return await rest.patch(rest.routes.interactions.commands.command(rest.applicationId, commandId), {
1132
+ body
1133
+ });
1031
1134
  },
1032
- async editGuild (guildId, options) {
1033
- return await rest.patch(rest.routes.guilds.guild(guildId), options);
1135
+ async editGuild (guildId, body) {
1136
+ return await rest.patch(rest.routes.guilds.guild(guildId), {
1137
+ body
1138
+ });
1034
1139
  },
1035
- async editGuildApplicationCommand (commandId, guildId, options) {
1036
- return await rest.patch(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId), options);
1140
+ async editGuildApplicationCommand (commandId, guildId, body) {
1141
+ return await rest.patch(rest.routes.interactions.commands.guilds.one(rest.applicationId, guildId, commandId), {
1142
+ body
1143
+ });
1037
1144
  },
1038
1145
  async editGuildMfaLevel (guildId, mfaLevel, reason) {
1039
- return await rest.post(rest.routes.guilds.mfa(guildId), {
1040
- level: mfaLevel,
1146
+ await rest.post(rest.routes.guilds.mfa(guildId), {
1147
+ body: {
1148
+ level: mfaLevel
1149
+ },
1041
1150
  reason
1042
1151
  });
1043
1152
  },
1044
- async editGuildSticker (guildId, stickerId, options) {
1045
- return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), options);
1153
+ async editGuildSticker (guildId, stickerId, body) {
1154
+ return await rest.patch(rest.routes.guilds.sticker(guildId, stickerId), {
1155
+ body
1156
+ });
1046
1157
  },
1047
- async editGuildTemplate (guildId, templateCode, options) {
1048
- return await rest.patch(rest.routes.guilds.templates.guild(guildId, templateCode), options);
1158
+ async editGuildTemplate (guildId, templateCode, body) {
1159
+ return await rest.patch(rest.routes.guilds.templates.guild(guildId, templateCode), {
1160
+ body
1161
+ });
1049
1162
  },
1050
- async editMessage (channelId, messageId, options) {
1051
- return await rest.patch(rest.routes.channels.message(channelId, messageId), options);
1163
+ async editMessage (channelId, messageId, body) {
1164
+ return await rest.patch(rest.routes.channels.message(channelId, messageId), {
1165
+ body
1166
+ });
1052
1167
  },
1053
- async editOriginalInteractionResponse (token, options) {
1054
- return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), options);
1168
+ async editOriginalInteractionResponse (token, body) {
1169
+ return await rest.patch(rest.routes.interactions.responses.original(rest.applicationId, token), {
1170
+ body
1171
+ });
1055
1172
  },
1056
1173
  async editOriginalWebhookMessage (webhookId, token, options) {
1057
1174
  return await rest.patch(rest.routes.webhooks.original(webhookId, token, options), {
1058
- type: InteractionResponseTypes.UpdateMessage,
1059
- data: options
1175
+ body: {
1176
+ type: InteractionResponseTypes.UpdateMessage,
1177
+ data: options
1178
+ }
1060
1179
  });
1061
1180
  },
1062
1181
  async editOwnVoiceState (guildId, options) {
1063
- return await rest.patch(rest.routes.guilds.voice(guildId), {
1064
- channel_id: options.channelId,
1065
- suppress: options.suppress,
1066
- request_to_speak_timestamp: options.requestToSpeakTimestamp ? new Date(options.requestToSpeakTimestamp).toISOString() : options.requestToSpeakTimestamp
1182
+ await rest.patch(rest.routes.guilds.voice(guildId), {
1183
+ body: {
1184
+ ...options,
1185
+ request_to_speak_timestamp: options.requestToSpeakTimestamp ? new Date(options.requestToSpeakTimestamp).toISOString() : options.requestToSpeakTimestamp
1186
+ }
1067
1187
  });
1068
1188
  },
1069
- async editScheduledEvent (guildId, eventId, options) {
1070
- return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), options);
1189
+ async editScheduledEvent (guildId, eventId, body) {
1190
+ return await rest.patch(rest.routes.guilds.events.event(guildId, eventId), {
1191
+ body
1192
+ });
1071
1193
  },
1072
- async editRole (guildId, roleId, options) {
1073
- return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), options);
1194
+ async editRole (guildId, roleId, body) {
1195
+ return await rest.patch(rest.routes.guilds.roles.one(guildId, roleId), {
1196
+ body
1197
+ });
1074
1198
  },
1075
- async editRolePositions (guildId, options) {
1076
- return await rest.patch(rest.routes.guilds.roles.all(guildId), options);
1199
+ async editRolePositions (guildId, body) {
1200
+ return await rest.patch(rest.routes.guilds.roles.all(guildId), {
1201
+ body
1202
+ });
1077
1203
  },
1078
- async editStageInstance (channelId, data) {
1204
+ async editStageInstance (channelId, topic, reason) {
1079
1205
  return await rest.patch(rest.routes.channels.stage(channelId), {
1080
- topic: data.topic
1206
+ body: {
1207
+ topic
1208
+ },
1209
+ reason
1081
1210
  });
1082
1211
  },
1083
1212
  async editUserVoiceState (guildId, options) {
1084
- return await rest.patch(rest.routes.guilds.voice(guildId, options.userId), {
1085
- channel_id: options.channelId,
1086
- suppress: options.suppress,
1087
- user_id: options.userId
1213
+ await rest.patch(rest.routes.guilds.voice(guildId, options.userId), {
1214
+ body: options
1088
1215
  });
1089
1216
  },
1090
- async editWebhook (webhookId, options) {
1091
- return await rest.patch(rest.routes.webhooks.id(webhookId), options);
1217
+ async editWebhook (webhookId, body) {
1218
+ return await rest.patch(rest.routes.webhooks.id(webhookId), {
1219
+ body
1220
+ });
1092
1221
  },
1093
1222
  async editWebhookMessage (webhookId, token, messageId, options) {
1094
- return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), options);
1223
+ return await rest.patch(rest.routes.webhooks.message(webhookId, token, messageId, options), {
1224
+ body: options
1225
+ });
1095
1226
  },
1096
- async editWebhookWithToken (webhookId, token, options) {
1097
- return await rest.patch(rest.routes.webhooks.webhook(webhookId, token), options);
1227
+ async editWebhookWithToken (webhookId, token, body) {
1228
+ return await rest.patch(rest.routes.webhooks.webhook(webhookId, token), {
1229
+ body
1230
+ });
1098
1231
  },
1099
- async editWelcomeScreen (guildId, options) {
1100
- return await rest.patch(rest.routes.guilds.welcome(guildId), options);
1232
+ async editWelcomeScreen (guildId, body) {
1233
+ return await rest.patch(rest.routes.guilds.welcome(guildId), {
1234
+ body
1235
+ });
1101
1236
  },
1102
- async editWidgetSettings (guildId, options) {
1103
- return await rest.patch(rest.routes.guilds.widget(guildId), options);
1237
+ async editWidgetSettings (guildId, body) {
1238
+ return await rest.patch(rest.routes.guilds.widget(guildId), {
1239
+ body
1240
+ });
1104
1241
  },
1105
1242
  async executeWebhook (webhookId, token, options) {
1106
- return await rest.post(rest.routes.webhooks.webhook(webhookId, token, options), options);
1243
+ return await rest.post(rest.routes.webhooks.webhook(webhookId, token, options), {
1244
+ body: options
1245
+ });
1107
1246
  },
1108
1247
  async followAnnouncement (sourceChannelId, targetChannelId) {
1109
1248
  return await rest.post(rest.routes.channels.follow(sourceChannelId), {
1110
- webhook_channel_id: targetChannelId
1249
+ body: {
1250
+ webhook_channel_id: targetChannelId
1251
+ }
1111
1252
  });
1112
1253
  },
1113
1254
  async getActiveThreads (guildId) {
@@ -1154,7 +1295,9 @@ export function createRestManager(options) {
1154
1295
  },
1155
1296
  async getDmChannel (userId) {
1156
1297
  return await rest.post(rest.routes.channels.dm(), {
1157
- recipient_id: userId.toString()
1298
+ body: {
1299
+ recipient_id: userId
1300
+ }
1158
1301
  });
1159
1302
  },
1160
1303
  async getEmoji (guildId, emojiId) {
@@ -1298,77 +1441,99 @@ export function createRestManager(options) {
1298
1441
  return await rest.get(rest.routes.guilds.widget(guildId));
1299
1442
  },
1300
1443
  async joinThread (channelId) {
1301
- return await rest.put(rest.routes.channels.threads.me(channelId));
1444
+ await rest.put(rest.routes.channels.threads.me(channelId));
1302
1445
  },
1303
1446
  async leaveGuild (guildId) {
1304
- return await rest.delete(rest.routes.guilds.leave(guildId));
1447
+ await rest.delete(rest.routes.guilds.leave(guildId));
1305
1448
  },
1306
1449
  async leaveThread (channelId) {
1307
- return await rest.delete(rest.routes.channels.threads.me(channelId));
1450
+ await rest.delete(rest.routes.channels.threads.me(channelId));
1308
1451
  },
1309
1452
  async publishMessage (channelId, messageId) {
1310
1453
  return await rest.post(rest.routes.channels.crosspost(channelId, messageId));
1311
1454
  },
1312
1455
  async removeRole (guildId, userId, roleId, reason) {
1313
- return await rest.delete(rest.routes.guilds.roles.member(guildId, userId, roleId), {
1456
+ await rest.delete(rest.routes.guilds.roles.member(guildId, userId, roleId), {
1314
1457
  reason
1315
1458
  });
1316
1459
  },
1317
1460
  async removeThreadMember (channelId, userId) {
1318
- return await rest.delete(rest.routes.channels.threads.user(channelId, userId));
1461
+ await rest.delete(rest.routes.channels.threads.user(channelId, userId));
1319
1462
  },
1463
+ // TODO: why that
1320
1464
  async sendFollowupMessage (token, options) {
1321
1465
  return await new Promise((resolve, reject)=>{
1322
1466
  rest.sendRequest({
1323
1467
  url: rest.routes.webhooks.webhook(rest.applicationId, token),
1324
1468
  method: 'POST',
1325
- body: options,
1469
+ options: {
1470
+ body: options
1471
+ },
1326
1472
  retryCount: 0,
1327
1473
  retryRequest: async function(options) {
1328
1474
  // TODO: should change to reprocess queue item
1329
1475
  await rest.sendRequest(options);
1330
1476
  },
1331
- resolve: (data)=>resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined),
1477
+ resolve: (data)=>{
1478
+ resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1479
+ },
1332
1480
  reject
1333
1481
  });
1334
1482
  });
1335
1483
  },
1484
+ // TODO: why that
1336
1485
  async sendInteractionResponse (interactionId, token, options) {
1337
- return await new Promise((resolve, reject)=>{
1486
+ await new Promise((resolve, reject)=>{
1338
1487
  rest.sendRequest({
1339
1488
  url: rest.routes.interactions.responses.callback(interactionId, token),
1340
1489
  method: 'POST',
1341
- body: options,
1490
+ options: {
1491
+ body: options
1492
+ },
1342
1493
  retryCount: 0,
1343
1494
  retryRequest: async function(options) {
1344
1495
  // TODO: should change to reprocess queue item
1345
1496
  await rest.sendRequest(options);
1346
1497
  },
1347
- resolve: (data)=>resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined),
1498
+ resolve: (data)=>{
1499
+ resolve(data.status !== 204 ? JSON.parse(data.body ?? '{}') : undefined);
1500
+ },
1348
1501
  reject
1349
1502
  });
1350
1503
  });
1351
1504
  },
1352
- async sendMessage (channelId, options) {
1353
- return await rest.post(rest.routes.channels.messages(channelId), options);
1505
+ async sendMessage (channelId, body) {
1506
+ return await rest.post(rest.routes.channels.messages(channelId), {
1507
+ body
1508
+ });
1354
1509
  },
1355
- async startThreadWithMessage (channelId, messageId, options) {
1356
- return await rest.post(rest.routes.channels.threads.message(channelId, messageId), options);
1510
+ async startThreadWithMessage (channelId, messageId, body) {
1511
+ return await rest.post(rest.routes.channels.threads.message(channelId, messageId), {
1512
+ body
1513
+ });
1357
1514
  },
1358
- async startThreadWithoutMessage (channelId, options) {
1359
- return await rest.post(rest.routes.channels.threads.all(channelId), options);
1515
+ async startThreadWithoutMessage (channelId, body) {
1516
+ return await rest.post(rest.routes.channels.threads.all(channelId), {
1517
+ body
1518
+ });
1360
1519
  },
1361
1520
  async syncGuildTemplate (guildId) {
1362
1521
  return await rest.put(rest.routes.guilds.templates.all(guildId));
1363
1522
  },
1364
- async banMember (guildId, userId, options) {
1365
- return await rest.put(rest.routes.guilds.members.ban(guildId, userId), options);
1523
+ async banMember (guildId, userId, body) {
1524
+ await rest.put(rest.routes.guilds.members.ban(guildId, userId), {
1525
+ body
1526
+ });
1366
1527
  },
1367
- async editBotMember (guildId, options) {
1368
- return await rest.patch(rest.routes.guilds.members.bot(guildId), options);
1528
+ async editBotMember (guildId, body) {
1529
+ return await rest.patch(rest.routes.guilds.members.bot(guildId), {
1530
+ body
1531
+ });
1369
1532
  },
1370
- async editMember (guildId, userId, options) {
1371
- return await rest.patch(rest.routes.guilds.members.member(guildId, userId), options);
1533
+ async editMember (guildId, userId, body) {
1534
+ return await rest.patch(rest.routes.guilds.members.member(guildId, userId), {
1535
+ body
1536
+ });
1372
1537
  },
1373
1538
  async getMember (guildId, userId) {
1374
1539
  return await rest.get(rest.routes.guilds.members.member(guildId, userId));
@@ -1377,37 +1542,43 @@ export function createRestManager(options) {
1377
1542
  return await rest.get(rest.routes.guilds.members.members(guildId, options));
1378
1543
  },
1379
1544
  async kickMember (guildId, userId, reason) {
1380
- return await rest.delete(rest.routes.guilds.members.member(guildId, userId), {
1545
+ await rest.delete(rest.routes.guilds.members.member(guildId, userId), {
1381
1546
  reason
1382
1547
  });
1383
1548
  },
1384
1549
  async pinMessage (channelId, messageId, reason) {
1385
- return await rest.put(rest.routes.channels.pin(channelId, messageId), reason ? {
1550
+ await rest.put(rest.routes.channels.pin(channelId, messageId), {
1386
1551
  reason
1387
- } : undefined);
1552
+ });
1388
1553
  },
1389
- async pruneMembers (guildId, options) {
1390
- return await rest.post(rest.routes.guilds.members.prune(guildId), options);
1554
+ async pruneMembers (guildId, body) {
1555
+ return await rest.post(rest.routes.guilds.members.prune(guildId), {
1556
+ body
1557
+ });
1391
1558
  },
1392
1559
  async searchMembers (guildId, query, options) {
1393
1560
  return await rest.get(rest.routes.guilds.members.search(guildId, query, options));
1394
1561
  },
1395
1562
  async unbanMember (guildId, userId) {
1396
- return await rest.delete(rest.routes.guilds.members.ban(guildId, userId));
1563
+ await rest.delete(rest.routes.guilds.members.ban(guildId, userId));
1397
1564
  },
1398
1565
  async unpinMessage (channelId, messageId, reason) {
1399
- return await rest.delete(rest.routes.channels.pin(channelId, messageId), reason ? {
1566
+ await rest.delete(rest.routes.channels.pin(channelId, messageId), {
1400
1567
  reason
1401
- } : undefined);
1568
+ });
1402
1569
  },
1403
1570
  async triggerTypingIndicator (channelId) {
1404
- return await rest.post(rest.routes.channels.typing(channelId));
1571
+ await rest.post(rest.routes.channels.typing(channelId));
1405
1572
  },
1406
- async upsertGlobalApplicationCommands (commands) {
1407
- return await rest.put(rest.routes.interactions.commands.commands(rest.applicationId), commands);
1573
+ async upsertGlobalApplicationCommands (body) {
1574
+ return await rest.put(rest.routes.interactions.commands.commands(rest.applicationId), {
1575
+ body
1576
+ });
1408
1577
  },
1409
- async upsertGuildApplicationCommands (guildId, commands) {
1410
- return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), commands);
1578
+ async upsertGuildApplicationCommands (guildId, body) {
1579
+ return await rest.put(rest.routes.interactions.commands.guilds.all(rest.applicationId, guildId), {
1580
+ body
1581
+ });
1411
1582
  }
1412
1583
  };
1413
1584
  return rest;