installed discord.js
This commit is contained in:
parent
d41ef1c9ed
commit
570273e743
|
@ -0,0 +1,264 @@
|
||||||
|
{
|
||||||
|
"name": "skopa",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"node_modules/@discordjs/builders": {
|
||||||
|
"version": "1.7.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/builders/-/builders-1.7.0.tgz",
|
||||||
|
"integrity": "sha512-GDtbKMkg433cOZur8Dv6c25EHxduNIBsxeHrsRoIM8+AwmEZ8r0tEpckx/sHwTLwQPOF3e2JWloZh9ofCaMfAw==",
|
||||||
|
"dependencies": {
|
||||||
|
"@discordjs/formatters": "^0.3.3",
|
||||||
|
"@discordjs/util": "^1.0.2",
|
||||||
|
"@sapphire/shapeshift": "^3.9.3",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"fast-deep-equal": "^3.1.3",
|
||||||
|
"ts-mixer": "^6.0.3",
|
||||||
|
"tslib": "^2.6.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/collection": {
|
||||||
|
"version": "1.5.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-1.5.3.tgz",
|
||||||
|
"integrity": "sha512-SVb428OMd3WO1paV3rm6tSjM4wC+Kecaa1EUGX7vc6/fddvw/6lg90z4QtCqm21zvVe92vMMDt9+DkIvjXImQQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/formatters": {
|
||||||
|
"version": "0.3.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/formatters/-/formatters-0.3.3.tgz",
|
||||||
|
"integrity": "sha512-wTcI1Q5cps1eSGhl6+6AzzZkBBlVrBdc9IUhJbijRgVjCNIIIZPgqnUj3ntFODsHrdbGU8BEG9XmDQmgEEYn3w==",
|
||||||
|
"dependencies": {
|
||||||
|
"discord-api-types": "0.37.61"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/rest": {
|
||||||
|
"version": "2.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/rest/-/rest-2.2.0.tgz",
|
||||||
|
"integrity": "sha512-nXm9wT8oqrYFRMEqTXQx9DUTeEtXUDMmnUKIhZn6O2EeDY9VCdwj23XCPq7fkqMPKdF7ldAfeVKyxxFdbZl59A==",
|
||||||
|
"dependencies": {
|
||||||
|
"@discordjs/collection": "^2.0.0",
|
||||||
|
"@discordjs/util": "^1.0.2",
|
||||||
|
"@sapphire/async-queue": "^1.5.0",
|
||||||
|
"@sapphire/snowflake": "^3.5.1",
|
||||||
|
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"magic-bytes.js": "^1.5.0",
|
||||||
|
"tslib": "^2.6.2",
|
||||||
|
"undici": "5.27.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/rest/node_modules/@discordjs/collection": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-YTWIXLrf5FsrLMycpMM9Q6vnZoR/lN2AWX23/Cuo8uOOtS8eHB2dyQaaGnaF8aZPYnttf2bkLMcXn/j6JUOi3w==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/util": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/util/-/util-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-IRNbimrmfb75GMNEjyznqM1tkI7HrZOf14njX7tCAAUetyZM1Pr8hX/EK2lxBCOgWDRmigbp24fD1hdMfQK5lw==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/ws": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/ws/-/ws-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-+XI82Rm2hKnFwAySXEep4A7Kfoowt6weO6381jgW+wVdTpMS/56qCvoXyFRY0slcv7c/U8My2PwIB2/wEaAh7Q==",
|
||||||
|
"dependencies": {
|
||||||
|
"@discordjs/collection": "^2.0.0",
|
||||||
|
"@discordjs/rest": "^2.1.0",
|
||||||
|
"@discordjs/util": "^1.0.2",
|
||||||
|
"@sapphire/async-queue": "^1.5.0",
|
||||||
|
"@types/ws": "^8.5.9",
|
||||||
|
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"tslib": "^2.6.2",
|
||||||
|
"ws": "^8.14.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@discordjs/ws/node_modules/@discordjs/collection": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-YTWIXLrf5FsrLMycpMM9Q6vnZoR/lN2AWX23/Cuo8uOOtS8eHB2dyQaaGnaF8aZPYnttf2bkLMcXn/j6JUOi3w==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@fastify/busboy": {
|
||||||
|
"version": "2.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@fastify/busboy/-/busboy-2.1.0.tgz",
|
||||||
|
"integrity": "sha512-+KpH+QxZU7O4675t3mnkQKcZZg56u+K/Ct2K+N2AZYNVK8kyeo/bI18tI8aPm3tvNNRyTWfj6s5tnGNlcbQRsA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@sapphire/async-queue": {
|
||||||
|
"version": "1.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@sapphire/async-queue/-/async-queue-1.5.1.tgz",
|
||||||
|
"integrity": "sha512-1RdpsmDQR/aWfp8oJzPtn4dNQrbpqSL5PIA0uAB/XwerPXUf994Ug1au1e7uGcD7ei8/F63UDjr5GWps1g/HxQ==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=v14.0.0",
|
||||||
|
"npm": ">=7.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@sapphire/shapeshift": {
|
||||||
|
"version": "3.9.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/@sapphire/shapeshift/-/shapeshift-3.9.5.tgz",
|
||||||
|
"integrity": "sha512-AGdHe+51gF7D3W8hBfuSFLBocURDCXVQczScTHXDS3RpNjNgrktIx/amlz5y8nHhm8SAdFt/X8EF8ZSfjJ0tnA==",
|
||||||
|
"dependencies": {
|
||||||
|
"fast-deep-equal": "^3.1.3",
|
||||||
|
"lodash": "^4.17.21"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=v18"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@sapphire/snowflake": {
|
||||||
|
"version": "3.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@sapphire/snowflake/-/snowflake-3.5.1.tgz",
|
||||||
|
"integrity": "sha512-BxcYGzgEsdlG0dKAyOm0ehLGm2CafIrfQTZGWgkfKYbj+pNNsorZ7EotuZukc2MT70E0UbppVbtpBrqpzVzjNA==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=v14.0.0",
|
||||||
|
"npm": ">=7.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@types/node": {
|
||||||
|
"version": "20.10.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/node/-/node-20.10.5.tgz",
|
||||||
|
"integrity": "sha512-nNPsNE65wjMxEKI93yOP+NPGGBJz/PoN3kZsVLee0XMiJolxSekEVD8wRwBUBqkwc7UWop0edW50yrCQW4CyRw==",
|
||||||
|
"dependencies": {
|
||||||
|
"undici-types": "~5.26.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@types/ws": {
|
||||||
|
"version": "8.5.9",
|
||||||
|
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.9.tgz",
|
||||||
|
"integrity": "sha512-jbdrY0a8lxfdTp/+r7Z4CkycbOFN8WX+IOchLJr3juT/xzbJ8URyTVSJ/hvNdadTgM1mnedb47n+Y31GsFnQlg==",
|
||||||
|
"dependencies": {
|
||||||
|
"@types/node": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@vladfrangu/async_event_emitter": {
|
||||||
|
"version": "2.2.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/@vladfrangu/async_event_emitter/-/async_event_emitter-2.2.4.tgz",
|
||||||
|
"integrity": "sha512-ButUPz9E9cXMLgvAW8aLAKKJJsPu1dY1/l/E8xzLFuysowXygs6GBcyunK9rnGC4zTsnIc2mQo71rGw9U+Ykug==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=v14.0.0",
|
||||||
|
"npm": ">=7.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/discord-api-types": {
|
||||||
|
"version": "0.37.61",
|
||||||
|
"resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.61.tgz",
|
||||||
|
"integrity": "sha512-o/dXNFfhBpYHpQFdT6FWzeO7pKc838QeeZ9d91CfVAtpr5XLK4B/zYxQbYgPdoMiTDvJfzcsLW5naXgmHGDNXw=="
|
||||||
|
},
|
||||||
|
"node_modules/discord.js": {
|
||||||
|
"version": "14.14.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/discord.js/-/discord.js-14.14.1.tgz",
|
||||||
|
"integrity": "sha512-/hUVzkIerxKHyRKopJy5xejp4MYKDPTszAnpYxzVVv4qJYf+Tkt+jnT2N29PIPschicaEEpXwF2ARrTYHYwQ5w==",
|
||||||
|
"dependencies": {
|
||||||
|
"@discordjs/builders": "^1.7.0",
|
||||||
|
"@discordjs/collection": "1.5.3",
|
||||||
|
"@discordjs/formatters": "^0.3.3",
|
||||||
|
"@discordjs/rest": "^2.1.0",
|
||||||
|
"@discordjs/util": "^1.0.2",
|
||||||
|
"@discordjs/ws": "^1.0.2",
|
||||||
|
"@sapphire/snowflake": "3.5.1",
|
||||||
|
"@types/ws": "8.5.9",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"fast-deep-equal": "3.1.3",
|
||||||
|
"lodash.snakecase": "4.1.1",
|
||||||
|
"tslib": "2.6.2",
|
||||||
|
"undici": "5.27.2",
|
||||||
|
"ws": "8.14.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/fast-deep-equal": {
|
||||||
|
"version": "3.1.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz",
|
||||||
|
"integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q=="
|
||||||
|
},
|
||||||
|
"node_modules/lodash": {
|
||||||
|
"version": "4.17.21",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||||
|
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
|
||||||
|
},
|
||||||
|
"node_modules/lodash.snakecase": {
|
||||||
|
"version": "4.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz",
|
||||||
|
"integrity": "sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw=="
|
||||||
|
},
|
||||||
|
"node_modules/magic-bytes.js": {
|
||||||
|
"version": "1.7.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.7.0.tgz",
|
||||||
|
"integrity": "sha512-YzVU2+/hrjwx8xcgAw+ffNq3jkactpj+f1iSL4LonrFKhvnwDzHSqtFdk/MMRP53y9ScouJ7cKEnqYsJwsHoYA=="
|
||||||
|
},
|
||||||
|
"node_modules/ts-mixer": {
|
||||||
|
"version": "6.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/ts-mixer/-/ts-mixer-6.0.3.tgz",
|
||||||
|
"integrity": "sha512-k43M7uCG1AkTyxgnmI5MPwKoUvS/bRvLvUb7+Pgpdlmok8AoqmUaZxUUw8zKM5B1lqZrt41GjYgnvAi0fppqgQ=="
|
||||||
|
},
|
||||||
|
"node_modules/tslib": {
|
||||||
|
"version": "2.6.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.6.2.tgz",
|
||||||
|
"integrity": "sha512-AEYxH93jGFPn/a2iVAwW87VuUIkR1FVUKB77NwMF7nBTDkDrrT/Hpt/IrCJ0QXhW27jTBDcf5ZY7w6RiqTMw2Q=="
|
||||||
|
},
|
||||||
|
"node_modules/undici": {
|
||||||
|
"version": "5.27.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/undici/-/undici-5.27.2.tgz",
|
||||||
|
"integrity": "sha512-iS857PdOEy/y3wlM3yRp+6SNQQ6xU0mmZcwRSriqk+et/cwWAtwmIGf6WkoDN2EK/AMdCO/dfXzIwi+rFMrjjQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"@fastify/busboy": "^2.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/undici-types": {
|
||||||
|
"version": "5.26.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz",
|
||||||
|
"integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA=="
|
||||||
|
},
|
||||||
|
"node_modules/ws": {
|
||||||
|
"version": "8.14.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/ws/-/ws-8.14.2.tgz",
|
||||||
|
"integrity": "sha512-wEBG1ftX4jcglPxgFCMJmZ2PLtSbJ2Peg6TmpJFTbe9GZYOQCDPdMYu/Tm0/bGZkw8paZnJY45J4K2PZrLYq8g==",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.0.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"bufferutil": "^4.0.1",
|
||||||
|
"utf-8-validate": ">=5.0.2"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"bufferutil": {
|
||||||
|
"optional": true
|
||||||
|
},
|
||||||
|
"utf-8-validate": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,191 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2021 Noel Buechler
|
||||||
|
Copyright 2021 Vlad Frangu
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,72 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/builders"><img src="https://img.shields.io/npm/v/@discordjs/builders.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/builders"><img src="https://img.shields.io/npm/dt/@discordjs/builders.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=builders" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/builders` is a utility package for easily building Discord API payloads.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 16.11.0 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/builders
|
||||||
|
yarn add @discordjs/builders
|
||||||
|
pnpm add @discordjs/builders
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
You can find examples of how to use the builders in the [Slash Command Builders][example] examples.
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[example]: https://github.com/discordjs/discord.js/blob/main/packages/builders/docs/examples/Slash%20Command%20Builders.md
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/builders/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/builders
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/builders
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,97 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/builders",
|
||||||
|
"version": "1.7.0",
|
||||||
|
"description": "A set of builders that you can use when creating your bot",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"discord",
|
||||||
|
"api",
|
||||||
|
"bot",
|
||||||
|
"client",
|
||||||
|
"node",
|
||||||
|
"discordapp",
|
||||||
|
"discordjs"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/builders"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"dependencies": {
|
||||||
|
"@sapphire/shapeshift": "^3.9.3",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"fast-deep-equal": "^3.1.3",
|
||||||
|
"ts-mixer": "^6.0.3",
|
||||||
|
"tslib": "^2.6.2",
|
||||||
|
"@discordjs/formatters": "^0.3.3",
|
||||||
|
"@discordjs/util": "^1.0.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "16.18.60",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"downlevel-dts": "^0.11.0",
|
||||||
|
"esbuild-plugin-version-injector": "^1.2.1",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^3.0.3",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json && downlevel-dts ./dist-docs ./dist-docs",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src __tests__",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src __tests__",
|
||||||
|
"fmt": "pnpm run format",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/builders/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,190 @@
|
||||||
|
# Changelog
|
||||||
|
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.3](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.5.2...@discordjs/collection@1.5.3) - (2023-08-17)
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Update Node.js requirement to 16.11.0 (#9764) ([188877c](https://github.com/discordjs/discord.js/commit/188877c50af70f0d5cffb246620fa277435c6ce6))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.2](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.5.1...@discordjs/collection@1.5.2) - (2023-07-31)
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- **collection:** Reduce `reduce`'s code (#9581) ([b85a3f2](https://github.com/discordjs/discord.js/commit/b85a3f2ddee8fc5974749b95fc07389a03093df2))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.1](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.5.0...@discordjs/collection@1.5.1) - (2023-05-01)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- Fix external links (#9313) ([a7425c2](https://github.com/discordjs/discord.js/commit/a7425c29c4f23f1b31f4c6a463107ca9eb7fd7e2))
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Generate static imports for types with api-extractor ([98a76db](https://github.com/discordjs/discord.js/commit/98a76db482879f79d6bb2fb2e5fc65ac2c34e2d9))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.2](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.5.1...@discordjs/collection@1.5.2) - (2023-07-31)
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- **collection:** Reduce `reduce`'s code (#9581) ([b85a3f2](https://github.com/discordjs/discord.js/commit/b85a3f2ddee8fc5974749b95fc07389a03093df2))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.1](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.5.0...@discordjs/collection@1.5.1) - (2023-05-01)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- Fix external links (#9313) ([a7425c2](https://github.com/discordjs/discord.js/commit/a7425c29c4f23f1b31f4c6a463107ca9eb7fd7e2))
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Generate static imports for types with api-extractor ([98a76db](https://github.com/discordjs/discord.js/commit/98a76db482879f79d6bb2fb2e5fc65ac2c34e2d9))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.1](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.5.0...@discordjs/collection@1.5.1) - (2023-05-01)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- Fix external links (#9313) ([a7425c2](https://github.com/discordjs/discord.js/commit/a7425c29c4f23f1b31f4c6a463107ca9eb7fd7e2))
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Generate static imports for types with api-extractor ([98a76db](https://github.com/discordjs/discord.js/commit/98a76db482879f79d6bb2fb2e5fc65ac2c34e2d9))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.5.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.4.0...@discordjs/collection@1.5.0) - (2023-04-01)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- **scripts:** Accessing tsComment ([d8d5f31](https://github.com/discordjs/discord.js/commit/d8d5f31d3927fd1de62f1fa3a1a6e454243ad87b))
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **website:** Render syntax and mdx on the server (#9086) ([ee5169e](https://github.com/discordjs/discord.js/commit/ee5169e0aadd7bbfcd752aae614ec0f69602b68b))
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- **collection:** Fix/silence linter warnings (#9266) ([d6f4e60](https://github.com/discordjs/discord.js/commit/d6f4e60efd1a1796fc84dbbfbac4f9790e480a1c))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.4.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.3.0...@discordjs/collection@1.4.0) - (2023-03-12)
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Fix version export (#9049) ([8b70f49](https://github.com/discordjs/discord.js/commit/8b70f497a1207e30edebdecd12b926c981c13d28))
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **website:** Add support for source file links (#9048) ([f6506e9](https://github.com/discordjs/discord.js/commit/f6506e99c496683ee0ab67db0726b105b929af38))
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- Compare with `undefined` directly (#9191) ([869153c](https://github.com/discordjs/discord.js/commit/869153c3fdf155783e7c0ecebd3627b087c3a026))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.3.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.2.0...@discordjs/collection@1.3.0) - (2022-11-28)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- Pin @types/node version ([9d8179c](https://github.com/discordjs/discord.js/commit/9d8179c6a78e1c7f9976f852804055964d5385d4))
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Add `Collection#subtract()` (#8393) ([291f36c](https://github.com/discordjs/discord.js/commit/291f36cd736b5dea058145a1335bf7c78ec1d81d))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.2.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.1.0...@discordjs/collection@1.2.0) - (2022-10-08)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- Footer / sidebar / deprecation alert ([ba3e0ed](https://github.com/discordjs/discord.js/commit/ba3e0ed348258fe8e51eefb4aa7379a1230616a9))
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Change name (#8604) ([dd5a089](https://github.com/discordjs/discord.js/commit/dd5a08944c258a847fc4377f1d5e953264ab47d0))
|
||||||
|
- Remove xml tag from collection#find (#8550) ([4032457](https://github.com/discordjs/discord.js/commit/40324574ebea9894cadcc967e0db0e4e21d62768))
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Web-components (#8715) ([0ac3e76](https://github.com/discordjs/discord.js/commit/0ac3e766bd9dbdeb106483fa4bb085d74de346a2))
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- Website components (#8600) ([c334157](https://github.com/discordjs/discord.js/commit/c3341570d983aea9ecc419979d5a01de658c9d67))
|
||||||
|
- Use `eslint-config-neon` for packages. (#8579) ([edadb9f](https://github.com/discordjs/discord.js/commit/edadb9fe5dfd9ff51a3cfc9b25cb242d3f9f5241))
|
||||||
|
|
||||||
|
## Typings
|
||||||
|
|
||||||
|
- **Collection:** Make fn return type unknown (#8676) ([822b7f2](https://github.com/discordjs/discord.js/commit/822b7f234af053c8f917b0a998b82abfccd33801))
|
||||||
|
|
||||||
|
# [@discordjs/collection@1.1.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@1.0.1...@discordjs/collection@1.1.0) - (2022-08-22)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- Use proper format for `@link` text (#8384) ([2655639](https://github.com/discordjs/discord.js/commit/26556390a3800e954974a00c1328ff47d3e67e9a))
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Fence examples in codeblocks ([193b252](https://github.com/discordjs/discord.js/commit/193b252672440a860318d3c2968aedd9cb88e0ce))
|
||||||
|
- Use link tags (#8382) ([5494791](https://github.com/discordjs/discord.js/commit/549479131318c659f86f0eb18578d597e22522d3))
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **website:** Show `constructor` information (#8540) ([e42fd16](https://github.com/discordjs/discord.js/commit/e42fd1636973b10dd7ed6fb4280ee1a4a8f82007))
|
||||||
|
- **website:** Show descriptions for `@typeParam` blocks (#8523) ([e475b63](https://github.com/discordjs/discord.js/commit/e475b63f257f6261d73cb89fee9ecbcdd84e2a6b))
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- **website:** Adjust typography (#8503) ([0f83402](https://github.com/discordjs/discord.js/commit/0f834029850d2448981596cf082ff59917018d66))
|
||||||
|
- Docs design (#8487) ([4ab1d09](https://github.com/discordjs/discord.js/commit/4ab1d09997a18879a9eb9bda39df6f15aa22557e))
|
||||||
|
|
||||||
|
# [@discordjs/collection@0.8.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@0.7.0...@discordjs/collection@0.8.0) - (2022-07-17)
|
||||||
|
|
||||||
|
## Bug Fixes
|
||||||
|
|
||||||
|
- **Collection:** Make error messages consistent (#8224) ([5bd6b28](https://github.com/discordjs/discord.js/commit/5bd6b28b3ebfced1cb9d23e83bd7c0def7a12404))
|
||||||
|
- Check for function type (#8064) ([3bb9c0e](https://github.com/discordjs/discord.js/commit/3bb9c0e5c37311044ff41761b572ac4f91cda57c))
|
||||||
|
|
||||||
|
## Documentation
|
||||||
|
|
||||||
|
- Add codecov coverage badge to readmes (#8226) ([f6db285](https://github.com/discordjs/discord.js/commit/f6db285c073898a749fe4591cbd4463d1896daf5))
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Codecov (#8219) ([f10f4cd](https://github.com/discordjs/discord.js/commit/f10f4cdcd88ca6be7ec735ed3a415ba13da83db0))
|
||||||
|
- **docgen:** Update typedoc ([b3346f4](https://github.com/discordjs/discord.js/commit/b3346f4b9b3d4f96443506643d4631dc1c6d7b21))
|
||||||
|
- Website (#8043) ([127931d](https://github.com/discordjs/discord.js/commit/127931d1df7a2a5c27923c2f2151dbf3824e50cc))
|
||||||
|
- **docgen:** Typescript support ([3279b40](https://github.com/discordjs/discord.js/commit/3279b40912e6aa61507bedb7db15a2b8668de44b))
|
||||||
|
- Docgen package (#8029) ([8b979c0](https://github.com/discordjs/discord.js/commit/8b979c0245c42fd824d8e98745ee869f5360fc86))
|
||||||
|
- Use vitest instead of jest for more speed ([8d8e6c0](https://github.com/discordjs/discord.js/commit/8d8e6c03decd7352a2aa180f6e5bc1a13602539b))
|
||||||
|
- Add scripts package for locally used scripts ([f2ae1f9](https://github.com/discordjs/discord.js/commit/f2ae1f9348bfd893332a9060f71a8a5f272a1b8b))
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- **collection:** Remove `default` property (#8055) ([c8f1690](https://github.com/discordjs/discord.js/commit/c8f1690896f55f06e05a83704262783cfc2bb91d))
|
||||||
|
- **collection:** Remove default export (#8053) ([16810f3](https://github.com/discordjs/discord.js/commit/16810f3e410bf35ed7e6e7412d517ea74c792c5d))
|
||||||
|
- Move all the config files to root (#8033) ([769ea0b](https://github.com/discordjs/discord.js/commit/769ea0bfe78c4f1d413c6b397c604ffe91e39c6a))
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
- **collection:** Improve coverage (#8222) ([a51f721](https://github.com/discordjs/discord.js/commit/a51f7215eca67a0f46fba8b2d706f7ec6f6dc228))
|
||||||
|
|
||||||
|
# [@discordjs/collection@0.7.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@0.6.0...@discordjs/collection@0.7.0) - (2022-06-04)
|
||||||
|
|
||||||
|
## Styling
|
||||||
|
|
||||||
|
- Cleanup tests and tsup configs ([6b8ef20](https://github.com/discordjs/discord.js/commit/6b8ef20cb3af5b5cfd176dd0aa0a1a1e98551629))
|
||||||
|
|
||||||
|
# [@discordjs/collection@0.6.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@0.5.0...@discordjs/collection@0.6.0) - (2022-04-17)
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- Add support for module: NodeNext in TS and ESM (#7598) ([8f1986a](https://github.com/discordjs/discord.js/commit/8f1986a6aa98365e09b00e84ad5f9f354ab61f3d))
|
||||||
|
- **builders:** Add attachment command option type (#7203) ([ae0f35f](https://github.com/discordjs/discord.js/commit/ae0f35f51d68dfa5a7dc43d161ef9365171debdb))
|
||||||
|
- **Collection:** Add merging functions (#7299) ([e4bd07b](https://github.com/discordjs/discord.js/commit/e4bd07b2394f227ea06b72eb6999de9ab3127b25))
|
||||||
|
|
||||||
|
# [@discordjs/collection@0.5.0](https://github.com/discordjs/discord.js/compare/@discordjs/collection@0.4.0...@discordjs/collection@0.5.0) - (2022-01-24)
|
||||||
|
|
||||||
|
## Refactor
|
||||||
|
|
||||||
|
- Make `intersect` perform a true intersection (#7211) ([d8efba2](https://github.com/discordjs/discord.js/commit/d8efba24e09aa2a8dbf028fc57a561a56e7833fd))
|
||||||
|
|
||||||
|
## Typings
|
||||||
|
|
||||||
|
- Add `ReadonlyCollection` (#7245) ([db25f52](https://github.com/discordjs/discord.js/commit/db25f529b26d7c819c1c42ad3e26c2263ea2da0e))
|
||||||
|
- **Collection:** Union types on `intersect` and `difference` (#7196) ([1f9b922](https://github.com/discordjs/discord.js/commit/1f9b9225f2066e9cc66c3355417139fd25cc403c))
|
|
@ -0,0 +1,191 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2021 Noel Buechler
|
||||||
|
Copyright 2015 Amish Shah
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,67 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/v/@discordjs/collection.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/dt/@discordjs/collection.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=collection" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/collection` is a powerful utility data structure used in discord.js.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 16.11.0 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/collection
|
||||||
|
yarn add @discordjs/collection
|
||||||
|
pnpm add @discordjs/collection
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/collection/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/collection
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/collection
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
|
@ -0,0 +1,457 @@
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface CollectionConstructor {
|
||||||
|
new (): Collection<unknown, unknown>;
|
||||||
|
new <K, V>(entries?: readonly (readonly [K, V])[] | null): Collection<K, V>;
|
||||||
|
new <K, V>(iterable: Iterable<readonly [K, V]>): Collection<K, V>;
|
||||||
|
readonly prototype: Collection<unknown, unknown>;
|
||||||
|
readonly [Symbol.species]: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an immutable version of a collection
|
||||||
|
*/
|
||||||
|
type ReadonlyCollection<K, V> = Omit<Collection<K, V>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<K, V>;
|
||||||
|
/**
|
||||||
|
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Collection<K, V> extends Map<K, V> {
|
||||||
|
constructor: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||||
|
* an ID, for significantly improved performance and ease-of-use.
|
||||||
|
*
|
||||||
|
* @typeParam K - The key type this collection holds
|
||||||
|
* @typeParam V - The value type this collection holds
|
||||||
|
*/
|
||||||
|
declare class Collection<K, V> extends Map<K, V> {
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V;
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys: K[]): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys: K[]): boolean;
|
||||||
|
/**
|
||||||
|
* Obtains the first value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the beginning
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||||
|
*/
|
||||||
|
first(): V | undefined;
|
||||||
|
first(amount: number): V[];
|
||||||
|
/**
|
||||||
|
* Obtains the first key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the beginning
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
firstKey(): K | undefined;
|
||||||
|
firstKey(amount: number): K[];
|
||||||
|
/**
|
||||||
|
* Obtains the last value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the end
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
last(): V | undefined;
|
||||||
|
last(amount: number): V[];
|
||||||
|
/**
|
||||||
|
* Obtains the last key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the end
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
lastKey(): K | undefined;
|
||||||
|
lastKey(amount: number): K[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index: number): V | undefined;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index: number): K | undefined;
|
||||||
|
/**
|
||||||
|
* Obtains unique random value(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain randomly
|
||||||
|
* @returns A single value if no amount is provided or an array of values
|
||||||
|
*/
|
||||||
|
random(): V | undefined;
|
||||||
|
random(amount: number): V[];
|
||||||
|
/**
|
||||||
|
* Obtains unique random key(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain randomly
|
||||||
|
* @returns A single key if no amount is provided or an array
|
||||||
|
*/
|
||||||
|
randomKey(): K | undefined;
|
||||||
|
randomKey(amount: number): K[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse(): this;
|
||||||
|
/**
|
||||||
|
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
|
||||||
|
* All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||||
|
* should use the `get` method. See
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.find(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
find<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): V2 | undefined;
|
||||||
|
find(fn: (value: V, key: K, collection: this) => unknown): V | undefined;
|
||||||
|
find<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): V2 | undefined;
|
||||||
|
find<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): V | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.findKey(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
findKey<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): K2 | undefined;
|
||||||
|
findKey(fn: (value: V, key: K, collection: this) => unknown): K | undefined;
|
||||||
|
findKey<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): K2 | undefined;
|
||||||
|
findKey<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): K | undefined;
|
||||||
|
/**
|
||||||
|
* Removes items that satisfy the provided filter function.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @returns The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn: (value: V, key: K, collection: this) => unknown): number;
|
||||||
|
sweep<T>(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): number;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.filter(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): Collection<K2, V>;
|
||||||
|
filter<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): Collection<K, V2>;
|
||||||
|
filter(fn: (value: V, key: K, collection: this) => unknown): Collection<K, V>;
|
||||||
|
filter<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): Collection<K2, V>;
|
||||||
|
filter<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): Collection<K, V2>;
|
||||||
|
filter<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
partition<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
|
||||||
|
partition<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
|
||||||
|
partition(fn: (value: V, key: K, collection: this) => unknown): [Collection<K, V>, Collection<K, V>];
|
||||||
|
partition<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
|
||||||
|
partition<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
|
||||||
|
partition<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): [Collection<K, V>, Collection<K, V>];
|
||||||
|
/**
|
||||||
|
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces a new Collection
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.flatMap(guild => guild.members.cache);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
flatMap<T>(fn: (value: V, key: K, collection: this) => Collection<K, T>): Collection<K, T>;
|
||||||
|
flatMap<T, This>(fn: (this: This, value: V, key: K, collection: this) => Collection<K, T>, thisArg: This): Collection<K, T>;
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into an array. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new array, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.map(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
map<T>(fn: (value: V, key: K, collection: this) => T): T[];
|
||||||
|
map<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[];
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into a collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new collection, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.mapValues(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
mapValues<T>(fn: (value: V, key: K, collection: this) => T): Collection<K, T>;
|
||||||
|
mapValues<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection<K, T>;
|
||||||
|
/**
|
||||||
|
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.some(user => user.discriminator === '0000');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
some(fn: (value: V, key: K, collection: this) => unknown): boolean;
|
||||||
|
some<T>(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if all items passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.every(user => !user.bot);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
every<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): this is Collection<K2, V>;
|
||||||
|
every<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): this is Collection<K, V2>;
|
||||||
|
every(fn: (value: V, key: K, collection: this) => unknown): boolean;
|
||||||
|
every<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): this is Collection<K2, V>;
|
||||||
|
every<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): this is Collection<K, V2>;
|
||||||
|
every<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce<T = V>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
|
||||||
|
* but returns the collection instead of undefined.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute for each element
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .each(user => console.log(user.username))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .each(user => console.log(user.username));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
each(fn: (value: V, key: K, collection: this) => void): this;
|
||||||
|
each<T>(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this;
|
||||||
|
/**
|
||||||
|
* Runs a function on the collection and returns the collection.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
tap(fn: (collection: this) => void): this;
|
||||||
|
tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this;
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone(): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections: ReadonlyCollection<K, V>[]): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection: ReadonlyCollection<K, V>): boolean;
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction?: Comparator<K, V>): this;
|
||||||
|
/**
|
||||||
|
* The intersect method returns a new structure containing items where the keys and values are present in both original structures.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
intersect<T>(other: ReadonlyCollection<K, T>): Collection<K, T>;
|
||||||
|
/**
|
||||||
|
* The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
subtract<T>(other: ReadonlyCollection<K, T>): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
difference<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V>;
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge<T, R>(other: ReadonlyCollection<K, T>, whenInSelf: (value: V, key: K) => Keep<R>, whenInOther: (valueOther: T, key: K) => Keep<R>, whenInBoth: (value: V, valueOther: T, key: K) => Keep<R>): Collection<K, R>;
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sorted(compareFunction?: Comparator<K, V>): Collection<K, V>;
|
||||||
|
toJSON(): V[];
|
||||||
|
private static defaultSort;
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries<K, V>(entries: Iterable<[K, V]>, combine: (firstValue: V, secondValue: V, key: K) => V): Collection<K, V>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Keep<V> = {
|
||||||
|
keep: false;
|
||||||
|
} | {
|
||||||
|
keep: true;
|
||||||
|
value: V;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Comparator<K, V> = (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
|
|
@ -0,0 +1,457 @@
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface CollectionConstructor {
|
||||||
|
new (): Collection<unknown, unknown>;
|
||||||
|
new <K, V>(entries?: readonly (readonly [K, V])[] | null): Collection<K, V>;
|
||||||
|
new <K, V>(iterable: Iterable<readonly [K, V]>): Collection<K, V>;
|
||||||
|
readonly prototype: Collection<unknown, unknown>;
|
||||||
|
readonly [Symbol.species]: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an immutable version of a collection
|
||||||
|
*/
|
||||||
|
type ReadonlyCollection<K, V> = Omit<Collection<K, V>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<K, V>;
|
||||||
|
/**
|
||||||
|
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Collection<K, V> extends Map<K, V> {
|
||||||
|
constructor: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||||
|
* an ID, for significantly improved performance and ease-of-use.
|
||||||
|
*
|
||||||
|
* @typeParam K - The key type this collection holds
|
||||||
|
* @typeParam V - The value type this collection holds
|
||||||
|
*/
|
||||||
|
declare class Collection<K, V> extends Map<K, V> {
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key: K, defaultValueGenerator: (key: K, collection: this) => V): V;
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys: K[]): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys: K[]): boolean;
|
||||||
|
/**
|
||||||
|
* Obtains the first value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the beginning
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||||
|
*/
|
||||||
|
first(): V | undefined;
|
||||||
|
first(amount: number): V[];
|
||||||
|
/**
|
||||||
|
* Obtains the first key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the beginning
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
firstKey(): K | undefined;
|
||||||
|
firstKey(amount: number): K[];
|
||||||
|
/**
|
||||||
|
* Obtains the last value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the end
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
last(): V | undefined;
|
||||||
|
last(amount: number): V[];
|
||||||
|
/**
|
||||||
|
* Obtains the last key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the end
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
lastKey(): K | undefined;
|
||||||
|
lastKey(amount: number): K[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index: number): V | undefined;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index: number): K | undefined;
|
||||||
|
/**
|
||||||
|
* Obtains unique random value(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain randomly
|
||||||
|
* @returns A single value if no amount is provided or an array of values
|
||||||
|
*/
|
||||||
|
random(): V | undefined;
|
||||||
|
random(amount: number): V[];
|
||||||
|
/**
|
||||||
|
* Obtains unique random key(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain randomly
|
||||||
|
* @returns A single key if no amount is provided or an array
|
||||||
|
*/
|
||||||
|
randomKey(): K | undefined;
|
||||||
|
randomKey(amount: number): K[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse(): this;
|
||||||
|
/**
|
||||||
|
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
|
||||||
|
* All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||||
|
* should use the `get` method. See
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.find(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
find<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): V2 | undefined;
|
||||||
|
find(fn: (value: V, key: K, collection: this) => unknown): V | undefined;
|
||||||
|
find<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): V2 | undefined;
|
||||||
|
find<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): V | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.findKey(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
findKey<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): K2 | undefined;
|
||||||
|
findKey(fn: (value: V, key: K, collection: this) => unknown): K | undefined;
|
||||||
|
findKey<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): K2 | undefined;
|
||||||
|
findKey<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): K | undefined;
|
||||||
|
/**
|
||||||
|
* Removes items that satisfy the provided filter function.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @returns The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn: (value: V, key: K, collection: this) => unknown): number;
|
||||||
|
sweep<T>(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): number;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.filter(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): Collection<K2, V>;
|
||||||
|
filter<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): Collection<K, V2>;
|
||||||
|
filter(fn: (value: V, key: K, collection: this) => unknown): Collection<K, V>;
|
||||||
|
filter<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): Collection<K2, V>;
|
||||||
|
filter<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): Collection<K, V2>;
|
||||||
|
filter<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
partition<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
|
||||||
|
partition<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
|
||||||
|
partition(fn: (value: V, key: K, collection: this) => unknown): [Collection<K, V>, Collection<K, V>];
|
||||||
|
partition<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): [Collection<K2, V>, Collection<Exclude<K, K2>, V>];
|
||||||
|
partition<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): [Collection<K, V2>, Collection<K, Exclude<V, V2>>];
|
||||||
|
partition<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): [Collection<K, V>, Collection<K, V>];
|
||||||
|
/**
|
||||||
|
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces a new Collection
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.flatMap(guild => guild.members.cache);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
flatMap<T>(fn: (value: V, key: K, collection: this) => Collection<K, T>): Collection<K, T>;
|
||||||
|
flatMap<T, This>(fn: (this: This, value: V, key: K, collection: this) => Collection<K, T>, thisArg: This): Collection<K, T>;
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into an array. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new array, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.map(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
map<T>(fn: (value: V, key: K, collection: this) => T): T[];
|
||||||
|
map<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): T[];
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into a collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new collection, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.mapValues(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
mapValues<T>(fn: (value: V, key: K, collection: this) => T): Collection<K, T>;
|
||||||
|
mapValues<This, T>(fn: (this: This, value: V, key: K, collection: this) => T, thisArg: This): Collection<K, T>;
|
||||||
|
/**
|
||||||
|
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.some(user => user.discriminator === '0000');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
some(fn: (value: V, key: K, collection: this) => unknown): boolean;
|
||||||
|
some<T>(fn: (this: T, value: V, key: K, collection: this) => unknown, thisArg: T): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if all items passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.every(user => !user.bot);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
every<K2 extends K>(fn: (value: V, key: K, collection: this) => key is K2): this is Collection<K2, V>;
|
||||||
|
every<V2 extends V>(fn: (value: V, key: K, collection: this) => value is V2): this is Collection<K, V2>;
|
||||||
|
every(fn: (value: V, key: K, collection: this) => unknown): boolean;
|
||||||
|
every<This, K2 extends K>(fn: (this: This, value: V, key: K, collection: this) => key is K2, thisArg: This): this is Collection<K2, V>;
|
||||||
|
every<This, V2 extends V>(fn: (this: This, value: V, key: K, collection: this) => value is V2, thisArg: This): this is Collection<K, V2>;
|
||||||
|
every<This>(fn: (this: This, value: V, key: K, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce<T = V>(fn: (accumulator: T, value: V, key: K, collection: this) => T, initialValue?: T): T;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
|
||||||
|
* but returns the collection instead of undefined.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute for each element
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .each(user => console.log(user.username))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .each(user => console.log(user.username));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
each(fn: (value: V, key: K, collection: this) => void): this;
|
||||||
|
each<T>(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this;
|
||||||
|
/**
|
||||||
|
* Runs a function on the collection and returns the collection.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute
|
||||||
|
* @param thisArg - Value to use as `this` when executing function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
tap(fn: (collection: this) => void): this;
|
||||||
|
tap<T>(fn: (this: T, collection: this) => void, thisArg: T): this;
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone(): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections: ReadonlyCollection<K, V>[]): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection: ReadonlyCollection<K, V>): boolean;
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction?: Comparator<K, V>): this;
|
||||||
|
/**
|
||||||
|
* The intersect method returns a new structure containing items where the keys and values are present in both original structures.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
intersect<T>(other: ReadonlyCollection<K, T>): Collection<K, T>;
|
||||||
|
/**
|
||||||
|
* The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
subtract<T>(other: ReadonlyCollection<K, T>): Collection<K, V>;
|
||||||
|
/**
|
||||||
|
* The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
difference<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V>;
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge<T, R>(other: ReadonlyCollection<K, T>, whenInSelf: (value: V, key: K) => Keep<R>, whenInOther: (valueOther: T, key: K) => Keep<R>, whenInBoth: (value: V, valueOther: T, key: K) => Keep<R>): Collection<K, R>;
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sorted(compareFunction?: Comparator<K, V>): Collection<K, V>;
|
||||||
|
toJSON(): V[];
|
||||||
|
private static defaultSort;
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries<K, V>(entries: Iterable<[K, V]>, combine: (firstValue: V, secondValue: V, key: K) => V): Collection<K, V>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Keep<V> = {
|
||||||
|
keep: false;
|
||||||
|
} | {
|
||||||
|
keep: true;
|
||||||
|
value: V;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Comparator<K, V> = (firstValue: V, secondValue: V, firstKey: K, secondKey: K) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
|
|
@ -0,0 +1,543 @@
|
||||||
|
"use strict";
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
var __export = (target, all) => {
|
||||||
|
for (var name in all)
|
||||||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||||||
|
};
|
||||||
|
var __copyProps = (to, from, except, desc) => {
|
||||||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||||||
|
for (let key of __getOwnPropNames(from))
|
||||||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
};
|
||||||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var src_exports = {};
|
||||||
|
__export(src_exports, {
|
||||||
|
Collection: () => Collection,
|
||||||
|
version: () => version
|
||||||
|
});
|
||||||
|
module.exports = __toCommonJS(src_exports);
|
||||||
|
|
||||||
|
// src/collection.ts
|
||||||
|
var Collection = class _Collection extends Map {
|
||||||
|
static {
|
||||||
|
__name(this, "Collection");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key, defaultValueGenerator) {
|
||||||
|
if (this.has(key))
|
||||||
|
return this.get(key);
|
||||||
|
if (typeof defaultValueGenerator !== "function")
|
||||||
|
throw new TypeError(`${defaultValueGenerator} is not a function`);
|
||||||
|
const defaultValue = defaultValueGenerator(key, this);
|
||||||
|
this.set(key, defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys) {
|
||||||
|
return keys.every((key) => super.has(key));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys) {
|
||||||
|
return keys.some((key) => super.has(key));
|
||||||
|
}
|
||||||
|
first(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.values().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.last(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.values();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
firstKey(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.keys().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.lastKey(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.keys();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
last(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.first(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
lastKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.firstKey(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.values()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
random(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
randomKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse() {
|
||||||
|
const entries = [...this.entries()].reverse();
|
||||||
|
this.clear();
|
||||||
|
for (const [key, value] of entries)
|
||||||
|
this.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
find(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
filter(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
results.set(key, val);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = [
|
||||||
|
new this.constructor[Symbol.species](),
|
||||||
|
new this.constructor[Symbol.species]()
|
||||||
|
];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
flatMap(fn, thisArg) {
|
||||||
|
const collections = this.map(fn, thisArg);
|
||||||
|
return new this.constructor[Symbol.species]().concat(...collections);
|
||||||
|
}
|
||||||
|
map(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const iter = this.entries();
|
||||||
|
return Array.from({ length: this.size }, () => {
|
||||||
|
const [key, value] = iter.next().value;
|
||||||
|
return fn(value, key, this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mapValues(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this)
|
||||||
|
coll.set(key, fn(val, key, this));
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
some(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
every(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (!fn(val, key, this))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
let accumulator;
|
||||||
|
const iterator = this.entries();
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (this.size === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
}
|
||||||
|
for (const [key, value] of iterator) {
|
||||||
|
accumulator = fn(accumulator, value, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
each(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
fn(value, key, this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
tap(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
fn(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone() {
|
||||||
|
return new this.constructor[Symbol.species](this);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections) {
|
||||||
|
const newColl = this.clone();
|
||||||
|
for (const coll of collections) {
|
||||||
|
for (const [key, val] of coll)
|
||||||
|
newColl.set(key, val);
|
||||||
|
}
|
||||||
|
return newColl;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection) {
|
||||||
|
if (!collection)
|
||||||
|
return false;
|
||||||
|
if (this === collection)
|
||||||
|
return true;
|
||||||
|
if (this.size !== collection.size)
|
||||||
|
return false;
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!collection.has(key) || value !== collection.get(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction = _Collection.defaultSort) {
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
||||||
|
super.clear();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
super.set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The intersect method returns a new structure containing items where the keys and values are present in both original structures.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
intersect(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (this.has(key) && Object.is(value, this.get(key))) {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
subtract(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key) || !Object.is(value, other.get(key))) {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
difference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!this.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge(other, whenInSelf, whenInOther, whenInBoth) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
|
||||||
|
for (const key of keys) {
|
||||||
|
const hasInSelf = this.has(key);
|
||||||
|
const hasInOther = other.has(key);
|
||||||
|
if (hasInSelf && hasInOther) {
|
||||||
|
const result = whenInBoth(this.get(key), other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInSelf) {
|
||||||
|
const result = whenInSelf(this.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInOther) {
|
||||||
|
const result = whenInOther(other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sorted(compareFunction = _Collection.defaultSort) {
|
||||||
|
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||||
|
}
|
||||||
|
toJSON() {
|
||||||
|
return [...this.values()];
|
||||||
|
}
|
||||||
|
static defaultSort(firstValue, secondValue) {
|
||||||
|
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries(entries, combine) {
|
||||||
|
const coll = new _Collection();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
if (coll.has(key)) {
|
||||||
|
coll.set(key, combine(coll.get(key), value, key));
|
||||||
|
} else {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var version = "1.5.3";
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
Collection,
|
||||||
|
version
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=index.js.map
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,517 @@
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
|
||||||
|
// src/collection.ts
|
||||||
|
var Collection = class _Collection extends Map {
|
||||||
|
static {
|
||||||
|
__name(this, "Collection");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key, defaultValueGenerator) {
|
||||||
|
if (this.has(key))
|
||||||
|
return this.get(key);
|
||||||
|
if (typeof defaultValueGenerator !== "function")
|
||||||
|
throw new TypeError(`${defaultValueGenerator} is not a function`);
|
||||||
|
const defaultValue = defaultValueGenerator(key, this);
|
||||||
|
this.set(key, defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys) {
|
||||||
|
return keys.every((key) => super.has(key));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys) {
|
||||||
|
return keys.some((key) => super.has(key));
|
||||||
|
}
|
||||||
|
first(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.values().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.last(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.values();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
firstKey(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.keys().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.lastKey(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.keys();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
last(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.first(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
lastKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.firstKey(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.values()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
random(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
randomKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse() {
|
||||||
|
const entries = [...this.entries()].reverse();
|
||||||
|
this.clear();
|
||||||
|
for (const [key, value] of entries)
|
||||||
|
this.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
find(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
filter(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
results.set(key, val);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = [
|
||||||
|
new this.constructor[Symbol.species](),
|
||||||
|
new this.constructor[Symbol.species]()
|
||||||
|
];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
flatMap(fn, thisArg) {
|
||||||
|
const collections = this.map(fn, thisArg);
|
||||||
|
return new this.constructor[Symbol.species]().concat(...collections);
|
||||||
|
}
|
||||||
|
map(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const iter = this.entries();
|
||||||
|
return Array.from({ length: this.size }, () => {
|
||||||
|
const [key, value] = iter.next().value;
|
||||||
|
return fn(value, key, this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mapValues(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this)
|
||||||
|
coll.set(key, fn(val, key, this));
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
some(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
every(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (!fn(val, key, this))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
let accumulator;
|
||||||
|
const iterator = this.entries();
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (this.size === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
}
|
||||||
|
for (const [key, value] of iterator) {
|
||||||
|
accumulator = fn(accumulator, value, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
each(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
fn(value, key, this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
tap(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
fn(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone() {
|
||||||
|
return new this.constructor[Symbol.species](this);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections) {
|
||||||
|
const newColl = this.clone();
|
||||||
|
for (const coll of collections) {
|
||||||
|
for (const [key, val] of coll)
|
||||||
|
newColl.set(key, val);
|
||||||
|
}
|
||||||
|
return newColl;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection) {
|
||||||
|
if (!collection)
|
||||||
|
return false;
|
||||||
|
if (this === collection)
|
||||||
|
return true;
|
||||||
|
if (this.size !== collection.size)
|
||||||
|
return false;
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!collection.has(key) || value !== collection.get(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction = _Collection.defaultSort) {
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
||||||
|
super.clear();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
super.set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The intersect method returns a new structure containing items where the keys and values are present in both original structures.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
intersect(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (this.has(key) && Object.is(value, this.get(key))) {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
subtract(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key) || !Object.is(value, other.get(key))) {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The difference method returns a new structure containing items where the key is present in one of the original structures but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
*/
|
||||||
|
difference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!this.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge(other, whenInSelf, whenInOther, whenInBoth) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
|
||||||
|
for (const key of keys) {
|
||||||
|
const hasInSelf = this.has(key);
|
||||||
|
const hasInOther = other.has(key);
|
||||||
|
if (hasInSelf && hasInOther) {
|
||||||
|
const result = whenInBoth(this.get(key), other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInSelf) {
|
||||||
|
const result = whenInSelf(this.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInOther) {
|
||||||
|
const result = whenInOther(other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sorted(compareFunction = _Collection.defaultSort) {
|
||||||
|
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||||
|
}
|
||||||
|
toJSON() {
|
||||||
|
return [...this.values()];
|
||||||
|
}
|
||||||
|
static defaultSort(firstValue, secondValue) {
|
||||||
|
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries(entries, combine) {
|
||||||
|
const coll = new _Collection();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
if (coll.has(key)) {
|
||||||
|
coll.set(key, combine(coll.get(key), value, key));
|
||||||
|
} else {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var version = "1.5.3";
|
||||||
|
export {
|
||||||
|
Collection,
|
||||||
|
version
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=index.mjs.map
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,76 @@
|
||||||
|
{
|
||||||
|
"name": "@discordjs/collection",
|
||||||
|
"version": "1.5.3",
|
||||||
|
"description": "Utility data structure used in discord.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint src __tests__ --ext .mjs,.js,.ts --format=pretty",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint src __tests__ --ext .mjs,.js,.ts --fix --format=pretty",
|
||||||
|
"fmt": "yarn format",
|
||||||
|
"docs": "yarn build:docs && api-extractor run --local && api-extractor run --local --config ./api-extractor-docs.json",
|
||||||
|
"prepack": "yarn lint && yarn test && yarn build",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/collection/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"exports": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"import": "./dist/index.mjs",
|
||||||
|
"require": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"map",
|
||||||
|
"collection",
|
||||||
|
"utility"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/collection"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.1.1",
|
||||||
|
"@microsoft/api-extractor": "^7.36.4",
|
||||||
|
"@types/node": "16.18.40",
|
||||||
|
"@vitest/coverage-v8": "^0.34.2",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"esbuild-plugin-version-injector": "^1.2.0",
|
||||||
|
"eslint": "^8.47.0",
|
||||||
|
"eslint-config-neon": "^0.1.47",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^2.8.8",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.12",
|
||||||
|
"typescript": "^5.1.6",
|
||||||
|
"vitest": "^0.34.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,191 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2021 Noel Buechler
|
||||||
|
Copyright 2021 Vlad Frangu
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,82 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/formatters"><img src="https://img.shields.io/npm/v/@discordjs/formatters.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/formatters"><img src="https://img.shields.io/npm/dt/@discordjs/formatters.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=formatters" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/formatters` is a collection of functions for formatting strings to be used on Discord.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 16.11.0 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/formatters
|
||||||
|
yarn add @discordjs/formatters
|
||||||
|
pnpm add @discordjs/formatters
|
||||||
|
bun add @discordjs/formatters
|
||||||
|
```
|
||||||
|
|
||||||
|
## Example usage
|
||||||
|
|
||||||
|
````ts
|
||||||
|
import { codeBlock } from '@discordjs/formatters';
|
||||||
|
|
||||||
|
const formattedCode = codeBlock('hello world!');
|
||||||
|
console.log(formattedCode);
|
||||||
|
|
||||||
|
// Prints:
|
||||||
|
// ```
|
||||||
|
// hello world!
|
||||||
|
// ```
|
||||||
|
````
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/formatters/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/formatters
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/formatters
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
|
@ -0,0 +1,586 @@
|
||||||
|
import { URL } from 'node:url';
|
||||||
|
import { Snowflake } from 'discord-api-types/globals';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options that affect what will be escaped.
|
||||||
|
*/
|
||||||
|
interface EscapeMarkdownOptions {
|
||||||
|
/**
|
||||||
|
* Whether to escape bold text.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
bold?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape bulleted lists.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
bulletedList?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape code blocks.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
codeBlock?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape text inside code blocks.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
codeBlockContent?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape `\`.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
escape?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape headings.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
heading?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape inline code.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
inlineCode?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape text inside inline code.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
inlineCodeContent?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape italics.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
italic?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape masked links.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
maskedLink?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape numbered lists.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
numberedList?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape spoilers.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
spoiler?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape strikethroughs.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
strikethrough?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape underlines.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
underline?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Escapes any Discord-flavored markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
* @param options - Options for escaping the markdown
|
||||||
|
*/
|
||||||
|
declare function escapeMarkdown(text: string, options?: EscapeMarkdownOptions): string;
|
||||||
|
/**
|
||||||
|
* Escapes code block markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeCodeBlock(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes inline code markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeInlineCode(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes italic markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeItalic(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes bold markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeBold(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes underline markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeUnderline(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes strikethrough markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeStrikethrough(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes spoiler markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeSpoiler(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes escape characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeEscape(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes heading characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeHeading(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes bulleted list characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeBulletedList(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes numbered list characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeNumberedList(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes masked link characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeMaskedLink(text: string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps the content inside a code block with no language.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function codeBlock<Content extends string>(content: Content): `\`\`\`\n${Content}\n\`\`\``;
|
||||||
|
/**
|
||||||
|
* Wraps the content inside a code block with the specified language.
|
||||||
|
*
|
||||||
|
* @typeParam Language - This is inferred by the supplied language
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param language - The language for the code block
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function codeBlock<Language extends string, Content extends string>(language: Language, content: Content): `\`\`\`${Language}\n${Content}\n\`\`\``;
|
||||||
|
/**
|
||||||
|
* Wraps the content inside \`backticks\` which formats it as inline code.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function inlineCode<Content extends string>(content: Content): `\`${Content}\``;
|
||||||
|
/**
|
||||||
|
* Formats the content into italic text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function italic<Content extends string>(content: Content): `_${Content}_`;
|
||||||
|
/**
|
||||||
|
* Formats the content into bold text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function bold<Content extends string>(content: Content): `**${Content}**`;
|
||||||
|
/**
|
||||||
|
* Formats the content into underscored text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function underscore<Content extends string>(content: Content): `__${Content}__`;
|
||||||
|
/**
|
||||||
|
* Formats the content into strike-through text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function strikethrough<Content extends string>(content: Content): `~~${Content}~~`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a quote.
|
||||||
|
*
|
||||||
|
* @remarks This needs to be at the start of the line for Discord to format it.
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function quote<Content extends string>(content: Content): `> ${Content}`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a block quote.
|
||||||
|
*
|
||||||
|
* @remarks This needs to be at the start of the line for Discord to format it.
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function blockQuote<Content extends string>(content: Content): `>>> ${Content}`;
|
||||||
|
/**
|
||||||
|
* Wraps the URL into `<>` which stops it from embedding.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param url - The URL to wrap
|
||||||
|
*/
|
||||||
|
declare function hideLinkEmbed<Content extends string>(url: Content): `<${Content}>`;
|
||||||
|
/**
|
||||||
|
* Wraps the URL into `<>` which stops it from embedding.
|
||||||
|
*
|
||||||
|
* @param url - The URL to wrap
|
||||||
|
*/
|
||||||
|
declare function hideLinkEmbed(url: URL): `<${string}>`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string>(content: Content, url: URL): `[${Content}](${string})`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @typeParam Url - This is inferred by the supplied URL
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string, Url extends string>(content: Content, url: Url): `[${Content}](${Url})`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL with a custom tooltip.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @typeParam Title - This is inferred by the supplied title
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
* @param title - The title shown when hovering on the masked link
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string, Title extends string>(content: Content, url: URL, title: Title): `[${Content}](${string} "${Title}")`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL with a custom tooltip.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @typeParam Url - This is inferred by the supplied URL
|
||||||
|
* @typeParam Title - This is inferred by the supplied title
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
* @param title - The title shown when hovering on the masked link
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string, Url extends string, Title extends string>(content: Content, url: Url, title: Title): `[${Content}](${Url} "${Title}")`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a spoiler.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function spoiler<Content extends string>(content: Content): `||${Content}||`;
|
||||||
|
/**
|
||||||
|
* Formats a user id into a user mention.
|
||||||
|
*
|
||||||
|
* @typeParam UserId - This is inferred by the supplied user id
|
||||||
|
* @param userId - The user id to format
|
||||||
|
*/
|
||||||
|
declare function userMention<UserId extends Snowflake>(userId: UserId): `<@${UserId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a channel id into a channel mention.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @param channelId - The channel id to format
|
||||||
|
*/
|
||||||
|
declare function channelMention<ChannelId extends Snowflake>(channelId: ChannelId): `<#${ChannelId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a role id into a role mention.
|
||||||
|
*
|
||||||
|
* @typeParam RoleId - This is inferred by the supplied role id
|
||||||
|
* @param roleId - The role id to format
|
||||||
|
*/
|
||||||
|
declare function roleMention<RoleId extends Snowflake>(roleId: RoleId): `<@&${RoleId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an application command name, subcommand group name, subcommand name, and id into an application command mention.
|
||||||
|
*
|
||||||
|
* @typeParam CommandName - This is inferred by the supplied command name
|
||||||
|
* @typeParam SubcommandGroupName - This is inferred by the supplied subcommand group name
|
||||||
|
* @typeParam SubcommandName - This is inferred by the supplied subcommand name
|
||||||
|
* @typeParam CommandId - This is inferred by the supplied command id
|
||||||
|
* @param commandName - The application command name to format
|
||||||
|
* @param subcommandGroupName - The subcommand group name to format
|
||||||
|
* @param subcommandName - The subcommand name to format
|
||||||
|
* @param commandId - The application command id to format
|
||||||
|
*/
|
||||||
|
declare function chatInputApplicationCommandMention<CommandName extends string, SubcommandGroupName extends string, SubcommandName extends string, CommandId extends Snowflake>(commandName: CommandName, subcommandGroupName: SubcommandGroupName, subcommandName: SubcommandName, commandId: CommandId): `</${CommandName} ${SubcommandGroupName} ${SubcommandName}:${CommandId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an application command name, subcommand name, and id into an application command mention.
|
||||||
|
*
|
||||||
|
* @typeParam CommandName - This is inferred by the supplied command name
|
||||||
|
* @typeParam SubcommandName - This is inferred by the supplied subcommand name
|
||||||
|
* @typeParam CommandId - This is inferred by the supplied command id
|
||||||
|
* @param commandName - The application command name to format
|
||||||
|
* @param subcommandName - The subcommand name to format
|
||||||
|
* @param commandId - The application command id to format
|
||||||
|
*/
|
||||||
|
declare function chatInputApplicationCommandMention<CommandName extends string, SubcommandName extends string, CommandId extends Snowflake>(commandName: CommandName, subcommandName: SubcommandName, commandId: CommandId): `</${CommandName} ${SubcommandName}:${CommandId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an application command name and id into an application command mention.
|
||||||
|
*
|
||||||
|
* @typeParam CommandName - This is inferred by the supplied command name
|
||||||
|
* @typeParam CommandId - This is inferred by the supplied command id
|
||||||
|
* @param commandName - The application command name to format
|
||||||
|
* @param commandId - The application command id to format
|
||||||
|
*/
|
||||||
|
declare function chatInputApplicationCommandMention<CommandName extends string, CommandId extends Snowflake>(commandName: CommandName, commandId: CommandId): `</${CommandName}:${CommandId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a non-animated emoji id into a fully qualified emoji identifier.
|
||||||
|
*
|
||||||
|
* @typeParam EmojiId - This is inferred by the supplied emoji id
|
||||||
|
* @param emojiId - The emoji id to format
|
||||||
|
*/
|
||||||
|
declare function formatEmoji<EmojiId extends Snowflake>(emojiId: EmojiId, animated?: false): `<:_:${EmojiId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an animated emoji id into a fully qualified emoji identifier.
|
||||||
|
*
|
||||||
|
* @typeParam EmojiId - This is inferred by the supplied emoji id
|
||||||
|
* @param emojiId - The emoji id to format
|
||||||
|
* @param animated - Whether the emoji is animated
|
||||||
|
*/
|
||||||
|
declare function formatEmoji<EmojiId extends Snowflake>(emojiId: EmojiId, animated?: true): `<a:_:${EmojiId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an emoji id into a fully qualified emoji identifier.
|
||||||
|
*
|
||||||
|
* @typeParam EmojiId - This is inferred by the supplied emoji id
|
||||||
|
* @param emojiId - The emoji id to format
|
||||||
|
* @param animated - Whether the emoji is animated
|
||||||
|
*/
|
||||||
|
declare function formatEmoji<EmojiId extends Snowflake>(emojiId: EmojiId, animated?: boolean): `<:_:${EmojiId}>` | `<a:_:${EmojiId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a channel link for a direct message channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
*/
|
||||||
|
declare function channelLink<ChannelId extends Snowflake>(channelId: ChannelId): `https://discord.com/channels/@me/${ChannelId}`;
|
||||||
|
/**
|
||||||
|
* Formats a channel link for a guild channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @typeParam GuildId - This is inferred by the supplied guild id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
* @param guildId - The guild's id
|
||||||
|
*/
|
||||||
|
declare function channelLink<ChannelId extends Snowflake, GuildId extends Snowflake>(channelId: ChannelId, guildId: GuildId): `https://discord.com/channels/${GuildId}/${ChannelId}`;
|
||||||
|
/**
|
||||||
|
* Formats a message link for a direct message channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @typeParam MessageId - This is inferred by the supplied message id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
* @param messageId - The message's id
|
||||||
|
*/
|
||||||
|
declare function messageLink<ChannelId extends Snowflake, MessageId extends Snowflake>(channelId: ChannelId, messageId: MessageId): `https://discord.com/channels/@me/${ChannelId}/${MessageId}`;
|
||||||
|
/**
|
||||||
|
* Formats a message link for a guild channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @typeParam MessageId - This is inferred by the supplied message id
|
||||||
|
* @typeParam GuildId - This is inferred by the supplied guild id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
* @param messageId - The message's id
|
||||||
|
* @param guildId - The guild's id
|
||||||
|
*/
|
||||||
|
declare function messageLink<ChannelId extends Snowflake, MessageId extends Snowflake, GuildId extends Snowflake>(channelId: ChannelId, messageId: MessageId, guildId: GuildId): `https://discord.com/channels/${GuildId}/${ChannelId}/${MessageId}`;
|
||||||
|
/**
|
||||||
|
* The heading levels for expanded markdown.
|
||||||
|
*/
|
||||||
|
declare enum HeadingLevel {
|
||||||
|
/**
|
||||||
|
* The first heading level.
|
||||||
|
*/
|
||||||
|
One = 1,
|
||||||
|
/**
|
||||||
|
* The second heading level.
|
||||||
|
*/
|
||||||
|
Two = 2,
|
||||||
|
/**
|
||||||
|
* The third heading level.
|
||||||
|
*/
|
||||||
|
Three = 3
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Formats the content into a heading level.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
* @param level - The heading level
|
||||||
|
*/
|
||||||
|
declare function heading<Content extends string>(content: Content, level?: HeadingLevel.One): `# ${Content}`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a heading level.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
* @param level - The heading level
|
||||||
|
*/
|
||||||
|
declare function heading<Content extends string>(content: Content, level: HeadingLevel.Two): `## ${Content}`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a heading level.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
* @param level - The heading level
|
||||||
|
*/
|
||||||
|
declare function heading<Content extends string>(content: Content, level: HeadingLevel.Three): `### ${Content}`;
|
||||||
|
/**
|
||||||
|
* A type that recursively traverses into arrays.
|
||||||
|
*/
|
||||||
|
type RecursiveArray<ItemType> = readonly (ItemType | RecursiveArray<ItemType>)[];
|
||||||
|
/**
|
||||||
|
* Formats the elements in the array to an ordered list.
|
||||||
|
*
|
||||||
|
* @param list - The array of elements to list
|
||||||
|
* @param startNumber - The starting number for the list
|
||||||
|
*/
|
||||||
|
declare function orderedList(list: RecursiveArray<string>, startNumber?: number): string;
|
||||||
|
/**
|
||||||
|
* Formats the elements in the array to an unordered list.
|
||||||
|
*
|
||||||
|
* @param list - The array of elements to list
|
||||||
|
*/
|
||||||
|
declare function unorderedList(list: RecursiveArray<string>): string;
|
||||||
|
/**
|
||||||
|
* Formats a date into a short date-time string.
|
||||||
|
*
|
||||||
|
* @param date - The date to format. Defaults to the current time
|
||||||
|
*/
|
||||||
|
declare function time(date?: Date): `<t:${bigint}>`;
|
||||||
|
/**
|
||||||
|
* Formats a date given a format style.
|
||||||
|
*
|
||||||
|
* @typeParam Style - This is inferred by the supplied {@link TimestampStylesString}
|
||||||
|
* @param date - The date to format
|
||||||
|
* @param style - The style to use
|
||||||
|
*/
|
||||||
|
declare function time<Style extends TimestampStylesString>(date: Date, style: Style): `<t:${bigint}:${Style}>`;
|
||||||
|
/**
|
||||||
|
* Formats the given timestamp into a short date-time string.
|
||||||
|
*
|
||||||
|
* @typeParam Seconds - This is inferred by the supplied timestamp
|
||||||
|
* @param seconds - A Unix timestamp in seconds
|
||||||
|
*/
|
||||||
|
declare function time<Seconds extends number>(seconds: Seconds): `<t:${Seconds}>`;
|
||||||
|
/**
|
||||||
|
* Formats the given timestamp into a short date-time string.
|
||||||
|
*
|
||||||
|
* @typeParam Seconds - This is inferred by the supplied timestamp
|
||||||
|
* @typeParam Style - This is inferred by the supplied {@link TimestampStylesString}
|
||||||
|
* @param seconds - A Unix timestamp in seconds
|
||||||
|
* @param style - The style to use
|
||||||
|
*/
|
||||||
|
declare function time<Seconds extends number, Style extends TimestampStylesString>(seconds: Seconds, style: Style): `<t:${Seconds}:${Style}>`;
|
||||||
|
/**
|
||||||
|
* The {@link https://discord.com/developers/docs/reference#message-formatting-timestamp-styles | message formatting timestamp styles}
|
||||||
|
* supported by Discord.
|
||||||
|
*/
|
||||||
|
declare const TimestampStyles: {
|
||||||
|
/**
|
||||||
|
* Short time format, consisting of hours and minutes.
|
||||||
|
*
|
||||||
|
* @example `16:20`
|
||||||
|
*/
|
||||||
|
readonly ShortTime: "t";
|
||||||
|
/**
|
||||||
|
* Long time format, consisting of hours, minutes, and seconds.
|
||||||
|
*
|
||||||
|
* @example `16:20:30`
|
||||||
|
*/
|
||||||
|
readonly LongTime: "T";
|
||||||
|
/**
|
||||||
|
* Short date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20/04/2021`
|
||||||
|
*/
|
||||||
|
readonly ShortDate: "d";
|
||||||
|
/**
|
||||||
|
* Long date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021`
|
||||||
|
*/
|
||||||
|
readonly LongDate: "D";
|
||||||
|
/**
|
||||||
|
* Short date-time format, consisting of short date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
readonly ShortDateTime: "f";
|
||||||
|
/**
|
||||||
|
* Long date-time format, consisting of long date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `Tuesday, 20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
readonly LongDateTime: "F";
|
||||||
|
/**
|
||||||
|
* Relative time format, consisting of a relative duration format.
|
||||||
|
*
|
||||||
|
* @example `2 months ago`
|
||||||
|
*/
|
||||||
|
readonly RelativeTime: "R";
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The possible {@link TimestampStyles} values.
|
||||||
|
*/
|
||||||
|
type TimestampStylesString = (typeof TimestampStyles)[keyof typeof TimestampStyles];
|
||||||
|
/**
|
||||||
|
* All the available faces from Discord's native slash commands.
|
||||||
|
*/
|
||||||
|
declare enum Faces {
|
||||||
|
/**
|
||||||
|
* `¯\_(ツ)_/¯`
|
||||||
|
*/
|
||||||
|
Shrug = "\u00AF_(\u30C4)_/\u00AF",
|
||||||
|
/**
|
||||||
|
* `(╯°□°)╯︵ ┻━┻`
|
||||||
|
*/
|
||||||
|
Tableflip = "(\u256F\u00B0\u25A1\u00B0)\u256F\uFE35 \u253B\u2501\u253B",
|
||||||
|
/**
|
||||||
|
* `┬─┬ノ( º _ ºノ)`
|
||||||
|
*/
|
||||||
|
Unflip = "\u252C\u2500\u252C\u30CE( \u00BA _ \u00BA\u30CE)"
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* All the available guild navigation mentions.
|
||||||
|
*/
|
||||||
|
declare enum GuildNavigationMentions {
|
||||||
|
/**
|
||||||
|
* Browse Channels tab.
|
||||||
|
*/
|
||||||
|
Browse = "<id:browse>",
|
||||||
|
/**
|
||||||
|
* Customize tab with the server's {@link https://discord.com/developers/docs/resources/guild#guild-onboarding-object | onboarding prompts}.
|
||||||
|
*/
|
||||||
|
Customize = "<id:customize>",
|
||||||
|
/**
|
||||||
|
* {@link https://support.discord.com/hc/articles/13497665141655 | Server Guide} tab.
|
||||||
|
*/
|
||||||
|
Guide = "<id:guide>"
|
||||||
|
}
|
||||||
|
|
||||||
|
export { EscapeMarkdownOptions, Faces, GuildNavigationMentions, HeadingLevel, RecursiveArray, TimestampStyles, TimestampStylesString, blockQuote, bold, channelLink, channelMention, chatInputApplicationCommandMention, codeBlock, escapeBold, escapeBulletedList, escapeCodeBlock, escapeEscape, escapeHeading, escapeInlineCode, escapeItalic, escapeMarkdown, escapeMaskedLink, escapeNumberedList, escapeSpoiler, escapeStrikethrough, escapeUnderline, formatEmoji, heading, hideLinkEmbed, hyperlink, inlineCode, italic, messageLink, orderedList, quote, roleMention, spoiler, strikethrough, time, underscore, unorderedList, userMention };
|
|
@ -0,0 +1,586 @@
|
||||||
|
import { URL } from 'node:url';
|
||||||
|
import { Snowflake } from 'discord-api-types/globals';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options that affect what will be escaped.
|
||||||
|
*/
|
||||||
|
interface EscapeMarkdownOptions {
|
||||||
|
/**
|
||||||
|
* Whether to escape bold text.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
bold?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape bulleted lists.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
bulletedList?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape code blocks.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
codeBlock?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape text inside code blocks.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
codeBlockContent?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape `\`.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
escape?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape headings.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
heading?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape inline code.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
inlineCode?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape text inside inline code.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
inlineCodeContent?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape italics.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
italic?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape masked links.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
maskedLink?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape numbered lists.
|
||||||
|
*
|
||||||
|
* @defaultValue `false`
|
||||||
|
*/
|
||||||
|
numberedList?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape spoilers.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
spoiler?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape strikethroughs.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
strikethrough?: boolean;
|
||||||
|
/**
|
||||||
|
* Whether to escape underlines.
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
underline?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Escapes any Discord-flavored markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
* @param options - Options for escaping the markdown
|
||||||
|
*/
|
||||||
|
declare function escapeMarkdown(text: string, options?: EscapeMarkdownOptions): string;
|
||||||
|
/**
|
||||||
|
* Escapes code block markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeCodeBlock(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes inline code markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeInlineCode(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes italic markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeItalic(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes bold markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeBold(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes underline markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeUnderline(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes strikethrough markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeStrikethrough(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes spoiler markdown in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeSpoiler(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes escape characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeEscape(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes heading characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeHeading(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes bulleted list characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeBulletedList(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes numbered list characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeNumberedList(text: string): string;
|
||||||
|
/**
|
||||||
|
* Escapes masked link characters in a string.
|
||||||
|
*
|
||||||
|
* @param text - Content to escape
|
||||||
|
*/
|
||||||
|
declare function escapeMaskedLink(text: string): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wraps the content inside a code block with no language.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function codeBlock<Content extends string>(content: Content): `\`\`\`\n${Content}\n\`\`\``;
|
||||||
|
/**
|
||||||
|
* Wraps the content inside a code block with the specified language.
|
||||||
|
*
|
||||||
|
* @typeParam Language - This is inferred by the supplied language
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param language - The language for the code block
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function codeBlock<Language extends string, Content extends string>(language: Language, content: Content): `\`\`\`${Language}\n${Content}\n\`\`\``;
|
||||||
|
/**
|
||||||
|
* Wraps the content inside \`backticks\` which formats it as inline code.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function inlineCode<Content extends string>(content: Content): `\`${Content}\``;
|
||||||
|
/**
|
||||||
|
* Formats the content into italic text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function italic<Content extends string>(content: Content): `_${Content}_`;
|
||||||
|
/**
|
||||||
|
* Formats the content into bold text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function bold<Content extends string>(content: Content): `**${Content}**`;
|
||||||
|
/**
|
||||||
|
* Formats the content into underscored text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function underscore<Content extends string>(content: Content): `__${Content}__`;
|
||||||
|
/**
|
||||||
|
* Formats the content into strike-through text.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function strikethrough<Content extends string>(content: Content): `~~${Content}~~`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a quote.
|
||||||
|
*
|
||||||
|
* @remarks This needs to be at the start of the line for Discord to format it.
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function quote<Content extends string>(content: Content): `> ${Content}`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a block quote.
|
||||||
|
*
|
||||||
|
* @remarks This needs to be at the start of the line for Discord to format it.
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function blockQuote<Content extends string>(content: Content): `>>> ${Content}`;
|
||||||
|
/**
|
||||||
|
* Wraps the URL into `<>` which stops it from embedding.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param url - The URL to wrap
|
||||||
|
*/
|
||||||
|
declare function hideLinkEmbed<Content extends string>(url: Content): `<${Content}>`;
|
||||||
|
/**
|
||||||
|
* Wraps the URL into `<>` which stops it from embedding.
|
||||||
|
*
|
||||||
|
* @param url - The URL to wrap
|
||||||
|
*/
|
||||||
|
declare function hideLinkEmbed(url: URL): `<${string}>`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string>(content: Content, url: URL): `[${Content}](${string})`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @typeParam Url - This is inferred by the supplied URL
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string, Url extends string>(content: Content, url: Url): `[${Content}](${Url})`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL with a custom tooltip.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @typeParam Title - This is inferred by the supplied title
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
* @param title - The title shown when hovering on the masked link
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string, Title extends string>(content: Content, url: URL, title: Title): `[${Content}](${string} "${Title}")`;
|
||||||
|
/**
|
||||||
|
* Formats the content and the URL into a masked URL with a custom tooltip.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @typeParam Url - This is inferred by the supplied URL
|
||||||
|
* @typeParam Title - This is inferred by the supplied title
|
||||||
|
* @param content - The content to display
|
||||||
|
* @param url - The URL the content links to
|
||||||
|
* @param title - The title shown when hovering on the masked link
|
||||||
|
*/
|
||||||
|
declare function hyperlink<Content extends string, Url extends string, Title extends string>(content: Content, url: Url, title: Title): `[${Content}](${Url} "${Title}")`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a spoiler.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
*/
|
||||||
|
declare function spoiler<Content extends string>(content: Content): `||${Content}||`;
|
||||||
|
/**
|
||||||
|
* Formats a user id into a user mention.
|
||||||
|
*
|
||||||
|
* @typeParam UserId - This is inferred by the supplied user id
|
||||||
|
* @param userId - The user id to format
|
||||||
|
*/
|
||||||
|
declare function userMention<UserId extends Snowflake>(userId: UserId): `<@${UserId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a channel id into a channel mention.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @param channelId - The channel id to format
|
||||||
|
*/
|
||||||
|
declare function channelMention<ChannelId extends Snowflake>(channelId: ChannelId): `<#${ChannelId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a role id into a role mention.
|
||||||
|
*
|
||||||
|
* @typeParam RoleId - This is inferred by the supplied role id
|
||||||
|
* @param roleId - The role id to format
|
||||||
|
*/
|
||||||
|
declare function roleMention<RoleId extends Snowflake>(roleId: RoleId): `<@&${RoleId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an application command name, subcommand group name, subcommand name, and id into an application command mention.
|
||||||
|
*
|
||||||
|
* @typeParam CommandName - This is inferred by the supplied command name
|
||||||
|
* @typeParam SubcommandGroupName - This is inferred by the supplied subcommand group name
|
||||||
|
* @typeParam SubcommandName - This is inferred by the supplied subcommand name
|
||||||
|
* @typeParam CommandId - This is inferred by the supplied command id
|
||||||
|
* @param commandName - The application command name to format
|
||||||
|
* @param subcommandGroupName - The subcommand group name to format
|
||||||
|
* @param subcommandName - The subcommand name to format
|
||||||
|
* @param commandId - The application command id to format
|
||||||
|
*/
|
||||||
|
declare function chatInputApplicationCommandMention<CommandName extends string, SubcommandGroupName extends string, SubcommandName extends string, CommandId extends Snowflake>(commandName: CommandName, subcommandGroupName: SubcommandGroupName, subcommandName: SubcommandName, commandId: CommandId): `</${CommandName} ${SubcommandGroupName} ${SubcommandName}:${CommandId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an application command name, subcommand name, and id into an application command mention.
|
||||||
|
*
|
||||||
|
* @typeParam CommandName - This is inferred by the supplied command name
|
||||||
|
* @typeParam SubcommandName - This is inferred by the supplied subcommand name
|
||||||
|
* @typeParam CommandId - This is inferred by the supplied command id
|
||||||
|
* @param commandName - The application command name to format
|
||||||
|
* @param subcommandName - The subcommand name to format
|
||||||
|
* @param commandId - The application command id to format
|
||||||
|
*/
|
||||||
|
declare function chatInputApplicationCommandMention<CommandName extends string, SubcommandName extends string, CommandId extends Snowflake>(commandName: CommandName, subcommandName: SubcommandName, commandId: CommandId): `</${CommandName} ${SubcommandName}:${CommandId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an application command name and id into an application command mention.
|
||||||
|
*
|
||||||
|
* @typeParam CommandName - This is inferred by the supplied command name
|
||||||
|
* @typeParam CommandId - This is inferred by the supplied command id
|
||||||
|
* @param commandName - The application command name to format
|
||||||
|
* @param commandId - The application command id to format
|
||||||
|
*/
|
||||||
|
declare function chatInputApplicationCommandMention<CommandName extends string, CommandId extends Snowflake>(commandName: CommandName, commandId: CommandId): `</${CommandName}:${CommandId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a non-animated emoji id into a fully qualified emoji identifier.
|
||||||
|
*
|
||||||
|
* @typeParam EmojiId - This is inferred by the supplied emoji id
|
||||||
|
* @param emojiId - The emoji id to format
|
||||||
|
*/
|
||||||
|
declare function formatEmoji<EmojiId extends Snowflake>(emojiId: EmojiId, animated?: false): `<:_:${EmojiId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an animated emoji id into a fully qualified emoji identifier.
|
||||||
|
*
|
||||||
|
* @typeParam EmojiId - This is inferred by the supplied emoji id
|
||||||
|
* @param emojiId - The emoji id to format
|
||||||
|
* @param animated - Whether the emoji is animated
|
||||||
|
*/
|
||||||
|
declare function formatEmoji<EmojiId extends Snowflake>(emojiId: EmojiId, animated?: true): `<a:_:${EmojiId}>`;
|
||||||
|
/**
|
||||||
|
* Formats an emoji id into a fully qualified emoji identifier.
|
||||||
|
*
|
||||||
|
* @typeParam EmojiId - This is inferred by the supplied emoji id
|
||||||
|
* @param emojiId - The emoji id to format
|
||||||
|
* @param animated - Whether the emoji is animated
|
||||||
|
*/
|
||||||
|
declare function formatEmoji<EmojiId extends Snowflake>(emojiId: EmojiId, animated?: boolean): `<:_:${EmojiId}>` | `<a:_:${EmojiId}>`;
|
||||||
|
/**
|
||||||
|
* Formats a channel link for a direct message channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
*/
|
||||||
|
declare function channelLink<ChannelId extends Snowflake>(channelId: ChannelId): `https://discord.com/channels/@me/${ChannelId}`;
|
||||||
|
/**
|
||||||
|
* Formats a channel link for a guild channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @typeParam GuildId - This is inferred by the supplied guild id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
* @param guildId - The guild's id
|
||||||
|
*/
|
||||||
|
declare function channelLink<ChannelId extends Snowflake, GuildId extends Snowflake>(channelId: ChannelId, guildId: GuildId): `https://discord.com/channels/${GuildId}/${ChannelId}`;
|
||||||
|
/**
|
||||||
|
* Formats a message link for a direct message channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @typeParam MessageId - This is inferred by the supplied message id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
* @param messageId - The message's id
|
||||||
|
*/
|
||||||
|
declare function messageLink<ChannelId extends Snowflake, MessageId extends Snowflake>(channelId: ChannelId, messageId: MessageId): `https://discord.com/channels/@me/${ChannelId}/${MessageId}`;
|
||||||
|
/**
|
||||||
|
* Formats a message link for a guild channel.
|
||||||
|
*
|
||||||
|
* @typeParam ChannelId - This is inferred by the supplied channel id
|
||||||
|
* @typeParam MessageId - This is inferred by the supplied message id
|
||||||
|
* @typeParam GuildId - This is inferred by the supplied guild id
|
||||||
|
* @param channelId - The channel's id
|
||||||
|
* @param messageId - The message's id
|
||||||
|
* @param guildId - The guild's id
|
||||||
|
*/
|
||||||
|
declare function messageLink<ChannelId extends Snowflake, MessageId extends Snowflake, GuildId extends Snowflake>(channelId: ChannelId, messageId: MessageId, guildId: GuildId): `https://discord.com/channels/${GuildId}/${ChannelId}/${MessageId}`;
|
||||||
|
/**
|
||||||
|
* The heading levels for expanded markdown.
|
||||||
|
*/
|
||||||
|
declare enum HeadingLevel {
|
||||||
|
/**
|
||||||
|
* The first heading level.
|
||||||
|
*/
|
||||||
|
One = 1,
|
||||||
|
/**
|
||||||
|
* The second heading level.
|
||||||
|
*/
|
||||||
|
Two = 2,
|
||||||
|
/**
|
||||||
|
* The third heading level.
|
||||||
|
*/
|
||||||
|
Three = 3
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Formats the content into a heading level.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
* @param level - The heading level
|
||||||
|
*/
|
||||||
|
declare function heading<Content extends string>(content: Content, level?: HeadingLevel.One): `# ${Content}`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a heading level.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
* @param level - The heading level
|
||||||
|
*/
|
||||||
|
declare function heading<Content extends string>(content: Content, level: HeadingLevel.Two): `## ${Content}`;
|
||||||
|
/**
|
||||||
|
* Formats the content into a heading level.
|
||||||
|
*
|
||||||
|
* @typeParam Content - This is inferred by the supplied content
|
||||||
|
* @param content - The content to wrap
|
||||||
|
* @param level - The heading level
|
||||||
|
*/
|
||||||
|
declare function heading<Content extends string>(content: Content, level: HeadingLevel.Three): `### ${Content}`;
|
||||||
|
/**
|
||||||
|
* A type that recursively traverses into arrays.
|
||||||
|
*/
|
||||||
|
type RecursiveArray<ItemType> = readonly (ItemType | RecursiveArray<ItemType>)[];
|
||||||
|
/**
|
||||||
|
* Formats the elements in the array to an ordered list.
|
||||||
|
*
|
||||||
|
* @param list - The array of elements to list
|
||||||
|
* @param startNumber - The starting number for the list
|
||||||
|
*/
|
||||||
|
declare function orderedList(list: RecursiveArray<string>, startNumber?: number): string;
|
||||||
|
/**
|
||||||
|
* Formats the elements in the array to an unordered list.
|
||||||
|
*
|
||||||
|
* @param list - The array of elements to list
|
||||||
|
*/
|
||||||
|
declare function unorderedList(list: RecursiveArray<string>): string;
|
||||||
|
/**
|
||||||
|
* Formats a date into a short date-time string.
|
||||||
|
*
|
||||||
|
* @param date - The date to format. Defaults to the current time
|
||||||
|
*/
|
||||||
|
declare function time(date?: Date): `<t:${bigint}>`;
|
||||||
|
/**
|
||||||
|
* Formats a date given a format style.
|
||||||
|
*
|
||||||
|
* @typeParam Style - This is inferred by the supplied {@link TimestampStylesString}
|
||||||
|
* @param date - The date to format
|
||||||
|
* @param style - The style to use
|
||||||
|
*/
|
||||||
|
declare function time<Style extends TimestampStylesString>(date: Date, style: Style): `<t:${bigint}:${Style}>`;
|
||||||
|
/**
|
||||||
|
* Formats the given timestamp into a short date-time string.
|
||||||
|
*
|
||||||
|
* @typeParam Seconds - This is inferred by the supplied timestamp
|
||||||
|
* @param seconds - A Unix timestamp in seconds
|
||||||
|
*/
|
||||||
|
declare function time<Seconds extends number>(seconds: Seconds): `<t:${Seconds}>`;
|
||||||
|
/**
|
||||||
|
* Formats the given timestamp into a short date-time string.
|
||||||
|
*
|
||||||
|
* @typeParam Seconds - This is inferred by the supplied timestamp
|
||||||
|
* @typeParam Style - This is inferred by the supplied {@link TimestampStylesString}
|
||||||
|
* @param seconds - A Unix timestamp in seconds
|
||||||
|
* @param style - The style to use
|
||||||
|
*/
|
||||||
|
declare function time<Seconds extends number, Style extends TimestampStylesString>(seconds: Seconds, style: Style): `<t:${Seconds}:${Style}>`;
|
||||||
|
/**
|
||||||
|
* The {@link https://discord.com/developers/docs/reference#message-formatting-timestamp-styles | message formatting timestamp styles}
|
||||||
|
* supported by Discord.
|
||||||
|
*/
|
||||||
|
declare const TimestampStyles: {
|
||||||
|
/**
|
||||||
|
* Short time format, consisting of hours and minutes.
|
||||||
|
*
|
||||||
|
* @example `16:20`
|
||||||
|
*/
|
||||||
|
readonly ShortTime: "t";
|
||||||
|
/**
|
||||||
|
* Long time format, consisting of hours, minutes, and seconds.
|
||||||
|
*
|
||||||
|
* @example `16:20:30`
|
||||||
|
*/
|
||||||
|
readonly LongTime: "T";
|
||||||
|
/**
|
||||||
|
* Short date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20/04/2021`
|
||||||
|
*/
|
||||||
|
readonly ShortDate: "d";
|
||||||
|
/**
|
||||||
|
* Long date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021`
|
||||||
|
*/
|
||||||
|
readonly LongDate: "D";
|
||||||
|
/**
|
||||||
|
* Short date-time format, consisting of short date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
readonly ShortDateTime: "f";
|
||||||
|
/**
|
||||||
|
* Long date-time format, consisting of long date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `Tuesday, 20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
readonly LongDateTime: "F";
|
||||||
|
/**
|
||||||
|
* Relative time format, consisting of a relative duration format.
|
||||||
|
*
|
||||||
|
* @example `2 months ago`
|
||||||
|
*/
|
||||||
|
readonly RelativeTime: "R";
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The possible {@link TimestampStyles} values.
|
||||||
|
*/
|
||||||
|
type TimestampStylesString = (typeof TimestampStyles)[keyof typeof TimestampStyles];
|
||||||
|
/**
|
||||||
|
* All the available faces from Discord's native slash commands.
|
||||||
|
*/
|
||||||
|
declare enum Faces {
|
||||||
|
/**
|
||||||
|
* `¯\_(ツ)_/¯`
|
||||||
|
*/
|
||||||
|
Shrug = "\u00AF_(\u30C4)_/\u00AF",
|
||||||
|
/**
|
||||||
|
* `(╯°□°)╯︵ ┻━┻`
|
||||||
|
*/
|
||||||
|
Tableflip = "(\u256F\u00B0\u25A1\u00B0)\u256F\uFE35 \u253B\u2501\u253B",
|
||||||
|
/**
|
||||||
|
* `┬─┬ノ( º _ ºノ)`
|
||||||
|
*/
|
||||||
|
Unflip = "\u252C\u2500\u252C\u30CE( \u00BA _ \u00BA\u30CE)"
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* All the available guild navigation mentions.
|
||||||
|
*/
|
||||||
|
declare enum GuildNavigationMentions {
|
||||||
|
/**
|
||||||
|
* Browse Channels tab.
|
||||||
|
*/
|
||||||
|
Browse = "<id:browse>",
|
||||||
|
/**
|
||||||
|
* Customize tab with the server's {@link https://discord.com/developers/docs/resources/guild#guild-onboarding-object | onboarding prompts}.
|
||||||
|
*/
|
||||||
|
Customize = "<id:customize>",
|
||||||
|
/**
|
||||||
|
* {@link https://support.discord.com/hc/articles/13497665141655 | Server Guide} tab.
|
||||||
|
*/
|
||||||
|
Guide = "<id:guide>"
|
||||||
|
}
|
||||||
|
|
||||||
|
export { EscapeMarkdownOptions, Faces, GuildNavigationMentions, HeadingLevel, RecursiveArray, TimestampStyles, TimestampStylesString, blockQuote, bold, channelLink, channelMention, chatInputApplicationCommandMention, codeBlock, escapeBold, escapeBulletedList, escapeCodeBlock, escapeEscape, escapeHeading, escapeInlineCode, escapeItalic, escapeMarkdown, escapeMaskedLink, escapeNumberedList, escapeSpoiler, escapeStrikethrough, escapeUnderline, formatEmoji, heading, hideLinkEmbed, hyperlink, inlineCode, italic, messageLink, orderedList, quote, roleMention, spoiler, strikethrough, time, underscore, unorderedList, userMention };
|
|
@ -0,0 +1,441 @@
|
||||||
|
"use strict";
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
var __export = (target, all) => {
|
||||||
|
for (var name in all)
|
||||||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||||||
|
};
|
||||||
|
var __copyProps = (to, from, except, desc) => {
|
||||||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||||||
|
for (let key of __getOwnPropNames(from))
|
||||||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
};
|
||||||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var src_exports = {};
|
||||||
|
__export(src_exports, {
|
||||||
|
Faces: () => Faces,
|
||||||
|
GuildNavigationMentions: () => GuildNavigationMentions,
|
||||||
|
HeadingLevel: () => HeadingLevel,
|
||||||
|
TimestampStyles: () => TimestampStyles,
|
||||||
|
blockQuote: () => blockQuote,
|
||||||
|
bold: () => bold,
|
||||||
|
channelLink: () => channelLink,
|
||||||
|
channelMention: () => channelMention,
|
||||||
|
chatInputApplicationCommandMention: () => chatInputApplicationCommandMention,
|
||||||
|
codeBlock: () => codeBlock,
|
||||||
|
escapeBold: () => escapeBold,
|
||||||
|
escapeBulletedList: () => escapeBulletedList,
|
||||||
|
escapeCodeBlock: () => escapeCodeBlock,
|
||||||
|
escapeEscape: () => escapeEscape,
|
||||||
|
escapeHeading: () => escapeHeading,
|
||||||
|
escapeInlineCode: () => escapeInlineCode,
|
||||||
|
escapeItalic: () => escapeItalic,
|
||||||
|
escapeMarkdown: () => escapeMarkdown,
|
||||||
|
escapeMaskedLink: () => escapeMaskedLink,
|
||||||
|
escapeNumberedList: () => escapeNumberedList,
|
||||||
|
escapeSpoiler: () => escapeSpoiler,
|
||||||
|
escapeStrikethrough: () => escapeStrikethrough,
|
||||||
|
escapeUnderline: () => escapeUnderline,
|
||||||
|
formatEmoji: () => formatEmoji,
|
||||||
|
heading: () => heading,
|
||||||
|
hideLinkEmbed: () => hideLinkEmbed,
|
||||||
|
hyperlink: () => hyperlink,
|
||||||
|
inlineCode: () => inlineCode,
|
||||||
|
italic: () => italic,
|
||||||
|
messageLink: () => messageLink,
|
||||||
|
orderedList: () => orderedList,
|
||||||
|
quote: () => quote,
|
||||||
|
roleMention: () => roleMention,
|
||||||
|
spoiler: () => spoiler,
|
||||||
|
strikethrough: () => strikethrough,
|
||||||
|
time: () => time,
|
||||||
|
underscore: () => underscore,
|
||||||
|
unorderedList: () => unorderedList,
|
||||||
|
userMention: () => userMention
|
||||||
|
});
|
||||||
|
module.exports = __toCommonJS(src_exports);
|
||||||
|
|
||||||
|
// src/escapers.ts
|
||||||
|
function escapeMarkdown(text, options = {}) {
|
||||||
|
const {
|
||||||
|
codeBlock: codeBlock2 = true,
|
||||||
|
inlineCode: inlineCode2 = true,
|
||||||
|
bold: bold2 = true,
|
||||||
|
italic: italic2 = true,
|
||||||
|
underline = true,
|
||||||
|
strikethrough: strikethrough2 = true,
|
||||||
|
spoiler: spoiler2 = true,
|
||||||
|
codeBlockContent = true,
|
||||||
|
inlineCodeContent = true,
|
||||||
|
escape = true,
|
||||||
|
heading: heading2 = false,
|
||||||
|
bulletedList = false,
|
||||||
|
numberedList = false,
|
||||||
|
maskedLink = false
|
||||||
|
} = options;
|
||||||
|
if (!codeBlockContent) {
|
||||||
|
return text.split("```").map((subString, index, array) => {
|
||||||
|
if (index % 2 && index !== array.length - 1)
|
||||||
|
return subString;
|
||||||
|
return escapeMarkdown(subString, {
|
||||||
|
inlineCode: inlineCode2,
|
||||||
|
bold: bold2,
|
||||||
|
italic: italic2,
|
||||||
|
underline,
|
||||||
|
strikethrough: strikethrough2,
|
||||||
|
spoiler: spoiler2,
|
||||||
|
inlineCodeContent,
|
||||||
|
escape,
|
||||||
|
heading: heading2,
|
||||||
|
bulletedList,
|
||||||
|
numberedList,
|
||||||
|
maskedLink
|
||||||
|
});
|
||||||
|
}).join(codeBlock2 ? "\\`\\`\\`" : "```");
|
||||||
|
}
|
||||||
|
if (!inlineCodeContent) {
|
||||||
|
return text.split(/(?<=^|[^`])`(?=[^`]|$)/g).map((subString, index, array) => {
|
||||||
|
if (index % 2 && index !== array.length - 1)
|
||||||
|
return subString;
|
||||||
|
return escapeMarkdown(subString, {
|
||||||
|
codeBlock: codeBlock2,
|
||||||
|
bold: bold2,
|
||||||
|
italic: italic2,
|
||||||
|
underline,
|
||||||
|
strikethrough: strikethrough2,
|
||||||
|
spoiler: spoiler2,
|
||||||
|
escape,
|
||||||
|
heading: heading2,
|
||||||
|
bulletedList,
|
||||||
|
numberedList,
|
||||||
|
maskedLink
|
||||||
|
});
|
||||||
|
}).join(inlineCode2 ? "\\`" : "`");
|
||||||
|
}
|
||||||
|
let res = text;
|
||||||
|
if (escape)
|
||||||
|
res = escapeEscape(res);
|
||||||
|
if (inlineCode2)
|
||||||
|
res = escapeInlineCode(res);
|
||||||
|
if (codeBlock2)
|
||||||
|
res = escapeCodeBlock(res);
|
||||||
|
if (italic2)
|
||||||
|
res = escapeItalic(res);
|
||||||
|
if (bold2)
|
||||||
|
res = escapeBold(res);
|
||||||
|
if (underline)
|
||||||
|
res = escapeUnderline(res);
|
||||||
|
if (strikethrough2)
|
||||||
|
res = escapeStrikethrough(res);
|
||||||
|
if (spoiler2)
|
||||||
|
res = escapeSpoiler(res);
|
||||||
|
if (heading2)
|
||||||
|
res = escapeHeading(res);
|
||||||
|
if (bulletedList)
|
||||||
|
res = escapeBulletedList(res);
|
||||||
|
if (numberedList)
|
||||||
|
res = escapeNumberedList(res);
|
||||||
|
if (maskedLink)
|
||||||
|
res = escapeMaskedLink(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
__name(escapeMarkdown, "escapeMarkdown");
|
||||||
|
function escapeCodeBlock(text) {
|
||||||
|
return text.replaceAll("```", "\\`\\`\\`");
|
||||||
|
}
|
||||||
|
__name(escapeCodeBlock, "escapeCodeBlock");
|
||||||
|
function escapeInlineCode(text) {
|
||||||
|
return text.replaceAll(/(?<=^|[^`])``?(?=[^`]|$)/g, (match) => match.length === 2 ? "\\`\\`" : "\\`");
|
||||||
|
}
|
||||||
|
__name(escapeInlineCode, "escapeInlineCode");
|
||||||
|
function escapeItalic(text) {
|
||||||
|
let idx = 0;
|
||||||
|
const newText = text.replaceAll(/(?<=^|[^*])\*([^*]|\*\*|$)/g, (_, match) => {
|
||||||
|
if (match === "**")
|
||||||
|
return ++idx % 2 ? `\\*${match}` : `${match}\\*`;
|
||||||
|
return `\\*${match}`;
|
||||||
|
});
|
||||||
|
idx = 0;
|
||||||
|
return newText.replaceAll(/(?<=^|[^_])(?<!<a?:.+)_(?!:\d+>)([^_]|__|$)/g, (_, match) => {
|
||||||
|
if (match === "__")
|
||||||
|
return ++idx % 2 ? `\\_${match}` : `${match}\\_`;
|
||||||
|
return `\\_${match}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
__name(escapeItalic, "escapeItalic");
|
||||||
|
function escapeBold(text) {
|
||||||
|
let idx = 0;
|
||||||
|
return text.replaceAll(/\*\*(\*)?/g, (_, match) => {
|
||||||
|
if (match)
|
||||||
|
return ++idx % 2 ? `${match}\\*\\*` : `\\*\\*${match}`;
|
||||||
|
return "\\*\\*";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
__name(escapeBold, "escapeBold");
|
||||||
|
function escapeUnderline(text) {
|
||||||
|
let idx = 0;
|
||||||
|
return text.replaceAll(/(?<!<a?:.+)__(_)?(?!:\d+>)/g, (_, match) => {
|
||||||
|
if (match)
|
||||||
|
return ++idx % 2 ? `${match}\\_\\_` : `\\_\\_${match}`;
|
||||||
|
return "\\_\\_";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
__name(escapeUnderline, "escapeUnderline");
|
||||||
|
function escapeStrikethrough(text) {
|
||||||
|
return text.replaceAll("~~", "\\~\\~");
|
||||||
|
}
|
||||||
|
__name(escapeStrikethrough, "escapeStrikethrough");
|
||||||
|
function escapeSpoiler(text) {
|
||||||
|
return text.replaceAll("||", "\\|\\|");
|
||||||
|
}
|
||||||
|
__name(escapeSpoiler, "escapeSpoiler");
|
||||||
|
function escapeEscape(text) {
|
||||||
|
return text.replaceAll("\\", "\\\\");
|
||||||
|
}
|
||||||
|
__name(escapeEscape, "escapeEscape");
|
||||||
|
function escapeHeading(text) {
|
||||||
|
return text.replaceAll(/^( {0,2})([*-] )?( *)(#{1,3} )/gm, "$1$2$3\\$4");
|
||||||
|
}
|
||||||
|
__name(escapeHeading, "escapeHeading");
|
||||||
|
function escapeBulletedList(text) {
|
||||||
|
return text.replaceAll(/^( *)([*-])( +)/gm, "$1\\$2$3");
|
||||||
|
}
|
||||||
|
__name(escapeBulletedList, "escapeBulletedList");
|
||||||
|
function escapeNumberedList(text) {
|
||||||
|
return text.replaceAll(/^( *\d+)\./gm, "$1\\.");
|
||||||
|
}
|
||||||
|
__name(escapeNumberedList, "escapeNumberedList");
|
||||||
|
function escapeMaskedLink(text) {
|
||||||
|
return text.replaceAll(/\[.+]\(.+\)/gm, "\\$&");
|
||||||
|
}
|
||||||
|
__name(escapeMaskedLink, "escapeMaskedLink");
|
||||||
|
|
||||||
|
// src/formatters.ts
|
||||||
|
function codeBlock(language, content) {
|
||||||
|
return content === void 0 ? `\`\`\`
|
||||||
|
${language}
|
||||||
|
\`\`\`` : `\`\`\`${language}
|
||||||
|
${content}
|
||||||
|
\`\`\``;
|
||||||
|
}
|
||||||
|
__name(codeBlock, "codeBlock");
|
||||||
|
function inlineCode(content) {
|
||||||
|
return `\`${content}\``;
|
||||||
|
}
|
||||||
|
__name(inlineCode, "inlineCode");
|
||||||
|
function italic(content) {
|
||||||
|
return `_${content}_`;
|
||||||
|
}
|
||||||
|
__name(italic, "italic");
|
||||||
|
function bold(content) {
|
||||||
|
return `**${content}**`;
|
||||||
|
}
|
||||||
|
__name(bold, "bold");
|
||||||
|
function underscore(content) {
|
||||||
|
return `__${content}__`;
|
||||||
|
}
|
||||||
|
__name(underscore, "underscore");
|
||||||
|
function strikethrough(content) {
|
||||||
|
return `~~${content}~~`;
|
||||||
|
}
|
||||||
|
__name(strikethrough, "strikethrough");
|
||||||
|
function quote(content) {
|
||||||
|
return `> ${content}`;
|
||||||
|
}
|
||||||
|
__name(quote, "quote");
|
||||||
|
function blockQuote(content) {
|
||||||
|
return `>>> ${content}`;
|
||||||
|
}
|
||||||
|
__name(blockQuote, "blockQuote");
|
||||||
|
function hideLinkEmbed(url) {
|
||||||
|
return `<${url}>`;
|
||||||
|
}
|
||||||
|
__name(hideLinkEmbed, "hideLinkEmbed");
|
||||||
|
function hyperlink(content, url, title) {
|
||||||
|
return title ? `[${content}](${url} "${title}")` : `[${content}](${url})`;
|
||||||
|
}
|
||||||
|
__name(hyperlink, "hyperlink");
|
||||||
|
function spoiler(content) {
|
||||||
|
return `||${content}||`;
|
||||||
|
}
|
||||||
|
__name(spoiler, "spoiler");
|
||||||
|
function userMention(userId) {
|
||||||
|
return `<@${userId}>`;
|
||||||
|
}
|
||||||
|
__name(userMention, "userMention");
|
||||||
|
function channelMention(channelId) {
|
||||||
|
return `<#${channelId}>`;
|
||||||
|
}
|
||||||
|
__name(channelMention, "channelMention");
|
||||||
|
function roleMention(roleId) {
|
||||||
|
return `<@&${roleId}>`;
|
||||||
|
}
|
||||||
|
__name(roleMention, "roleMention");
|
||||||
|
function chatInputApplicationCommandMention(commandName, subcommandGroupName, subcommandName, commandId) {
|
||||||
|
if (commandId !== void 0) {
|
||||||
|
return `</${commandName} ${subcommandGroupName} ${subcommandName}:${commandId}>`;
|
||||||
|
}
|
||||||
|
if (subcommandName !== void 0) {
|
||||||
|
return `</${commandName} ${subcommandGroupName}:${subcommandName}>`;
|
||||||
|
}
|
||||||
|
return `</${commandName}:${subcommandGroupName}>`;
|
||||||
|
}
|
||||||
|
__name(chatInputApplicationCommandMention, "chatInputApplicationCommandMention");
|
||||||
|
function formatEmoji(emojiId, animated = false) {
|
||||||
|
return `<${animated ? "a" : ""}:_:${emojiId}>`;
|
||||||
|
}
|
||||||
|
__name(formatEmoji, "formatEmoji");
|
||||||
|
function channelLink(channelId, guildId) {
|
||||||
|
return `https://discord.com/channels/${guildId ?? "@me"}/${channelId}`;
|
||||||
|
}
|
||||||
|
__name(channelLink, "channelLink");
|
||||||
|
function messageLink(channelId, messageId, guildId) {
|
||||||
|
return `${guildId === void 0 ? channelLink(channelId) : channelLink(channelId, guildId)}/${messageId}`;
|
||||||
|
}
|
||||||
|
__name(messageLink, "messageLink");
|
||||||
|
var HeadingLevel = /* @__PURE__ */ ((HeadingLevel2) => {
|
||||||
|
HeadingLevel2[HeadingLevel2["One"] = 1] = "One";
|
||||||
|
HeadingLevel2[HeadingLevel2["Two"] = 2] = "Two";
|
||||||
|
HeadingLevel2[HeadingLevel2["Three"] = 3] = "Three";
|
||||||
|
return HeadingLevel2;
|
||||||
|
})(HeadingLevel || {});
|
||||||
|
function heading(content, level) {
|
||||||
|
switch (level) {
|
||||||
|
case 3 /* Three */:
|
||||||
|
return `### ${content}`;
|
||||||
|
case 2 /* Two */:
|
||||||
|
return `## ${content}`;
|
||||||
|
default:
|
||||||
|
return `# ${content}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
__name(heading, "heading");
|
||||||
|
function listCallback(element, startNumber, depth = 0) {
|
||||||
|
if (Array.isArray(element)) {
|
||||||
|
return element.map((element2) => listCallback(element2, startNumber, depth + 1)).join("\n");
|
||||||
|
}
|
||||||
|
return `${" ".repeat(depth - 1)}${startNumber ? `${startNumber}.` : "-"} ${element}`;
|
||||||
|
}
|
||||||
|
__name(listCallback, "listCallback");
|
||||||
|
function orderedList(list, startNumber = 1) {
|
||||||
|
return listCallback(list, Math.max(startNumber, 1));
|
||||||
|
}
|
||||||
|
__name(orderedList, "orderedList");
|
||||||
|
function unorderedList(list) {
|
||||||
|
return listCallback(list);
|
||||||
|
}
|
||||||
|
__name(unorderedList, "unorderedList");
|
||||||
|
function time(timeOrSeconds, style) {
|
||||||
|
if (typeof timeOrSeconds !== "number") {
|
||||||
|
timeOrSeconds = Math.floor((timeOrSeconds?.getTime() ?? Date.now()) / 1e3);
|
||||||
|
}
|
||||||
|
return typeof style === "string" ? `<t:${timeOrSeconds}:${style}>` : `<t:${timeOrSeconds}>`;
|
||||||
|
}
|
||||||
|
__name(time, "time");
|
||||||
|
var TimestampStyles = {
|
||||||
|
/**
|
||||||
|
* Short time format, consisting of hours and minutes.
|
||||||
|
*
|
||||||
|
* @example `16:20`
|
||||||
|
*/
|
||||||
|
ShortTime: "t",
|
||||||
|
/**
|
||||||
|
* Long time format, consisting of hours, minutes, and seconds.
|
||||||
|
*
|
||||||
|
* @example `16:20:30`
|
||||||
|
*/
|
||||||
|
LongTime: "T",
|
||||||
|
/**
|
||||||
|
* Short date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20/04/2021`
|
||||||
|
*/
|
||||||
|
ShortDate: "d",
|
||||||
|
/**
|
||||||
|
* Long date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021`
|
||||||
|
*/
|
||||||
|
LongDate: "D",
|
||||||
|
/**
|
||||||
|
* Short date-time format, consisting of short date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
ShortDateTime: "f",
|
||||||
|
/**
|
||||||
|
* Long date-time format, consisting of long date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `Tuesday, 20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
LongDateTime: "F",
|
||||||
|
/**
|
||||||
|
* Relative time format, consisting of a relative duration format.
|
||||||
|
*
|
||||||
|
* @example `2 months ago`
|
||||||
|
*/
|
||||||
|
RelativeTime: "R"
|
||||||
|
};
|
||||||
|
var Faces = /* @__PURE__ */ ((Faces2) => {
|
||||||
|
Faces2["Shrug"] = "\xAF_(\u30C4)_/\xAF";
|
||||||
|
Faces2["Tableflip"] = "(\u256F\xB0\u25A1\xB0)\u256F\uFE35 \u253B\u2501\u253B";
|
||||||
|
Faces2["Unflip"] = "\u252C\u2500\u252C\u30CE( \xBA _ \xBA\u30CE)";
|
||||||
|
return Faces2;
|
||||||
|
})(Faces || {});
|
||||||
|
var GuildNavigationMentions = /* @__PURE__ */ ((GuildNavigationMentions2) => {
|
||||||
|
GuildNavigationMentions2["Browse"] = "<id:browse>";
|
||||||
|
GuildNavigationMentions2["Customize"] = "<id:customize>";
|
||||||
|
GuildNavigationMentions2["Guide"] = "<id:guide>";
|
||||||
|
return GuildNavigationMentions2;
|
||||||
|
})(GuildNavigationMentions || {});
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
Faces,
|
||||||
|
GuildNavigationMentions,
|
||||||
|
HeadingLevel,
|
||||||
|
TimestampStyles,
|
||||||
|
blockQuote,
|
||||||
|
bold,
|
||||||
|
channelLink,
|
||||||
|
channelMention,
|
||||||
|
chatInputApplicationCommandMention,
|
||||||
|
codeBlock,
|
||||||
|
escapeBold,
|
||||||
|
escapeBulletedList,
|
||||||
|
escapeCodeBlock,
|
||||||
|
escapeEscape,
|
||||||
|
escapeHeading,
|
||||||
|
escapeInlineCode,
|
||||||
|
escapeItalic,
|
||||||
|
escapeMarkdown,
|
||||||
|
escapeMaskedLink,
|
||||||
|
escapeNumberedList,
|
||||||
|
escapeSpoiler,
|
||||||
|
escapeStrikethrough,
|
||||||
|
escapeUnderline,
|
||||||
|
formatEmoji,
|
||||||
|
heading,
|
||||||
|
hideLinkEmbed,
|
||||||
|
hyperlink,
|
||||||
|
inlineCode,
|
||||||
|
italic,
|
||||||
|
messageLink,
|
||||||
|
orderedList,
|
||||||
|
quote,
|
||||||
|
roleMention,
|
||||||
|
spoiler,
|
||||||
|
strikethrough,
|
||||||
|
time,
|
||||||
|
underscore,
|
||||||
|
unorderedList,
|
||||||
|
userMention
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=index.js.map
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,378 @@
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
|
||||||
|
// src/escapers.ts
|
||||||
|
function escapeMarkdown(text, options = {}) {
|
||||||
|
const {
|
||||||
|
codeBlock: codeBlock2 = true,
|
||||||
|
inlineCode: inlineCode2 = true,
|
||||||
|
bold: bold2 = true,
|
||||||
|
italic: italic2 = true,
|
||||||
|
underline = true,
|
||||||
|
strikethrough: strikethrough2 = true,
|
||||||
|
spoiler: spoiler2 = true,
|
||||||
|
codeBlockContent = true,
|
||||||
|
inlineCodeContent = true,
|
||||||
|
escape = true,
|
||||||
|
heading: heading2 = false,
|
||||||
|
bulletedList = false,
|
||||||
|
numberedList = false,
|
||||||
|
maskedLink = false
|
||||||
|
} = options;
|
||||||
|
if (!codeBlockContent) {
|
||||||
|
return text.split("```").map((subString, index, array) => {
|
||||||
|
if (index % 2 && index !== array.length - 1)
|
||||||
|
return subString;
|
||||||
|
return escapeMarkdown(subString, {
|
||||||
|
inlineCode: inlineCode2,
|
||||||
|
bold: bold2,
|
||||||
|
italic: italic2,
|
||||||
|
underline,
|
||||||
|
strikethrough: strikethrough2,
|
||||||
|
spoiler: spoiler2,
|
||||||
|
inlineCodeContent,
|
||||||
|
escape,
|
||||||
|
heading: heading2,
|
||||||
|
bulletedList,
|
||||||
|
numberedList,
|
||||||
|
maskedLink
|
||||||
|
});
|
||||||
|
}).join(codeBlock2 ? "\\`\\`\\`" : "```");
|
||||||
|
}
|
||||||
|
if (!inlineCodeContent) {
|
||||||
|
return text.split(/(?<=^|[^`])`(?=[^`]|$)/g).map((subString, index, array) => {
|
||||||
|
if (index % 2 && index !== array.length - 1)
|
||||||
|
return subString;
|
||||||
|
return escapeMarkdown(subString, {
|
||||||
|
codeBlock: codeBlock2,
|
||||||
|
bold: bold2,
|
||||||
|
italic: italic2,
|
||||||
|
underline,
|
||||||
|
strikethrough: strikethrough2,
|
||||||
|
spoiler: spoiler2,
|
||||||
|
escape,
|
||||||
|
heading: heading2,
|
||||||
|
bulletedList,
|
||||||
|
numberedList,
|
||||||
|
maskedLink
|
||||||
|
});
|
||||||
|
}).join(inlineCode2 ? "\\`" : "`");
|
||||||
|
}
|
||||||
|
let res = text;
|
||||||
|
if (escape)
|
||||||
|
res = escapeEscape(res);
|
||||||
|
if (inlineCode2)
|
||||||
|
res = escapeInlineCode(res);
|
||||||
|
if (codeBlock2)
|
||||||
|
res = escapeCodeBlock(res);
|
||||||
|
if (italic2)
|
||||||
|
res = escapeItalic(res);
|
||||||
|
if (bold2)
|
||||||
|
res = escapeBold(res);
|
||||||
|
if (underline)
|
||||||
|
res = escapeUnderline(res);
|
||||||
|
if (strikethrough2)
|
||||||
|
res = escapeStrikethrough(res);
|
||||||
|
if (spoiler2)
|
||||||
|
res = escapeSpoiler(res);
|
||||||
|
if (heading2)
|
||||||
|
res = escapeHeading(res);
|
||||||
|
if (bulletedList)
|
||||||
|
res = escapeBulletedList(res);
|
||||||
|
if (numberedList)
|
||||||
|
res = escapeNumberedList(res);
|
||||||
|
if (maskedLink)
|
||||||
|
res = escapeMaskedLink(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
__name(escapeMarkdown, "escapeMarkdown");
|
||||||
|
function escapeCodeBlock(text) {
|
||||||
|
return text.replaceAll("```", "\\`\\`\\`");
|
||||||
|
}
|
||||||
|
__name(escapeCodeBlock, "escapeCodeBlock");
|
||||||
|
function escapeInlineCode(text) {
|
||||||
|
return text.replaceAll(/(?<=^|[^`])``?(?=[^`]|$)/g, (match) => match.length === 2 ? "\\`\\`" : "\\`");
|
||||||
|
}
|
||||||
|
__name(escapeInlineCode, "escapeInlineCode");
|
||||||
|
function escapeItalic(text) {
|
||||||
|
let idx = 0;
|
||||||
|
const newText = text.replaceAll(/(?<=^|[^*])\*([^*]|\*\*|$)/g, (_, match) => {
|
||||||
|
if (match === "**")
|
||||||
|
return ++idx % 2 ? `\\*${match}` : `${match}\\*`;
|
||||||
|
return `\\*${match}`;
|
||||||
|
});
|
||||||
|
idx = 0;
|
||||||
|
return newText.replaceAll(/(?<=^|[^_])(?<!<a?:.+)_(?!:\d+>)([^_]|__|$)/g, (_, match) => {
|
||||||
|
if (match === "__")
|
||||||
|
return ++idx % 2 ? `\\_${match}` : `${match}\\_`;
|
||||||
|
return `\\_${match}`;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
__name(escapeItalic, "escapeItalic");
|
||||||
|
function escapeBold(text) {
|
||||||
|
let idx = 0;
|
||||||
|
return text.replaceAll(/\*\*(\*)?/g, (_, match) => {
|
||||||
|
if (match)
|
||||||
|
return ++idx % 2 ? `${match}\\*\\*` : `\\*\\*${match}`;
|
||||||
|
return "\\*\\*";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
__name(escapeBold, "escapeBold");
|
||||||
|
function escapeUnderline(text) {
|
||||||
|
let idx = 0;
|
||||||
|
return text.replaceAll(/(?<!<a?:.+)__(_)?(?!:\d+>)/g, (_, match) => {
|
||||||
|
if (match)
|
||||||
|
return ++idx % 2 ? `${match}\\_\\_` : `\\_\\_${match}`;
|
||||||
|
return "\\_\\_";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
__name(escapeUnderline, "escapeUnderline");
|
||||||
|
function escapeStrikethrough(text) {
|
||||||
|
return text.replaceAll("~~", "\\~\\~");
|
||||||
|
}
|
||||||
|
__name(escapeStrikethrough, "escapeStrikethrough");
|
||||||
|
function escapeSpoiler(text) {
|
||||||
|
return text.replaceAll("||", "\\|\\|");
|
||||||
|
}
|
||||||
|
__name(escapeSpoiler, "escapeSpoiler");
|
||||||
|
function escapeEscape(text) {
|
||||||
|
return text.replaceAll("\\", "\\\\");
|
||||||
|
}
|
||||||
|
__name(escapeEscape, "escapeEscape");
|
||||||
|
function escapeHeading(text) {
|
||||||
|
return text.replaceAll(/^( {0,2})([*-] )?( *)(#{1,3} )/gm, "$1$2$3\\$4");
|
||||||
|
}
|
||||||
|
__name(escapeHeading, "escapeHeading");
|
||||||
|
function escapeBulletedList(text) {
|
||||||
|
return text.replaceAll(/^( *)([*-])( +)/gm, "$1\\$2$3");
|
||||||
|
}
|
||||||
|
__name(escapeBulletedList, "escapeBulletedList");
|
||||||
|
function escapeNumberedList(text) {
|
||||||
|
return text.replaceAll(/^( *\d+)\./gm, "$1\\.");
|
||||||
|
}
|
||||||
|
__name(escapeNumberedList, "escapeNumberedList");
|
||||||
|
function escapeMaskedLink(text) {
|
||||||
|
return text.replaceAll(/\[.+]\(.+\)/gm, "\\$&");
|
||||||
|
}
|
||||||
|
__name(escapeMaskedLink, "escapeMaskedLink");
|
||||||
|
|
||||||
|
// src/formatters.ts
|
||||||
|
function codeBlock(language, content) {
|
||||||
|
return content === void 0 ? `\`\`\`
|
||||||
|
${language}
|
||||||
|
\`\`\`` : `\`\`\`${language}
|
||||||
|
${content}
|
||||||
|
\`\`\``;
|
||||||
|
}
|
||||||
|
__name(codeBlock, "codeBlock");
|
||||||
|
function inlineCode(content) {
|
||||||
|
return `\`${content}\``;
|
||||||
|
}
|
||||||
|
__name(inlineCode, "inlineCode");
|
||||||
|
function italic(content) {
|
||||||
|
return `_${content}_`;
|
||||||
|
}
|
||||||
|
__name(italic, "italic");
|
||||||
|
function bold(content) {
|
||||||
|
return `**${content}**`;
|
||||||
|
}
|
||||||
|
__name(bold, "bold");
|
||||||
|
function underscore(content) {
|
||||||
|
return `__${content}__`;
|
||||||
|
}
|
||||||
|
__name(underscore, "underscore");
|
||||||
|
function strikethrough(content) {
|
||||||
|
return `~~${content}~~`;
|
||||||
|
}
|
||||||
|
__name(strikethrough, "strikethrough");
|
||||||
|
function quote(content) {
|
||||||
|
return `> ${content}`;
|
||||||
|
}
|
||||||
|
__name(quote, "quote");
|
||||||
|
function blockQuote(content) {
|
||||||
|
return `>>> ${content}`;
|
||||||
|
}
|
||||||
|
__name(blockQuote, "blockQuote");
|
||||||
|
function hideLinkEmbed(url) {
|
||||||
|
return `<${url}>`;
|
||||||
|
}
|
||||||
|
__name(hideLinkEmbed, "hideLinkEmbed");
|
||||||
|
function hyperlink(content, url, title) {
|
||||||
|
return title ? `[${content}](${url} "${title}")` : `[${content}](${url})`;
|
||||||
|
}
|
||||||
|
__name(hyperlink, "hyperlink");
|
||||||
|
function spoiler(content) {
|
||||||
|
return `||${content}||`;
|
||||||
|
}
|
||||||
|
__name(spoiler, "spoiler");
|
||||||
|
function userMention(userId) {
|
||||||
|
return `<@${userId}>`;
|
||||||
|
}
|
||||||
|
__name(userMention, "userMention");
|
||||||
|
function channelMention(channelId) {
|
||||||
|
return `<#${channelId}>`;
|
||||||
|
}
|
||||||
|
__name(channelMention, "channelMention");
|
||||||
|
function roleMention(roleId) {
|
||||||
|
return `<@&${roleId}>`;
|
||||||
|
}
|
||||||
|
__name(roleMention, "roleMention");
|
||||||
|
function chatInputApplicationCommandMention(commandName, subcommandGroupName, subcommandName, commandId) {
|
||||||
|
if (commandId !== void 0) {
|
||||||
|
return `</${commandName} ${subcommandGroupName} ${subcommandName}:${commandId}>`;
|
||||||
|
}
|
||||||
|
if (subcommandName !== void 0) {
|
||||||
|
return `</${commandName} ${subcommandGroupName}:${subcommandName}>`;
|
||||||
|
}
|
||||||
|
return `</${commandName}:${subcommandGroupName}>`;
|
||||||
|
}
|
||||||
|
__name(chatInputApplicationCommandMention, "chatInputApplicationCommandMention");
|
||||||
|
function formatEmoji(emojiId, animated = false) {
|
||||||
|
return `<${animated ? "a" : ""}:_:${emojiId}>`;
|
||||||
|
}
|
||||||
|
__name(formatEmoji, "formatEmoji");
|
||||||
|
function channelLink(channelId, guildId) {
|
||||||
|
return `https://discord.com/channels/${guildId ?? "@me"}/${channelId}`;
|
||||||
|
}
|
||||||
|
__name(channelLink, "channelLink");
|
||||||
|
function messageLink(channelId, messageId, guildId) {
|
||||||
|
return `${guildId === void 0 ? channelLink(channelId) : channelLink(channelId, guildId)}/${messageId}`;
|
||||||
|
}
|
||||||
|
__name(messageLink, "messageLink");
|
||||||
|
var HeadingLevel = /* @__PURE__ */ ((HeadingLevel2) => {
|
||||||
|
HeadingLevel2[HeadingLevel2["One"] = 1] = "One";
|
||||||
|
HeadingLevel2[HeadingLevel2["Two"] = 2] = "Two";
|
||||||
|
HeadingLevel2[HeadingLevel2["Three"] = 3] = "Three";
|
||||||
|
return HeadingLevel2;
|
||||||
|
})(HeadingLevel || {});
|
||||||
|
function heading(content, level) {
|
||||||
|
switch (level) {
|
||||||
|
case 3 /* Three */:
|
||||||
|
return `### ${content}`;
|
||||||
|
case 2 /* Two */:
|
||||||
|
return `## ${content}`;
|
||||||
|
default:
|
||||||
|
return `# ${content}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
__name(heading, "heading");
|
||||||
|
function listCallback(element, startNumber, depth = 0) {
|
||||||
|
if (Array.isArray(element)) {
|
||||||
|
return element.map((element2) => listCallback(element2, startNumber, depth + 1)).join("\n");
|
||||||
|
}
|
||||||
|
return `${" ".repeat(depth - 1)}${startNumber ? `${startNumber}.` : "-"} ${element}`;
|
||||||
|
}
|
||||||
|
__name(listCallback, "listCallback");
|
||||||
|
function orderedList(list, startNumber = 1) {
|
||||||
|
return listCallback(list, Math.max(startNumber, 1));
|
||||||
|
}
|
||||||
|
__name(orderedList, "orderedList");
|
||||||
|
function unorderedList(list) {
|
||||||
|
return listCallback(list);
|
||||||
|
}
|
||||||
|
__name(unorderedList, "unorderedList");
|
||||||
|
function time(timeOrSeconds, style) {
|
||||||
|
if (typeof timeOrSeconds !== "number") {
|
||||||
|
timeOrSeconds = Math.floor((timeOrSeconds?.getTime() ?? Date.now()) / 1e3);
|
||||||
|
}
|
||||||
|
return typeof style === "string" ? `<t:${timeOrSeconds}:${style}>` : `<t:${timeOrSeconds}>`;
|
||||||
|
}
|
||||||
|
__name(time, "time");
|
||||||
|
var TimestampStyles = {
|
||||||
|
/**
|
||||||
|
* Short time format, consisting of hours and minutes.
|
||||||
|
*
|
||||||
|
* @example `16:20`
|
||||||
|
*/
|
||||||
|
ShortTime: "t",
|
||||||
|
/**
|
||||||
|
* Long time format, consisting of hours, minutes, and seconds.
|
||||||
|
*
|
||||||
|
* @example `16:20:30`
|
||||||
|
*/
|
||||||
|
LongTime: "T",
|
||||||
|
/**
|
||||||
|
* Short date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20/04/2021`
|
||||||
|
*/
|
||||||
|
ShortDate: "d",
|
||||||
|
/**
|
||||||
|
* Long date format, consisting of day, month, and year.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021`
|
||||||
|
*/
|
||||||
|
LongDate: "D",
|
||||||
|
/**
|
||||||
|
* Short date-time format, consisting of short date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
ShortDateTime: "f",
|
||||||
|
/**
|
||||||
|
* Long date-time format, consisting of long date and short time formats.
|
||||||
|
*
|
||||||
|
* @example `Tuesday, 20 April 2021 16:20`
|
||||||
|
*/
|
||||||
|
LongDateTime: "F",
|
||||||
|
/**
|
||||||
|
* Relative time format, consisting of a relative duration format.
|
||||||
|
*
|
||||||
|
* @example `2 months ago`
|
||||||
|
*/
|
||||||
|
RelativeTime: "R"
|
||||||
|
};
|
||||||
|
var Faces = /* @__PURE__ */ ((Faces2) => {
|
||||||
|
Faces2["Shrug"] = "\xAF_(\u30C4)_/\xAF";
|
||||||
|
Faces2["Tableflip"] = "(\u256F\xB0\u25A1\xB0)\u256F\uFE35 \u253B\u2501\u253B";
|
||||||
|
Faces2["Unflip"] = "\u252C\u2500\u252C\u30CE( \xBA _ \xBA\u30CE)";
|
||||||
|
return Faces2;
|
||||||
|
})(Faces || {});
|
||||||
|
var GuildNavigationMentions = /* @__PURE__ */ ((GuildNavigationMentions2) => {
|
||||||
|
GuildNavigationMentions2["Browse"] = "<id:browse>";
|
||||||
|
GuildNavigationMentions2["Customize"] = "<id:customize>";
|
||||||
|
GuildNavigationMentions2["Guide"] = "<id:guide>";
|
||||||
|
return GuildNavigationMentions2;
|
||||||
|
})(GuildNavigationMentions || {});
|
||||||
|
export {
|
||||||
|
Faces,
|
||||||
|
GuildNavigationMentions,
|
||||||
|
HeadingLevel,
|
||||||
|
TimestampStyles,
|
||||||
|
blockQuote,
|
||||||
|
bold,
|
||||||
|
channelLink,
|
||||||
|
channelMention,
|
||||||
|
chatInputApplicationCommandMention,
|
||||||
|
codeBlock,
|
||||||
|
escapeBold,
|
||||||
|
escapeBulletedList,
|
||||||
|
escapeCodeBlock,
|
||||||
|
escapeEscape,
|
||||||
|
escapeHeading,
|
||||||
|
escapeInlineCode,
|
||||||
|
escapeItalic,
|
||||||
|
escapeMarkdown,
|
||||||
|
escapeMaskedLink,
|
||||||
|
escapeNumberedList,
|
||||||
|
escapeSpoiler,
|
||||||
|
escapeStrikethrough,
|
||||||
|
escapeUnderline,
|
||||||
|
formatEmoji,
|
||||||
|
heading,
|
||||||
|
hideLinkEmbed,
|
||||||
|
hyperlink,
|
||||||
|
inlineCode,
|
||||||
|
italic,
|
||||||
|
messageLink,
|
||||||
|
orderedList,
|
||||||
|
quote,
|
||||||
|
roleMention,
|
||||||
|
spoiler,
|
||||||
|
strikethrough,
|
||||||
|
time,
|
||||||
|
underscore,
|
||||||
|
unorderedList,
|
||||||
|
userMention
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=index.mjs.map
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,79 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/formatters",
|
||||||
|
"version": "0.3.3",
|
||||||
|
"description": "A set of functions to format strings for Discord.",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/formatters"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"dependencies": {
|
||||||
|
"discord-api-types": "0.37.61"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "16.18.60",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^3.0.3",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src __tests__",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src __tests__",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/formatters/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,192 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2021 Noel Buechler
|
||||||
|
Copyright 2021 Vlad Frangu
|
||||||
|
Copyright 2021 Aura Román
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,139 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/rest"><img src="https://img.shields.io/npm/v/@discordjs/rest.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/rest"><img src="https://img.shields.io/npm/dt/@discordjs/rest.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Tests status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=rest" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/rest` is a module that allows you to easily make REST requests to the Discord API.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 16.11.0 or newer is required.**
|
||||||
|
|
||||||
|
Note: native fetch (not recommended) is unavailable in this node version, either use a newer node version or use the more performant `undiciRequest` strategy (default)
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/rest
|
||||||
|
yarn add @discordjs/rest
|
||||||
|
pnpm add @discordjs/rest
|
||||||
|
bun add @discordjs/rest
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
Install all required dependencies:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/rest discord-api-types
|
||||||
|
yarn add @discordjs/rest discord-api-types
|
||||||
|
pnpm add @discordjs/rest discord-api-types
|
||||||
|
bun add @discordjs/rest discord-api-types
|
||||||
|
```
|
||||||
|
|
||||||
|
Send a basic message:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
import { Routes } from 'discord-api-types/v10';
|
||||||
|
|
||||||
|
const rest = new REST({ version: '10' }).setToken(TOKEN);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await rest.post(Routes.channelMessages(CHANNEL_ID), {
|
||||||
|
body: {
|
||||||
|
content: 'A message via REST!',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Create a thread from an existing message to be archived after 60 minutes of inactivity:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
import { Routes } from 'discord-api-types/v10';
|
||||||
|
|
||||||
|
const rest = new REST({ version: '10' }).setToken(TOKEN);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await rest.post(Routes.threads(CHANNEL_ID, MESSAGE_ID), {
|
||||||
|
body: {
|
||||||
|
name: 'Thread',
|
||||||
|
auto_archive_duration: 60,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Send a basic message in an edge environment:
|
||||||
|
|
||||||
|
```js
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
import { Routes } from 'discord-api-types/v10';
|
||||||
|
|
||||||
|
const rest = new REST({ version: '10', makeRequest: fetch }).setToken(TOKEN);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await rest.post(Routes.channelMessages(CHANNEL_ID), {
|
||||||
|
body: {
|
||||||
|
content: 'A message via REST from the edge!',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/rest/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/rest
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/rest
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
|
@ -0,0 +1,901 @@
|
||||||
|
import * as url from 'url';
|
||||||
|
import { Snowflake } from 'discord-api-types/v10';
|
||||||
|
import { Readable } from 'node:stream';
|
||||||
|
import { ReadableStream } from 'node:stream/web';
|
||||||
|
import { Collection } from '@discordjs/collection';
|
||||||
|
import { Awaitable } from '@discordjs/util';
|
||||||
|
import * as undici from 'undici';
|
||||||
|
import { RequestInit, Dispatcher, Response, BodyInit, Agent } from 'undici';
|
||||||
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||||
|
|
||||||
|
interface IHandler {
|
||||||
|
/**
|
||||||
|
* The unique id of the handler
|
||||||
|
*/
|
||||||
|
readonly id: string;
|
||||||
|
/**
|
||||||
|
* If the bucket is currently inactive (no pending requests)
|
||||||
|
*/
|
||||||
|
get inactive(): boolean;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param routeId - The generalized api route with literal ids for major parameters
|
||||||
|
* @param url - The url to do the request on
|
||||||
|
* @param options - All the information needed to make a request
|
||||||
|
* @param requestData - Extra data from the user's request needed for errors and additional processing
|
||||||
|
*/
|
||||||
|
queueRequest(routeId: RouteData, url: string, options: RequestInit, requestData: HandlerRequestData): Promise<ResponseLike>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RestEvents {
|
||||||
|
handlerSweep: [sweptHandlers: Collection<string, IHandler>];
|
||||||
|
hashSweep: [sweptHashes: Collection<string, HashData>];
|
||||||
|
invalidRequestWarning: [invalidRequestInfo: InvalidRequestWarningData];
|
||||||
|
rateLimited: [rateLimitInfo: RateLimitData];
|
||||||
|
response: [request: APIRequest, response: ResponseLike];
|
||||||
|
restDebug: [info: string];
|
||||||
|
}
|
||||||
|
type RestEventsMap = {
|
||||||
|
[K in keyof RestEvents]: RestEvents[K];
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Options to be passed when creating the REST instance
|
||||||
|
*/
|
||||||
|
interface RESTOptions {
|
||||||
|
/**
|
||||||
|
* The agent to set globally
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
/**
|
||||||
|
* The base api path, without version
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://discord.com/api'`
|
||||||
|
*/
|
||||||
|
api: string;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for requests, useful if you want to use
|
||||||
|
* bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The cdn path
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://cdn.discordapp.com'`
|
||||||
|
*/
|
||||||
|
cdn: string;
|
||||||
|
/**
|
||||||
|
* How many requests to allow sending per second (Infinity for unlimited, 50 for the standard global limit used by Discord)
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
globalRequestsPerSecond: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 1h)
|
||||||
|
*
|
||||||
|
* @defaultValue `3_600_000`
|
||||||
|
*/
|
||||||
|
handlerSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* The maximum amount of time a hash can exist in milliseconds without being hit with a request (defaults to 24h)
|
||||||
|
*
|
||||||
|
* @defaultValue `86_400_000`
|
||||||
|
*/
|
||||||
|
hashLifetime: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 4h)
|
||||||
|
*
|
||||||
|
* @defaultValue `14_400_000`
|
||||||
|
*/
|
||||||
|
hashSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* Additional headers to send for all API requests
|
||||||
|
*
|
||||||
|
* @defaultValue `{}`
|
||||||
|
*/
|
||||||
|
headers: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* The number of invalid REST requests (those that return 401, 403, or 429) in a 10 minute window between emitted warnings (0 for no warnings).
|
||||||
|
* That is, if set to 500, warnings will be emitted at invalid request number 500, 1000, 1500, and so on.
|
||||||
|
*
|
||||||
|
* @defaultValue `0`
|
||||||
|
*/
|
||||||
|
invalidRequestWarningInterval: number;
|
||||||
|
/**
|
||||||
|
* The method called to perform the actual HTTP request given a url and web `fetch` options
|
||||||
|
* For example, to use global fetch, simply provide `makeRequest: fetch`
|
||||||
|
*/
|
||||||
|
makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* The extra offset to add to rate limits in milliseconds
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
offset: number;
|
||||||
|
/**
|
||||||
|
* Determines how rate limiting and pre-emptive throttling should be handled.
|
||||||
|
* When an array of strings, each element is treated as a prefix for the request route
|
||||||
|
* (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
|
||||||
|
* for which to throw {@link RateLimitError}s. All other request routes will be queued normally
|
||||||
|
*
|
||||||
|
* @defaultValue `null`
|
||||||
|
*/
|
||||||
|
rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
|
||||||
|
/**
|
||||||
|
* The number of retries for errors with the 500 code, or errors
|
||||||
|
* that timeout
|
||||||
|
*
|
||||||
|
* @defaultValue `3`
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The time to wait in milliseconds before a request is aborted
|
||||||
|
*
|
||||||
|
* @defaultValue `15_000`
|
||||||
|
*/
|
||||||
|
timeout: number;
|
||||||
|
/**
|
||||||
|
* Extra information to add to the user agent
|
||||||
|
*
|
||||||
|
* @defaultValue DefaultUserAgentAppendix
|
||||||
|
*/
|
||||||
|
userAgentAppendix: string;
|
||||||
|
/**
|
||||||
|
* The version of the API to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'10'`
|
||||||
|
*/
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Data emitted on `RESTEvents.RateLimited`
|
||||||
|
*/
|
||||||
|
interface RateLimitData {
|
||||||
|
/**
|
||||||
|
* Whether the rate limit that was reached was the global limit
|
||||||
|
*/
|
||||||
|
global: boolean;
|
||||||
|
/**
|
||||||
|
* The bucket hash for this request
|
||||||
|
*/
|
||||||
|
hash: string;
|
||||||
|
/**
|
||||||
|
* The amount of requests we can perform before locking requests
|
||||||
|
*/
|
||||||
|
limit: number;
|
||||||
|
/**
|
||||||
|
* The major parameter of the route
|
||||||
|
*
|
||||||
|
* For example, in `/channels/x`, this will be `x`.
|
||||||
|
* If there is no major parameter (e.g: `/bot/gateway`) this will be `global`.
|
||||||
|
*/
|
||||||
|
majorParameter: string;
|
||||||
|
/**
|
||||||
|
* The HTTP method being performed
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before this specific request can be retried
|
||||||
|
*/
|
||||||
|
retryAfter: number;
|
||||||
|
/**
|
||||||
|
* The route being hit in this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
/**
|
||||||
|
* The scope of the rate limit that was hit.
|
||||||
|
*
|
||||||
|
* This can be `user` for rate limits that are per client, `global` for rate limits that affect all clients or `shared` for rate limits that
|
||||||
|
* are shared per resource.
|
||||||
|
*/
|
||||||
|
scope: 'global' | 'shared' | 'user';
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before the sublimit lock for the route resets, and requests that fall under a sublimit
|
||||||
|
* can be retried
|
||||||
|
*
|
||||||
|
* This is only present on certain sublimits, and `0` otherwise
|
||||||
|
*/
|
||||||
|
sublimitTimeout: number;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, until the route's request-lock is reset
|
||||||
|
*/
|
||||||
|
timeToReset: number;
|
||||||
|
/**
|
||||||
|
* The full URL for this request
|
||||||
|
*/
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A function that determines whether the rate limit hit should throw an Error
|
||||||
|
*/
|
||||||
|
type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Awaitable<boolean>;
|
||||||
|
interface APIRequest {
|
||||||
|
/**
|
||||||
|
* The data that was used to form the body of this request
|
||||||
|
*/
|
||||||
|
data: HandlerRequestData;
|
||||||
|
/**
|
||||||
|
* The HTTP method used in this request
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* Additional HTTP options for this request
|
||||||
|
*/
|
||||||
|
options: RequestInit;
|
||||||
|
/**
|
||||||
|
* The full path used to make the request
|
||||||
|
*/
|
||||||
|
path: RouteLike;
|
||||||
|
/**
|
||||||
|
* The number of times this request has been attempted
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The API route identifying the ratelimit for this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
}
|
||||||
|
interface ResponseLike extends Pick<Response, 'arrayBuffer' | 'bodyUsed' | 'headers' | 'json' | 'ok' | 'status' | 'statusText' | 'text'> {
|
||||||
|
body: Readable | ReadableStream | null;
|
||||||
|
}
|
||||||
|
interface InvalidRequestWarningData {
|
||||||
|
/**
|
||||||
|
* Number of invalid requests that have been made in the window
|
||||||
|
*/
|
||||||
|
count: number;
|
||||||
|
/**
|
||||||
|
* Time in milliseconds remaining before the count resets
|
||||||
|
*/
|
||||||
|
remainingTime: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a file to be added to the request
|
||||||
|
*/
|
||||||
|
interface RawFile {
|
||||||
|
/**
|
||||||
|
* Content-Type of the file
|
||||||
|
*/
|
||||||
|
contentType?: string;
|
||||||
|
/**
|
||||||
|
* The actual data for the file
|
||||||
|
*/
|
||||||
|
data: Buffer | Uint8Array | boolean | number | string;
|
||||||
|
/**
|
||||||
|
* An explicit key to use for key of the formdata field for this file.
|
||||||
|
* When not provided, the index of the file in the files array is used in the form `files[${index}]`.
|
||||||
|
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
|
||||||
|
*/
|
||||||
|
key?: string;
|
||||||
|
/**
|
||||||
|
* The name of the file
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents possible data to be given to an endpoint
|
||||||
|
*/
|
||||||
|
interface RequestData {
|
||||||
|
/**
|
||||||
|
* Whether to append JSON data to form data instead of `payload_json` when sending files
|
||||||
|
*/
|
||||||
|
appendToFormData?: boolean;
|
||||||
|
/**
|
||||||
|
* If this request needs the `Authorization` header
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
auth?: boolean;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for this request, useful if you use this with bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix?: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The body to send to this request.
|
||||||
|
* If providing as BodyInit, set `passThroughBody: true`
|
||||||
|
*/
|
||||||
|
body?: BodyInit | unknown;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} to use for the request.
|
||||||
|
*/
|
||||||
|
dispatcher?: Agent;
|
||||||
|
/**
|
||||||
|
* Files to be attached to this request
|
||||||
|
*/
|
||||||
|
files?: RawFile[] | undefined;
|
||||||
|
/**
|
||||||
|
* Additional headers to add to this request
|
||||||
|
*/
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* Whether to pass-through the body property directly to `fetch()`.
|
||||||
|
* <warn>This only applies when files is NOT present</warn>
|
||||||
|
*/
|
||||||
|
passThroughBody?: boolean;
|
||||||
|
/**
|
||||||
|
* Query string parameters to append to the called endpoint
|
||||||
|
*/
|
||||||
|
query?: URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Reason to show in the audit logs
|
||||||
|
*/
|
||||||
|
reason?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The signal to abort the queue entry or the REST call, where applicable
|
||||||
|
*/
|
||||||
|
signal?: AbortSignal | undefined;
|
||||||
|
/**
|
||||||
|
* If this request should be versioned
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
versioned?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible headers for an API call
|
||||||
|
*/
|
||||||
|
interface RequestHeaders {
|
||||||
|
Authorization?: string;
|
||||||
|
'User-Agent': string;
|
||||||
|
'X-Audit-Log-Reason'?: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible API methods to be used when doing requests
|
||||||
|
*/
|
||||||
|
declare enum RequestMethod {
|
||||||
|
Delete = "DELETE",
|
||||||
|
Get = "GET",
|
||||||
|
Patch = "PATCH",
|
||||||
|
Post = "POST",
|
||||||
|
Put = "PUT"
|
||||||
|
}
|
||||||
|
type RouteLike = `/${string}`;
|
||||||
|
/**
|
||||||
|
* Internal request options
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface InternalRequest extends RequestData {
|
||||||
|
fullRoute: RouteLike;
|
||||||
|
method: RequestMethod;
|
||||||
|
}
|
||||||
|
type HandlerRequestData = Pick<InternalRequest, 'auth' | 'body' | 'files' | 'signal'>;
|
||||||
|
/**
|
||||||
|
* Parsed route data for an endpoint
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface RouteData {
|
||||||
|
bucketRoute: string;
|
||||||
|
majorParameter: string;
|
||||||
|
original: RouteLike;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a hash and its associated fields
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface HashData {
|
||||||
|
lastAccess: number;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare const DefaultUserAgent: `DiscordBot (https://discord.js.org, ${string})`;
|
||||||
|
/**
|
||||||
|
* The default string to append onto the user agent.
|
||||||
|
*/
|
||||||
|
declare const DefaultUserAgentAppendix: string;
|
||||||
|
declare const DefaultRestOptions: {
|
||||||
|
readonly agent: null;
|
||||||
|
readonly api: "https://discord.com/api";
|
||||||
|
readonly authPrefix: "Bot";
|
||||||
|
readonly cdn: "https://cdn.discordapp.com";
|
||||||
|
readonly headers: {};
|
||||||
|
readonly invalidRequestWarningInterval: 0;
|
||||||
|
readonly globalRequestsPerSecond: 50;
|
||||||
|
readonly offset: 50;
|
||||||
|
readonly rejectOnRateLimit: null;
|
||||||
|
readonly retries: 3;
|
||||||
|
readonly timeout: 15000;
|
||||||
|
readonly userAgentAppendix: string;
|
||||||
|
readonly version: "10";
|
||||||
|
readonly hashSweepInterval: 14400000;
|
||||||
|
readonly hashLifetime: 86400000;
|
||||||
|
readonly handlerSweepInterval: 3600000;
|
||||||
|
readonly makeRequest: (url: string, init: undici.RequestInit) => Promise<ResponseLike>;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The events that the REST manager emits
|
||||||
|
*/
|
||||||
|
declare enum RESTEvents {
|
||||||
|
Debug = "restDebug",
|
||||||
|
HandlerSweep = "handlerSweep",
|
||||||
|
HashSweep = "hashSweep",
|
||||||
|
InvalidRequestWarning = "invalidRequestWarning",
|
||||||
|
RateLimited = "rateLimited",
|
||||||
|
Response = "response"
|
||||||
|
}
|
||||||
|
declare const ALLOWED_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif"];
|
||||||
|
declare const ALLOWED_STICKER_EXTENSIONS: readonly ["png", "json", "gif"];
|
||||||
|
declare const ALLOWED_SIZES: readonly [16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
|
||||||
|
type ImageExtension = (typeof ALLOWED_EXTENSIONS)[number];
|
||||||
|
type StickerExtension = (typeof ALLOWED_STICKER_EXTENSIONS)[number];
|
||||||
|
type ImageSize = (typeof ALLOWED_SIZES)[number];
|
||||||
|
declare const OverwrittenMimeTypes: {
|
||||||
|
readonly 'image/apng': "image/png";
|
||||||
|
};
|
||||||
|
declare const BurstHandlerMajorIdKey = "burst";
|
||||||
|
/**
|
||||||
|
* Prefix for deprecation warnings.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
declare const DEPRECATION_WARNING_PREFIX: "DeprecationWarning";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options used for image URLs
|
||||||
|
*/
|
||||||
|
interface BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: ImageExtension;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options used for image URLs with animated content
|
||||||
|
*/
|
||||||
|
interface ImageURLOptions extends BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* Whether or not to prefer the static version of an image asset.
|
||||||
|
*/
|
||||||
|
forceStatic?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options to use when making a CDN URL
|
||||||
|
*/
|
||||||
|
interface MakeURLOptions {
|
||||||
|
/**
|
||||||
|
* The allowed extensions that can be used
|
||||||
|
*/
|
||||||
|
allowedExtensions?: readonly string[];
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The CDN link builder
|
||||||
|
*/
|
||||||
|
declare class CDN {
|
||||||
|
private readonly base;
|
||||||
|
constructor(base?: string);
|
||||||
|
/**
|
||||||
|
* Generates an app asset URL for a client's asset.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the asset
|
||||||
|
* @param assetHash - The hash provided by Discord for this asset
|
||||||
|
* @param options - Optional options for the asset
|
||||||
|
*/
|
||||||
|
appAsset(clientId: string, assetHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an app icon URL for a client's icon.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
appIcon(clientId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an avatar URL, e.g. for a user or a webhook.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
avatar(id: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a user avatar decoration URL.
|
||||||
|
*
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param userAvatarDecoration - The hash provided by Discord for this avatar decoration
|
||||||
|
* @param options - Optional options for the avatar decoration
|
||||||
|
*/
|
||||||
|
avatarDecoration(userId: string, userAvatarDecoration: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a banner URL, e.g. for a user or a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the banner splash
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
banner(id: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL for a channel, e.g. a group DM.
|
||||||
|
*
|
||||||
|
* @param channelId - The channel id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this channel
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
channelIcon(channelId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a default avatar URL
|
||||||
|
*
|
||||||
|
* @param index - The default avatar index
|
||||||
|
* @remarks
|
||||||
|
* To calculate the index for a user do `(userId >> 22) % 6`,
|
||||||
|
* or `discriminator % 5` if they're using the legacy username system.
|
||||||
|
*/
|
||||||
|
defaultAvatar(index: number): string;
|
||||||
|
/**
|
||||||
|
* Generates a discovery splash URL for a guild's discovery splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the discovery splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
discoverySplash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param options - Optional options for the emoji
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param extension - The extension of the emoji
|
||||||
|
* @deprecated This overload is deprecated. Pass an object containing the extension instead.
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, extension?: ImageExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member avatar URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
guildMemberAvatar(guildId: string, userId: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member banner URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
guildMemberBanner(guildId: string, userId: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL, e.g. for a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon splash
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
icon(id: string, iconHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a URL for the icon of a role
|
||||||
|
*
|
||||||
|
* @param roleId - The id of the role that has the icon
|
||||||
|
* @param roleIconHash - The hash provided by Discord for this role icon
|
||||||
|
* @param options - Optional options for the role icon
|
||||||
|
*/
|
||||||
|
roleIcon(roleId: string, roleIconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild invite splash URL for a guild's invite splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the invite splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
splash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker URL.
|
||||||
|
*
|
||||||
|
* @param stickerId - The sticker id
|
||||||
|
* @param extension - The extension of the sticker
|
||||||
|
* @privateRemarks
|
||||||
|
* Stickers cannot have a `.webp` extension, so we default to a `.png`
|
||||||
|
*/
|
||||||
|
sticker(stickerId: string, extension?: StickerExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker pack banner URL.
|
||||||
|
*
|
||||||
|
* @param bannerId - The banner id
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
stickerPackBanner(bannerId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a team icon URL for a team's icon.
|
||||||
|
*
|
||||||
|
* @param teamId - The team id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
teamIcon(teamId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a cover image for a guild scheduled event.
|
||||||
|
*
|
||||||
|
* @param scheduledEventId - The scheduled event id
|
||||||
|
* @param coverHash - The hash provided by discord for this cover image
|
||||||
|
* @param options - Optional options for the cover image
|
||||||
|
*/
|
||||||
|
guildScheduledEventCover(scheduledEventId: string, coverHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param hash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the link
|
||||||
|
*/
|
||||||
|
private dynamicMakeURL;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param options - The extension/size options for the link
|
||||||
|
*/
|
||||||
|
private makeURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DiscordErrorFieldInformation {
|
||||||
|
code: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface DiscordErrorGroupWrapper {
|
||||||
|
_errors: DiscordError[];
|
||||||
|
}
|
||||||
|
type DiscordError = DiscordErrorFieldInformation | DiscordErrorGroupWrapper | string | {
|
||||||
|
[k: string]: DiscordError;
|
||||||
|
};
|
||||||
|
interface DiscordErrorData {
|
||||||
|
code: number;
|
||||||
|
errors?: DiscordError;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface OAuthErrorData {
|
||||||
|
error: string;
|
||||||
|
error_description?: string;
|
||||||
|
}
|
||||||
|
interface RequestBody {
|
||||||
|
files: RawFile[] | undefined;
|
||||||
|
json: unknown | undefined;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an API error returned by Discord
|
||||||
|
*/
|
||||||
|
declare class DiscordAPIError extends Error {
|
||||||
|
rawError: DiscordErrorData | OAuthErrorData;
|
||||||
|
code: number | string;
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
/**
|
||||||
|
* @param rawError - The error reported by Discord
|
||||||
|
* @param code - The error code reported by Discord
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(rawError: DiscordErrorData | OAuthErrorData, code: number | string, status: number, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
private static getMessage;
|
||||||
|
private static flattenDiscordError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a HTTP error
|
||||||
|
*/
|
||||||
|
declare class HTTPError extends Error {
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
name: string;
|
||||||
|
/**
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param statusText - The status text of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(status: number, statusText: string, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class RateLimitError extends Error implements RateLimitData {
|
||||||
|
timeToReset: number;
|
||||||
|
limit: number;
|
||||||
|
method: string;
|
||||||
|
hash: string;
|
||||||
|
url: string;
|
||||||
|
route: string;
|
||||||
|
majorParameter: string;
|
||||||
|
global: boolean;
|
||||||
|
retryAfter: number;
|
||||||
|
sublimitTimeout: number;
|
||||||
|
scope: RateLimitData['scope'];
|
||||||
|
constructor({ timeToReset, limit, method, hash, url, route, majorParameter, global, retryAfter, sublimitTimeout, scope, }: RateLimitData);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the class that manages handlers for endpoints
|
||||||
|
*/
|
||||||
|
declare class REST extends AsyncEventEmitter<RestEventsMap> {
|
||||||
|
#private;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
|
||||||
|
* performed by this manager.
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
readonly cdn: CDN;
|
||||||
|
/**
|
||||||
|
* The number of requests remaining in the global bucket
|
||||||
|
*/
|
||||||
|
globalRemaining: number;
|
||||||
|
/**
|
||||||
|
* The promise used to wait out the global rate limit
|
||||||
|
*/
|
||||||
|
globalDelay: Promise<void> | null;
|
||||||
|
/**
|
||||||
|
* The timestamp at which the global bucket resets
|
||||||
|
*/
|
||||||
|
globalReset: number;
|
||||||
|
/**
|
||||||
|
* API bucket hashes that are cached from provided routes
|
||||||
|
*/
|
||||||
|
readonly hashes: Collection<string, HashData>;
|
||||||
|
/**
|
||||||
|
* Request handlers created from the bucket hash and the major parameters
|
||||||
|
*/
|
||||||
|
readonly handlers: Collection<string, IHandler>;
|
||||||
|
private hashTimer;
|
||||||
|
private handlerTimer;
|
||||||
|
readonly options: RESTOptions;
|
||||||
|
constructor(options?: Partial<RESTOptions>);
|
||||||
|
private setupSweepers;
|
||||||
|
/**
|
||||||
|
* Runs a get request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
get(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a delete request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
delete(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a post request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
post(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a put request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
put(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a patch request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
patch(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a request from the api
|
||||||
|
*
|
||||||
|
* @param options - Request options
|
||||||
|
*/
|
||||||
|
request(options: InternalRequest): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Sets the default agent to use for requests performed by this manager
|
||||||
|
*
|
||||||
|
* @param agent - The agent to use
|
||||||
|
*/
|
||||||
|
setAgent(agent: Dispatcher): this;
|
||||||
|
/**
|
||||||
|
* Sets the authorization token that should be used for requests
|
||||||
|
*
|
||||||
|
* @param token - The authorization token to use
|
||||||
|
*/
|
||||||
|
setToken(token: string): this;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param request - All the information needed to make a request
|
||||||
|
* @returns The response from the api request
|
||||||
|
*/
|
||||||
|
queueRequest(request: InternalRequest): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* Creates a new rate limit handler from a hash, based on the hash and the major parameter
|
||||||
|
*
|
||||||
|
* @param hash - The hash for the route
|
||||||
|
* @param majorParameter - The major parameter for this handler
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private createHandler;
|
||||||
|
/**
|
||||||
|
* Formats the request data to a usable format for fetch
|
||||||
|
*
|
||||||
|
* @param request - The request data
|
||||||
|
*/
|
||||||
|
private resolveRequest;
|
||||||
|
/**
|
||||||
|
* Stops the hash sweeping interval
|
||||||
|
*/
|
||||||
|
clearHashSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Stops the request handler sweeping interval
|
||||||
|
*/
|
||||||
|
clearHandlerSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Generates route data for an endpoint:method
|
||||||
|
*
|
||||||
|
* @param endpoint - The raw endpoint to generalize
|
||||||
|
* @param method - The HTTP method this endpoint is called without
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static generateRouteData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and populates an URLSearchParams instance from an object, stripping
|
||||||
|
* out null and undefined values, while also coercing non-strings to strings.
|
||||||
|
*
|
||||||
|
* @param options - The options to use
|
||||||
|
* @returns A populated URLSearchParams instance
|
||||||
|
*/
|
||||||
|
declare function makeURLSearchParams<OptionsType extends object>(options?: Readonly<OptionsType>): url.URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Converts the response to usable data
|
||||||
|
*
|
||||||
|
* @param res - The fetch response
|
||||||
|
*/
|
||||||
|
declare function parseResponse(res: ResponseLike): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Calculates the default avatar index for a given user id.
|
||||||
|
*
|
||||||
|
* @param userId - The user id to calculate the default avatar index for
|
||||||
|
*/
|
||||||
|
declare function calculateUserDefaultAvatarIndex(userId: Snowflake): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/rest#readme | @discordjs/rest} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { ALLOWED_EXTENSIONS, ALLOWED_SIZES, ALLOWED_STICKER_EXTENSIONS, APIRequest, BaseImageURLOptions, BurstHandlerMajorIdKey, CDN, DEPRECATION_WARNING_PREFIX, DefaultRestOptions, DefaultUserAgent, DefaultUserAgentAppendix, DiscordAPIError, DiscordErrorData, HTTPError, HandlerRequestData, HashData, ImageExtension, ImageSize, ImageURLOptions, InternalRequest, InvalidRequestWarningData, MakeURLOptions, OAuthErrorData, OverwrittenMimeTypes, REST, RESTEvents, RESTOptions, RateLimitData, RateLimitError, RateLimitQueueFilter, RawFile, RequestBody, RequestData, RequestHeaders, RequestMethod, ResponseLike, RestEvents, RestEventsMap, RouteData, RouteLike, StickerExtension, calculateUserDefaultAvatarIndex, makeURLSearchParams, parseResponse, version };
|
|
@ -0,0 +1,901 @@
|
||||||
|
import * as url from 'url';
|
||||||
|
import { Snowflake } from 'discord-api-types/v10';
|
||||||
|
import { Readable } from 'node:stream';
|
||||||
|
import { ReadableStream } from 'node:stream/web';
|
||||||
|
import { Collection } from '@discordjs/collection';
|
||||||
|
import { Awaitable } from '@discordjs/util';
|
||||||
|
import * as undici from 'undici';
|
||||||
|
import { RequestInit, Dispatcher, Response, BodyInit, Agent } from 'undici';
|
||||||
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||||
|
|
||||||
|
interface IHandler {
|
||||||
|
/**
|
||||||
|
* The unique id of the handler
|
||||||
|
*/
|
||||||
|
readonly id: string;
|
||||||
|
/**
|
||||||
|
* If the bucket is currently inactive (no pending requests)
|
||||||
|
*/
|
||||||
|
get inactive(): boolean;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param routeId - The generalized api route with literal ids for major parameters
|
||||||
|
* @param url - The url to do the request on
|
||||||
|
* @param options - All the information needed to make a request
|
||||||
|
* @param requestData - Extra data from the user's request needed for errors and additional processing
|
||||||
|
*/
|
||||||
|
queueRequest(routeId: RouteData, url: string, options: RequestInit, requestData: HandlerRequestData): Promise<ResponseLike>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RestEvents {
|
||||||
|
handlerSweep: [sweptHandlers: Collection<string, IHandler>];
|
||||||
|
hashSweep: [sweptHashes: Collection<string, HashData>];
|
||||||
|
invalidRequestWarning: [invalidRequestInfo: InvalidRequestWarningData];
|
||||||
|
rateLimited: [rateLimitInfo: RateLimitData];
|
||||||
|
response: [request: APIRequest, response: ResponseLike];
|
||||||
|
restDebug: [info: string];
|
||||||
|
}
|
||||||
|
type RestEventsMap = {
|
||||||
|
[K in keyof RestEvents]: RestEvents[K];
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Options to be passed when creating the REST instance
|
||||||
|
*/
|
||||||
|
interface RESTOptions {
|
||||||
|
/**
|
||||||
|
* The agent to set globally
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
/**
|
||||||
|
* The base api path, without version
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://discord.com/api'`
|
||||||
|
*/
|
||||||
|
api: string;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for requests, useful if you want to use
|
||||||
|
* bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The cdn path
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://cdn.discordapp.com'`
|
||||||
|
*/
|
||||||
|
cdn: string;
|
||||||
|
/**
|
||||||
|
* How many requests to allow sending per second (Infinity for unlimited, 50 for the standard global limit used by Discord)
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
globalRequestsPerSecond: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 1h)
|
||||||
|
*
|
||||||
|
* @defaultValue `3_600_000`
|
||||||
|
*/
|
||||||
|
handlerSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* The maximum amount of time a hash can exist in milliseconds without being hit with a request (defaults to 24h)
|
||||||
|
*
|
||||||
|
* @defaultValue `86_400_000`
|
||||||
|
*/
|
||||||
|
hashLifetime: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 4h)
|
||||||
|
*
|
||||||
|
* @defaultValue `14_400_000`
|
||||||
|
*/
|
||||||
|
hashSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* Additional headers to send for all API requests
|
||||||
|
*
|
||||||
|
* @defaultValue `{}`
|
||||||
|
*/
|
||||||
|
headers: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* The number of invalid REST requests (those that return 401, 403, or 429) in a 10 minute window between emitted warnings (0 for no warnings).
|
||||||
|
* That is, if set to 500, warnings will be emitted at invalid request number 500, 1000, 1500, and so on.
|
||||||
|
*
|
||||||
|
* @defaultValue `0`
|
||||||
|
*/
|
||||||
|
invalidRequestWarningInterval: number;
|
||||||
|
/**
|
||||||
|
* The method called to perform the actual HTTP request given a url and web `fetch` options
|
||||||
|
* For example, to use global fetch, simply provide `makeRequest: fetch`
|
||||||
|
*/
|
||||||
|
makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* The extra offset to add to rate limits in milliseconds
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
offset: number;
|
||||||
|
/**
|
||||||
|
* Determines how rate limiting and pre-emptive throttling should be handled.
|
||||||
|
* When an array of strings, each element is treated as a prefix for the request route
|
||||||
|
* (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
|
||||||
|
* for which to throw {@link RateLimitError}s. All other request routes will be queued normally
|
||||||
|
*
|
||||||
|
* @defaultValue `null`
|
||||||
|
*/
|
||||||
|
rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
|
||||||
|
/**
|
||||||
|
* The number of retries for errors with the 500 code, or errors
|
||||||
|
* that timeout
|
||||||
|
*
|
||||||
|
* @defaultValue `3`
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The time to wait in milliseconds before a request is aborted
|
||||||
|
*
|
||||||
|
* @defaultValue `15_000`
|
||||||
|
*/
|
||||||
|
timeout: number;
|
||||||
|
/**
|
||||||
|
* Extra information to add to the user agent
|
||||||
|
*
|
||||||
|
* @defaultValue DefaultUserAgentAppendix
|
||||||
|
*/
|
||||||
|
userAgentAppendix: string;
|
||||||
|
/**
|
||||||
|
* The version of the API to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'10'`
|
||||||
|
*/
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Data emitted on `RESTEvents.RateLimited`
|
||||||
|
*/
|
||||||
|
interface RateLimitData {
|
||||||
|
/**
|
||||||
|
* Whether the rate limit that was reached was the global limit
|
||||||
|
*/
|
||||||
|
global: boolean;
|
||||||
|
/**
|
||||||
|
* The bucket hash for this request
|
||||||
|
*/
|
||||||
|
hash: string;
|
||||||
|
/**
|
||||||
|
* The amount of requests we can perform before locking requests
|
||||||
|
*/
|
||||||
|
limit: number;
|
||||||
|
/**
|
||||||
|
* The major parameter of the route
|
||||||
|
*
|
||||||
|
* For example, in `/channels/x`, this will be `x`.
|
||||||
|
* If there is no major parameter (e.g: `/bot/gateway`) this will be `global`.
|
||||||
|
*/
|
||||||
|
majorParameter: string;
|
||||||
|
/**
|
||||||
|
* The HTTP method being performed
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before this specific request can be retried
|
||||||
|
*/
|
||||||
|
retryAfter: number;
|
||||||
|
/**
|
||||||
|
* The route being hit in this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
/**
|
||||||
|
* The scope of the rate limit that was hit.
|
||||||
|
*
|
||||||
|
* This can be `user` for rate limits that are per client, `global` for rate limits that affect all clients or `shared` for rate limits that
|
||||||
|
* are shared per resource.
|
||||||
|
*/
|
||||||
|
scope: 'global' | 'shared' | 'user';
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before the sublimit lock for the route resets, and requests that fall under a sublimit
|
||||||
|
* can be retried
|
||||||
|
*
|
||||||
|
* This is only present on certain sublimits, and `0` otherwise
|
||||||
|
*/
|
||||||
|
sublimitTimeout: number;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, until the route's request-lock is reset
|
||||||
|
*/
|
||||||
|
timeToReset: number;
|
||||||
|
/**
|
||||||
|
* The full URL for this request
|
||||||
|
*/
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A function that determines whether the rate limit hit should throw an Error
|
||||||
|
*/
|
||||||
|
type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Awaitable<boolean>;
|
||||||
|
interface APIRequest {
|
||||||
|
/**
|
||||||
|
* The data that was used to form the body of this request
|
||||||
|
*/
|
||||||
|
data: HandlerRequestData;
|
||||||
|
/**
|
||||||
|
* The HTTP method used in this request
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* Additional HTTP options for this request
|
||||||
|
*/
|
||||||
|
options: RequestInit;
|
||||||
|
/**
|
||||||
|
* The full path used to make the request
|
||||||
|
*/
|
||||||
|
path: RouteLike;
|
||||||
|
/**
|
||||||
|
* The number of times this request has been attempted
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The API route identifying the ratelimit for this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
}
|
||||||
|
interface ResponseLike extends Pick<Response, 'arrayBuffer' | 'bodyUsed' | 'headers' | 'json' | 'ok' | 'status' | 'statusText' | 'text'> {
|
||||||
|
body: Readable | ReadableStream | null;
|
||||||
|
}
|
||||||
|
interface InvalidRequestWarningData {
|
||||||
|
/**
|
||||||
|
* Number of invalid requests that have been made in the window
|
||||||
|
*/
|
||||||
|
count: number;
|
||||||
|
/**
|
||||||
|
* Time in milliseconds remaining before the count resets
|
||||||
|
*/
|
||||||
|
remainingTime: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a file to be added to the request
|
||||||
|
*/
|
||||||
|
interface RawFile {
|
||||||
|
/**
|
||||||
|
* Content-Type of the file
|
||||||
|
*/
|
||||||
|
contentType?: string;
|
||||||
|
/**
|
||||||
|
* The actual data for the file
|
||||||
|
*/
|
||||||
|
data: Buffer | Uint8Array | boolean | number | string;
|
||||||
|
/**
|
||||||
|
* An explicit key to use for key of the formdata field for this file.
|
||||||
|
* When not provided, the index of the file in the files array is used in the form `files[${index}]`.
|
||||||
|
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
|
||||||
|
*/
|
||||||
|
key?: string;
|
||||||
|
/**
|
||||||
|
* The name of the file
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents possible data to be given to an endpoint
|
||||||
|
*/
|
||||||
|
interface RequestData {
|
||||||
|
/**
|
||||||
|
* Whether to append JSON data to form data instead of `payload_json` when sending files
|
||||||
|
*/
|
||||||
|
appendToFormData?: boolean;
|
||||||
|
/**
|
||||||
|
* If this request needs the `Authorization` header
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
auth?: boolean;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for this request, useful if you use this with bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix?: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The body to send to this request.
|
||||||
|
* If providing as BodyInit, set `passThroughBody: true`
|
||||||
|
*/
|
||||||
|
body?: BodyInit | unknown;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} to use for the request.
|
||||||
|
*/
|
||||||
|
dispatcher?: Agent;
|
||||||
|
/**
|
||||||
|
* Files to be attached to this request
|
||||||
|
*/
|
||||||
|
files?: RawFile[] | undefined;
|
||||||
|
/**
|
||||||
|
* Additional headers to add to this request
|
||||||
|
*/
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* Whether to pass-through the body property directly to `fetch()`.
|
||||||
|
* <warn>This only applies when files is NOT present</warn>
|
||||||
|
*/
|
||||||
|
passThroughBody?: boolean;
|
||||||
|
/**
|
||||||
|
* Query string parameters to append to the called endpoint
|
||||||
|
*/
|
||||||
|
query?: URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Reason to show in the audit logs
|
||||||
|
*/
|
||||||
|
reason?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The signal to abort the queue entry or the REST call, where applicable
|
||||||
|
*/
|
||||||
|
signal?: AbortSignal | undefined;
|
||||||
|
/**
|
||||||
|
* If this request should be versioned
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
versioned?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible headers for an API call
|
||||||
|
*/
|
||||||
|
interface RequestHeaders {
|
||||||
|
Authorization?: string;
|
||||||
|
'User-Agent': string;
|
||||||
|
'X-Audit-Log-Reason'?: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible API methods to be used when doing requests
|
||||||
|
*/
|
||||||
|
declare enum RequestMethod {
|
||||||
|
Delete = "DELETE",
|
||||||
|
Get = "GET",
|
||||||
|
Patch = "PATCH",
|
||||||
|
Post = "POST",
|
||||||
|
Put = "PUT"
|
||||||
|
}
|
||||||
|
type RouteLike = `/${string}`;
|
||||||
|
/**
|
||||||
|
* Internal request options
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface InternalRequest extends RequestData {
|
||||||
|
fullRoute: RouteLike;
|
||||||
|
method: RequestMethod;
|
||||||
|
}
|
||||||
|
type HandlerRequestData = Pick<InternalRequest, 'auth' | 'body' | 'files' | 'signal'>;
|
||||||
|
/**
|
||||||
|
* Parsed route data for an endpoint
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface RouteData {
|
||||||
|
bucketRoute: string;
|
||||||
|
majorParameter: string;
|
||||||
|
original: RouteLike;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a hash and its associated fields
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface HashData {
|
||||||
|
lastAccess: number;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare const DefaultUserAgent: `DiscordBot (https://discord.js.org, ${string})`;
|
||||||
|
/**
|
||||||
|
* The default string to append onto the user agent.
|
||||||
|
*/
|
||||||
|
declare const DefaultUserAgentAppendix: string;
|
||||||
|
declare const DefaultRestOptions: {
|
||||||
|
readonly agent: null;
|
||||||
|
readonly api: "https://discord.com/api";
|
||||||
|
readonly authPrefix: "Bot";
|
||||||
|
readonly cdn: "https://cdn.discordapp.com";
|
||||||
|
readonly headers: {};
|
||||||
|
readonly invalidRequestWarningInterval: 0;
|
||||||
|
readonly globalRequestsPerSecond: 50;
|
||||||
|
readonly offset: 50;
|
||||||
|
readonly rejectOnRateLimit: null;
|
||||||
|
readonly retries: 3;
|
||||||
|
readonly timeout: 15000;
|
||||||
|
readonly userAgentAppendix: string;
|
||||||
|
readonly version: "10";
|
||||||
|
readonly hashSweepInterval: 14400000;
|
||||||
|
readonly hashLifetime: 86400000;
|
||||||
|
readonly handlerSweepInterval: 3600000;
|
||||||
|
readonly makeRequest: (url: string, init: undici.RequestInit) => Promise<ResponseLike>;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The events that the REST manager emits
|
||||||
|
*/
|
||||||
|
declare enum RESTEvents {
|
||||||
|
Debug = "restDebug",
|
||||||
|
HandlerSweep = "handlerSweep",
|
||||||
|
HashSweep = "hashSweep",
|
||||||
|
InvalidRequestWarning = "invalidRequestWarning",
|
||||||
|
RateLimited = "rateLimited",
|
||||||
|
Response = "response"
|
||||||
|
}
|
||||||
|
declare const ALLOWED_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif"];
|
||||||
|
declare const ALLOWED_STICKER_EXTENSIONS: readonly ["png", "json", "gif"];
|
||||||
|
declare const ALLOWED_SIZES: readonly [16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
|
||||||
|
type ImageExtension = (typeof ALLOWED_EXTENSIONS)[number];
|
||||||
|
type StickerExtension = (typeof ALLOWED_STICKER_EXTENSIONS)[number];
|
||||||
|
type ImageSize = (typeof ALLOWED_SIZES)[number];
|
||||||
|
declare const OverwrittenMimeTypes: {
|
||||||
|
readonly 'image/apng': "image/png";
|
||||||
|
};
|
||||||
|
declare const BurstHandlerMajorIdKey = "burst";
|
||||||
|
/**
|
||||||
|
* Prefix for deprecation warnings.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
declare const DEPRECATION_WARNING_PREFIX: "DeprecationWarning";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options used for image URLs
|
||||||
|
*/
|
||||||
|
interface BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: ImageExtension;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options used for image URLs with animated content
|
||||||
|
*/
|
||||||
|
interface ImageURLOptions extends BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* Whether or not to prefer the static version of an image asset.
|
||||||
|
*/
|
||||||
|
forceStatic?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options to use when making a CDN URL
|
||||||
|
*/
|
||||||
|
interface MakeURLOptions {
|
||||||
|
/**
|
||||||
|
* The allowed extensions that can be used
|
||||||
|
*/
|
||||||
|
allowedExtensions?: readonly string[];
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The CDN link builder
|
||||||
|
*/
|
||||||
|
declare class CDN {
|
||||||
|
private readonly base;
|
||||||
|
constructor(base?: string);
|
||||||
|
/**
|
||||||
|
* Generates an app asset URL for a client's asset.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the asset
|
||||||
|
* @param assetHash - The hash provided by Discord for this asset
|
||||||
|
* @param options - Optional options for the asset
|
||||||
|
*/
|
||||||
|
appAsset(clientId: string, assetHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an app icon URL for a client's icon.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
appIcon(clientId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an avatar URL, e.g. for a user or a webhook.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
avatar(id: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a user avatar decoration URL.
|
||||||
|
*
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param userAvatarDecoration - The hash provided by Discord for this avatar decoration
|
||||||
|
* @param options - Optional options for the avatar decoration
|
||||||
|
*/
|
||||||
|
avatarDecoration(userId: string, userAvatarDecoration: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a banner URL, e.g. for a user or a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the banner splash
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
banner(id: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL for a channel, e.g. a group DM.
|
||||||
|
*
|
||||||
|
* @param channelId - The channel id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this channel
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
channelIcon(channelId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a default avatar URL
|
||||||
|
*
|
||||||
|
* @param index - The default avatar index
|
||||||
|
* @remarks
|
||||||
|
* To calculate the index for a user do `(userId >> 22) % 6`,
|
||||||
|
* or `discriminator % 5` if they're using the legacy username system.
|
||||||
|
*/
|
||||||
|
defaultAvatar(index: number): string;
|
||||||
|
/**
|
||||||
|
* Generates a discovery splash URL for a guild's discovery splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the discovery splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
discoverySplash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param options - Optional options for the emoji
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param extension - The extension of the emoji
|
||||||
|
* @deprecated This overload is deprecated. Pass an object containing the extension instead.
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, extension?: ImageExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member avatar URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
guildMemberAvatar(guildId: string, userId: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member banner URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
guildMemberBanner(guildId: string, userId: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL, e.g. for a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon splash
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
icon(id: string, iconHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a URL for the icon of a role
|
||||||
|
*
|
||||||
|
* @param roleId - The id of the role that has the icon
|
||||||
|
* @param roleIconHash - The hash provided by Discord for this role icon
|
||||||
|
* @param options - Optional options for the role icon
|
||||||
|
*/
|
||||||
|
roleIcon(roleId: string, roleIconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild invite splash URL for a guild's invite splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the invite splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
splash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker URL.
|
||||||
|
*
|
||||||
|
* @param stickerId - The sticker id
|
||||||
|
* @param extension - The extension of the sticker
|
||||||
|
* @privateRemarks
|
||||||
|
* Stickers cannot have a `.webp` extension, so we default to a `.png`
|
||||||
|
*/
|
||||||
|
sticker(stickerId: string, extension?: StickerExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker pack banner URL.
|
||||||
|
*
|
||||||
|
* @param bannerId - The banner id
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
stickerPackBanner(bannerId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a team icon URL for a team's icon.
|
||||||
|
*
|
||||||
|
* @param teamId - The team id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
teamIcon(teamId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a cover image for a guild scheduled event.
|
||||||
|
*
|
||||||
|
* @param scheduledEventId - The scheduled event id
|
||||||
|
* @param coverHash - The hash provided by discord for this cover image
|
||||||
|
* @param options - Optional options for the cover image
|
||||||
|
*/
|
||||||
|
guildScheduledEventCover(scheduledEventId: string, coverHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param hash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the link
|
||||||
|
*/
|
||||||
|
private dynamicMakeURL;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param options - The extension/size options for the link
|
||||||
|
*/
|
||||||
|
private makeURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DiscordErrorFieldInformation {
|
||||||
|
code: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface DiscordErrorGroupWrapper {
|
||||||
|
_errors: DiscordError[];
|
||||||
|
}
|
||||||
|
type DiscordError = DiscordErrorFieldInformation | DiscordErrorGroupWrapper | string | {
|
||||||
|
[k: string]: DiscordError;
|
||||||
|
};
|
||||||
|
interface DiscordErrorData {
|
||||||
|
code: number;
|
||||||
|
errors?: DiscordError;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface OAuthErrorData {
|
||||||
|
error: string;
|
||||||
|
error_description?: string;
|
||||||
|
}
|
||||||
|
interface RequestBody {
|
||||||
|
files: RawFile[] | undefined;
|
||||||
|
json: unknown | undefined;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an API error returned by Discord
|
||||||
|
*/
|
||||||
|
declare class DiscordAPIError extends Error {
|
||||||
|
rawError: DiscordErrorData | OAuthErrorData;
|
||||||
|
code: number | string;
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
/**
|
||||||
|
* @param rawError - The error reported by Discord
|
||||||
|
* @param code - The error code reported by Discord
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(rawError: DiscordErrorData | OAuthErrorData, code: number | string, status: number, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
private static getMessage;
|
||||||
|
private static flattenDiscordError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a HTTP error
|
||||||
|
*/
|
||||||
|
declare class HTTPError extends Error {
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
name: string;
|
||||||
|
/**
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param statusText - The status text of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(status: number, statusText: string, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class RateLimitError extends Error implements RateLimitData {
|
||||||
|
timeToReset: number;
|
||||||
|
limit: number;
|
||||||
|
method: string;
|
||||||
|
hash: string;
|
||||||
|
url: string;
|
||||||
|
route: string;
|
||||||
|
majorParameter: string;
|
||||||
|
global: boolean;
|
||||||
|
retryAfter: number;
|
||||||
|
sublimitTimeout: number;
|
||||||
|
scope: RateLimitData['scope'];
|
||||||
|
constructor({ timeToReset, limit, method, hash, url, route, majorParameter, global, retryAfter, sublimitTimeout, scope, }: RateLimitData);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the class that manages handlers for endpoints
|
||||||
|
*/
|
||||||
|
declare class REST extends AsyncEventEmitter<RestEventsMap> {
|
||||||
|
#private;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
|
||||||
|
* performed by this manager.
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
readonly cdn: CDN;
|
||||||
|
/**
|
||||||
|
* The number of requests remaining in the global bucket
|
||||||
|
*/
|
||||||
|
globalRemaining: number;
|
||||||
|
/**
|
||||||
|
* The promise used to wait out the global rate limit
|
||||||
|
*/
|
||||||
|
globalDelay: Promise<void> | null;
|
||||||
|
/**
|
||||||
|
* The timestamp at which the global bucket resets
|
||||||
|
*/
|
||||||
|
globalReset: number;
|
||||||
|
/**
|
||||||
|
* API bucket hashes that are cached from provided routes
|
||||||
|
*/
|
||||||
|
readonly hashes: Collection<string, HashData>;
|
||||||
|
/**
|
||||||
|
* Request handlers created from the bucket hash and the major parameters
|
||||||
|
*/
|
||||||
|
readonly handlers: Collection<string, IHandler>;
|
||||||
|
private hashTimer;
|
||||||
|
private handlerTimer;
|
||||||
|
readonly options: RESTOptions;
|
||||||
|
constructor(options?: Partial<RESTOptions>);
|
||||||
|
private setupSweepers;
|
||||||
|
/**
|
||||||
|
* Runs a get request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
get(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a delete request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
delete(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a post request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
post(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a put request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
put(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a patch request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
patch(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a request from the api
|
||||||
|
*
|
||||||
|
* @param options - Request options
|
||||||
|
*/
|
||||||
|
request(options: InternalRequest): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Sets the default agent to use for requests performed by this manager
|
||||||
|
*
|
||||||
|
* @param agent - The agent to use
|
||||||
|
*/
|
||||||
|
setAgent(agent: Dispatcher): this;
|
||||||
|
/**
|
||||||
|
* Sets the authorization token that should be used for requests
|
||||||
|
*
|
||||||
|
* @param token - The authorization token to use
|
||||||
|
*/
|
||||||
|
setToken(token: string): this;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param request - All the information needed to make a request
|
||||||
|
* @returns The response from the api request
|
||||||
|
*/
|
||||||
|
queueRequest(request: InternalRequest): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* Creates a new rate limit handler from a hash, based on the hash and the major parameter
|
||||||
|
*
|
||||||
|
* @param hash - The hash for the route
|
||||||
|
* @param majorParameter - The major parameter for this handler
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private createHandler;
|
||||||
|
/**
|
||||||
|
* Formats the request data to a usable format for fetch
|
||||||
|
*
|
||||||
|
* @param request - The request data
|
||||||
|
*/
|
||||||
|
private resolveRequest;
|
||||||
|
/**
|
||||||
|
* Stops the hash sweeping interval
|
||||||
|
*/
|
||||||
|
clearHashSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Stops the request handler sweeping interval
|
||||||
|
*/
|
||||||
|
clearHandlerSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Generates route data for an endpoint:method
|
||||||
|
*
|
||||||
|
* @param endpoint - The raw endpoint to generalize
|
||||||
|
* @param method - The HTTP method this endpoint is called without
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static generateRouteData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and populates an URLSearchParams instance from an object, stripping
|
||||||
|
* out null and undefined values, while also coercing non-strings to strings.
|
||||||
|
*
|
||||||
|
* @param options - The options to use
|
||||||
|
* @returns A populated URLSearchParams instance
|
||||||
|
*/
|
||||||
|
declare function makeURLSearchParams<OptionsType extends object>(options?: Readonly<OptionsType>): url.URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Converts the response to usable data
|
||||||
|
*
|
||||||
|
* @param res - The fetch response
|
||||||
|
*/
|
||||||
|
declare function parseResponse(res: ResponseLike): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Calculates the default avatar index for a given user id.
|
||||||
|
*
|
||||||
|
* @param userId - The user id to calculate the default avatar index for
|
||||||
|
*/
|
||||||
|
declare function calculateUserDefaultAvatarIndex(userId: Snowflake): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/rest#readme | @discordjs/rest} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { ALLOWED_EXTENSIONS, ALLOWED_SIZES, ALLOWED_STICKER_EXTENSIONS, APIRequest, BaseImageURLOptions, BurstHandlerMajorIdKey, CDN, DEPRECATION_WARNING_PREFIX, DefaultRestOptions, DefaultUserAgent, DefaultUserAgentAppendix, DiscordAPIError, DiscordErrorData, HTTPError, HandlerRequestData, HashData, ImageExtension, ImageSize, ImageURLOptions, InternalRequest, InvalidRequestWarningData, MakeURLOptions, OAuthErrorData, OverwrittenMimeTypes, REST, RESTEvents, RESTOptions, RateLimitData, RateLimitError, RateLimitQueueFilter, RawFile, RequestBody, RequestData, RequestHeaders, RequestMethod, ResponseLike, RestEvents, RestEventsMap, RouteData, RouteLike, StickerExtension, calculateUserDefaultAvatarIndex, makeURLSearchParams, parseResponse, version };
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,13 @@
|
||||||
|
import { Response, request, RequestInit } from 'undici';
|
||||||
|
import { Readable } from 'node:stream';
|
||||||
|
import { ReadableStream } from 'node:stream/web';
|
||||||
|
|
||||||
|
interface ResponseLike extends Pick<Response, 'arrayBuffer' | 'bodyUsed' | 'headers' | 'json' | 'ok' | 'status' | 'statusText' | 'text'> {
|
||||||
|
body: Readable | ReadableStream | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>;
|
||||||
|
declare function makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
|
||||||
|
declare function resolveBody(body: RequestInit['body']): Promise<Exclude<RequestOptions['body'], undefined>>;
|
||||||
|
|
||||||
|
export { RequestOptions, makeRequest, resolveBody };
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { Response, request, RequestInit } from 'undici';
|
||||||
|
import { Readable } from 'node:stream';
|
||||||
|
import { ReadableStream } from 'node:stream/web';
|
||||||
|
|
||||||
|
interface ResponseLike extends Pick<Response, 'arrayBuffer' | 'bodyUsed' | 'headers' | 'json' | 'ok' | 'status' | 'statusText' | 'text'> {
|
||||||
|
body: Readable | ReadableStream | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>;
|
||||||
|
declare function makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
|
||||||
|
declare function resolveBody(body: RequestInit['body']): Promise<Exclude<RequestOptions['body'], undefined>>;
|
||||||
|
|
||||||
|
export { RequestOptions, makeRequest, resolveBody };
|
|
@ -0,0 +1,94 @@
|
||||||
|
"use strict";
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
var __export = (target, all) => {
|
||||||
|
for (var name in all)
|
||||||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||||||
|
};
|
||||||
|
var __copyProps = (to, from, except, desc) => {
|
||||||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||||||
|
for (let key of __getOwnPropNames(from))
|
||||||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
};
|
||||||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||||
|
|
||||||
|
// src/strategies/undiciRequest.ts
|
||||||
|
var undiciRequest_exports = {};
|
||||||
|
__export(undiciRequest_exports, {
|
||||||
|
makeRequest: () => makeRequest,
|
||||||
|
resolveBody: () => resolveBody
|
||||||
|
});
|
||||||
|
module.exports = __toCommonJS(undiciRequest_exports);
|
||||||
|
var import_node_http = require("http");
|
||||||
|
var import_node_url = require("url");
|
||||||
|
var import_node_util = require("util");
|
||||||
|
var import_undici = require("undici");
|
||||||
|
async function makeRequest(url, init) {
|
||||||
|
const options = {
|
||||||
|
...init,
|
||||||
|
body: await resolveBody(init.body)
|
||||||
|
};
|
||||||
|
const res = await (0, import_undici.request)(url, options);
|
||||||
|
return {
|
||||||
|
body: res.body,
|
||||||
|
async arrayBuffer() {
|
||||||
|
return res.body.arrayBuffer();
|
||||||
|
},
|
||||||
|
async json() {
|
||||||
|
return res.body.json();
|
||||||
|
},
|
||||||
|
async text() {
|
||||||
|
return res.body.text();
|
||||||
|
},
|
||||||
|
get bodyUsed() {
|
||||||
|
return res.body.bodyUsed;
|
||||||
|
},
|
||||||
|
headers: new import_undici.Headers(res.headers),
|
||||||
|
status: res.statusCode,
|
||||||
|
statusText: import_node_http.STATUS_CODES[res.statusCode],
|
||||||
|
ok: res.statusCode >= 200 && res.statusCode < 300
|
||||||
|
};
|
||||||
|
}
|
||||||
|
__name(makeRequest, "makeRequest");
|
||||||
|
async function resolveBody(body) {
|
||||||
|
if (body == null) {
|
||||||
|
return null;
|
||||||
|
} else if (typeof body === "string") {
|
||||||
|
return body;
|
||||||
|
} else if (import_node_util.types.isUint8Array(body)) {
|
||||||
|
return body;
|
||||||
|
} else if (import_node_util.types.isArrayBuffer(body)) {
|
||||||
|
return new Uint8Array(body);
|
||||||
|
} else if (body instanceof import_node_url.URLSearchParams) {
|
||||||
|
return body.toString();
|
||||||
|
} else if (body instanceof DataView) {
|
||||||
|
return new Uint8Array(body.buffer);
|
||||||
|
} else if (body instanceof Blob) {
|
||||||
|
return new Uint8Array(await body.arrayBuffer());
|
||||||
|
} else if (body instanceof FormData) {
|
||||||
|
return body;
|
||||||
|
} else if (body[Symbol.iterator]) {
|
||||||
|
const chunks = [...body];
|
||||||
|
return Buffer.concat(chunks);
|
||||||
|
} else if (body[Symbol.asyncIterator]) {
|
||||||
|
const chunks = [];
|
||||||
|
for await (const chunk of body) {
|
||||||
|
chunks.push(chunk);
|
||||||
|
}
|
||||||
|
return Buffer.concat(chunks);
|
||||||
|
}
|
||||||
|
throw new TypeError(`Unable to resolve body.`);
|
||||||
|
}
|
||||||
|
__name(resolveBody, "resolveBody");
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
makeRequest,
|
||||||
|
resolveBody
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=undiciRequest.js.map
|
|
@ -0,0 +1 @@
|
||||||
|
{"version":3,"sources":["../../src/strategies/undiciRequest.ts"],"sourcesContent":["import { STATUS_CODES } from 'node:http';\nimport { URLSearchParams } from 'node:url';\nimport { types } from 'node:util';\nimport { type RequestInit, request, Headers } from 'undici';\nimport type { ResponseLike } from '../shared.js';\n\nexport type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>;\n\nexport async function makeRequest(url: string, init: RequestInit): Promise<ResponseLike> {\n\t// The cast is necessary because `headers` and `method` are narrower types in `undici.request`\n\t// our request path guarantees they are of acceptable type for `undici.request`\n\tconst options = {\n\t\t...init,\n\t\tbody: await resolveBody(init.body),\n\t} as RequestOptions;\n\tconst res = await request(url, options);\n\treturn {\n\t\tbody: res.body,\n\t\tasync arrayBuffer() {\n\t\t\treturn res.body.arrayBuffer();\n\t\t},\n\t\tasync json() {\n\t\t\treturn res.body.json();\n\t\t},\n\t\tasync text() {\n\t\t\treturn res.body.text();\n\t\t},\n\t\tget bodyUsed() {\n\t\t\treturn res.body.bodyUsed;\n\t\t},\n\t\theaders: new Headers(res.headers as Record<string, string[] | string>),\n\t\tstatus: res.statusCode,\n\t\tstatusText: STATUS_CODES[res.statusCode]!,\n\t\tok: res.statusCode >= 200 && res.statusCode < 300,\n\t};\n}\n\nexport async function resolveBody(body: RequestInit['body']): Promise<Exclude<RequestOptions['body'], undefined>> {\n\t// eslint-disable-next-line no-eq-null, eqeqeq\n\tif (body == null) {\n\t\treturn null;\n\t} else if (typeof body === 'string') {\n\t\treturn body;\n\t} else if (types.isUint8Array(body)) {\n\t\treturn body;\n\t} else if (types.isArrayBuffer(body)) {\n\t\treturn new Uint8Array(body);\n\t} else if (body instanceof URLSearchParams) {\n\t\treturn body.toString();\n\t} else if (body instanceof DataView) {\n\t\treturn new Uint8Array(body.buffer);\n\t} else if (body instanceof Blob) {\n\t\treturn new Uint8Array(await body.arrayBuffer());\n\t} else if (body instanceof FormData) {\n\t\treturn body;\n\t} else if ((body as Iterable<Uint8Array>)[Symbol.iterator]) {\n\t\tconst chunks = [...(body as Iterable<Uint8Array>)];\n\n\t\treturn Buffer.concat(chunks);\n\t} else if ((body as AsyncIterable<Uint8Array>)[Symbol.asyncIterator]) {\n\t\tconst chunks: Uint8Array[] = [];\n\n\t\tfor await (const chunk of body as AsyncIterable<Uint8Array>) {\n\t\t\tchunks.push(chunk);\n\t\t}\n\n\t\treturn Buffer.concat(chunks);\n\t}\n\n\tthrow new TypeError(`Unable to resolve body.`);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,uBAA6B;AAC7B,sBAAgC;AAChC,uBAAsB;AACtB,oBAAmD;AAKnD,eAAsB,YAAY,KAAa,MAA0C;AAGxF,QAAM,UAAU;AAAA,IACf,GAAG;AAAA,IACH,MAAM,MAAM,YAAY,KAAK,IAAI;AAAA,EAClC;AACA,QAAM,MAAM,UAAM,uBAAQ,KAAK,OAAO;AACtC,SAAO;AAAA,IACN,MAAM,IAAI;AAAA,IACV,MAAM,cAAc;AACnB,aAAO,IAAI,KAAK,YAAY;AAAA,IAC7B;AAAA,IACA,MAAM,OAAO;AACZ,aAAO,IAAI,KAAK,KAAK;AAAA,IACtB;AAAA,IACA,MAAM,OAAO;AACZ,aAAO,IAAI,KAAK,KAAK;AAAA,IACtB;AAAA,IACA,IAAI,WAAW;AACd,aAAO,IAAI,KAAK;AAAA,IACjB;AAAA,IACA,SAAS,IAAI,sBAAQ,IAAI,OAA4C;AAAA,IACrE,QAAQ,IAAI;AAAA,IACZ,YAAY,8BAAa,IAAI,UAAU;AAAA,IACvC,IAAI,IAAI,cAAc,OAAO,IAAI,aAAa;AAAA,EAC/C;AACD;AA3BsB;AA6BtB,eAAsB,YAAY,MAAgF;AAEjH,MAAI,QAAQ,MAAM;AACjB,WAAO;AAAA,EACR,WAAW,OAAO,SAAS,UAAU;AACpC,WAAO;AAAA,EACR,WAAW,uBAAM,aAAa,IAAI,GAAG;AACpC,WAAO;AAAA,EACR,WAAW,uBAAM,cAAc,IAAI,GAAG;AACrC,WAAO,IAAI,WAAW,IAAI;AAAA,EAC3B,WAAW,gBAAgB,iCAAiB;AAC3C,WAAO,KAAK,SAAS;AAAA,EACtB,WAAW,gBAAgB,UAAU;AACpC,WAAO,IAAI,WAAW,KAAK,MAAM;AAAA,EAClC,WAAW,gBAAgB,MAAM;AAChC,WAAO,IAAI,WAAW,MAAM,KAAK,YAAY,CAAC;AAAA,EAC/C,WAAW,gBAAgB,UAAU;AACpC,WAAO;AAAA,EACR,WAAY,KAA8B,OAAO,QAAQ,GAAG;AAC3D,UAAM,SAAS,CAAC,GAAI,IAA6B;AAEjD,WAAO,OAAO,OAAO,MAAM;AAAA,EAC5B,WAAY,KAAmC,OAAO,aAAa,GAAG;AACrE,UAAM,SAAuB,CAAC;AAE9B,qBAAiB,SAAS,MAAmC;AAC5D,aAAO,KAAK,KAAK;AAAA,IAClB;AAEA,WAAO,OAAO,OAAO,MAAM;AAAA,EAC5B;AAEA,QAAM,IAAI,UAAU,yBAAyB;AAC9C;AAjCsB;","names":[]}
|
|
@ -0,0 +1,70 @@
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
|
||||||
|
// src/strategies/undiciRequest.ts
|
||||||
|
import { STATUS_CODES } from "node:http";
|
||||||
|
import { URLSearchParams } from "node:url";
|
||||||
|
import { types } from "node:util";
|
||||||
|
import { request, Headers } from "undici";
|
||||||
|
async function makeRequest(url, init) {
|
||||||
|
const options = {
|
||||||
|
...init,
|
||||||
|
body: await resolveBody(init.body)
|
||||||
|
};
|
||||||
|
const res = await request(url, options);
|
||||||
|
return {
|
||||||
|
body: res.body,
|
||||||
|
async arrayBuffer() {
|
||||||
|
return res.body.arrayBuffer();
|
||||||
|
},
|
||||||
|
async json() {
|
||||||
|
return res.body.json();
|
||||||
|
},
|
||||||
|
async text() {
|
||||||
|
return res.body.text();
|
||||||
|
},
|
||||||
|
get bodyUsed() {
|
||||||
|
return res.body.bodyUsed;
|
||||||
|
},
|
||||||
|
headers: new Headers(res.headers),
|
||||||
|
status: res.statusCode,
|
||||||
|
statusText: STATUS_CODES[res.statusCode],
|
||||||
|
ok: res.statusCode >= 200 && res.statusCode < 300
|
||||||
|
};
|
||||||
|
}
|
||||||
|
__name(makeRequest, "makeRequest");
|
||||||
|
async function resolveBody(body) {
|
||||||
|
if (body == null) {
|
||||||
|
return null;
|
||||||
|
} else if (typeof body === "string") {
|
||||||
|
return body;
|
||||||
|
} else if (types.isUint8Array(body)) {
|
||||||
|
return body;
|
||||||
|
} else if (types.isArrayBuffer(body)) {
|
||||||
|
return new Uint8Array(body);
|
||||||
|
} else if (body instanceof URLSearchParams) {
|
||||||
|
return body.toString();
|
||||||
|
} else if (body instanceof DataView) {
|
||||||
|
return new Uint8Array(body.buffer);
|
||||||
|
} else if (body instanceof Blob) {
|
||||||
|
return new Uint8Array(await body.arrayBuffer());
|
||||||
|
} else if (body instanceof FormData) {
|
||||||
|
return body;
|
||||||
|
} else if (body[Symbol.iterator]) {
|
||||||
|
const chunks = [...body];
|
||||||
|
return Buffer.concat(chunks);
|
||||||
|
} else if (body[Symbol.asyncIterator]) {
|
||||||
|
const chunks = [];
|
||||||
|
for await (const chunk of body) {
|
||||||
|
chunks.push(chunk);
|
||||||
|
}
|
||||||
|
return Buffer.concat(chunks);
|
||||||
|
}
|
||||||
|
throw new TypeError(`Unable to resolve body.`);
|
||||||
|
}
|
||||||
|
__name(resolveBody, "resolveBody");
|
||||||
|
export {
|
||||||
|
makeRequest,
|
||||||
|
resolveBody
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=undiciRequest.mjs.map
|
|
@ -0,0 +1 @@
|
||||||
|
{"version":3,"sources":["../../src/strategies/undiciRequest.ts"],"sourcesContent":["import { STATUS_CODES } from 'node:http';\nimport { URLSearchParams } from 'node:url';\nimport { types } from 'node:util';\nimport { type RequestInit, request, Headers } from 'undici';\nimport type { ResponseLike } from '../shared.js';\n\nexport type RequestOptions = Exclude<Parameters<typeof request>[1], undefined>;\n\nexport async function makeRequest(url: string, init: RequestInit): Promise<ResponseLike> {\n\t// The cast is necessary because `headers` and `method` are narrower types in `undici.request`\n\t// our request path guarantees they are of acceptable type for `undici.request`\n\tconst options = {\n\t\t...init,\n\t\tbody: await resolveBody(init.body),\n\t} as RequestOptions;\n\tconst res = await request(url, options);\n\treturn {\n\t\tbody: res.body,\n\t\tasync arrayBuffer() {\n\t\t\treturn res.body.arrayBuffer();\n\t\t},\n\t\tasync json() {\n\t\t\treturn res.body.json();\n\t\t},\n\t\tasync text() {\n\t\t\treturn res.body.text();\n\t\t},\n\t\tget bodyUsed() {\n\t\t\treturn res.body.bodyUsed;\n\t\t},\n\t\theaders: new Headers(res.headers as Record<string, string[] | string>),\n\t\tstatus: res.statusCode,\n\t\tstatusText: STATUS_CODES[res.statusCode]!,\n\t\tok: res.statusCode >= 200 && res.statusCode < 300,\n\t};\n}\n\nexport async function resolveBody(body: RequestInit['body']): Promise<Exclude<RequestOptions['body'], undefined>> {\n\t// eslint-disable-next-line no-eq-null, eqeqeq\n\tif (body == null) {\n\t\treturn null;\n\t} else if (typeof body === 'string') {\n\t\treturn body;\n\t} else if (types.isUint8Array(body)) {\n\t\treturn body;\n\t} else if (types.isArrayBuffer(body)) {\n\t\treturn new Uint8Array(body);\n\t} else if (body instanceof URLSearchParams) {\n\t\treturn body.toString();\n\t} else if (body instanceof DataView) {\n\t\treturn new Uint8Array(body.buffer);\n\t} else if (body instanceof Blob) {\n\t\treturn new Uint8Array(await body.arrayBuffer());\n\t} else if (body instanceof FormData) {\n\t\treturn body;\n\t} else if ((body as Iterable<Uint8Array>)[Symbol.iterator]) {\n\t\tconst chunks = [...(body as Iterable<Uint8Array>)];\n\n\t\treturn Buffer.concat(chunks);\n\t} else if ((body as AsyncIterable<Uint8Array>)[Symbol.asyncIterator]) {\n\t\tconst chunks: Uint8Array[] = [];\n\n\t\tfor await (const chunk of body as AsyncIterable<Uint8Array>) {\n\t\t\tchunks.push(chunk);\n\t\t}\n\n\t\treturn Buffer.concat(chunks);\n\t}\n\n\tthrow new TypeError(`Unable to resolve body.`);\n}\n"],"mappings":";;;;AAAA,SAAS,oBAAoB;AAC7B,SAAS,uBAAuB;AAChC,SAAS,aAAa;AACtB,SAA2B,SAAS,eAAe;AAKnD,eAAsB,YAAY,KAAa,MAA0C;AAGxF,QAAM,UAAU;AAAA,IACf,GAAG;AAAA,IACH,MAAM,MAAM,YAAY,KAAK,IAAI;AAAA,EAClC;AACA,QAAM,MAAM,MAAM,QAAQ,KAAK,OAAO;AACtC,SAAO;AAAA,IACN,MAAM,IAAI;AAAA,IACV,MAAM,cAAc;AACnB,aAAO,IAAI,KAAK,YAAY;AAAA,IAC7B;AAAA,IACA,MAAM,OAAO;AACZ,aAAO,IAAI,KAAK,KAAK;AAAA,IACtB;AAAA,IACA,MAAM,OAAO;AACZ,aAAO,IAAI,KAAK,KAAK;AAAA,IACtB;AAAA,IACA,IAAI,WAAW;AACd,aAAO,IAAI,KAAK;AAAA,IACjB;AAAA,IACA,SAAS,IAAI,QAAQ,IAAI,OAA4C;AAAA,IACrE,QAAQ,IAAI;AAAA,IACZ,YAAY,aAAa,IAAI,UAAU;AAAA,IACvC,IAAI,IAAI,cAAc,OAAO,IAAI,aAAa;AAAA,EAC/C;AACD;AA3BsB;AA6BtB,eAAsB,YAAY,MAAgF;AAEjH,MAAI,QAAQ,MAAM;AACjB,WAAO;AAAA,EACR,WAAW,OAAO,SAAS,UAAU;AACpC,WAAO;AAAA,EACR,WAAW,MAAM,aAAa,IAAI,GAAG;AACpC,WAAO;AAAA,EACR,WAAW,MAAM,cAAc,IAAI,GAAG;AACrC,WAAO,IAAI,WAAW,IAAI;AAAA,EAC3B,WAAW,gBAAgB,iBAAiB;AAC3C,WAAO,KAAK,SAAS;AAAA,EACtB,WAAW,gBAAgB,UAAU;AACpC,WAAO,IAAI,WAAW,KAAK,MAAM;AAAA,EAClC,WAAW,gBAAgB,MAAM;AAChC,WAAO,IAAI,WAAW,MAAM,KAAK,YAAY,CAAC;AAAA,EAC/C,WAAW,gBAAgB,UAAU;AACpC,WAAO;AAAA,EACR,WAAY,KAA8B,OAAO,QAAQ,GAAG;AAC3D,UAAM,SAAS,CAAC,GAAI,IAA6B;AAEjD,WAAO,OAAO,OAAO,MAAM;AAAA,EAC5B,WAAY,KAAmC,OAAO,aAAa,GAAG;AACrE,UAAM,SAAuB,CAAC;AAE9B,qBAAiB,SAAS,MAAmC;AAC5D,aAAO,KAAK,KAAK;AAAA,IAClB;AAEA,WAAO,OAAO,OAAO,MAAM;AAAA,EAC5B;AAEA,QAAM,IAAI,UAAU,yBAAyB;AAC9C;AAjCsB;","names":[]}
|
|
@ -0,0 +1,901 @@
|
||||||
|
import * as url from 'url';
|
||||||
|
import { Snowflake } from 'discord-api-types/v10';
|
||||||
|
import { Readable } from 'node:stream';
|
||||||
|
import { ReadableStream } from 'node:stream/web';
|
||||||
|
import { Collection } from '@discordjs/collection';
|
||||||
|
import { Awaitable } from '@discordjs/util';
|
||||||
|
import * as undici from 'undici';
|
||||||
|
import { RequestInit, Dispatcher, Response, BodyInit, Agent } from 'undici';
|
||||||
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||||
|
|
||||||
|
interface IHandler {
|
||||||
|
/**
|
||||||
|
* The unique id of the handler
|
||||||
|
*/
|
||||||
|
readonly id: string;
|
||||||
|
/**
|
||||||
|
* If the bucket is currently inactive (no pending requests)
|
||||||
|
*/
|
||||||
|
get inactive(): boolean;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param routeId - The generalized api route with literal ids for major parameters
|
||||||
|
* @param url - The url to do the request on
|
||||||
|
* @param options - All the information needed to make a request
|
||||||
|
* @param requestData - Extra data from the user's request needed for errors and additional processing
|
||||||
|
*/
|
||||||
|
queueRequest(routeId: RouteData, url: string, options: RequestInit, requestData: HandlerRequestData): Promise<ResponseLike>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RestEvents {
|
||||||
|
handlerSweep: [sweptHandlers: Collection<string, IHandler>];
|
||||||
|
hashSweep: [sweptHashes: Collection<string, HashData>];
|
||||||
|
invalidRequestWarning: [invalidRequestInfo: InvalidRequestWarningData];
|
||||||
|
rateLimited: [rateLimitInfo: RateLimitData];
|
||||||
|
response: [request: APIRequest, response: ResponseLike];
|
||||||
|
restDebug: [info: string];
|
||||||
|
}
|
||||||
|
type RestEventsMap = {
|
||||||
|
[K in keyof RestEvents]: RestEvents[K];
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Options to be passed when creating the REST instance
|
||||||
|
*/
|
||||||
|
interface RESTOptions {
|
||||||
|
/**
|
||||||
|
* The agent to set globally
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
/**
|
||||||
|
* The base api path, without version
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://discord.com/api'`
|
||||||
|
*/
|
||||||
|
api: string;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for requests, useful if you want to use
|
||||||
|
* bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The cdn path
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://cdn.discordapp.com'`
|
||||||
|
*/
|
||||||
|
cdn: string;
|
||||||
|
/**
|
||||||
|
* How many requests to allow sending per second (Infinity for unlimited, 50 for the standard global limit used by Discord)
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
globalRequestsPerSecond: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 1h)
|
||||||
|
*
|
||||||
|
* @defaultValue `3_600_000`
|
||||||
|
*/
|
||||||
|
handlerSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* The maximum amount of time a hash can exist in milliseconds without being hit with a request (defaults to 24h)
|
||||||
|
*
|
||||||
|
* @defaultValue `86_400_000`
|
||||||
|
*/
|
||||||
|
hashLifetime: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 4h)
|
||||||
|
*
|
||||||
|
* @defaultValue `14_400_000`
|
||||||
|
*/
|
||||||
|
hashSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* Additional headers to send for all API requests
|
||||||
|
*
|
||||||
|
* @defaultValue `{}`
|
||||||
|
*/
|
||||||
|
headers: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* The number of invalid REST requests (those that return 401, 403, or 429) in a 10 minute window between emitted warnings (0 for no warnings).
|
||||||
|
* That is, if set to 500, warnings will be emitted at invalid request number 500, 1000, 1500, and so on.
|
||||||
|
*
|
||||||
|
* @defaultValue `0`
|
||||||
|
*/
|
||||||
|
invalidRequestWarningInterval: number;
|
||||||
|
/**
|
||||||
|
* The method called to perform the actual HTTP request given a url and web `fetch` options
|
||||||
|
* For example, to use global fetch, simply provide `makeRequest: fetch`
|
||||||
|
*/
|
||||||
|
makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* The extra offset to add to rate limits in milliseconds
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
offset: number;
|
||||||
|
/**
|
||||||
|
* Determines how rate limiting and pre-emptive throttling should be handled.
|
||||||
|
* When an array of strings, each element is treated as a prefix for the request route
|
||||||
|
* (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
|
||||||
|
* for which to throw {@link RateLimitError}s. All other request routes will be queued normally
|
||||||
|
*
|
||||||
|
* @defaultValue `null`
|
||||||
|
*/
|
||||||
|
rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
|
||||||
|
/**
|
||||||
|
* The number of retries for errors with the 500 code, or errors
|
||||||
|
* that timeout
|
||||||
|
*
|
||||||
|
* @defaultValue `3`
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The time to wait in milliseconds before a request is aborted
|
||||||
|
*
|
||||||
|
* @defaultValue `15_000`
|
||||||
|
*/
|
||||||
|
timeout: number;
|
||||||
|
/**
|
||||||
|
* Extra information to add to the user agent
|
||||||
|
*
|
||||||
|
* @defaultValue DefaultUserAgentAppendix
|
||||||
|
*/
|
||||||
|
userAgentAppendix: string;
|
||||||
|
/**
|
||||||
|
* The version of the API to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'10'`
|
||||||
|
*/
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Data emitted on `RESTEvents.RateLimited`
|
||||||
|
*/
|
||||||
|
interface RateLimitData {
|
||||||
|
/**
|
||||||
|
* Whether the rate limit that was reached was the global limit
|
||||||
|
*/
|
||||||
|
global: boolean;
|
||||||
|
/**
|
||||||
|
* The bucket hash for this request
|
||||||
|
*/
|
||||||
|
hash: string;
|
||||||
|
/**
|
||||||
|
* The amount of requests we can perform before locking requests
|
||||||
|
*/
|
||||||
|
limit: number;
|
||||||
|
/**
|
||||||
|
* The major parameter of the route
|
||||||
|
*
|
||||||
|
* For example, in `/channels/x`, this will be `x`.
|
||||||
|
* If there is no major parameter (e.g: `/bot/gateway`) this will be `global`.
|
||||||
|
*/
|
||||||
|
majorParameter: string;
|
||||||
|
/**
|
||||||
|
* The HTTP method being performed
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before this specific request can be retried
|
||||||
|
*/
|
||||||
|
retryAfter: number;
|
||||||
|
/**
|
||||||
|
* The route being hit in this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
/**
|
||||||
|
* The scope of the rate limit that was hit.
|
||||||
|
*
|
||||||
|
* This can be `user` for rate limits that are per client, `global` for rate limits that affect all clients or `shared` for rate limits that
|
||||||
|
* are shared per resource.
|
||||||
|
*/
|
||||||
|
scope: 'global' | 'shared' | 'user';
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before the sublimit lock for the route resets, and requests that fall under a sublimit
|
||||||
|
* can be retried
|
||||||
|
*
|
||||||
|
* This is only present on certain sublimits, and `0` otherwise
|
||||||
|
*/
|
||||||
|
sublimitTimeout: number;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, until the route's request-lock is reset
|
||||||
|
*/
|
||||||
|
timeToReset: number;
|
||||||
|
/**
|
||||||
|
* The full URL for this request
|
||||||
|
*/
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A function that determines whether the rate limit hit should throw an Error
|
||||||
|
*/
|
||||||
|
type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Awaitable<boolean>;
|
||||||
|
interface APIRequest {
|
||||||
|
/**
|
||||||
|
* The data that was used to form the body of this request
|
||||||
|
*/
|
||||||
|
data: HandlerRequestData;
|
||||||
|
/**
|
||||||
|
* The HTTP method used in this request
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* Additional HTTP options for this request
|
||||||
|
*/
|
||||||
|
options: RequestInit;
|
||||||
|
/**
|
||||||
|
* The full path used to make the request
|
||||||
|
*/
|
||||||
|
path: RouteLike;
|
||||||
|
/**
|
||||||
|
* The number of times this request has been attempted
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The API route identifying the ratelimit for this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
}
|
||||||
|
interface ResponseLike extends Pick<Response, 'arrayBuffer' | 'bodyUsed' | 'headers' | 'json' | 'ok' | 'status' | 'statusText' | 'text'> {
|
||||||
|
body: Readable | ReadableStream | null;
|
||||||
|
}
|
||||||
|
interface InvalidRequestWarningData {
|
||||||
|
/**
|
||||||
|
* Number of invalid requests that have been made in the window
|
||||||
|
*/
|
||||||
|
count: number;
|
||||||
|
/**
|
||||||
|
* Time in milliseconds remaining before the count resets
|
||||||
|
*/
|
||||||
|
remainingTime: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a file to be added to the request
|
||||||
|
*/
|
||||||
|
interface RawFile {
|
||||||
|
/**
|
||||||
|
* Content-Type of the file
|
||||||
|
*/
|
||||||
|
contentType?: string;
|
||||||
|
/**
|
||||||
|
* The actual data for the file
|
||||||
|
*/
|
||||||
|
data: Buffer | Uint8Array | boolean | number | string;
|
||||||
|
/**
|
||||||
|
* An explicit key to use for key of the formdata field for this file.
|
||||||
|
* When not provided, the index of the file in the files array is used in the form `files[${index}]`.
|
||||||
|
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
|
||||||
|
*/
|
||||||
|
key?: string;
|
||||||
|
/**
|
||||||
|
* The name of the file
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents possible data to be given to an endpoint
|
||||||
|
*/
|
||||||
|
interface RequestData {
|
||||||
|
/**
|
||||||
|
* Whether to append JSON data to form data instead of `payload_json` when sending files
|
||||||
|
*/
|
||||||
|
appendToFormData?: boolean;
|
||||||
|
/**
|
||||||
|
* If this request needs the `Authorization` header
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
auth?: boolean;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for this request, useful if you use this with bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix?: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The body to send to this request.
|
||||||
|
* If providing as BodyInit, set `passThroughBody: true`
|
||||||
|
*/
|
||||||
|
body?: BodyInit | unknown;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} to use for the request.
|
||||||
|
*/
|
||||||
|
dispatcher?: Agent;
|
||||||
|
/**
|
||||||
|
* Files to be attached to this request
|
||||||
|
*/
|
||||||
|
files?: RawFile[] | undefined;
|
||||||
|
/**
|
||||||
|
* Additional headers to add to this request
|
||||||
|
*/
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* Whether to pass-through the body property directly to `fetch()`.
|
||||||
|
* <warn>This only applies when files is NOT present</warn>
|
||||||
|
*/
|
||||||
|
passThroughBody?: boolean;
|
||||||
|
/**
|
||||||
|
* Query string parameters to append to the called endpoint
|
||||||
|
*/
|
||||||
|
query?: URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Reason to show in the audit logs
|
||||||
|
*/
|
||||||
|
reason?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The signal to abort the queue entry or the REST call, where applicable
|
||||||
|
*/
|
||||||
|
signal?: AbortSignal | undefined;
|
||||||
|
/**
|
||||||
|
* If this request should be versioned
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
versioned?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible headers for an API call
|
||||||
|
*/
|
||||||
|
interface RequestHeaders {
|
||||||
|
Authorization?: string;
|
||||||
|
'User-Agent': string;
|
||||||
|
'X-Audit-Log-Reason'?: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible API methods to be used when doing requests
|
||||||
|
*/
|
||||||
|
declare enum RequestMethod {
|
||||||
|
Delete = "DELETE",
|
||||||
|
Get = "GET",
|
||||||
|
Patch = "PATCH",
|
||||||
|
Post = "POST",
|
||||||
|
Put = "PUT"
|
||||||
|
}
|
||||||
|
type RouteLike = `/${string}`;
|
||||||
|
/**
|
||||||
|
* Internal request options
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface InternalRequest extends RequestData {
|
||||||
|
fullRoute: RouteLike;
|
||||||
|
method: RequestMethod;
|
||||||
|
}
|
||||||
|
type HandlerRequestData = Pick<InternalRequest, 'auth' | 'body' | 'files' | 'signal'>;
|
||||||
|
/**
|
||||||
|
* Parsed route data for an endpoint
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface RouteData {
|
||||||
|
bucketRoute: string;
|
||||||
|
majorParameter: string;
|
||||||
|
original: RouteLike;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a hash and its associated fields
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface HashData {
|
||||||
|
lastAccess: number;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare const DefaultUserAgent: `DiscordBot (https://discord.js.org, ${string})`;
|
||||||
|
/**
|
||||||
|
* The default string to append onto the user agent.
|
||||||
|
*/
|
||||||
|
declare const DefaultUserAgentAppendix: string;
|
||||||
|
declare const DefaultRestOptions: {
|
||||||
|
readonly agent: null;
|
||||||
|
readonly api: "https://discord.com/api";
|
||||||
|
readonly authPrefix: "Bot";
|
||||||
|
readonly cdn: "https://cdn.discordapp.com";
|
||||||
|
readonly headers: {};
|
||||||
|
readonly invalidRequestWarningInterval: 0;
|
||||||
|
readonly globalRequestsPerSecond: 50;
|
||||||
|
readonly offset: 50;
|
||||||
|
readonly rejectOnRateLimit: null;
|
||||||
|
readonly retries: 3;
|
||||||
|
readonly timeout: 15000;
|
||||||
|
readonly userAgentAppendix: string;
|
||||||
|
readonly version: "10";
|
||||||
|
readonly hashSweepInterval: 14400000;
|
||||||
|
readonly hashLifetime: 86400000;
|
||||||
|
readonly handlerSweepInterval: 3600000;
|
||||||
|
readonly makeRequest: (url: string, init: undici.RequestInit) => Promise<ResponseLike>;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The events that the REST manager emits
|
||||||
|
*/
|
||||||
|
declare enum RESTEvents {
|
||||||
|
Debug = "restDebug",
|
||||||
|
HandlerSweep = "handlerSweep",
|
||||||
|
HashSweep = "hashSweep",
|
||||||
|
InvalidRequestWarning = "invalidRequestWarning",
|
||||||
|
RateLimited = "rateLimited",
|
||||||
|
Response = "response"
|
||||||
|
}
|
||||||
|
declare const ALLOWED_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif"];
|
||||||
|
declare const ALLOWED_STICKER_EXTENSIONS: readonly ["png", "json", "gif"];
|
||||||
|
declare const ALLOWED_SIZES: readonly [16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
|
||||||
|
type ImageExtension = (typeof ALLOWED_EXTENSIONS)[number];
|
||||||
|
type StickerExtension = (typeof ALLOWED_STICKER_EXTENSIONS)[number];
|
||||||
|
type ImageSize = (typeof ALLOWED_SIZES)[number];
|
||||||
|
declare const OverwrittenMimeTypes: {
|
||||||
|
readonly 'image/apng': "image/png";
|
||||||
|
};
|
||||||
|
declare const BurstHandlerMajorIdKey = "burst";
|
||||||
|
/**
|
||||||
|
* Prefix for deprecation warnings.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
declare const DEPRECATION_WARNING_PREFIX: "DeprecationWarning";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options used for image URLs
|
||||||
|
*/
|
||||||
|
interface BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: ImageExtension;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options used for image URLs with animated content
|
||||||
|
*/
|
||||||
|
interface ImageURLOptions extends BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* Whether or not to prefer the static version of an image asset.
|
||||||
|
*/
|
||||||
|
forceStatic?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options to use when making a CDN URL
|
||||||
|
*/
|
||||||
|
interface MakeURLOptions {
|
||||||
|
/**
|
||||||
|
* The allowed extensions that can be used
|
||||||
|
*/
|
||||||
|
allowedExtensions?: readonly string[];
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The CDN link builder
|
||||||
|
*/
|
||||||
|
declare class CDN {
|
||||||
|
private readonly base;
|
||||||
|
constructor(base?: string);
|
||||||
|
/**
|
||||||
|
* Generates an app asset URL for a client's asset.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the asset
|
||||||
|
* @param assetHash - The hash provided by Discord for this asset
|
||||||
|
* @param options - Optional options for the asset
|
||||||
|
*/
|
||||||
|
appAsset(clientId: string, assetHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an app icon URL for a client's icon.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
appIcon(clientId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an avatar URL, e.g. for a user or a webhook.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
avatar(id: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a user avatar decoration URL.
|
||||||
|
*
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param userAvatarDecoration - The hash provided by Discord for this avatar decoration
|
||||||
|
* @param options - Optional options for the avatar decoration
|
||||||
|
*/
|
||||||
|
avatarDecoration(userId: string, userAvatarDecoration: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a banner URL, e.g. for a user or a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the banner splash
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
banner(id: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL for a channel, e.g. a group DM.
|
||||||
|
*
|
||||||
|
* @param channelId - The channel id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this channel
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
channelIcon(channelId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a default avatar URL
|
||||||
|
*
|
||||||
|
* @param index - The default avatar index
|
||||||
|
* @remarks
|
||||||
|
* To calculate the index for a user do `(userId >> 22) % 6`,
|
||||||
|
* or `discriminator % 5` if they're using the legacy username system.
|
||||||
|
*/
|
||||||
|
defaultAvatar(index: number): string;
|
||||||
|
/**
|
||||||
|
* Generates a discovery splash URL for a guild's discovery splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the discovery splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
discoverySplash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param options - Optional options for the emoji
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param extension - The extension of the emoji
|
||||||
|
* @deprecated This overload is deprecated. Pass an object containing the extension instead.
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, extension?: ImageExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member avatar URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
guildMemberAvatar(guildId: string, userId: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member banner URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
guildMemberBanner(guildId: string, userId: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL, e.g. for a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon splash
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
icon(id: string, iconHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a URL for the icon of a role
|
||||||
|
*
|
||||||
|
* @param roleId - The id of the role that has the icon
|
||||||
|
* @param roleIconHash - The hash provided by Discord for this role icon
|
||||||
|
* @param options - Optional options for the role icon
|
||||||
|
*/
|
||||||
|
roleIcon(roleId: string, roleIconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild invite splash URL for a guild's invite splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the invite splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
splash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker URL.
|
||||||
|
*
|
||||||
|
* @param stickerId - The sticker id
|
||||||
|
* @param extension - The extension of the sticker
|
||||||
|
* @privateRemarks
|
||||||
|
* Stickers cannot have a `.webp` extension, so we default to a `.png`
|
||||||
|
*/
|
||||||
|
sticker(stickerId: string, extension?: StickerExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker pack banner URL.
|
||||||
|
*
|
||||||
|
* @param bannerId - The banner id
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
stickerPackBanner(bannerId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a team icon URL for a team's icon.
|
||||||
|
*
|
||||||
|
* @param teamId - The team id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
teamIcon(teamId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a cover image for a guild scheduled event.
|
||||||
|
*
|
||||||
|
* @param scheduledEventId - The scheduled event id
|
||||||
|
* @param coverHash - The hash provided by discord for this cover image
|
||||||
|
* @param options - Optional options for the cover image
|
||||||
|
*/
|
||||||
|
guildScheduledEventCover(scheduledEventId: string, coverHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param hash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the link
|
||||||
|
*/
|
||||||
|
private dynamicMakeURL;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param options - The extension/size options for the link
|
||||||
|
*/
|
||||||
|
private makeURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DiscordErrorFieldInformation {
|
||||||
|
code: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface DiscordErrorGroupWrapper {
|
||||||
|
_errors: DiscordError[];
|
||||||
|
}
|
||||||
|
type DiscordError = DiscordErrorFieldInformation | DiscordErrorGroupWrapper | string | {
|
||||||
|
[k: string]: DiscordError;
|
||||||
|
};
|
||||||
|
interface DiscordErrorData {
|
||||||
|
code: number;
|
||||||
|
errors?: DiscordError;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface OAuthErrorData {
|
||||||
|
error: string;
|
||||||
|
error_description?: string;
|
||||||
|
}
|
||||||
|
interface RequestBody {
|
||||||
|
files: RawFile[] | undefined;
|
||||||
|
json: unknown | undefined;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an API error returned by Discord
|
||||||
|
*/
|
||||||
|
declare class DiscordAPIError extends Error {
|
||||||
|
rawError: DiscordErrorData | OAuthErrorData;
|
||||||
|
code: number | string;
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
/**
|
||||||
|
* @param rawError - The error reported by Discord
|
||||||
|
* @param code - The error code reported by Discord
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(rawError: DiscordErrorData | OAuthErrorData, code: number | string, status: number, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
private static getMessage;
|
||||||
|
private static flattenDiscordError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a HTTP error
|
||||||
|
*/
|
||||||
|
declare class HTTPError extends Error {
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
name: string;
|
||||||
|
/**
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param statusText - The status text of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(status: number, statusText: string, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class RateLimitError extends Error implements RateLimitData {
|
||||||
|
timeToReset: number;
|
||||||
|
limit: number;
|
||||||
|
method: string;
|
||||||
|
hash: string;
|
||||||
|
url: string;
|
||||||
|
route: string;
|
||||||
|
majorParameter: string;
|
||||||
|
global: boolean;
|
||||||
|
retryAfter: number;
|
||||||
|
sublimitTimeout: number;
|
||||||
|
scope: RateLimitData['scope'];
|
||||||
|
constructor({ timeToReset, limit, method, hash, url, route, majorParameter, global, retryAfter, sublimitTimeout, scope, }: RateLimitData);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the class that manages handlers for endpoints
|
||||||
|
*/
|
||||||
|
declare class REST extends AsyncEventEmitter<RestEventsMap> {
|
||||||
|
#private;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
|
||||||
|
* performed by this manager.
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
readonly cdn: CDN;
|
||||||
|
/**
|
||||||
|
* The number of requests remaining in the global bucket
|
||||||
|
*/
|
||||||
|
globalRemaining: number;
|
||||||
|
/**
|
||||||
|
* The promise used to wait out the global rate limit
|
||||||
|
*/
|
||||||
|
globalDelay: Promise<void> | null;
|
||||||
|
/**
|
||||||
|
* The timestamp at which the global bucket resets
|
||||||
|
*/
|
||||||
|
globalReset: number;
|
||||||
|
/**
|
||||||
|
* API bucket hashes that are cached from provided routes
|
||||||
|
*/
|
||||||
|
readonly hashes: Collection<string, HashData>;
|
||||||
|
/**
|
||||||
|
* Request handlers created from the bucket hash and the major parameters
|
||||||
|
*/
|
||||||
|
readonly handlers: Collection<string, IHandler>;
|
||||||
|
private hashTimer;
|
||||||
|
private handlerTimer;
|
||||||
|
readonly options: RESTOptions;
|
||||||
|
constructor(options?: Partial<RESTOptions>);
|
||||||
|
private setupSweepers;
|
||||||
|
/**
|
||||||
|
* Runs a get request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
get(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a delete request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
delete(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a post request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
post(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a put request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
put(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a patch request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
patch(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a request from the api
|
||||||
|
*
|
||||||
|
* @param options - Request options
|
||||||
|
*/
|
||||||
|
request(options: InternalRequest): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Sets the default agent to use for requests performed by this manager
|
||||||
|
*
|
||||||
|
* @param agent - The agent to use
|
||||||
|
*/
|
||||||
|
setAgent(agent: Dispatcher): this;
|
||||||
|
/**
|
||||||
|
* Sets the authorization token that should be used for requests
|
||||||
|
*
|
||||||
|
* @param token - The authorization token to use
|
||||||
|
*/
|
||||||
|
setToken(token: string): this;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param request - All the information needed to make a request
|
||||||
|
* @returns The response from the api request
|
||||||
|
*/
|
||||||
|
queueRequest(request: InternalRequest): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* Creates a new rate limit handler from a hash, based on the hash and the major parameter
|
||||||
|
*
|
||||||
|
* @param hash - The hash for the route
|
||||||
|
* @param majorParameter - The major parameter for this handler
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private createHandler;
|
||||||
|
/**
|
||||||
|
* Formats the request data to a usable format for fetch
|
||||||
|
*
|
||||||
|
* @param request - The request data
|
||||||
|
*/
|
||||||
|
private resolveRequest;
|
||||||
|
/**
|
||||||
|
* Stops the hash sweeping interval
|
||||||
|
*/
|
||||||
|
clearHashSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Stops the request handler sweeping interval
|
||||||
|
*/
|
||||||
|
clearHandlerSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Generates route data for an endpoint:method
|
||||||
|
*
|
||||||
|
* @param endpoint - The raw endpoint to generalize
|
||||||
|
* @param method - The HTTP method this endpoint is called without
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static generateRouteData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and populates an URLSearchParams instance from an object, stripping
|
||||||
|
* out null and undefined values, while also coercing non-strings to strings.
|
||||||
|
*
|
||||||
|
* @param options - The options to use
|
||||||
|
* @returns A populated URLSearchParams instance
|
||||||
|
*/
|
||||||
|
declare function makeURLSearchParams<OptionsType extends object>(options?: Readonly<OptionsType>): url.URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Converts the response to usable data
|
||||||
|
*
|
||||||
|
* @param res - The fetch response
|
||||||
|
*/
|
||||||
|
declare function parseResponse(res: ResponseLike): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Calculates the default avatar index for a given user id.
|
||||||
|
*
|
||||||
|
* @param userId - The user id to calculate the default avatar index for
|
||||||
|
*/
|
||||||
|
declare function calculateUserDefaultAvatarIndex(userId: Snowflake): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/rest#readme | @discordjs/rest} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { ALLOWED_EXTENSIONS, ALLOWED_SIZES, ALLOWED_STICKER_EXTENSIONS, APIRequest, BaseImageURLOptions, BurstHandlerMajorIdKey, CDN, DEPRECATION_WARNING_PREFIX, DefaultRestOptions, DefaultUserAgent, DefaultUserAgentAppendix, DiscordAPIError, DiscordErrorData, HTTPError, HandlerRequestData, HashData, ImageExtension, ImageSize, ImageURLOptions, InternalRequest, InvalidRequestWarningData, MakeURLOptions, OAuthErrorData, OverwrittenMimeTypes, REST, RESTEvents, RESTOptions, RateLimitData, RateLimitError, RateLimitQueueFilter, RawFile, RequestBody, RequestData, RequestHeaders, RequestMethod, ResponseLike, RestEvents, RestEventsMap, RouteData, RouteLike, StickerExtension, calculateUserDefaultAvatarIndex, makeURLSearchParams, parseResponse, version };
|
|
@ -0,0 +1,901 @@
|
||||||
|
import * as url from 'url';
|
||||||
|
import { Snowflake } from 'discord-api-types/v10';
|
||||||
|
import { Readable } from 'node:stream';
|
||||||
|
import { ReadableStream } from 'node:stream/web';
|
||||||
|
import { Collection } from '@discordjs/collection';
|
||||||
|
import { Awaitable } from '@discordjs/util';
|
||||||
|
import * as undici from 'undici';
|
||||||
|
import { RequestInit, Dispatcher, Response, BodyInit, Agent } from 'undici';
|
||||||
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||||
|
|
||||||
|
interface IHandler {
|
||||||
|
/**
|
||||||
|
* The unique id of the handler
|
||||||
|
*/
|
||||||
|
readonly id: string;
|
||||||
|
/**
|
||||||
|
* If the bucket is currently inactive (no pending requests)
|
||||||
|
*/
|
||||||
|
get inactive(): boolean;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param routeId - The generalized api route with literal ids for major parameters
|
||||||
|
* @param url - The url to do the request on
|
||||||
|
* @param options - All the information needed to make a request
|
||||||
|
* @param requestData - Extra data from the user's request needed for errors and additional processing
|
||||||
|
*/
|
||||||
|
queueRequest(routeId: RouteData, url: string, options: RequestInit, requestData: HandlerRequestData): Promise<ResponseLike>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface RestEvents {
|
||||||
|
handlerSweep: [sweptHandlers: Collection<string, IHandler>];
|
||||||
|
hashSweep: [sweptHashes: Collection<string, HashData>];
|
||||||
|
invalidRequestWarning: [invalidRequestInfo: InvalidRequestWarningData];
|
||||||
|
rateLimited: [rateLimitInfo: RateLimitData];
|
||||||
|
response: [request: APIRequest, response: ResponseLike];
|
||||||
|
restDebug: [info: string];
|
||||||
|
}
|
||||||
|
type RestEventsMap = {
|
||||||
|
[K in keyof RestEvents]: RestEvents[K];
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Options to be passed when creating the REST instance
|
||||||
|
*/
|
||||||
|
interface RESTOptions {
|
||||||
|
/**
|
||||||
|
* The agent to set globally
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
/**
|
||||||
|
* The base api path, without version
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://discord.com/api'`
|
||||||
|
*/
|
||||||
|
api: string;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for requests, useful if you want to use
|
||||||
|
* bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The cdn path
|
||||||
|
*
|
||||||
|
* @defaultValue `'https://cdn.discordapp.com'`
|
||||||
|
*/
|
||||||
|
cdn: string;
|
||||||
|
/**
|
||||||
|
* How many requests to allow sending per second (Infinity for unlimited, 50 for the standard global limit used by Discord)
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
globalRequestsPerSecond: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 1h)
|
||||||
|
*
|
||||||
|
* @defaultValue `3_600_000`
|
||||||
|
*/
|
||||||
|
handlerSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* The maximum amount of time a hash can exist in milliseconds without being hit with a request (defaults to 24h)
|
||||||
|
*
|
||||||
|
* @defaultValue `86_400_000`
|
||||||
|
*/
|
||||||
|
hashLifetime: number;
|
||||||
|
/**
|
||||||
|
* The amount of time in milliseconds that passes between each hash sweep. (defaults to 4h)
|
||||||
|
*
|
||||||
|
* @defaultValue `14_400_000`
|
||||||
|
*/
|
||||||
|
hashSweepInterval: number;
|
||||||
|
/**
|
||||||
|
* Additional headers to send for all API requests
|
||||||
|
*
|
||||||
|
* @defaultValue `{}`
|
||||||
|
*/
|
||||||
|
headers: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* The number of invalid REST requests (those that return 401, 403, or 429) in a 10 minute window between emitted warnings (0 for no warnings).
|
||||||
|
* That is, if set to 500, warnings will be emitted at invalid request number 500, 1000, 1500, and so on.
|
||||||
|
*
|
||||||
|
* @defaultValue `0`
|
||||||
|
*/
|
||||||
|
invalidRequestWarningInterval: number;
|
||||||
|
/**
|
||||||
|
* The method called to perform the actual HTTP request given a url and web `fetch` options
|
||||||
|
* For example, to use global fetch, simply provide `makeRequest: fetch`
|
||||||
|
*/
|
||||||
|
makeRequest(url: string, init: RequestInit): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* The extra offset to add to rate limits in milliseconds
|
||||||
|
*
|
||||||
|
* @defaultValue `50`
|
||||||
|
*/
|
||||||
|
offset: number;
|
||||||
|
/**
|
||||||
|
* Determines how rate limiting and pre-emptive throttling should be handled.
|
||||||
|
* When an array of strings, each element is treated as a prefix for the request route
|
||||||
|
* (e.g. `/channels` to match any route starting with `/channels` such as `/channels/:id/messages`)
|
||||||
|
* for which to throw {@link RateLimitError}s. All other request routes will be queued normally
|
||||||
|
*
|
||||||
|
* @defaultValue `null`
|
||||||
|
*/
|
||||||
|
rejectOnRateLimit: RateLimitQueueFilter | string[] | null;
|
||||||
|
/**
|
||||||
|
* The number of retries for errors with the 500 code, or errors
|
||||||
|
* that timeout
|
||||||
|
*
|
||||||
|
* @defaultValue `3`
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The time to wait in milliseconds before a request is aborted
|
||||||
|
*
|
||||||
|
* @defaultValue `15_000`
|
||||||
|
*/
|
||||||
|
timeout: number;
|
||||||
|
/**
|
||||||
|
* Extra information to add to the user agent
|
||||||
|
*
|
||||||
|
* @defaultValue DefaultUserAgentAppendix
|
||||||
|
*/
|
||||||
|
userAgentAppendix: string;
|
||||||
|
/**
|
||||||
|
* The version of the API to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'10'`
|
||||||
|
*/
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Data emitted on `RESTEvents.RateLimited`
|
||||||
|
*/
|
||||||
|
interface RateLimitData {
|
||||||
|
/**
|
||||||
|
* Whether the rate limit that was reached was the global limit
|
||||||
|
*/
|
||||||
|
global: boolean;
|
||||||
|
/**
|
||||||
|
* The bucket hash for this request
|
||||||
|
*/
|
||||||
|
hash: string;
|
||||||
|
/**
|
||||||
|
* The amount of requests we can perform before locking requests
|
||||||
|
*/
|
||||||
|
limit: number;
|
||||||
|
/**
|
||||||
|
* The major parameter of the route
|
||||||
|
*
|
||||||
|
* For example, in `/channels/x`, this will be `x`.
|
||||||
|
* If there is no major parameter (e.g: `/bot/gateway`) this will be `global`.
|
||||||
|
*/
|
||||||
|
majorParameter: string;
|
||||||
|
/**
|
||||||
|
* The HTTP method being performed
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before this specific request can be retried
|
||||||
|
*/
|
||||||
|
retryAfter: number;
|
||||||
|
/**
|
||||||
|
* The route being hit in this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
/**
|
||||||
|
* The scope of the rate limit that was hit.
|
||||||
|
*
|
||||||
|
* This can be `user` for rate limits that are per client, `global` for rate limits that affect all clients or `shared` for rate limits that
|
||||||
|
* are shared per resource.
|
||||||
|
*/
|
||||||
|
scope: 'global' | 'shared' | 'user';
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, that will need to pass before the sublimit lock for the route resets, and requests that fall under a sublimit
|
||||||
|
* can be retried
|
||||||
|
*
|
||||||
|
* This is only present on certain sublimits, and `0` otherwise
|
||||||
|
*/
|
||||||
|
sublimitTimeout: number;
|
||||||
|
/**
|
||||||
|
* The time, in milliseconds, until the route's request-lock is reset
|
||||||
|
*/
|
||||||
|
timeToReset: number;
|
||||||
|
/**
|
||||||
|
* The full URL for this request
|
||||||
|
*/
|
||||||
|
url: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A function that determines whether the rate limit hit should throw an Error
|
||||||
|
*/
|
||||||
|
type RateLimitQueueFilter = (rateLimitData: RateLimitData) => Awaitable<boolean>;
|
||||||
|
interface APIRequest {
|
||||||
|
/**
|
||||||
|
* The data that was used to form the body of this request
|
||||||
|
*/
|
||||||
|
data: HandlerRequestData;
|
||||||
|
/**
|
||||||
|
* The HTTP method used in this request
|
||||||
|
*/
|
||||||
|
method: string;
|
||||||
|
/**
|
||||||
|
* Additional HTTP options for this request
|
||||||
|
*/
|
||||||
|
options: RequestInit;
|
||||||
|
/**
|
||||||
|
* The full path used to make the request
|
||||||
|
*/
|
||||||
|
path: RouteLike;
|
||||||
|
/**
|
||||||
|
* The number of times this request has been attempted
|
||||||
|
*/
|
||||||
|
retries: number;
|
||||||
|
/**
|
||||||
|
* The API route identifying the ratelimit for this request
|
||||||
|
*/
|
||||||
|
route: string;
|
||||||
|
}
|
||||||
|
interface ResponseLike extends Pick<Response, 'arrayBuffer' | 'bodyUsed' | 'headers' | 'json' | 'ok' | 'status' | 'statusText' | 'text'> {
|
||||||
|
body: Readable | ReadableStream | null;
|
||||||
|
}
|
||||||
|
interface InvalidRequestWarningData {
|
||||||
|
/**
|
||||||
|
* Number of invalid requests that have been made in the window
|
||||||
|
*/
|
||||||
|
count: number;
|
||||||
|
/**
|
||||||
|
* Time in milliseconds remaining before the count resets
|
||||||
|
*/
|
||||||
|
remainingTime: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a file to be added to the request
|
||||||
|
*/
|
||||||
|
interface RawFile {
|
||||||
|
/**
|
||||||
|
* Content-Type of the file
|
||||||
|
*/
|
||||||
|
contentType?: string;
|
||||||
|
/**
|
||||||
|
* The actual data for the file
|
||||||
|
*/
|
||||||
|
data: Buffer | Uint8Array | boolean | number | string;
|
||||||
|
/**
|
||||||
|
* An explicit key to use for key of the formdata field for this file.
|
||||||
|
* When not provided, the index of the file in the files array is used in the form `files[${index}]`.
|
||||||
|
* If you wish to alter the placeholder snowflake, you must provide this property in the same form (`files[${placeholder}]`)
|
||||||
|
*/
|
||||||
|
key?: string;
|
||||||
|
/**
|
||||||
|
* The name of the file
|
||||||
|
*/
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents possible data to be given to an endpoint
|
||||||
|
*/
|
||||||
|
interface RequestData {
|
||||||
|
/**
|
||||||
|
* Whether to append JSON data to form data instead of `payload_json` when sending files
|
||||||
|
*/
|
||||||
|
appendToFormData?: boolean;
|
||||||
|
/**
|
||||||
|
* If this request needs the `Authorization` header
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
auth?: boolean;
|
||||||
|
/**
|
||||||
|
* The authorization prefix to use for this request, useful if you use this with bearer tokens
|
||||||
|
*
|
||||||
|
* @defaultValue `'Bot'`
|
||||||
|
*/
|
||||||
|
authPrefix?: 'Bearer' | 'Bot';
|
||||||
|
/**
|
||||||
|
* The body to send to this request.
|
||||||
|
* If providing as BodyInit, set `passThroughBody: true`
|
||||||
|
*/
|
||||||
|
body?: BodyInit | unknown;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} to use for the request.
|
||||||
|
*/
|
||||||
|
dispatcher?: Agent;
|
||||||
|
/**
|
||||||
|
* Files to be attached to this request
|
||||||
|
*/
|
||||||
|
files?: RawFile[] | undefined;
|
||||||
|
/**
|
||||||
|
* Additional headers to add to this request
|
||||||
|
*/
|
||||||
|
headers?: Record<string, string>;
|
||||||
|
/**
|
||||||
|
* Whether to pass-through the body property directly to `fetch()`.
|
||||||
|
* <warn>This only applies when files is NOT present</warn>
|
||||||
|
*/
|
||||||
|
passThroughBody?: boolean;
|
||||||
|
/**
|
||||||
|
* Query string parameters to append to the called endpoint
|
||||||
|
*/
|
||||||
|
query?: URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Reason to show in the audit logs
|
||||||
|
*/
|
||||||
|
reason?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The signal to abort the queue entry or the REST call, where applicable
|
||||||
|
*/
|
||||||
|
signal?: AbortSignal | undefined;
|
||||||
|
/**
|
||||||
|
* If this request should be versioned
|
||||||
|
*
|
||||||
|
* @defaultValue `true`
|
||||||
|
*/
|
||||||
|
versioned?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible headers for an API call
|
||||||
|
*/
|
||||||
|
interface RequestHeaders {
|
||||||
|
Authorization?: string;
|
||||||
|
'User-Agent': string;
|
||||||
|
'X-Audit-Log-Reason'?: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Possible API methods to be used when doing requests
|
||||||
|
*/
|
||||||
|
declare enum RequestMethod {
|
||||||
|
Delete = "DELETE",
|
||||||
|
Get = "GET",
|
||||||
|
Patch = "PATCH",
|
||||||
|
Post = "POST",
|
||||||
|
Put = "PUT"
|
||||||
|
}
|
||||||
|
type RouteLike = `/${string}`;
|
||||||
|
/**
|
||||||
|
* Internal request options
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface InternalRequest extends RequestData {
|
||||||
|
fullRoute: RouteLike;
|
||||||
|
method: RequestMethod;
|
||||||
|
}
|
||||||
|
type HandlerRequestData = Pick<InternalRequest, 'auth' | 'body' | 'files' | 'signal'>;
|
||||||
|
/**
|
||||||
|
* Parsed route data for an endpoint
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface RouteData {
|
||||||
|
bucketRoute: string;
|
||||||
|
majorParameter: string;
|
||||||
|
original: RouteLike;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents a hash and its associated fields
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface HashData {
|
||||||
|
lastAccess: number;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare const DefaultUserAgent: `DiscordBot (https://discord.js.org, ${string})`;
|
||||||
|
/**
|
||||||
|
* The default string to append onto the user agent.
|
||||||
|
*/
|
||||||
|
declare const DefaultUserAgentAppendix: string;
|
||||||
|
declare const DefaultRestOptions: {
|
||||||
|
readonly agent: null;
|
||||||
|
readonly api: "https://discord.com/api";
|
||||||
|
readonly authPrefix: "Bot";
|
||||||
|
readonly cdn: "https://cdn.discordapp.com";
|
||||||
|
readonly headers: {};
|
||||||
|
readonly invalidRequestWarningInterval: 0;
|
||||||
|
readonly globalRequestsPerSecond: 50;
|
||||||
|
readonly offset: 50;
|
||||||
|
readonly rejectOnRateLimit: null;
|
||||||
|
readonly retries: 3;
|
||||||
|
readonly timeout: 15000;
|
||||||
|
readonly userAgentAppendix: string;
|
||||||
|
readonly version: "10";
|
||||||
|
readonly hashSweepInterval: 14400000;
|
||||||
|
readonly hashLifetime: 86400000;
|
||||||
|
readonly handlerSweepInterval: 3600000;
|
||||||
|
readonly makeRequest: (url: string, init: undici.RequestInit) => Promise<ResponseLike>;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* The events that the REST manager emits
|
||||||
|
*/
|
||||||
|
declare enum RESTEvents {
|
||||||
|
Debug = "restDebug",
|
||||||
|
HandlerSweep = "handlerSweep",
|
||||||
|
HashSweep = "hashSweep",
|
||||||
|
InvalidRequestWarning = "invalidRequestWarning",
|
||||||
|
RateLimited = "rateLimited",
|
||||||
|
Response = "response"
|
||||||
|
}
|
||||||
|
declare const ALLOWED_EXTENSIONS: readonly ["webp", "png", "jpg", "jpeg", "gif"];
|
||||||
|
declare const ALLOWED_STICKER_EXTENSIONS: readonly ["png", "json", "gif"];
|
||||||
|
declare const ALLOWED_SIZES: readonly [16, 32, 64, 128, 256, 512, 1024, 2048, 4096];
|
||||||
|
type ImageExtension = (typeof ALLOWED_EXTENSIONS)[number];
|
||||||
|
type StickerExtension = (typeof ALLOWED_STICKER_EXTENSIONS)[number];
|
||||||
|
type ImageSize = (typeof ALLOWED_SIZES)[number];
|
||||||
|
declare const OverwrittenMimeTypes: {
|
||||||
|
readonly 'image/apng': "image/png";
|
||||||
|
};
|
||||||
|
declare const BurstHandlerMajorIdKey = "burst";
|
||||||
|
/**
|
||||||
|
* Prefix for deprecation warnings.
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
declare const DEPRECATION_WARNING_PREFIX: "DeprecationWarning";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The options used for image URLs
|
||||||
|
*/
|
||||||
|
interface BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: ImageExtension;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options used for image URLs with animated content
|
||||||
|
*/
|
||||||
|
interface ImageURLOptions extends BaseImageURLOptions {
|
||||||
|
/**
|
||||||
|
* Whether or not to prefer the static version of an image asset.
|
||||||
|
*/
|
||||||
|
forceStatic?: boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The options to use when making a CDN URL
|
||||||
|
*/
|
||||||
|
interface MakeURLOptions {
|
||||||
|
/**
|
||||||
|
* The allowed extensions that can be used
|
||||||
|
*/
|
||||||
|
allowedExtensions?: readonly string[];
|
||||||
|
/**
|
||||||
|
* The extension to use for the image URL
|
||||||
|
*
|
||||||
|
* @defaultValue `'webp'`
|
||||||
|
*/
|
||||||
|
extension?: string | undefined;
|
||||||
|
/**
|
||||||
|
* The size specified in the image URL
|
||||||
|
*/
|
||||||
|
size?: ImageSize;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The CDN link builder
|
||||||
|
*/
|
||||||
|
declare class CDN {
|
||||||
|
private readonly base;
|
||||||
|
constructor(base?: string);
|
||||||
|
/**
|
||||||
|
* Generates an app asset URL for a client's asset.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the asset
|
||||||
|
* @param assetHash - The hash provided by Discord for this asset
|
||||||
|
* @param options - Optional options for the asset
|
||||||
|
*/
|
||||||
|
appAsset(clientId: string, assetHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an app icon URL for a client's icon.
|
||||||
|
*
|
||||||
|
* @param clientId - The client id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
appIcon(clientId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an avatar URL, e.g. for a user or a webhook.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
avatar(id: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a user avatar decoration URL.
|
||||||
|
*
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param userAvatarDecoration - The hash provided by Discord for this avatar decoration
|
||||||
|
* @param options - Optional options for the avatar decoration
|
||||||
|
*/
|
||||||
|
avatarDecoration(userId: string, userAvatarDecoration: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a banner URL, e.g. for a user or a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the banner splash
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
banner(id: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL for a channel, e.g. a group DM.
|
||||||
|
*
|
||||||
|
* @param channelId - The channel id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this channel
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
channelIcon(channelId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a default avatar URL
|
||||||
|
*
|
||||||
|
* @param index - The default avatar index
|
||||||
|
* @remarks
|
||||||
|
* To calculate the index for a user do `(userId >> 22) % 6`,
|
||||||
|
* or `discriminator % 5` if they're using the legacy username system.
|
||||||
|
*/
|
||||||
|
defaultAvatar(index: number): string;
|
||||||
|
/**
|
||||||
|
* Generates a discovery splash URL for a guild's discovery splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the discovery splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
discoverySplash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param options - Optional options for the emoji
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an emoji's URL for an emoji.
|
||||||
|
*
|
||||||
|
* @param emojiId - The emoji id
|
||||||
|
* @param extension - The extension of the emoji
|
||||||
|
* @deprecated This overload is deprecated. Pass an object containing the extension instead.
|
||||||
|
*/
|
||||||
|
emoji(emojiId: string, extension?: ImageExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member avatar URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param avatarHash - The hash provided by Discord for this avatar
|
||||||
|
* @param options - Optional options for the avatar
|
||||||
|
*/
|
||||||
|
guildMemberAvatar(guildId: string, userId: string, avatarHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild member banner URL.
|
||||||
|
*
|
||||||
|
* @param guildId - The id of the guild
|
||||||
|
* @param userId - The id of the user
|
||||||
|
* @param bannerHash - The hash provided by Discord for this banner
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
guildMemberBanner(guildId: string, userId: string, bannerHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates an icon URL, e.g. for a guild.
|
||||||
|
*
|
||||||
|
* @param id - The id that has the icon splash
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
icon(id: string, iconHash: string, options?: Readonly<ImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a URL for the icon of a role
|
||||||
|
*
|
||||||
|
* @param roleId - The id of the role that has the icon
|
||||||
|
* @param roleIconHash - The hash provided by Discord for this role icon
|
||||||
|
* @param options - Optional options for the role icon
|
||||||
|
*/
|
||||||
|
roleIcon(roleId: string, roleIconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a guild invite splash URL for a guild's invite splash.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id that has the invite splash
|
||||||
|
* @param splashHash - The hash provided by Discord for this splash
|
||||||
|
* @param options - Optional options for the splash
|
||||||
|
*/
|
||||||
|
splash(guildId: string, splashHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker URL.
|
||||||
|
*
|
||||||
|
* @param stickerId - The sticker id
|
||||||
|
* @param extension - The extension of the sticker
|
||||||
|
* @privateRemarks
|
||||||
|
* Stickers cannot have a `.webp` extension, so we default to a `.png`
|
||||||
|
*/
|
||||||
|
sticker(stickerId: string, extension?: StickerExtension): string;
|
||||||
|
/**
|
||||||
|
* Generates a sticker pack banner URL.
|
||||||
|
*
|
||||||
|
* @param bannerId - The banner id
|
||||||
|
* @param options - Optional options for the banner
|
||||||
|
*/
|
||||||
|
stickerPackBanner(bannerId: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a team icon URL for a team's icon.
|
||||||
|
*
|
||||||
|
* @param teamId - The team id that has the icon
|
||||||
|
* @param iconHash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the icon
|
||||||
|
*/
|
||||||
|
teamIcon(teamId: string, iconHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Generates a cover image for a guild scheduled event.
|
||||||
|
*
|
||||||
|
* @param scheduledEventId - The scheduled event id
|
||||||
|
* @param coverHash - The hash provided by discord for this cover image
|
||||||
|
* @param options - Optional options for the cover image
|
||||||
|
*/
|
||||||
|
guildScheduledEventCover(scheduledEventId: string, coverHash: string, options?: Readonly<BaseImageURLOptions>): string;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource, checking whether or not `hash` starts with `a_` if `dynamic` is set to `true`.
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param hash - The hash provided by Discord for this icon
|
||||||
|
* @param options - Optional options for the link
|
||||||
|
*/
|
||||||
|
private dynamicMakeURL;
|
||||||
|
/**
|
||||||
|
* Constructs the URL for the resource
|
||||||
|
*
|
||||||
|
* @param route - The base cdn route
|
||||||
|
* @param options - The extension/size options for the link
|
||||||
|
*/
|
||||||
|
private makeURL;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface DiscordErrorFieldInformation {
|
||||||
|
code: string;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface DiscordErrorGroupWrapper {
|
||||||
|
_errors: DiscordError[];
|
||||||
|
}
|
||||||
|
type DiscordError = DiscordErrorFieldInformation | DiscordErrorGroupWrapper | string | {
|
||||||
|
[k: string]: DiscordError;
|
||||||
|
};
|
||||||
|
interface DiscordErrorData {
|
||||||
|
code: number;
|
||||||
|
errors?: DiscordError;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
interface OAuthErrorData {
|
||||||
|
error: string;
|
||||||
|
error_description?: string;
|
||||||
|
}
|
||||||
|
interface RequestBody {
|
||||||
|
files: RawFile[] | undefined;
|
||||||
|
json: unknown | undefined;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an API error returned by Discord
|
||||||
|
*/
|
||||||
|
declare class DiscordAPIError extends Error {
|
||||||
|
rawError: DiscordErrorData | OAuthErrorData;
|
||||||
|
code: number | string;
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
/**
|
||||||
|
* @param rawError - The error reported by Discord
|
||||||
|
* @param code - The error code reported by Discord
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(rawError: DiscordErrorData | OAuthErrorData, code: number | string, status: number, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
private static getMessage;
|
||||||
|
private static flattenDiscordError;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a HTTP error
|
||||||
|
*/
|
||||||
|
declare class HTTPError extends Error {
|
||||||
|
status: number;
|
||||||
|
method: string;
|
||||||
|
url: string;
|
||||||
|
requestBody: RequestBody;
|
||||||
|
name: string;
|
||||||
|
/**
|
||||||
|
* @param status - The status code of the response
|
||||||
|
* @param statusText - The status text of the response
|
||||||
|
* @param method - The method of the request that erred
|
||||||
|
* @param url - The url of the request that erred
|
||||||
|
* @param bodyData - The unparsed data for the request that errored
|
||||||
|
*/
|
||||||
|
constructor(status: number, statusText: string, method: string, url: string, bodyData: Pick<InternalRequest, 'body' | 'files'>);
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class RateLimitError extends Error implements RateLimitData {
|
||||||
|
timeToReset: number;
|
||||||
|
limit: number;
|
||||||
|
method: string;
|
||||||
|
hash: string;
|
||||||
|
url: string;
|
||||||
|
route: string;
|
||||||
|
majorParameter: string;
|
||||||
|
global: boolean;
|
||||||
|
retryAfter: number;
|
||||||
|
sublimitTimeout: number;
|
||||||
|
scope: RateLimitData['scope'];
|
||||||
|
constructor({ timeToReset, limit, method, hash, url, route, majorParameter, global, retryAfter, sublimitTimeout, scope, }: RateLimitData);
|
||||||
|
/**
|
||||||
|
* The name of the error
|
||||||
|
*/
|
||||||
|
get name(): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the class that manages handlers for endpoints
|
||||||
|
*/
|
||||||
|
declare class REST extends AsyncEventEmitter<RestEventsMap> {
|
||||||
|
#private;
|
||||||
|
/**
|
||||||
|
* The {@link https://undici.nodejs.org/#/docs/api/Agent | Agent} for all requests
|
||||||
|
* performed by this manager.
|
||||||
|
*/
|
||||||
|
agent: Dispatcher | null;
|
||||||
|
readonly cdn: CDN;
|
||||||
|
/**
|
||||||
|
* The number of requests remaining in the global bucket
|
||||||
|
*/
|
||||||
|
globalRemaining: number;
|
||||||
|
/**
|
||||||
|
* The promise used to wait out the global rate limit
|
||||||
|
*/
|
||||||
|
globalDelay: Promise<void> | null;
|
||||||
|
/**
|
||||||
|
* The timestamp at which the global bucket resets
|
||||||
|
*/
|
||||||
|
globalReset: number;
|
||||||
|
/**
|
||||||
|
* API bucket hashes that are cached from provided routes
|
||||||
|
*/
|
||||||
|
readonly hashes: Collection<string, HashData>;
|
||||||
|
/**
|
||||||
|
* Request handlers created from the bucket hash and the major parameters
|
||||||
|
*/
|
||||||
|
readonly handlers: Collection<string, IHandler>;
|
||||||
|
private hashTimer;
|
||||||
|
private handlerTimer;
|
||||||
|
readonly options: RESTOptions;
|
||||||
|
constructor(options?: Partial<RESTOptions>);
|
||||||
|
private setupSweepers;
|
||||||
|
/**
|
||||||
|
* Runs a get request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
get(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a delete request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
delete(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a post request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
post(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a put request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
put(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a patch request from the api
|
||||||
|
*
|
||||||
|
* @param fullRoute - The full route to query
|
||||||
|
* @param options - Optional request options
|
||||||
|
*/
|
||||||
|
patch(fullRoute: RouteLike, options?: RequestData): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Runs a request from the api
|
||||||
|
*
|
||||||
|
* @param options - Request options
|
||||||
|
*/
|
||||||
|
request(options: InternalRequest): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Sets the default agent to use for requests performed by this manager
|
||||||
|
*
|
||||||
|
* @param agent - The agent to use
|
||||||
|
*/
|
||||||
|
setAgent(agent: Dispatcher): this;
|
||||||
|
/**
|
||||||
|
* Sets the authorization token that should be used for requests
|
||||||
|
*
|
||||||
|
* @param token - The authorization token to use
|
||||||
|
*/
|
||||||
|
setToken(token: string): this;
|
||||||
|
/**
|
||||||
|
* Queues a request to be sent
|
||||||
|
*
|
||||||
|
* @param request - All the information needed to make a request
|
||||||
|
* @returns The response from the api request
|
||||||
|
*/
|
||||||
|
queueRequest(request: InternalRequest): Promise<ResponseLike>;
|
||||||
|
/**
|
||||||
|
* Creates a new rate limit handler from a hash, based on the hash and the major parameter
|
||||||
|
*
|
||||||
|
* @param hash - The hash for the route
|
||||||
|
* @param majorParameter - The major parameter for this handler
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private createHandler;
|
||||||
|
/**
|
||||||
|
* Formats the request data to a usable format for fetch
|
||||||
|
*
|
||||||
|
* @param request - The request data
|
||||||
|
*/
|
||||||
|
private resolveRequest;
|
||||||
|
/**
|
||||||
|
* Stops the hash sweeping interval
|
||||||
|
*/
|
||||||
|
clearHashSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Stops the request handler sweeping interval
|
||||||
|
*/
|
||||||
|
clearHandlerSweeper(): void;
|
||||||
|
/**
|
||||||
|
* Generates route data for an endpoint:method
|
||||||
|
*
|
||||||
|
* @param endpoint - The raw endpoint to generalize
|
||||||
|
* @param method - The HTTP method this endpoint is called without
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
private static generateRouteData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and populates an URLSearchParams instance from an object, stripping
|
||||||
|
* out null and undefined values, while also coercing non-strings to strings.
|
||||||
|
*
|
||||||
|
* @param options - The options to use
|
||||||
|
* @returns A populated URLSearchParams instance
|
||||||
|
*/
|
||||||
|
declare function makeURLSearchParams<OptionsType extends object>(options?: Readonly<OptionsType>): url.URLSearchParams;
|
||||||
|
/**
|
||||||
|
* Converts the response to usable data
|
||||||
|
*
|
||||||
|
* @param res - The fetch response
|
||||||
|
*/
|
||||||
|
declare function parseResponse(res: ResponseLike): Promise<unknown>;
|
||||||
|
/**
|
||||||
|
* Calculates the default avatar index for a given user id.
|
||||||
|
*
|
||||||
|
* @param userId - The user id to calculate the default avatar index for
|
||||||
|
*/
|
||||||
|
declare function calculateUserDefaultAvatarIndex(userId: Snowflake): number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/rest#readme | @discordjs/rest} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { ALLOWED_EXTENSIONS, ALLOWED_SIZES, ALLOWED_STICKER_EXTENSIONS, APIRequest, BaseImageURLOptions, BurstHandlerMajorIdKey, CDN, DEPRECATION_WARNING_PREFIX, DefaultRestOptions, DefaultUserAgent, DefaultUserAgentAppendix, DiscordAPIError, DiscordErrorData, HTTPError, HandlerRequestData, HashData, ImageExtension, ImageSize, ImageURLOptions, InternalRequest, InvalidRequestWarningData, MakeURLOptions, OAuthErrorData, OverwrittenMimeTypes, REST, RESTEvents, RESTOptions, RateLimitData, RateLimitError, RateLimitQueueFilter, RawFile, RequestBody, RequestData, RequestHeaders, RequestMethod, ResponseLike, RestEvents, RestEventsMap, RouteData, RouteLike, StickerExtension, calculateUserDefaultAvatarIndex, makeURLSearchParams, parseResponse, version };
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
191
node_modules/@discordjs/rest/node_modules/@discordjs/collection/LICENSE
generated
vendored
Normal file
191
node_modules/@discordjs/rest/node_modules/@discordjs/collection/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2021 Noel Buechler
|
||||||
|
Copyright 2015 Amish Shah
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
67
node_modules/@discordjs/rest/node_modules/@discordjs/collection/README.md
generated
vendored
Normal file
67
node_modules/@discordjs/rest/node_modules/@discordjs/collection/README.md
generated
vendored
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/v/@discordjs/collection.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/dt/@discordjs/collection.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=collection" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/collection` is a powerful utility data structure used in discord.js.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 18 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/collection
|
||||||
|
yarn add @discordjs/collection
|
||||||
|
pnpm add @discordjs/collection
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/collection/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/collection
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/collection
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
535
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.d.mts
generated
vendored
Normal file
535
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.d.mts
generated
vendored
Normal file
|
@ -0,0 +1,535 @@
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface CollectionConstructor {
|
||||||
|
new (): Collection<unknown, unknown>;
|
||||||
|
new <Key, Value>(entries?: readonly (readonly [Key, Value])[] | null): Collection<Key, Value>;
|
||||||
|
new <Key, Value>(iterable: Iterable<readonly [Key, Value]>): Collection<Key, Value>;
|
||||||
|
readonly prototype: Collection<unknown, unknown>;
|
||||||
|
readonly [Symbol.species]: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an immutable version of a collection
|
||||||
|
*/
|
||||||
|
type ReadonlyCollection<Key, Value> = Omit<Collection<Key, Value>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
constructor: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||||
|
* an ID, for significantly improved performance and ease-of-use.
|
||||||
|
*
|
||||||
|
* @typeParam Key - The key type this collection holds
|
||||||
|
* @typeParam Value - The value type this collection holds
|
||||||
|
*/
|
||||||
|
declare class Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key: Key, defaultValueGenerator: (key: Key, collection: this) => Value): Value;
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Obtains the first value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the beginning
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||||
|
*/
|
||||||
|
first(): Value | undefined;
|
||||||
|
first(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the first key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the beginning
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
firstKey(): Key | undefined;
|
||||||
|
firstKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Obtains the last value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the end
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
last(): Value | undefined;
|
||||||
|
last(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the last key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the end
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
lastKey(): Key | undefined;
|
||||||
|
lastKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index: number): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index: number): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Obtains unique random value(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain randomly
|
||||||
|
* @returns A single value if no amount is provided or an array of values
|
||||||
|
*/
|
||||||
|
random(): Value | undefined;
|
||||||
|
random(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains unique random key(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain randomly
|
||||||
|
* @returns A single key if no amount is provided or an array
|
||||||
|
*/
|
||||||
|
randomKey(): Key | undefined;
|
||||||
|
randomKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse(): this;
|
||||||
|
/**
|
||||||
|
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
|
||||||
|
* All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||||
|
* should use the `get` method. See
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.find(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
find<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
find(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
find<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
find<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.findKey(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
findKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast | Array.findLast()}.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLast<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
findLast(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
findLast<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
findLast<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex | Array.findLastIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLastKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findLastKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findLastKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findLastKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Removes items that satisfy the provided filter function.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @returns The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn: (value: Value, key: Key, collection: this) => unknown): number;
|
||||||
|
sweep<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): number;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.filter(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): Collection<NewKey, Value>;
|
||||||
|
filter<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): Collection<Key, NewValue>;
|
||||||
|
filter(fn: (value: Value, key: Key, collection: this) => unknown): Collection<Key, Value>;
|
||||||
|
filter<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): Collection<NewKey, Value>;
|
||||||
|
filter<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
filter<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
partition<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition(fn: (value: Value, key: Key, collection: this) => unknown): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
partition<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
/**
|
||||||
|
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces a new Collection
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.flatMap(guild => guild.members.cache);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
flatMap<NewValue>(fn: (value: Value, key: Key, collection: this) => Collection<Key, NewValue>): Collection<Key, NewValue>;
|
||||||
|
flatMap<NewValue, This>(fn: (this: This, value: Value, key: Key, collection: this) => Collection<Key, NewValue>, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into an array. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new array, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.map(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
map<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): NewValue[];
|
||||||
|
map<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): NewValue[];
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into a collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new collection, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.mapValues(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
mapValues<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): Collection<Key, NewValue>;
|
||||||
|
mapValues<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.some(user => user.discriminator === '0000');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
some(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
some<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if all items passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.every(user => !user.bot);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
every<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): this is Collection<NewKey, Value>;
|
||||||
|
every<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): this is Collection<Key, NewValue>;
|
||||||
|
every(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
every<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): this is Collection<NewKey, Value>;
|
||||||
|
every<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): this is Collection<Key, NewValue>;
|
||||||
|
every<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce<InitialValue = Value>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight<InitialValue>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
|
||||||
|
* but returns the collection instead of undefined.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute for each element
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .each(user => console.log(user.username))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .each(user => console.log(user.username));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
each(fn: (value: Value, key: Key, collection: this) => void): this;
|
||||||
|
each<This>(fn: (this: This, value: Value, key: Key, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Runs a function on the collection and returns the collection.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
tap(fn: (collection: this) => void): this;
|
||||||
|
tap<This>(fn: (this: This, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections: ReadonlyCollection<Key, Value>[]): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection: ReadonlyCollection<Key, Value>): boolean;
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction?: Comparator<Key, Value>): this;
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge<OtherValue, ResultValue>(other: ReadonlyCollection<Key, OtherValue>, whenInSelf: (value: Value, key: Key) => Keep<ResultValue>, whenInOther: (valueOther: OtherValue, key: Key) => Keep<ResultValue>, whenInBoth: (value: Value, valueOther: OtherValue, key: Key) => Keep<ResultValue>): Collection<Key, ResultValue>;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction?: Comparator<Key, Value>): Collection<Key, Value>;
|
||||||
|
toJSON(): [Key, Value][];
|
||||||
|
private static defaultSort;
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries<Key, Value>(entries: Iterable<[Key, Value]>, combine: (firstValue: Value, secondValue: Value, key: Key) => Value): Collection<Key, Value>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Keep<Value> = {
|
||||||
|
keep: false;
|
||||||
|
} | {
|
||||||
|
keep: true;
|
||||||
|
value: Value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Comparator<Key, Value> = (firstValue: Value, secondValue: Value, firstKey: Key, secondKey: Key) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
|
535
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.d.ts
generated
vendored
Normal file
535
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,535 @@
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface CollectionConstructor {
|
||||||
|
new (): Collection<unknown, unknown>;
|
||||||
|
new <Key, Value>(entries?: readonly (readonly [Key, Value])[] | null): Collection<Key, Value>;
|
||||||
|
new <Key, Value>(iterable: Iterable<readonly [Key, Value]>): Collection<Key, Value>;
|
||||||
|
readonly prototype: Collection<unknown, unknown>;
|
||||||
|
readonly [Symbol.species]: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an immutable version of a collection
|
||||||
|
*/
|
||||||
|
type ReadonlyCollection<Key, Value> = Omit<Collection<Key, Value>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
constructor: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||||
|
* an ID, for significantly improved performance and ease-of-use.
|
||||||
|
*
|
||||||
|
* @typeParam Key - The key type this collection holds
|
||||||
|
* @typeParam Value - The value type this collection holds
|
||||||
|
*/
|
||||||
|
declare class Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key: Key, defaultValueGenerator: (key: Key, collection: this) => Value): Value;
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Obtains the first value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the beginning
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||||
|
*/
|
||||||
|
first(): Value | undefined;
|
||||||
|
first(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the first key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the beginning
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
firstKey(): Key | undefined;
|
||||||
|
firstKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Obtains the last value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the end
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
last(): Value | undefined;
|
||||||
|
last(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the last key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the end
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
lastKey(): Key | undefined;
|
||||||
|
lastKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index: number): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index: number): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Obtains unique random value(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain randomly
|
||||||
|
* @returns A single value if no amount is provided or an array of values
|
||||||
|
*/
|
||||||
|
random(): Value | undefined;
|
||||||
|
random(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains unique random key(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain randomly
|
||||||
|
* @returns A single key if no amount is provided or an array
|
||||||
|
*/
|
||||||
|
randomKey(): Key | undefined;
|
||||||
|
randomKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse(): this;
|
||||||
|
/**
|
||||||
|
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
|
||||||
|
* All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||||
|
* should use the `get` method. See
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.find(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
find<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
find(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
find<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
find<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.findKey(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
findKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast | Array.findLast()}.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLast<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
findLast(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
findLast<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
findLast<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex | Array.findLastIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLastKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findLastKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findLastKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findLastKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Removes items that satisfy the provided filter function.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @returns The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn: (value: Value, key: Key, collection: this) => unknown): number;
|
||||||
|
sweep<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): number;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.filter(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): Collection<NewKey, Value>;
|
||||||
|
filter<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): Collection<Key, NewValue>;
|
||||||
|
filter(fn: (value: Value, key: Key, collection: this) => unknown): Collection<Key, Value>;
|
||||||
|
filter<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): Collection<NewKey, Value>;
|
||||||
|
filter<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
filter<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
partition<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition(fn: (value: Value, key: Key, collection: this) => unknown): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
partition<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
/**
|
||||||
|
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces a new Collection
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.flatMap(guild => guild.members.cache);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
flatMap<NewValue>(fn: (value: Value, key: Key, collection: this) => Collection<Key, NewValue>): Collection<Key, NewValue>;
|
||||||
|
flatMap<NewValue, This>(fn: (this: This, value: Value, key: Key, collection: this) => Collection<Key, NewValue>, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into an array. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new array, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.map(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
map<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): NewValue[];
|
||||||
|
map<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): NewValue[];
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into a collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new collection, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.mapValues(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
mapValues<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): Collection<Key, NewValue>;
|
||||||
|
mapValues<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.some(user => user.discriminator === '0000');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
some(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
some<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if all items passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.every(user => !user.bot);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
every<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): this is Collection<NewKey, Value>;
|
||||||
|
every<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): this is Collection<Key, NewValue>;
|
||||||
|
every(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
every<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): this is Collection<NewKey, Value>;
|
||||||
|
every<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): this is Collection<Key, NewValue>;
|
||||||
|
every<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce<InitialValue = Value>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight<InitialValue>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
|
||||||
|
* but returns the collection instead of undefined.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute for each element
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .each(user => console.log(user.username))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .each(user => console.log(user.username));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
each(fn: (value: Value, key: Key, collection: this) => void): this;
|
||||||
|
each<This>(fn: (this: This, value: Value, key: Key, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Runs a function on the collection and returns the collection.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
tap(fn: (collection: this) => void): this;
|
||||||
|
tap<This>(fn: (this: This, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections: ReadonlyCollection<Key, Value>[]): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection: ReadonlyCollection<Key, Value>): boolean;
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction?: Comparator<Key, Value>): this;
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge<OtherValue, ResultValue>(other: ReadonlyCollection<Key, OtherValue>, whenInSelf: (value: Value, key: Key) => Keep<ResultValue>, whenInOther: (valueOther: OtherValue, key: Key) => Keep<ResultValue>, whenInBoth: (value: Value, valueOther: OtherValue, key: Key) => Keep<ResultValue>): Collection<Key, ResultValue>;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction?: Comparator<Key, Value>): Collection<Key, Value>;
|
||||||
|
toJSON(): [Key, Value][];
|
||||||
|
private static defaultSort;
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries<Key, Value>(entries: Iterable<[Key, Value]>, combine: (firstValue: Value, secondValue: Value, key: Key) => Value): Collection<Key, Value>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Keep<Value> = {
|
||||||
|
keep: false;
|
||||||
|
} | {
|
||||||
|
keep: true;
|
||||||
|
value: Value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Comparator<Key, Value> = (firstValue: Value, secondValue: Value, firstKey: Key, secondKey: Key) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
|
654
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.js
generated
vendored
Normal file
654
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.js
generated
vendored
Normal file
|
@ -0,0 +1,654 @@
|
||||||
|
"use strict";
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
var __export = (target, all) => {
|
||||||
|
for (var name in all)
|
||||||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||||||
|
};
|
||||||
|
var __copyProps = (to, from, except, desc) => {
|
||||||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||||||
|
for (let key of __getOwnPropNames(from))
|
||||||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
};
|
||||||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var src_exports = {};
|
||||||
|
__export(src_exports, {
|
||||||
|
Collection: () => Collection,
|
||||||
|
version: () => version
|
||||||
|
});
|
||||||
|
module.exports = __toCommonJS(src_exports);
|
||||||
|
|
||||||
|
// src/collection.ts
|
||||||
|
var Collection = class _Collection extends Map {
|
||||||
|
static {
|
||||||
|
__name(this, "Collection");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key, defaultValueGenerator) {
|
||||||
|
if (this.has(key))
|
||||||
|
return this.get(key);
|
||||||
|
if (typeof defaultValueGenerator !== "function")
|
||||||
|
throw new TypeError(`${defaultValueGenerator} is not a function`);
|
||||||
|
const defaultValue = defaultValueGenerator(key, this);
|
||||||
|
this.set(key, defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys) {
|
||||||
|
return keys.every((key) => super.has(key));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys) {
|
||||||
|
return keys.some((key) => super.has(key));
|
||||||
|
}
|
||||||
|
first(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.values().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.last(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.values();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
firstKey(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.keys().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.lastKey(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.keys();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
last(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.first(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
lastKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.firstKey(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.values()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
random(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
randomKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse() {
|
||||||
|
const entries = [...this.entries()].reverse();
|
||||||
|
this.clear();
|
||||||
|
for (const [key, value] of entries)
|
||||||
|
this.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
find(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLast(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const val = entries[index][1];
|
||||||
|
const key = entries[index][0];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLastKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
filter(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
results.set(key, val);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = [
|
||||||
|
new this.constructor[Symbol.species](),
|
||||||
|
new this.constructor[Symbol.species]()
|
||||||
|
];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
flatMap(fn, thisArg) {
|
||||||
|
const collections = this.map(fn, thisArg);
|
||||||
|
return new this.constructor[Symbol.species]().concat(...collections);
|
||||||
|
}
|
||||||
|
map(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const iter = this.entries();
|
||||||
|
return Array.from({ length: this.size }, () => {
|
||||||
|
const [key, value] = iter.next().value;
|
||||||
|
return fn(value, key, this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mapValues(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this)
|
||||||
|
coll.set(key, fn(val, key, this));
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
some(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
every(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (!fn(val, key, this))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
let accumulator;
|
||||||
|
const iterator = this.entries();
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (this.size === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
}
|
||||||
|
for (const [key, value] of iterator) {
|
||||||
|
accumulator = fn(accumulator, value, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
let accumulator;
|
||||||
|
let index;
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (entries.length === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = entries[entries.length - 1][1];
|
||||||
|
index = entries.length - 1;
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
index = entries.length;
|
||||||
|
}
|
||||||
|
while (--index >= 0) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
accumulator = fn(accumulator, val, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
each(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
fn(value, key, this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
tap(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
fn(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone() {
|
||||||
|
return new this.constructor[Symbol.species](this);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections) {
|
||||||
|
const newColl = this.clone();
|
||||||
|
for (const coll of collections) {
|
||||||
|
for (const [key, val] of coll)
|
||||||
|
newColl.set(key, val);
|
||||||
|
}
|
||||||
|
return newColl;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection) {
|
||||||
|
if (!collection)
|
||||||
|
return false;
|
||||||
|
if (this === collection)
|
||||||
|
return true;
|
||||||
|
if (this.size !== collection.size)
|
||||||
|
return false;
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!collection.has(key) || value !== collection.get(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction = _Collection.defaultSort) {
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
||||||
|
super.clear();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
super.set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species](this);
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!coll.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!this.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge(other, whenInSelf, whenInOther, whenInBoth) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
|
||||||
|
for (const key of keys) {
|
||||||
|
const hasInSelf = this.has(key);
|
||||||
|
const hasInOther = other.has(key);
|
||||||
|
if (hasInSelf && hasInOther) {
|
||||||
|
const result = whenInBoth(this.get(key), other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInSelf) {
|
||||||
|
const result = whenInSelf(this.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInOther) {
|
||||||
|
const result = whenInOther(other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed() {
|
||||||
|
return new this.constructor[Symbol.species](this).reverse();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction = _Collection.defaultSort) {
|
||||||
|
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||||
|
}
|
||||||
|
toJSON() {
|
||||||
|
return [...this.entries()];
|
||||||
|
}
|
||||||
|
static defaultSort(firstValue, secondValue) {
|
||||||
|
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries(entries, combine) {
|
||||||
|
const coll = new _Collection();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
if (coll.has(key)) {
|
||||||
|
coll.set(key, combine(coll.get(key), value, key));
|
||||||
|
} else {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var version = "2.0.0";
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
Collection,
|
||||||
|
version
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=index.js.map
|
1
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.js.map
generated
vendored
Normal file
1
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
628
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.mjs
generated
vendored
Normal file
628
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.mjs
generated
vendored
Normal file
|
@ -0,0 +1,628 @@
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
|
||||||
|
// src/collection.ts
|
||||||
|
var Collection = class _Collection extends Map {
|
||||||
|
static {
|
||||||
|
__name(this, "Collection");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key, defaultValueGenerator) {
|
||||||
|
if (this.has(key))
|
||||||
|
return this.get(key);
|
||||||
|
if (typeof defaultValueGenerator !== "function")
|
||||||
|
throw new TypeError(`${defaultValueGenerator} is not a function`);
|
||||||
|
const defaultValue = defaultValueGenerator(key, this);
|
||||||
|
this.set(key, defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys) {
|
||||||
|
return keys.every((key) => super.has(key));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys) {
|
||||||
|
return keys.some((key) => super.has(key));
|
||||||
|
}
|
||||||
|
first(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.values().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.last(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.values();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
firstKey(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.keys().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.lastKey(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.keys();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
last(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.first(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
lastKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.firstKey(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.values()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
random(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
randomKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse() {
|
||||||
|
const entries = [...this.entries()].reverse();
|
||||||
|
this.clear();
|
||||||
|
for (const [key, value] of entries)
|
||||||
|
this.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
find(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLast(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const val = entries[index][1];
|
||||||
|
const key = entries[index][0];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLastKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
filter(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
results.set(key, val);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = [
|
||||||
|
new this.constructor[Symbol.species](),
|
||||||
|
new this.constructor[Symbol.species]()
|
||||||
|
];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
flatMap(fn, thisArg) {
|
||||||
|
const collections = this.map(fn, thisArg);
|
||||||
|
return new this.constructor[Symbol.species]().concat(...collections);
|
||||||
|
}
|
||||||
|
map(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const iter = this.entries();
|
||||||
|
return Array.from({ length: this.size }, () => {
|
||||||
|
const [key, value] = iter.next().value;
|
||||||
|
return fn(value, key, this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mapValues(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this)
|
||||||
|
coll.set(key, fn(val, key, this));
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
some(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
every(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (!fn(val, key, this))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
let accumulator;
|
||||||
|
const iterator = this.entries();
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (this.size === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
}
|
||||||
|
for (const [key, value] of iterator) {
|
||||||
|
accumulator = fn(accumulator, value, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
let accumulator;
|
||||||
|
let index;
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (entries.length === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = entries[entries.length - 1][1];
|
||||||
|
index = entries.length - 1;
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
index = entries.length;
|
||||||
|
}
|
||||||
|
while (--index >= 0) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
accumulator = fn(accumulator, val, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
each(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
fn(value, key, this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
tap(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
fn(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone() {
|
||||||
|
return new this.constructor[Symbol.species](this);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections) {
|
||||||
|
const newColl = this.clone();
|
||||||
|
for (const coll of collections) {
|
||||||
|
for (const [key, val] of coll)
|
||||||
|
newColl.set(key, val);
|
||||||
|
}
|
||||||
|
return newColl;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection) {
|
||||||
|
if (!collection)
|
||||||
|
return false;
|
||||||
|
if (this === collection)
|
||||||
|
return true;
|
||||||
|
if (this.size !== collection.size)
|
||||||
|
return false;
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!collection.has(key) || value !== collection.get(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction = _Collection.defaultSort) {
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
||||||
|
super.clear();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
super.set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species](this);
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!coll.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!this.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge(other, whenInSelf, whenInOther, whenInBoth) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
|
||||||
|
for (const key of keys) {
|
||||||
|
const hasInSelf = this.has(key);
|
||||||
|
const hasInOther = other.has(key);
|
||||||
|
if (hasInSelf && hasInOther) {
|
||||||
|
const result = whenInBoth(this.get(key), other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInSelf) {
|
||||||
|
const result = whenInSelf(this.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInOther) {
|
||||||
|
const result = whenInOther(other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed() {
|
||||||
|
return new this.constructor[Symbol.species](this).reverse();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction = _Collection.defaultSort) {
|
||||||
|
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||||
|
}
|
||||||
|
toJSON() {
|
||||||
|
return [...this.entries()];
|
||||||
|
}
|
||||||
|
static defaultSort(firstValue, secondValue) {
|
||||||
|
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries(entries, combine) {
|
||||||
|
const coll = new _Collection();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
if (coll.has(key)) {
|
||||||
|
coll.set(key, combine(coll.get(key), value, key));
|
||||||
|
} else {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var version = "2.0.0";
|
||||||
|
export {
|
||||||
|
Collection,
|
||||||
|
version
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=index.mjs.map
|
1
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.mjs.map
generated
vendored
Normal file
1
node_modules/@discordjs/rest/node_modules/@discordjs/collection/dist/index.mjs.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
83
node_modules/@discordjs/rest/node_modules/@discordjs/collection/package.json
generated
vendored
Normal file
83
node_modules/@discordjs/rest/node_modules/@discordjs/collection/package.json
generated
vendored
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/collection",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"description": "Utility data structure used in discord.js",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"map",
|
||||||
|
"collection",
|
||||||
|
"utility"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/collection"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "18.18.8",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"esbuild-plugin-version-injector": "^1.2.1",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^3.0.3",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src __tests__",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src __tests__",
|
||||||
|
"fmt": "pnpm run format",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/collection/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,116 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/rest",
|
||||||
|
"version": "2.2.0",
|
||||||
|
"description": "The REST API for discord.js",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"node": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"default": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/web.d.ts",
|
||||||
|
"default": "./dist/web.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/web.d.mts",
|
||||||
|
"default": "./dist/web.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"./*": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/strategies/*.d.ts",
|
||||||
|
"default": "./dist/strategies/*.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/strategies/*.d.mts",
|
||||||
|
"default": "./dist/strategies/*.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"discord",
|
||||||
|
"api",
|
||||||
|
"rest",
|
||||||
|
"discordapp",
|
||||||
|
"discordjs"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/rest"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"dependencies": {
|
||||||
|
"@sapphire/async-queue": "^1.5.0",
|
||||||
|
"@sapphire/snowflake": "^3.5.1",
|
||||||
|
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"magic-bytes.js": "^1.5.0",
|
||||||
|
"tslib": "^2.6.2",
|
||||||
|
"undici": "5.27.2",
|
||||||
|
"@discordjs/util": "^1.0.2",
|
||||||
|
"@discordjs/collection": "^2.0.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "18.17.9",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"esbuild-plugin-version-injector": "^1.2.1",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^3.1.0",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src __tests__",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src __tests__",
|
||||||
|
"fmt": "pnpm run format",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local --minify",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/rest/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,190 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2022 Noel Buechler
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,65 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/util` is a collection of utility functions for use with discord.js.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 16.11.0 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/util
|
||||||
|
yarn add @discordjs/util
|
||||||
|
pnpm add @discordjs/util
|
||||||
|
bun add @discordjs/util
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/util/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/util
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/util
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
|
@ -0,0 +1,117 @@
|
||||||
|
/**
|
||||||
|
* Represents a type that may or may not be a promise
|
||||||
|
*/
|
||||||
|
type Awaitable<Value> = PromiseLike<Value> | Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazy is a wrapper around a value that is computed lazily. It is useful for
|
||||||
|
* cases where the value is expensive to compute and the computation may not
|
||||||
|
* be needed at all.
|
||||||
|
*
|
||||||
|
* @param cb - The callback to lazily evaluate
|
||||||
|
* @typeParam Value - The type of the value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const value = lazy(() => computeExpensiveValue());
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
declare function lazy<Value>(cb: () => Value): () => Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options for creating a range
|
||||||
|
*/
|
||||||
|
interface RangeOptions {
|
||||||
|
/**
|
||||||
|
* The end of the range (exclusive)
|
||||||
|
*/
|
||||||
|
end: number;
|
||||||
|
/**
|
||||||
|
* The start of the range (inclusive)
|
||||||
|
*/
|
||||||
|
start: number;
|
||||||
|
/**
|
||||||
|
* The amount to increment by
|
||||||
|
*
|
||||||
|
* @defaultValue `1`
|
||||||
|
*/
|
||||||
|
step?: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A generator to yield numbers in a given range
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is end-exclusive, for example the last number yielded by `range(5)` is 4. If you
|
||||||
|
* prefer for the end to be included add 1 to the range or `end` option.
|
||||||
|
* @param range - A number representing the the range to yield (exclusive) or an object with start, end and step
|
||||||
|
* @example
|
||||||
|
* Basic range
|
||||||
|
* ```ts
|
||||||
|
* for (const number of range(5)) {
|
||||||
|
* console.log(number);
|
||||||
|
* }
|
||||||
|
* // Prints 0, 1, 2, 3, 4
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* Range with a step
|
||||||
|
* ```ts
|
||||||
|
* for (const number of range({ start: 3, end: 10, step: 2 })) {
|
||||||
|
* console.log(number);
|
||||||
|
* }
|
||||||
|
* // Prints 3, 5, 7, 9
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
declare function range(range: RangeOptions | number): Generator<number, void, unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the shard id for a given guild id.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id to calculate the shard id for
|
||||||
|
* @param shardCount - The total number of shards
|
||||||
|
*/
|
||||||
|
declare function calculateShardId(guildId: string, shardCount: number): number;
|
||||||
|
|
||||||
|
declare function shouldUseGlobalFetchAndWebSocket(): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves the user agent appendix string for the current environment.
|
||||||
|
*/
|
||||||
|
declare function getUserAgentAppendix(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an object capable of representing itself as a JSON object
|
||||||
|
*
|
||||||
|
* @typeParam Value - The JSON type corresponding to {@link JSONEncodable.toJSON} outputs.
|
||||||
|
*/
|
||||||
|
interface JSONEncodable<Value> {
|
||||||
|
/**
|
||||||
|
* Transforms this object to its JSON format
|
||||||
|
*/
|
||||||
|
toJSON(): Value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Indicates if an object is encodable or not.
|
||||||
|
*
|
||||||
|
* @param maybeEncodable - The object to check against
|
||||||
|
*/
|
||||||
|
declare function isJSONEncodable(maybeEncodable: unknown): maybeEncodable is JSONEncodable<unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a structure that can be checked against another
|
||||||
|
* given structure for equality
|
||||||
|
*
|
||||||
|
* @typeParam Value - The type of object to compare the current object to
|
||||||
|
*/
|
||||||
|
interface Equatable<Value> {
|
||||||
|
/**
|
||||||
|
* Whether or not this is equal to another structure
|
||||||
|
*/
|
||||||
|
equals(other: Value): boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Indicates if an object is equatable or not.
|
||||||
|
*
|
||||||
|
* @param maybeEquatable - The object to check against
|
||||||
|
*/
|
||||||
|
declare function isEquatable(maybeEquatable: unknown): maybeEquatable is Equatable<unknown>;
|
||||||
|
|
||||||
|
export { Awaitable, Equatable, JSONEncodable, RangeOptions, calculateShardId, getUserAgentAppendix, isEquatable, isJSONEncodable, lazy, range, shouldUseGlobalFetchAndWebSocket };
|
|
@ -0,0 +1,117 @@
|
||||||
|
/**
|
||||||
|
* Represents a type that may or may not be a promise
|
||||||
|
*/
|
||||||
|
type Awaitable<Value> = PromiseLike<Value> | Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lazy is a wrapper around a value that is computed lazily. It is useful for
|
||||||
|
* cases where the value is expensive to compute and the computation may not
|
||||||
|
* be needed at all.
|
||||||
|
*
|
||||||
|
* @param cb - The callback to lazily evaluate
|
||||||
|
* @typeParam Value - The type of the value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const value = lazy(() => computeExpensiveValue());
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
declare function lazy<Value>(cb: () => Value): () => Value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options for creating a range
|
||||||
|
*/
|
||||||
|
interface RangeOptions {
|
||||||
|
/**
|
||||||
|
* The end of the range (exclusive)
|
||||||
|
*/
|
||||||
|
end: number;
|
||||||
|
/**
|
||||||
|
* The start of the range (inclusive)
|
||||||
|
*/
|
||||||
|
start: number;
|
||||||
|
/**
|
||||||
|
* The amount to increment by
|
||||||
|
*
|
||||||
|
* @defaultValue `1`
|
||||||
|
*/
|
||||||
|
step?: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A generator to yield numbers in a given range
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
* This method is end-exclusive, for example the last number yielded by `range(5)` is 4. If you
|
||||||
|
* prefer for the end to be included add 1 to the range or `end` option.
|
||||||
|
* @param range - A number representing the the range to yield (exclusive) or an object with start, end and step
|
||||||
|
* @example
|
||||||
|
* Basic range
|
||||||
|
* ```ts
|
||||||
|
* for (const number of range(5)) {
|
||||||
|
* console.log(number);
|
||||||
|
* }
|
||||||
|
* // Prints 0, 1, 2, 3, 4
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* Range with a step
|
||||||
|
* ```ts
|
||||||
|
* for (const number of range({ start: 3, end: 10, step: 2 })) {
|
||||||
|
* console.log(number);
|
||||||
|
* }
|
||||||
|
* // Prints 3, 5, 7, 9
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
declare function range(range: RangeOptions | number): Generator<number, void, unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the shard id for a given guild id.
|
||||||
|
*
|
||||||
|
* @param guildId - The guild id to calculate the shard id for
|
||||||
|
* @param shardCount - The total number of shards
|
||||||
|
*/
|
||||||
|
declare function calculateShardId(guildId: string, shardCount: number): number;
|
||||||
|
|
||||||
|
declare function shouldUseGlobalFetchAndWebSocket(): boolean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves the user agent appendix string for the current environment.
|
||||||
|
*/
|
||||||
|
declare function getUserAgentAppendix(): string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents an object capable of representing itself as a JSON object
|
||||||
|
*
|
||||||
|
* @typeParam Value - The JSON type corresponding to {@link JSONEncodable.toJSON} outputs.
|
||||||
|
*/
|
||||||
|
interface JSONEncodable<Value> {
|
||||||
|
/**
|
||||||
|
* Transforms this object to its JSON format
|
||||||
|
*/
|
||||||
|
toJSON(): Value;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Indicates if an object is encodable or not.
|
||||||
|
*
|
||||||
|
* @param maybeEncodable - The object to check against
|
||||||
|
*/
|
||||||
|
declare function isJSONEncodable(maybeEncodable: unknown): maybeEncodable is JSONEncodable<unknown>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a structure that can be checked against another
|
||||||
|
* given structure for equality
|
||||||
|
*
|
||||||
|
* @typeParam Value - The type of object to compare the current object to
|
||||||
|
*/
|
||||||
|
interface Equatable<Value> {
|
||||||
|
/**
|
||||||
|
* Whether or not this is equal to another structure
|
||||||
|
*/
|
||||||
|
equals(other: Value): boolean;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Indicates if an object is equatable or not.
|
||||||
|
*
|
||||||
|
* @param maybeEquatable - The object to check against
|
||||||
|
*/
|
||||||
|
declare function isEquatable(maybeEquatable: unknown): maybeEquatable is Equatable<unknown>;
|
||||||
|
|
||||||
|
export { Awaitable, Equatable, JSONEncodable, RangeOptions, calculateShardId, getUserAgentAppendix, isEquatable, isJSONEncodable, lazy, range, shouldUseGlobalFetchAndWebSocket };
|
|
@ -0,0 +1,130 @@
|
||||||
|
"use strict";
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
var __export = (target, all) => {
|
||||||
|
for (var name in all)
|
||||||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||||||
|
};
|
||||||
|
var __copyProps = (to, from, except, desc) => {
|
||||||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||||||
|
for (let key of __getOwnPropNames(from))
|
||||||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
};
|
||||||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var src_exports = {};
|
||||||
|
__export(src_exports, {
|
||||||
|
calculateShardId: () => calculateShardId,
|
||||||
|
getUserAgentAppendix: () => getUserAgentAppendix,
|
||||||
|
isEquatable: () => isEquatable,
|
||||||
|
isJSONEncodable: () => isJSONEncodable,
|
||||||
|
lazy: () => lazy,
|
||||||
|
range: () => range,
|
||||||
|
shouldUseGlobalFetchAndWebSocket: () => shouldUseGlobalFetchAndWebSocket
|
||||||
|
});
|
||||||
|
module.exports = __toCommonJS(src_exports);
|
||||||
|
|
||||||
|
// src/functions/lazy.ts
|
||||||
|
function lazy(cb) {
|
||||||
|
let defaultValue;
|
||||||
|
return () => defaultValue ??= cb();
|
||||||
|
}
|
||||||
|
__name(lazy, "lazy");
|
||||||
|
|
||||||
|
// src/functions/range.ts
|
||||||
|
function* range(range2) {
|
||||||
|
let rangeEnd;
|
||||||
|
let start = 0;
|
||||||
|
let step = 1;
|
||||||
|
if (typeof range2 === "number") {
|
||||||
|
rangeEnd = range2;
|
||||||
|
} else {
|
||||||
|
start = range2.start;
|
||||||
|
rangeEnd = range2.end;
|
||||||
|
step = range2.step ?? 1;
|
||||||
|
}
|
||||||
|
for (let index = start; index < rangeEnd; index += step) {
|
||||||
|
yield index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
__name(range, "range");
|
||||||
|
|
||||||
|
// src/functions/calculateShardId.ts
|
||||||
|
function calculateShardId(guildId, shardCount) {
|
||||||
|
return Number(BigInt(guildId) >> 22n) % shardCount;
|
||||||
|
}
|
||||||
|
__name(calculateShardId, "calculateShardId");
|
||||||
|
|
||||||
|
// src/functions/runtime.ts
|
||||||
|
function shouldUseGlobalFetchAndWebSocket() {
|
||||||
|
if (typeof globalThis.process === "undefined") {
|
||||||
|
return "fetch" in globalThis && "WebSocket" in globalThis;
|
||||||
|
}
|
||||||
|
if ("versions" in globalThis.process) {
|
||||||
|
return "deno" in globalThis.process.versions || "bun" in globalThis.process.versions;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
__name(shouldUseGlobalFetchAndWebSocket, "shouldUseGlobalFetchAndWebSocket");
|
||||||
|
|
||||||
|
// src/functions/userAgentAppendix.ts
|
||||||
|
function getUserAgentAppendix() {
|
||||||
|
if (typeof globalThis.EdgeRuntime !== "undefined") {
|
||||||
|
return "Vercel-Edge-Functions";
|
||||||
|
}
|
||||||
|
if (typeof globalThis.R2 !== "undefined" && typeof globalThis.WebSocketPair !== "undefined") {
|
||||||
|
return "Cloudflare-Workers";
|
||||||
|
}
|
||||||
|
if (typeof globalThis.Netlify !== "undefined") {
|
||||||
|
return "Netlify-Edge-Functions";
|
||||||
|
}
|
||||||
|
if (typeof globalThis.process !== "object") {
|
||||||
|
if (typeof globalThis.navigator === "object") {
|
||||||
|
return globalThis.navigator.userAgent;
|
||||||
|
}
|
||||||
|
return "UnknownEnvironment";
|
||||||
|
}
|
||||||
|
if ("versions" in globalThis.process) {
|
||||||
|
if ("deno" in globalThis.process.versions) {
|
||||||
|
return `Deno/${globalThis.process.versions.deno}`;
|
||||||
|
}
|
||||||
|
if ("bun" in globalThis.process.versions) {
|
||||||
|
return `Bun/${globalThis.process.versions.bun}`;
|
||||||
|
}
|
||||||
|
if ("node" in globalThis.process.versions) {
|
||||||
|
return `Node.js/${globalThis.process.versions.node}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "UnknownEnvironment";
|
||||||
|
}
|
||||||
|
__name(getUserAgentAppendix, "getUserAgentAppendix");
|
||||||
|
|
||||||
|
// src/JSONEncodable.ts
|
||||||
|
function isJSONEncodable(maybeEncodable) {
|
||||||
|
return maybeEncodable !== null && typeof maybeEncodable === "object" && "toJSON" in maybeEncodable;
|
||||||
|
}
|
||||||
|
__name(isJSONEncodable, "isJSONEncodable");
|
||||||
|
|
||||||
|
// src/Equatable.ts
|
||||||
|
function isEquatable(maybeEquatable) {
|
||||||
|
return maybeEquatable !== null && typeof maybeEquatable === "object" && "equals" in maybeEquatable;
|
||||||
|
}
|
||||||
|
__name(isEquatable, "isEquatable");
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
calculateShardId,
|
||||||
|
getUserAgentAppendix,
|
||||||
|
isEquatable,
|
||||||
|
isJSONEncodable,
|
||||||
|
lazy,
|
||||||
|
range,
|
||||||
|
shouldUseGlobalFetchAndWebSocket
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=index.js.map
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,99 @@
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
|
||||||
|
// src/functions/lazy.ts
|
||||||
|
function lazy(cb) {
|
||||||
|
let defaultValue;
|
||||||
|
return () => defaultValue ??= cb();
|
||||||
|
}
|
||||||
|
__name(lazy, "lazy");
|
||||||
|
|
||||||
|
// src/functions/range.ts
|
||||||
|
function* range(range2) {
|
||||||
|
let rangeEnd;
|
||||||
|
let start = 0;
|
||||||
|
let step = 1;
|
||||||
|
if (typeof range2 === "number") {
|
||||||
|
rangeEnd = range2;
|
||||||
|
} else {
|
||||||
|
start = range2.start;
|
||||||
|
rangeEnd = range2.end;
|
||||||
|
step = range2.step ?? 1;
|
||||||
|
}
|
||||||
|
for (let index = start; index < rangeEnd; index += step) {
|
||||||
|
yield index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
__name(range, "range");
|
||||||
|
|
||||||
|
// src/functions/calculateShardId.ts
|
||||||
|
function calculateShardId(guildId, shardCount) {
|
||||||
|
return Number(BigInt(guildId) >> 22n) % shardCount;
|
||||||
|
}
|
||||||
|
__name(calculateShardId, "calculateShardId");
|
||||||
|
|
||||||
|
// src/functions/runtime.ts
|
||||||
|
function shouldUseGlobalFetchAndWebSocket() {
|
||||||
|
if (typeof globalThis.process === "undefined") {
|
||||||
|
return "fetch" in globalThis && "WebSocket" in globalThis;
|
||||||
|
}
|
||||||
|
if ("versions" in globalThis.process) {
|
||||||
|
return "deno" in globalThis.process.versions || "bun" in globalThis.process.versions;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
__name(shouldUseGlobalFetchAndWebSocket, "shouldUseGlobalFetchAndWebSocket");
|
||||||
|
|
||||||
|
// src/functions/userAgentAppendix.ts
|
||||||
|
function getUserAgentAppendix() {
|
||||||
|
if (typeof globalThis.EdgeRuntime !== "undefined") {
|
||||||
|
return "Vercel-Edge-Functions";
|
||||||
|
}
|
||||||
|
if (typeof globalThis.R2 !== "undefined" && typeof globalThis.WebSocketPair !== "undefined") {
|
||||||
|
return "Cloudflare-Workers";
|
||||||
|
}
|
||||||
|
if (typeof globalThis.Netlify !== "undefined") {
|
||||||
|
return "Netlify-Edge-Functions";
|
||||||
|
}
|
||||||
|
if (typeof globalThis.process !== "object") {
|
||||||
|
if (typeof globalThis.navigator === "object") {
|
||||||
|
return globalThis.navigator.userAgent;
|
||||||
|
}
|
||||||
|
return "UnknownEnvironment";
|
||||||
|
}
|
||||||
|
if ("versions" in globalThis.process) {
|
||||||
|
if ("deno" in globalThis.process.versions) {
|
||||||
|
return `Deno/${globalThis.process.versions.deno}`;
|
||||||
|
}
|
||||||
|
if ("bun" in globalThis.process.versions) {
|
||||||
|
return `Bun/${globalThis.process.versions.bun}`;
|
||||||
|
}
|
||||||
|
if ("node" in globalThis.process.versions) {
|
||||||
|
return `Node.js/${globalThis.process.versions.node}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "UnknownEnvironment";
|
||||||
|
}
|
||||||
|
__name(getUserAgentAppendix, "getUserAgentAppendix");
|
||||||
|
|
||||||
|
// src/JSONEncodable.ts
|
||||||
|
function isJSONEncodable(maybeEncodable) {
|
||||||
|
return maybeEncodable !== null && typeof maybeEncodable === "object" && "toJSON" in maybeEncodable;
|
||||||
|
}
|
||||||
|
__name(isJSONEncodable, "isJSONEncodable");
|
||||||
|
|
||||||
|
// src/Equatable.ts
|
||||||
|
function isEquatable(maybeEquatable) {
|
||||||
|
return maybeEquatable !== null && typeof maybeEquatable === "object" && "equals" in maybeEquatable;
|
||||||
|
}
|
||||||
|
__name(isEquatable, "isEquatable");
|
||||||
|
export {
|
||||||
|
calculateShardId,
|
||||||
|
getUserAgentAppendix,
|
||||||
|
isEquatable,
|
||||||
|
isJSONEncodable,
|
||||||
|
lazy,
|
||||||
|
range,
|
||||||
|
shouldUseGlobalFetchAndWebSocket
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=index.mjs.map
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,87 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/util",
|
||||||
|
"version": "1.0.2",
|
||||||
|
"description": "Utilities shared across Discord.js packages",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"api",
|
||||||
|
"bot",
|
||||||
|
"client",
|
||||||
|
"node",
|
||||||
|
"discordjs"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/util"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "16.18.60",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^3.0.3",
|
||||||
|
"tsd": "^0.29.0",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"tsd": {
|
||||||
|
"directory": "__tests__/types"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"test": "vitest run && tsd",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src",
|
||||||
|
"fmt": "pnpm run format",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/util/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,191 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2022 Noel Buechler
|
||||||
|
Copyright 2022 Denis Cristea
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,197 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/ws"><img src="https://img.shields.io/npm/v/@discordjs/ws.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/ws"><img src="https://img.shields.io/npm/dt/@discordjs/ws.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=ws" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/ws` is a powerful wrapper around Discord's gateway.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 16.11.0 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/ws
|
||||||
|
yarn add @discordjs/ws
|
||||||
|
pnpm add @discordjs/ws
|
||||||
|
bun add @discordjs/ws
|
||||||
|
```
|
||||||
|
|
||||||
|
### Optional packages
|
||||||
|
|
||||||
|
- [zlib-sync](https://www.npmjs.com/package/zlib-sync) for WebSocket data compression and inflation (`npm install zlib-sync`)
|
||||||
|
- [bufferutil](https://www.npmjs.com/package/bufferutil) for a much faster WebSocket connection (`npm install bufferutil`)
|
||||||
|
- [utf-8-validate](https://www.npmjs.com/package/utf-8-validate) in combination with `bufferutil` for much faster WebSocket processing (`npm install utf-8-validate`)
|
||||||
|
|
||||||
|
## Example usage
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { WebSocketManager, WebSocketShardEvents, CompressionMethod } from '@discordjs/ws';
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
|
||||||
|
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
|
||||||
|
// This example will spawn Discord's recommended shard count, all under the current process.
|
||||||
|
const manager = new WebSocketManager({
|
||||||
|
token: process.env.DISCORD_TOKEN,
|
||||||
|
intents: 0, // for no intents
|
||||||
|
rest,
|
||||||
|
// uncomment if you have zlib-sync installed and want to use compression
|
||||||
|
// compression: CompressionMethod.ZlibStream,
|
||||||
|
});
|
||||||
|
|
||||||
|
manager.on(WebSocketShardEvents.Dispatch, (event) => {
|
||||||
|
// Process gateway events here.
|
||||||
|
});
|
||||||
|
|
||||||
|
await manager.connect();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Specify shards
|
||||||
|
|
||||||
|
```ts
|
||||||
|
// Spawn 4 shards
|
||||||
|
const manager = new WebSocketManager({
|
||||||
|
token: process.env.DISCORD_TOKEN,
|
||||||
|
intents: 0,
|
||||||
|
rest,
|
||||||
|
shardCount: 4,
|
||||||
|
});
|
||||||
|
|
||||||
|
// The manager also supports being responsible for only a subset of your shards:
|
||||||
|
|
||||||
|
// Your bot will run 8 shards overall
|
||||||
|
// This manager will only take care of 0, 2, 4, and 6
|
||||||
|
const manager = new WebSocketManager({
|
||||||
|
token: process.env.DISCORD_TOKEN,
|
||||||
|
intents: 0,
|
||||||
|
rest,
|
||||||
|
shardCount: 8,
|
||||||
|
shardIds: [0, 2, 4, 6],
|
||||||
|
});
|
||||||
|
|
||||||
|
// Alternatively, if your shards are consecutive, you can pass in a range
|
||||||
|
const manager = new WebSocketManager({
|
||||||
|
token: process.env.DISCORD_TOKEN,
|
||||||
|
intents: 0,
|
||||||
|
rest,
|
||||||
|
shardCount: 8,
|
||||||
|
shardIds: {
|
||||||
|
start: 0,
|
||||||
|
end: 4,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Specify `worker_threads`
|
||||||
|
|
||||||
|
You can also have the shards spawn in worker threads:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { WebSocketManager, WorkerShardingStrategy } from '@discordjs/ws';
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
|
||||||
|
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
|
||||||
|
const manager = new WebSocketManager({
|
||||||
|
token: process.env.DISCORD_TOKEN,
|
||||||
|
intents: 0,
|
||||||
|
rest,
|
||||||
|
shardCount: 6,
|
||||||
|
// This will cause 3 workers to spawn, 2 shards per each
|
||||||
|
buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 }),
|
||||||
|
// Or maybe you want all your shards under a single worker
|
||||||
|
buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 'all' }),
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note**: By default, this will cause the workers to effectively only be responsible for the WebSocket connection, they simply pass up all the events back to the main process for the manager to emit. If you want to have the workers handle events as well, you can pass in a `workerPath` option to the `WorkerShardingStrategy` constructor:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { WebSocketManager, WorkerShardingStrategy } from '@discordjs/ws';
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
|
||||||
|
const rest = new REST().setToken(process.env.DISCORD_TOKEN);
|
||||||
|
const manager = new WebSocketManager({
|
||||||
|
token: process.env.DISCORD_TOKEN,
|
||||||
|
intents: 0,
|
||||||
|
rest,
|
||||||
|
buildStrategy: (manager) =>
|
||||||
|
new WorkerShardingStrategy(manager, {
|
||||||
|
shardsPerWorker: 2,
|
||||||
|
workerPath: './worker.js',
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
And your `worker.ts` file:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { WorkerBootstrapper, WebSocketShardEvents } from '@discordjs/ws';
|
||||||
|
|
||||||
|
const bootstrapper = new WorkerBootstrapper();
|
||||||
|
void bootstrapper.bootstrap({
|
||||||
|
// Those will be sent to the main thread for the manager to emit
|
||||||
|
forwardEvents: [
|
||||||
|
WebSocketShardEvents.Closed,
|
||||||
|
WebSocketShardEvents.Debug,
|
||||||
|
WebSocketShardEvents.Hello,
|
||||||
|
WebSocketShardEvents.Ready,
|
||||||
|
WebSocketShardEvents.Resumed,
|
||||||
|
],
|
||||||
|
shardCallback: (shard) => {
|
||||||
|
shard.on(WebSocketShardEvents.Dispatch, (event) => {
|
||||||
|
// Process gateway events here however you want (e.g. send them through a message broker)
|
||||||
|
// You also have access to shard.id if you need it
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/ws/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/ws
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/ws
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
export { }
|
|
@ -0,0 +1,2 @@
|
||||||
|
|
||||||
|
export { }
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,669 @@
|
||||||
|
import * as _discordjs_util from '@discordjs/util';
|
||||||
|
import { Awaitable } from '@discordjs/util';
|
||||||
|
import { GatewayDispatchPayload, GatewayReadyDispatchData, GatewaySendPayload, GatewayOpcodes, GatewayIntentBits, GatewayIdentifyProperties, GatewayPresenceUpdateData, APIGatewayBotInfo } from 'discord-api-types/v10';
|
||||||
|
import * as _discordjs_collection from '@discordjs/collection';
|
||||||
|
import { Collection } from '@discordjs/collection';
|
||||||
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
import { AsyncQueue } from '@sapphire/async-queue';
|
||||||
|
|
||||||
|
declare enum WebSocketShardEvents {
|
||||||
|
Closed = "closed",
|
||||||
|
Debug = "debug",
|
||||||
|
Dispatch = "dispatch",
|
||||||
|
Error = "error",
|
||||||
|
HeartbeatComplete = "heartbeat",
|
||||||
|
Hello = "hello",
|
||||||
|
Ready = "ready",
|
||||||
|
Resumed = "resumed"
|
||||||
|
}
|
||||||
|
declare enum WebSocketShardStatus {
|
||||||
|
Idle = 0,
|
||||||
|
Connecting = 1,
|
||||||
|
Resuming = 2,
|
||||||
|
Ready = 3
|
||||||
|
}
|
||||||
|
declare enum WebSocketShardDestroyRecovery {
|
||||||
|
Reconnect = 0,
|
||||||
|
Resume = 1
|
||||||
|
}
|
||||||
|
type WebSocketShardEventsMap = {
|
||||||
|
[WebSocketShardEvents.Closed]: [{
|
||||||
|
code: number;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Debug]: [payload: {
|
||||||
|
message: string;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Dispatch]: [payload: {
|
||||||
|
data: GatewayDispatchPayload;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Error]: [payload: {
|
||||||
|
error: Error;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Hello]: [];
|
||||||
|
[WebSocketShardEvents.Ready]: [payload: {
|
||||||
|
data: GatewayReadyDispatchData;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Resumed]: [];
|
||||||
|
[WebSocketShardEvents.HeartbeatComplete]: [payload: {
|
||||||
|
ackAt: number;
|
||||||
|
heartbeatAt: number;
|
||||||
|
latency: number;
|
||||||
|
}];
|
||||||
|
};
|
||||||
|
interface WebSocketShardDestroyOptions {
|
||||||
|
code?: number;
|
||||||
|
reason?: string;
|
||||||
|
recover?: WebSocketShardDestroyRecovery;
|
||||||
|
}
|
||||||
|
declare enum CloseCodes {
|
||||||
|
Normal = 1000,
|
||||||
|
Resuming = 4200
|
||||||
|
}
|
||||||
|
interface SendRateLimitState {
|
||||||
|
remaining: number;
|
||||||
|
resetAt: number;
|
||||||
|
}
|
||||||
|
declare class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
|
||||||
|
#private;
|
||||||
|
private connection;
|
||||||
|
private useIdentifyCompress;
|
||||||
|
private inflate;
|
||||||
|
private readonly textDecoder;
|
||||||
|
private replayedEvents;
|
||||||
|
private isAck;
|
||||||
|
private sendRateLimitState;
|
||||||
|
private initialHeartbeatTimeoutController;
|
||||||
|
private heartbeatInterval;
|
||||||
|
private lastHeartbeatAt;
|
||||||
|
private initialConnectResolved;
|
||||||
|
private failedToConnectDueToNetworkError;
|
||||||
|
private readonly sendQueue;
|
||||||
|
private readonly timeoutAbortControllers;
|
||||||
|
private readonly strategy;
|
||||||
|
readonly id: number;
|
||||||
|
get status(): WebSocketShardStatus;
|
||||||
|
constructor(strategy: IContextFetchingStrategy, id: number);
|
||||||
|
connect(): Promise<void>;
|
||||||
|
private internalConnect;
|
||||||
|
destroy(options?: WebSocketShardDestroyOptions): Promise<void>;
|
||||||
|
private waitForEvent;
|
||||||
|
send(payload: GatewaySendPayload): Promise<void>;
|
||||||
|
private identify;
|
||||||
|
private resume;
|
||||||
|
private heartbeat;
|
||||||
|
private unpackMessage;
|
||||||
|
private onMessage;
|
||||||
|
private onError;
|
||||||
|
private onClose;
|
||||||
|
private debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strategies responsible for spawning, initializing connections, destroying shards, and relaying events
|
||||||
|
*/
|
||||||
|
interface IShardingStrategy {
|
||||||
|
/**
|
||||||
|
* Initializes all the shards
|
||||||
|
*/
|
||||||
|
connect(): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Destroys all the shards
|
||||||
|
*/
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Fetches the status of all the shards
|
||||||
|
*/
|
||||||
|
fetchStatus(): Awaitable<Collection<number, WebSocketShardStatus>>;
|
||||||
|
/**
|
||||||
|
* Sends a payload to a shard
|
||||||
|
*/
|
||||||
|
send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Spawns all the shards
|
||||||
|
*/
|
||||||
|
spawn(shardIds: number[]): Awaitable<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IdentifyThrottlers are responsible for dictating when a shard is allowed to identify.
|
||||||
|
*
|
||||||
|
* @see {@link https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency}
|
||||||
|
*/
|
||||||
|
interface IIdentifyThrottler {
|
||||||
|
/**
|
||||||
|
* Resolves once the given shard should be allowed to identify, or rejects if the operation was aborted.
|
||||||
|
*/
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple strategy that just spawns shards in the current process
|
||||||
|
*/
|
||||||
|
declare class SimpleShardingStrategy implements IShardingStrategy {
|
||||||
|
private readonly manager;
|
||||||
|
private readonly shards;
|
||||||
|
constructor(manager: WebSocketManager);
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.spawn}
|
||||||
|
*/
|
||||||
|
spawn(shardIds: number[]): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.connect}
|
||||||
|
*/
|
||||||
|
connect(): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.destroy}
|
||||||
|
*/
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.send}
|
||||||
|
*/
|
||||||
|
send(shardId: number, payload: GatewaySendPayload): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.fetchStatus}
|
||||||
|
*/
|
||||||
|
fetchStatus(): Promise<Collection<number, WebSocketShardStatus>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The state of a rate limit key's identify queue.
|
||||||
|
*/
|
||||||
|
interface IdentifyState {
|
||||||
|
queue: AsyncQueue;
|
||||||
|
resetsAt: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Local, in-memory identify throttler.
|
||||||
|
*/
|
||||||
|
declare class SimpleIdentifyThrottler implements IIdentifyThrottler {
|
||||||
|
private readonly maxConcurrency;
|
||||||
|
private readonly states;
|
||||||
|
constructor(maxConcurrency: number);
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IIdentifyThrottler.waitForIdentify}
|
||||||
|
*/
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valid encoding types
|
||||||
|
*/
|
||||||
|
declare enum Encoding {
|
||||||
|
JSON = "json"
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Valid compression methods
|
||||||
|
*/
|
||||||
|
declare enum CompressionMethod {
|
||||||
|
ZlibStream = "zlib-stream"
|
||||||
|
}
|
||||||
|
declare const DefaultDeviceProperty: `@discordjs/ws ${string}`;
|
||||||
|
/**
|
||||||
|
* Default options used by the manager
|
||||||
|
*/
|
||||||
|
declare const DefaultWebSocketManagerOptions: {
|
||||||
|
readonly buildIdentifyThrottler: (manager: WebSocketManager) => Promise<SimpleIdentifyThrottler>;
|
||||||
|
readonly buildStrategy: (manager: WebSocketManager) => SimpleShardingStrategy;
|
||||||
|
readonly shardCount: null;
|
||||||
|
readonly shardIds: null;
|
||||||
|
readonly largeThreshold: null;
|
||||||
|
readonly initialPresence: null;
|
||||||
|
readonly identifyProperties: {
|
||||||
|
readonly browser: `@discordjs/ws ${string}`;
|
||||||
|
readonly device: `@discordjs/ws ${string}`;
|
||||||
|
readonly os: NodeJS.Platform;
|
||||||
|
};
|
||||||
|
readonly version: "10";
|
||||||
|
readonly encoding: Encoding;
|
||||||
|
readonly compression: null;
|
||||||
|
readonly retrieveSessionInfo: (shardId: number) => SessionInfo | null;
|
||||||
|
readonly updateSessionInfo: (shardId: number, info: SessionInfo | null) => void;
|
||||||
|
readonly handshakeTimeout: 30000;
|
||||||
|
readonly helloTimeout: 60000;
|
||||||
|
readonly readyTimeout: 15000;
|
||||||
|
};
|
||||||
|
declare const ImportantGatewayOpcodes: Set<GatewayOpcodes>;
|
||||||
|
declare function getInitialSendRateLimitState(): SendRateLimitState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a range of shard ids
|
||||||
|
*/
|
||||||
|
interface ShardRange {
|
||||||
|
end: number;
|
||||||
|
start: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Session information for a given shard, used to resume a session
|
||||||
|
*/
|
||||||
|
interface SessionInfo {
|
||||||
|
/**
|
||||||
|
* URL to use when resuming
|
||||||
|
*/
|
||||||
|
resumeURL: string;
|
||||||
|
/**
|
||||||
|
* The sequence number of the last message sent by the shard
|
||||||
|
*/
|
||||||
|
sequence: number;
|
||||||
|
/**
|
||||||
|
* Session id for this shard
|
||||||
|
*/
|
||||||
|
sessionId: string;
|
||||||
|
/**
|
||||||
|
* The total number of shards at the time of this shard identifying
|
||||||
|
*/
|
||||||
|
shardCount: number;
|
||||||
|
/**
|
||||||
|
* The id of the shard
|
||||||
|
*/
|
||||||
|
shardId: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Required options for the WebSocketManager
|
||||||
|
*/
|
||||||
|
interface RequiredWebSocketManagerOptions {
|
||||||
|
/**
|
||||||
|
* The intents to request
|
||||||
|
*/
|
||||||
|
intents: GatewayIntentBits | 0;
|
||||||
|
/**
|
||||||
|
* The REST instance to use for fetching gateway information
|
||||||
|
*/
|
||||||
|
rest: REST;
|
||||||
|
/**
|
||||||
|
* The token to use for identifying with the gateway
|
||||||
|
*/
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Optional additional configuration for the WebSocketManager
|
||||||
|
*/
|
||||||
|
interface OptionalWebSocketManagerOptions {
|
||||||
|
/**
|
||||||
|
* Builds an identify throttler to use for this manager's shards
|
||||||
|
*/
|
||||||
|
buildIdentifyThrottler(manager: WebSocketManager): Awaitable<IIdentifyThrottler>;
|
||||||
|
/**
|
||||||
|
* Builds the strategy to use for sharding
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* token: process.env.DISCORD_TOKEN,
|
||||||
|
* intents: 0, // for no intents
|
||||||
|
* rest,
|
||||||
|
* buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 }),
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
buildStrategy(manager: WebSocketManager): IShardingStrategy;
|
||||||
|
/**
|
||||||
|
* The compression method to use
|
||||||
|
*
|
||||||
|
* @defaultValue `null` (no compression)
|
||||||
|
*/
|
||||||
|
compression: CompressionMethod | null;
|
||||||
|
/**
|
||||||
|
* The encoding to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'json'`
|
||||||
|
*/
|
||||||
|
encoding: Encoding;
|
||||||
|
/**
|
||||||
|
* How long to wait for a shard to connect before giving up
|
||||||
|
*/
|
||||||
|
handshakeTimeout: number | null;
|
||||||
|
/**
|
||||||
|
* How long to wait for a shard's HELLO packet before giving up
|
||||||
|
*/
|
||||||
|
helloTimeout: number | null;
|
||||||
|
/**
|
||||||
|
* Properties to send to the gateway when identifying
|
||||||
|
*/
|
||||||
|
identifyProperties: GatewayIdentifyProperties;
|
||||||
|
/**
|
||||||
|
* Initial presence data to send to the gateway when identifying
|
||||||
|
*/
|
||||||
|
initialPresence: GatewayPresenceUpdateData | null;
|
||||||
|
/**
|
||||||
|
* Value between 50 and 250, total number of members where the gateway will stop sending offline members in the guild member list
|
||||||
|
*/
|
||||||
|
largeThreshold: number | null;
|
||||||
|
/**
|
||||||
|
* How long to wait for a shard's READY packet before giving up
|
||||||
|
*/
|
||||||
|
readyTimeout: number | null;
|
||||||
|
/**
|
||||||
|
* Function used to retrieve session information (and attempt to resume) for a given shard
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* async retrieveSessionInfo(shardId): Awaitable<SessionInfo | null> {
|
||||||
|
* // Fetch this info from redis or similar
|
||||||
|
* return { sessionId: string, sequence: number };
|
||||||
|
* // Return null if no information is found
|
||||||
|
* },
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
|
||||||
|
/**
|
||||||
|
* The total number of shards across all WebsocketManagers you intend to instantiate.
|
||||||
|
* Use `null` to use Discord's recommended shard count
|
||||||
|
*/
|
||||||
|
shardCount: number | null;
|
||||||
|
/**
|
||||||
|
* The ids of the shards this WebSocketManager should manage.
|
||||||
|
* Use `null` to simply spawn 0 through `shardCount - 1`
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* shardIds: [1, 3, 7], // spawns shard 1, 3, and 7, nothing else
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* shardIds: {
|
||||||
|
* start: 3,
|
||||||
|
* end: 6,
|
||||||
|
* }, // spawns shards 3, 4, 5, and 6
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
shardIds: number[] | ShardRange | null;
|
||||||
|
/**
|
||||||
|
* Function used to store session information for a given shard
|
||||||
|
*/
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* The gateway version to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'10'`
|
||||||
|
*/
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
type WebSocketManagerOptions = OptionalWebSocketManagerOptions & RequiredWebSocketManagerOptions;
|
||||||
|
type ManagerShardEventsMap = {
|
||||||
|
[K in keyof WebSocketShardEventsMap]: [
|
||||||
|
WebSocketShardEventsMap[K] extends [] ? {
|
||||||
|
shardId: number;
|
||||||
|
} : WebSocketShardEventsMap[K][0] & {
|
||||||
|
shardId: number;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
declare class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||||
|
/**
|
||||||
|
* The options being used by this manager
|
||||||
|
*/
|
||||||
|
readonly options: WebSocketManagerOptions;
|
||||||
|
/**
|
||||||
|
* Internal cache for a GET /gateway/bot result
|
||||||
|
*/
|
||||||
|
private gatewayInformation;
|
||||||
|
/**
|
||||||
|
* Internal cache for the shard ids
|
||||||
|
*/
|
||||||
|
private shardIds;
|
||||||
|
/**
|
||||||
|
* Strategy used to manage shards
|
||||||
|
*
|
||||||
|
* @defaultValue `SimpleShardingStrategy`
|
||||||
|
*/
|
||||||
|
private readonly strategy;
|
||||||
|
constructor(options: Partial<OptionalWebSocketManagerOptions> & RequiredWebSocketManagerOptions);
|
||||||
|
/**
|
||||||
|
* Fetches the gateway information from Discord - or returns it from cache if available
|
||||||
|
*
|
||||||
|
* @param force - Whether to ignore the cache and force a fresh fetch
|
||||||
|
*/
|
||||||
|
fetchGatewayInformation(force?: boolean): Promise<APIGatewayBotInfo>;
|
||||||
|
/**
|
||||||
|
* Updates your total shard count on-the-fly, spawning shards as needed
|
||||||
|
*
|
||||||
|
* @param shardCount - The new shard count to use
|
||||||
|
*/
|
||||||
|
updateShardCount(shardCount: number | null): Promise<this>;
|
||||||
|
/**
|
||||||
|
* Yields the total number of shards across for your bot, accounting for Discord recommendations
|
||||||
|
*/
|
||||||
|
getShardCount(): Promise<number>;
|
||||||
|
/**
|
||||||
|
* Yields the ids of the shards this manager should manage
|
||||||
|
*/
|
||||||
|
getShardIds(force?: boolean): Promise<number[]>;
|
||||||
|
connect(): Promise<void>;
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Awaitable<void>;
|
||||||
|
send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
|
||||||
|
fetchStatus(): Awaitable<_discordjs_collection.Collection<number, WebSocketShardStatus>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FetchingStrategyOptions extends Omit<WebSocketManagerOptions, 'buildIdentifyThrottler' | 'buildStrategy' | 'rest' | 'retrieveSessionInfo' | 'shardCount' | 'shardIds' | 'updateSessionInfo'> {
|
||||||
|
readonly gatewayInformation: APIGatewayBotInfo;
|
||||||
|
readonly shardCount: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Strategies responsible solely for making manager information accessible
|
||||||
|
*/
|
||||||
|
interface IContextFetchingStrategy {
|
||||||
|
readonly options: FetchingStrategyOptions;
|
||||||
|
retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Resolves once the given shard should be allowed to identify
|
||||||
|
* This should correctly handle the signal and reject with an abort error if the operation is aborted.
|
||||||
|
* Other errors will cause the shard to reconnect.
|
||||||
|
*/
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
declare function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions>;
|
||||||
|
|
||||||
|
declare class SimpleContextFetchingStrategy implements IContextFetchingStrategy {
|
||||||
|
private readonly manager;
|
||||||
|
readonly options: FetchingStrategyOptions;
|
||||||
|
private static throttlerCache;
|
||||||
|
private static ensureThrottler;
|
||||||
|
constructor(manager: WebSocketManager, options: FetchingStrategyOptions);
|
||||||
|
retrieveSessionInfo(shardId: number): Promise<SessionInfo | null>;
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): _discordjs_util.Awaitable<void>;
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
|
||||||
|
readonly options: FetchingStrategyOptions;
|
||||||
|
private readonly sessionPromises;
|
||||||
|
private readonly waitForIdentifyPromises;
|
||||||
|
constructor(options: FetchingStrategyOptions);
|
||||||
|
retrieveSessionInfo(shardId: number): Promise<SessionInfo | null>;
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): void;
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface WorkerData extends FetchingStrategyOptions {
|
||||||
|
shardIds: number[];
|
||||||
|
}
|
||||||
|
declare enum WorkerSendPayloadOp {
|
||||||
|
Connect = 0,
|
||||||
|
Destroy = 1,
|
||||||
|
Send = 2,
|
||||||
|
SessionInfoResponse = 3,
|
||||||
|
ShardIdentifyResponse = 4,
|
||||||
|
FetchStatus = 5
|
||||||
|
}
|
||||||
|
type WorkerSendPayload = {
|
||||||
|
nonce: number;
|
||||||
|
ok: boolean;
|
||||||
|
op: WorkerSendPayloadOp.ShardIdentifyResponse;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerSendPayloadOp.FetchStatus;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerSendPayloadOp.SessionInfoResponse;
|
||||||
|
session: SessionInfo | null;
|
||||||
|
} | {
|
||||||
|
op: WorkerSendPayloadOp.Connect;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerSendPayloadOp.Destroy;
|
||||||
|
options?: WebSocketShardDestroyOptions;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerSendPayloadOp.Send;
|
||||||
|
payload: GatewaySendPayload;
|
||||||
|
shardId: number;
|
||||||
|
};
|
||||||
|
declare enum WorkerReceivePayloadOp {
|
||||||
|
Connected = 0,
|
||||||
|
Destroyed = 1,
|
||||||
|
Event = 2,
|
||||||
|
RetrieveSessionInfo = 3,
|
||||||
|
UpdateSessionInfo = 4,
|
||||||
|
WaitForIdentify = 5,
|
||||||
|
FetchStatusResponse = 6,
|
||||||
|
WorkerReady = 7,
|
||||||
|
CancelIdentify = 8
|
||||||
|
}
|
||||||
|
type WorkerReceivePayload = {
|
||||||
|
data: any;
|
||||||
|
event: WebSocketShardEvents;
|
||||||
|
op: WorkerReceivePayloadOp.Event;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.CancelIdentify;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.FetchStatusResponse;
|
||||||
|
status: WebSocketShardStatus;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.RetrieveSessionInfo;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.WaitForIdentify;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.Connected;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.Destroyed;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.UpdateSessionInfo;
|
||||||
|
session: SessionInfo | null;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.WorkerReady;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Options for a {@link WorkerShardingStrategy}
|
||||||
|
*/
|
||||||
|
interface WorkerShardingStrategyOptions {
|
||||||
|
/**
|
||||||
|
* Dictates how many shards should be spawned per worker thread.
|
||||||
|
*/
|
||||||
|
shardsPerWorker: number | 'all';
|
||||||
|
/**
|
||||||
|
* Path to the worker file to use. The worker requires quite a bit of setup, it is recommended you leverage the {@link WorkerBootstrapper} class.
|
||||||
|
*/
|
||||||
|
workerPath?: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Strategy used to spawn threads in worker_threads
|
||||||
|
*/
|
||||||
|
declare class WorkerShardingStrategy implements IShardingStrategy {
|
||||||
|
#private;
|
||||||
|
private readonly manager;
|
||||||
|
private readonly options;
|
||||||
|
private readonly connectPromises;
|
||||||
|
private readonly destroyPromises;
|
||||||
|
private readonly fetchStatusPromises;
|
||||||
|
private readonly waitForIdentifyControllers;
|
||||||
|
private throttler?;
|
||||||
|
constructor(manager: WebSocketManager, options: WorkerShardingStrategyOptions);
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.spawn}
|
||||||
|
*/
|
||||||
|
spawn(shardIds: number[]): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.connect}
|
||||||
|
*/
|
||||||
|
connect(): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.destroy}
|
||||||
|
*/
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.send}
|
||||||
|
*/
|
||||||
|
send(shardId: number, data: GatewaySendPayload): void;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.fetchStatus}
|
||||||
|
*/
|
||||||
|
fetchStatus(): Promise<Collection<number, WebSocketShardStatus>>;
|
||||||
|
private setupWorker;
|
||||||
|
private resolveWorkerPath;
|
||||||
|
private waitForWorkerReady;
|
||||||
|
private onMessage;
|
||||||
|
private ensureThrottler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options for bootstrapping the worker
|
||||||
|
*/
|
||||||
|
interface BootstrapOptions {
|
||||||
|
/**
|
||||||
|
* Shard events to just arbitrarily forward to the parent thread for the manager to emit
|
||||||
|
* Note: By default, this will include ALL events
|
||||||
|
* you most likely want to handle dispatch within the worker itself
|
||||||
|
*/
|
||||||
|
forwardEvents?: WebSocketShardEvents[];
|
||||||
|
/**
|
||||||
|
* Function to call when a shard is created for additional setup
|
||||||
|
*/
|
||||||
|
shardCallback?(shard: WebSocketShard): Awaitable<void>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Utility class for bootstrapping a worker thread to be used for sharding
|
||||||
|
*/
|
||||||
|
declare class WorkerBootstrapper {
|
||||||
|
/**
|
||||||
|
* The data passed to the worker thread
|
||||||
|
*/
|
||||||
|
protected readonly data: WorkerData;
|
||||||
|
/**
|
||||||
|
* The shards that are managed by this worker
|
||||||
|
*/
|
||||||
|
protected readonly shards: Collection<number, WebSocketShard>;
|
||||||
|
constructor();
|
||||||
|
/**
|
||||||
|
* Helper method to initiate a shard's connection process
|
||||||
|
*/
|
||||||
|
protected connect(shardId: number): Promise<void>;
|
||||||
|
/**
|
||||||
|
* Helper method to destroy a shard
|
||||||
|
*/
|
||||||
|
protected destroy(shardId: number, options?: WebSocketShardDestroyOptions): Promise<void>;
|
||||||
|
/**
|
||||||
|
* Helper method to attach event listeners to the parentPort
|
||||||
|
*/
|
||||||
|
protected setupThreadEvents(): void;
|
||||||
|
/**
|
||||||
|
* Bootstraps the worker thread with the provided options
|
||||||
|
*/
|
||||||
|
bootstrap(options?: Readonly<BootstrapOptions>): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/ws/#readme | @discordjs/ws} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { BootstrapOptions, CloseCodes, CompressionMethod, DefaultDeviceProperty, DefaultWebSocketManagerOptions, Encoding, FetchingStrategyOptions, IContextFetchingStrategy, IIdentifyThrottler, IShardingStrategy, IdentifyState, ImportantGatewayOpcodes, ManagerShardEventsMap, OptionalWebSocketManagerOptions, RequiredWebSocketManagerOptions, SendRateLimitState, SessionInfo, ShardRange, SimpleContextFetchingStrategy, SimpleIdentifyThrottler, SimpleShardingStrategy, WebSocketManager, WebSocketManagerOptions, WebSocketShard, WebSocketShardDestroyOptions, WebSocketShardDestroyRecovery, WebSocketShardEvents, WebSocketShardEventsMap, WebSocketShardStatus, WorkerBootstrapper, WorkerContextFetchingStrategy, WorkerData, WorkerReceivePayload, WorkerReceivePayloadOp, WorkerSendPayload, WorkerSendPayloadOp, WorkerShardingStrategy, WorkerShardingStrategyOptions, getInitialSendRateLimitState, managerToFetchingStrategyOptions, version };
|
|
@ -0,0 +1,669 @@
|
||||||
|
import * as _discordjs_util from '@discordjs/util';
|
||||||
|
import { Awaitable } from '@discordjs/util';
|
||||||
|
import { GatewayDispatchPayload, GatewayReadyDispatchData, GatewaySendPayload, GatewayOpcodes, GatewayIntentBits, GatewayIdentifyProperties, GatewayPresenceUpdateData, APIGatewayBotInfo } from 'discord-api-types/v10';
|
||||||
|
import * as _discordjs_collection from '@discordjs/collection';
|
||||||
|
import { Collection } from '@discordjs/collection';
|
||||||
|
import { AsyncEventEmitter } from '@vladfrangu/async_event_emitter';
|
||||||
|
import { REST } from '@discordjs/rest';
|
||||||
|
import { AsyncQueue } from '@sapphire/async-queue';
|
||||||
|
|
||||||
|
declare enum WebSocketShardEvents {
|
||||||
|
Closed = "closed",
|
||||||
|
Debug = "debug",
|
||||||
|
Dispatch = "dispatch",
|
||||||
|
Error = "error",
|
||||||
|
HeartbeatComplete = "heartbeat",
|
||||||
|
Hello = "hello",
|
||||||
|
Ready = "ready",
|
||||||
|
Resumed = "resumed"
|
||||||
|
}
|
||||||
|
declare enum WebSocketShardStatus {
|
||||||
|
Idle = 0,
|
||||||
|
Connecting = 1,
|
||||||
|
Resuming = 2,
|
||||||
|
Ready = 3
|
||||||
|
}
|
||||||
|
declare enum WebSocketShardDestroyRecovery {
|
||||||
|
Reconnect = 0,
|
||||||
|
Resume = 1
|
||||||
|
}
|
||||||
|
type WebSocketShardEventsMap = {
|
||||||
|
[WebSocketShardEvents.Closed]: [{
|
||||||
|
code: number;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Debug]: [payload: {
|
||||||
|
message: string;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Dispatch]: [payload: {
|
||||||
|
data: GatewayDispatchPayload;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Error]: [payload: {
|
||||||
|
error: Error;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Hello]: [];
|
||||||
|
[WebSocketShardEvents.Ready]: [payload: {
|
||||||
|
data: GatewayReadyDispatchData;
|
||||||
|
}];
|
||||||
|
[WebSocketShardEvents.Resumed]: [];
|
||||||
|
[WebSocketShardEvents.HeartbeatComplete]: [payload: {
|
||||||
|
ackAt: number;
|
||||||
|
heartbeatAt: number;
|
||||||
|
latency: number;
|
||||||
|
}];
|
||||||
|
};
|
||||||
|
interface WebSocketShardDestroyOptions {
|
||||||
|
code?: number;
|
||||||
|
reason?: string;
|
||||||
|
recover?: WebSocketShardDestroyRecovery;
|
||||||
|
}
|
||||||
|
declare enum CloseCodes {
|
||||||
|
Normal = 1000,
|
||||||
|
Resuming = 4200
|
||||||
|
}
|
||||||
|
interface SendRateLimitState {
|
||||||
|
remaining: number;
|
||||||
|
resetAt: number;
|
||||||
|
}
|
||||||
|
declare class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
|
||||||
|
#private;
|
||||||
|
private connection;
|
||||||
|
private useIdentifyCompress;
|
||||||
|
private inflate;
|
||||||
|
private readonly textDecoder;
|
||||||
|
private replayedEvents;
|
||||||
|
private isAck;
|
||||||
|
private sendRateLimitState;
|
||||||
|
private initialHeartbeatTimeoutController;
|
||||||
|
private heartbeatInterval;
|
||||||
|
private lastHeartbeatAt;
|
||||||
|
private initialConnectResolved;
|
||||||
|
private failedToConnectDueToNetworkError;
|
||||||
|
private readonly sendQueue;
|
||||||
|
private readonly timeoutAbortControllers;
|
||||||
|
private readonly strategy;
|
||||||
|
readonly id: number;
|
||||||
|
get status(): WebSocketShardStatus;
|
||||||
|
constructor(strategy: IContextFetchingStrategy, id: number);
|
||||||
|
connect(): Promise<void>;
|
||||||
|
private internalConnect;
|
||||||
|
destroy(options?: WebSocketShardDestroyOptions): Promise<void>;
|
||||||
|
private waitForEvent;
|
||||||
|
send(payload: GatewaySendPayload): Promise<void>;
|
||||||
|
private identify;
|
||||||
|
private resume;
|
||||||
|
private heartbeat;
|
||||||
|
private unpackMessage;
|
||||||
|
private onMessage;
|
||||||
|
private onError;
|
||||||
|
private onClose;
|
||||||
|
private debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Strategies responsible for spawning, initializing connections, destroying shards, and relaying events
|
||||||
|
*/
|
||||||
|
interface IShardingStrategy {
|
||||||
|
/**
|
||||||
|
* Initializes all the shards
|
||||||
|
*/
|
||||||
|
connect(): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Destroys all the shards
|
||||||
|
*/
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Fetches the status of all the shards
|
||||||
|
*/
|
||||||
|
fetchStatus(): Awaitable<Collection<number, WebSocketShardStatus>>;
|
||||||
|
/**
|
||||||
|
* Sends a payload to a shard
|
||||||
|
*/
|
||||||
|
send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Spawns all the shards
|
||||||
|
*/
|
||||||
|
spawn(shardIds: number[]): Awaitable<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IdentifyThrottlers are responsible for dictating when a shard is allowed to identify.
|
||||||
|
*
|
||||||
|
* @see {@link https://discord.com/developers/docs/topics/gateway#sharding-max-concurrency}
|
||||||
|
*/
|
||||||
|
interface IIdentifyThrottler {
|
||||||
|
/**
|
||||||
|
* Resolves once the given shard should be allowed to identify, or rejects if the operation was aborted.
|
||||||
|
*/
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple strategy that just spawns shards in the current process
|
||||||
|
*/
|
||||||
|
declare class SimpleShardingStrategy implements IShardingStrategy {
|
||||||
|
private readonly manager;
|
||||||
|
private readonly shards;
|
||||||
|
constructor(manager: WebSocketManager);
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.spawn}
|
||||||
|
*/
|
||||||
|
spawn(shardIds: number[]): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.connect}
|
||||||
|
*/
|
||||||
|
connect(): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.destroy}
|
||||||
|
*/
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.send}
|
||||||
|
*/
|
||||||
|
send(shardId: number, payload: GatewaySendPayload): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.fetchStatus}
|
||||||
|
*/
|
||||||
|
fetchStatus(): Promise<Collection<number, WebSocketShardStatus>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The state of a rate limit key's identify queue.
|
||||||
|
*/
|
||||||
|
interface IdentifyState {
|
||||||
|
queue: AsyncQueue;
|
||||||
|
resetsAt: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Local, in-memory identify throttler.
|
||||||
|
*/
|
||||||
|
declare class SimpleIdentifyThrottler implements IIdentifyThrottler {
|
||||||
|
private readonly maxConcurrency;
|
||||||
|
private readonly states;
|
||||||
|
constructor(maxConcurrency: number);
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IIdentifyThrottler.waitForIdentify}
|
||||||
|
*/
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Valid encoding types
|
||||||
|
*/
|
||||||
|
declare enum Encoding {
|
||||||
|
JSON = "json"
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Valid compression methods
|
||||||
|
*/
|
||||||
|
declare enum CompressionMethod {
|
||||||
|
ZlibStream = "zlib-stream"
|
||||||
|
}
|
||||||
|
declare const DefaultDeviceProperty: `@discordjs/ws ${string}`;
|
||||||
|
/**
|
||||||
|
* Default options used by the manager
|
||||||
|
*/
|
||||||
|
declare const DefaultWebSocketManagerOptions: {
|
||||||
|
readonly buildIdentifyThrottler: (manager: WebSocketManager) => Promise<SimpleIdentifyThrottler>;
|
||||||
|
readonly buildStrategy: (manager: WebSocketManager) => SimpleShardingStrategy;
|
||||||
|
readonly shardCount: null;
|
||||||
|
readonly shardIds: null;
|
||||||
|
readonly largeThreshold: null;
|
||||||
|
readonly initialPresence: null;
|
||||||
|
readonly identifyProperties: {
|
||||||
|
readonly browser: `@discordjs/ws ${string}`;
|
||||||
|
readonly device: `@discordjs/ws ${string}`;
|
||||||
|
readonly os: NodeJS.Platform;
|
||||||
|
};
|
||||||
|
readonly version: "10";
|
||||||
|
readonly encoding: Encoding;
|
||||||
|
readonly compression: null;
|
||||||
|
readonly retrieveSessionInfo: (shardId: number) => SessionInfo | null;
|
||||||
|
readonly updateSessionInfo: (shardId: number, info: SessionInfo | null) => void;
|
||||||
|
readonly handshakeTimeout: 30000;
|
||||||
|
readonly helloTimeout: 60000;
|
||||||
|
readonly readyTimeout: 15000;
|
||||||
|
};
|
||||||
|
declare const ImportantGatewayOpcodes: Set<GatewayOpcodes>;
|
||||||
|
declare function getInitialSendRateLimitState(): SendRateLimitState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a range of shard ids
|
||||||
|
*/
|
||||||
|
interface ShardRange {
|
||||||
|
end: number;
|
||||||
|
start: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Session information for a given shard, used to resume a session
|
||||||
|
*/
|
||||||
|
interface SessionInfo {
|
||||||
|
/**
|
||||||
|
* URL to use when resuming
|
||||||
|
*/
|
||||||
|
resumeURL: string;
|
||||||
|
/**
|
||||||
|
* The sequence number of the last message sent by the shard
|
||||||
|
*/
|
||||||
|
sequence: number;
|
||||||
|
/**
|
||||||
|
* Session id for this shard
|
||||||
|
*/
|
||||||
|
sessionId: string;
|
||||||
|
/**
|
||||||
|
* The total number of shards at the time of this shard identifying
|
||||||
|
*/
|
||||||
|
shardCount: number;
|
||||||
|
/**
|
||||||
|
* The id of the shard
|
||||||
|
*/
|
||||||
|
shardId: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Required options for the WebSocketManager
|
||||||
|
*/
|
||||||
|
interface RequiredWebSocketManagerOptions {
|
||||||
|
/**
|
||||||
|
* The intents to request
|
||||||
|
*/
|
||||||
|
intents: GatewayIntentBits | 0;
|
||||||
|
/**
|
||||||
|
* The REST instance to use for fetching gateway information
|
||||||
|
*/
|
||||||
|
rest: REST;
|
||||||
|
/**
|
||||||
|
* The token to use for identifying with the gateway
|
||||||
|
*/
|
||||||
|
token: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Optional additional configuration for the WebSocketManager
|
||||||
|
*/
|
||||||
|
interface OptionalWebSocketManagerOptions {
|
||||||
|
/**
|
||||||
|
* Builds an identify throttler to use for this manager's shards
|
||||||
|
*/
|
||||||
|
buildIdentifyThrottler(manager: WebSocketManager): Awaitable<IIdentifyThrottler>;
|
||||||
|
/**
|
||||||
|
* Builds the strategy to use for sharding
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* token: process.env.DISCORD_TOKEN,
|
||||||
|
* intents: 0, // for no intents
|
||||||
|
* rest,
|
||||||
|
* buildStrategy: (manager) => new WorkerShardingStrategy(manager, { shardsPerWorker: 2 }),
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
buildStrategy(manager: WebSocketManager): IShardingStrategy;
|
||||||
|
/**
|
||||||
|
* The compression method to use
|
||||||
|
*
|
||||||
|
* @defaultValue `null` (no compression)
|
||||||
|
*/
|
||||||
|
compression: CompressionMethod | null;
|
||||||
|
/**
|
||||||
|
* The encoding to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'json'`
|
||||||
|
*/
|
||||||
|
encoding: Encoding;
|
||||||
|
/**
|
||||||
|
* How long to wait for a shard to connect before giving up
|
||||||
|
*/
|
||||||
|
handshakeTimeout: number | null;
|
||||||
|
/**
|
||||||
|
* How long to wait for a shard's HELLO packet before giving up
|
||||||
|
*/
|
||||||
|
helloTimeout: number | null;
|
||||||
|
/**
|
||||||
|
* Properties to send to the gateway when identifying
|
||||||
|
*/
|
||||||
|
identifyProperties: GatewayIdentifyProperties;
|
||||||
|
/**
|
||||||
|
* Initial presence data to send to the gateway when identifying
|
||||||
|
*/
|
||||||
|
initialPresence: GatewayPresenceUpdateData | null;
|
||||||
|
/**
|
||||||
|
* Value between 50 and 250, total number of members where the gateway will stop sending offline members in the guild member list
|
||||||
|
*/
|
||||||
|
largeThreshold: number | null;
|
||||||
|
/**
|
||||||
|
* How long to wait for a shard's READY packet before giving up
|
||||||
|
*/
|
||||||
|
readyTimeout: number | null;
|
||||||
|
/**
|
||||||
|
* Function used to retrieve session information (and attempt to resume) for a given shard
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* async retrieveSessionInfo(shardId): Awaitable<SessionInfo | null> {
|
||||||
|
* // Fetch this info from redis or similar
|
||||||
|
* return { sessionId: string, sequence: number };
|
||||||
|
* // Return null if no information is found
|
||||||
|
* },
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
|
||||||
|
/**
|
||||||
|
* The total number of shards across all WebsocketManagers you intend to instantiate.
|
||||||
|
* Use `null` to use Discord's recommended shard count
|
||||||
|
*/
|
||||||
|
shardCount: number | null;
|
||||||
|
/**
|
||||||
|
* The ids of the shards this WebSocketManager should manage.
|
||||||
|
* Use `null` to simply spawn 0 through `shardCount - 1`
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* shardIds: [1, 3, 7], // spawns shard 1, 3, and 7, nothing else
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const manager = new WebSocketManager({
|
||||||
|
* shardIds: {
|
||||||
|
* start: 3,
|
||||||
|
* end: 6,
|
||||||
|
* }, // spawns shards 3, 4, 5, and 6
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
shardIds: number[] | ShardRange | null;
|
||||||
|
/**
|
||||||
|
* Function used to store session information for a given shard
|
||||||
|
*/
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* The gateway version to use
|
||||||
|
*
|
||||||
|
* @defaultValue `'10'`
|
||||||
|
*/
|
||||||
|
version: string;
|
||||||
|
}
|
||||||
|
type WebSocketManagerOptions = OptionalWebSocketManagerOptions & RequiredWebSocketManagerOptions;
|
||||||
|
type ManagerShardEventsMap = {
|
||||||
|
[K in keyof WebSocketShardEventsMap]: [
|
||||||
|
WebSocketShardEventsMap[K] extends [] ? {
|
||||||
|
shardId: number;
|
||||||
|
} : WebSocketShardEventsMap[K][0] & {
|
||||||
|
shardId: number;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
declare class WebSocketManager extends AsyncEventEmitter<ManagerShardEventsMap> {
|
||||||
|
/**
|
||||||
|
* The options being used by this manager
|
||||||
|
*/
|
||||||
|
readonly options: WebSocketManagerOptions;
|
||||||
|
/**
|
||||||
|
* Internal cache for a GET /gateway/bot result
|
||||||
|
*/
|
||||||
|
private gatewayInformation;
|
||||||
|
/**
|
||||||
|
* Internal cache for the shard ids
|
||||||
|
*/
|
||||||
|
private shardIds;
|
||||||
|
/**
|
||||||
|
* Strategy used to manage shards
|
||||||
|
*
|
||||||
|
* @defaultValue `SimpleShardingStrategy`
|
||||||
|
*/
|
||||||
|
private readonly strategy;
|
||||||
|
constructor(options: Partial<OptionalWebSocketManagerOptions> & RequiredWebSocketManagerOptions);
|
||||||
|
/**
|
||||||
|
* Fetches the gateway information from Discord - or returns it from cache if available
|
||||||
|
*
|
||||||
|
* @param force - Whether to ignore the cache and force a fresh fetch
|
||||||
|
*/
|
||||||
|
fetchGatewayInformation(force?: boolean): Promise<APIGatewayBotInfo>;
|
||||||
|
/**
|
||||||
|
* Updates your total shard count on-the-fly, spawning shards as needed
|
||||||
|
*
|
||||||
|
* @param shardCount - The new shard count to use
|
||||||
|
*/
|
||||||
|
updateShardCount(shardCount: number | null): Promise<this>;
|
||||||
|
/**
|
||||||
|
* Yields the total number of shards across for your bot, accounting for Discord recommendations
|
||||||
|
*/
|
||||||
|
getShardCount(): Promise<number>;
|
||||||
|
/**
|
||||||
|
* Yields the ids of the shards this manager should manage
|
||||||
|
*/
|
||||||
|
getShardIds(force?: boolean): Promise<number[]>;
|
||||||
|
connect(): Promise<void>;
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Awaitable<void>;
|
||||||
|
send(shardId: number, payload: GatewaySendPayload): Awaitable<void>;
|
||||||
|
fetchStatus(): Awaitable<_discordjs_collection.Collection<number, WebSocketShardStatus>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface FetchingStrategyOptions extends Omit<WebSocketManagerOptions, 'buildIdentifyThrottler' | 'buildStrategy' | 'rest' | 'retrieveSessionInfo' | 'shardCount' | 'shardIds' | 'updateSessionInfo'> {
|
||||||
|
readonly gatewayInformation: APIGatewayBotInfo;
|
||||||
|
readonly shardCount: number;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Strategies responsible solely for making manager information accessible
|
||||||
|
*/
|
||||||
|
interface IContextFetchingStrategy {
|
||||||
|
readonly options: FetchingStrategyOptions;
|
||||||
|
retrieveSessionInfo(shardId: number): Awaitable<SessionInfo | null>;
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): Awaitable<void>;
|
||||||
|
/**
|
||||||
|
* Resolves once the given shard should be allowed to identify
|
||||||
|
* This should correctly handle the signal and reject with an abort error if the operation is aborted.
|
||||||
|
* Other errors will cause the shard to reconnect.
|
||||||
|
*/
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
declare function managerToFetchingStrategyOptions(manager: WebSocketManager): Promise<FetchingStrategyOptions>;
|
||||||
|
|
||||||
|
declare class SimpleContextFetchingStrategy implements IContextFetchingStrategy {
|
||||||
|
private readonly manager;
|
||||||
|
readonly options: FetchingStrategyOptions;
|
||||||
|
private static throttlerCache;
|
||||||
|
private static ensureThrottler;
|
||||||
|
constructor(manager: WebSocketManager, options: FetchingStrategyOptions);
|
||||||
|
retrieveSessionInfo(shardId: number): Promise<SessionInfo | null>;
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): _discordjs_util.Awaitable<void>;
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare class WorkerContextFetchingStrategy implements IContextFetchingStrategy {
|
||||||
|
readonly options: FetchingStrategyOptions;
|
||||||
|
private readonly sessionPromises;
|
||||||
|
private readonly waitForIdentifyPromises;
|
||||||
|
constructor(options: FetchingStrategyOptions);
|
||||||
|
retrieveSessionInfo(shardId: number): Promise<SessionInfo | null>;
|
||||||
|
updateSessionInfo(shardId: number, sessionInfo: SessionInfo | null): void;
|
||||||
|
waitForIdentify(shardId: number, signal: AbortSignal): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface WorkerData extends FetchingStrategyOptions {
|
||||||
|
shardIds: number[];
|
||||||
|
}
|
||||||
|
declare enum WorkerSendPayloadOp {
|
||||||
|
Connect = 0,
|
||||||
|
Destroy = 1,
|
||||||
|
Send = 2,
|
||||||
|
SessionInfoResponse = 3,
|
||||||
|
ShardIdentifyResponse = 4,
|
||||||
|
FetchStatus = 5
|
||||||
|
}
|
||||||
|
type WorkerSendPayload = {
|
||||||
|
nonce: number;
|
||||||
|
ok: boolean;
|
||||||
|
op: WorkerSendPayloadOp.ShardIdentifyResponse;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerSendPayloadOp.FetchStatus;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerSendPayloadOp.SessionInfoResponse;
|
||||||
|
session: SessionInfo | null;
|
||||||
|
} | {
|
||||||
|
op: WorkerSendPayloadOp.Connect;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerSendPayloadOp.Destroy;
|
||||||
|
options?: WebSocketShardDestroyOptions;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerSendPayloadOp.Send;
|
||||||
|
payload: GatewaySendPayload;
|
||||||
|
shardId: number;
|
||||||
|
};
|
||||||
|
declare enum WorkerReceivePayloadOp {
|
||||||
|
Connected = 0,
|
||||||
|
Destroyed = 1,
|
||||||
|
Event = 2,
|
||||||
|
RetrieveSessionInfo = 3,
|
||||||
|
UpdateSessionInfo = 4,
|
||||||
|
WaitForIdentify = 5,
|
||||||
|
FetchStatusResponse = 6,
|
||||||
|
WorkerReady = 7,
|
||||||
|
CancelIdentify = 8
|
||||||
|
}
|
||||||
|
type WorkerReceivePayload = {
|
||||||
|
data: any;
|
||||||
|
event: WebSocketShardEvents;
|
||||||
|
op: WorkerReceivePayloadOp.Event;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.CancelIdentify;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.FetchStatusResponse;
|
||||||
|
status: WebSocketShardStatus;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.RetrieveSessionInfo;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
nonce: number;
|
||||||
|
op: WorkerReceivePayloadOp.WaitForIdentify;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.Connected;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.Destroyed;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.UpdateSessionInfo;
|
||||||
|
session: SessionInfo | null;
|
||||||
|
shardId: number;
|
||||||
|
} | {
|
||||||
|
op: WorkerReceivePayloadOp.WorkerReady;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Options for a {@link WorkerShardingStrategy}
|
||||||
|
*/
|
||||||
|
interface WorkerShardingStrategyOptions {
|
||||||
|
/**
|
||||||
|
* Dictates how many shards should be spawned per worker thread.
|
||||||
|
*/
|
||||||
|
shardsPerWorker: number | 'all';
|
||||||
|
/**
|
||||||
|
* Path to the worker file to use. The worker requires quite a bit of setup, it is recommended you leverage the {@link WorkerBootstrapper} class.
|
||||||
|
*/
|
||||||
|
workerPath?: string;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Strategy used to spawn threads in worker_threads
|
||||||
|
*/
|
||||||
|
declare class WorkerShardingStrategy implements IShardingStrategy {
|
||||||
|
#private;
|
||||||
|
private readonly manager;
|
||||||
|
private readonly options;
|
||||||
|
private readonly connectPromises;
|
||||||
|
private readonly destroyPromises;
|
||||||
|
private readonly fetchStatusPromises;
|
||||||
|
private readonly waitForIdentifyControllers;
|
||||||
|
private throttler?;
|
||||||
|
constructor(manager: WebSocketManager, options: WorkerShardingStrategyOptions);
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.spawn}
|
||||||
|
*/
|
||||||
|
spawn(shardIds: number[]): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.connect}
|
||||||
|
*/
|
||||||
|
connect(): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.destroy}
|
||||||
|
*/
|
||||||
|
destroy(options?: Omit<WebSocketShardDestroyOptions, 'recover'>): Promise<void>;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.send}
|
||||||
|
*/
|
||||||
|
send(shardId: number, data: GatewaySendPayload): void;
|
||||||
|
/**
|
||||||
|
* {@inheritDoc IShardingStrategy.fetchStatus}
|
||||||
|
*/
|
||||||
|
fetchStatus(): Promise<Collection<number, WebSocketShardStatus>>;
|
||||||
|
private setupWorker;
|
||||||
|
private resolveWorkerPath;
|
||||||
|
private waitForWorkerReady;
|
||||||
|
private onMessage;
|
||||||
|
private ensureThrottler;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options for bootstrapping the worker
|
||||||
|
*/
|
||||||
|
interface BootstrapOptions {
|
||||||
|
/**
|
||||||
|
* Shard events to just arbitrarily forward to the parent thread for the manager to emit
|
||||||
|
* Note: By default, this will include ALL events
|
||||||
|
* you most likely want to handle dispatch within the worker itself
|
||||||
|
*/
|
||||||
|
forwardEvents?: WebSocketShardEvents[];
|
||||||
|
/**
|
||||||
|
* Function to call when a shard is created for additional setup
|
||||||
|
*/
|
||||||
|
shardCallback?(shard: WebSocketShard): Awaitable<void>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Utility class for bootstrapping a worker thread to be used for sharding
|
||||||
|
*/
|
||||||
|
declare class WorkerBootstrapper {
|
||||||
|
/**
|
||||||
|
* The data passed to the worker thread
|
||||||
|
*/
|
||||||
|
protected readonly data: WorkerData;
|
||||||
|
/**
|
||||||
|
* The shards that are managed by this worker
|
||||||
|
*/
|
||||||
|
protected readonly shards: Collection<number, WebSocketShard>;
|
||||||
|
constructor();
|
||||||
|
/**
|
||||||
|
* Helper method to initiate a shard's connection process
|
||||||
|
*/
|
||||||
|
protected connect(shardId: number): Promise<void>;
|
||||||
|
/**
|
||||||
|
* Helper method to destroy a shard
|
||||||
|
*/
|
||||||
|
protected destroy(shardId: number, options?: WebSocketShardDestroyOptions): Promise<void>;
|
||||||
|
/**
|
||||||
|
* Helper method to attach event listeners to the parentPort
|
||||||
|
*/
|
||||||
|
protected setupThreadEvents(): void;
|
||||||
|
/**
|
||||||
|
* Bootstraps the worker thread with the provided options
|
||||||
|
*/
|
||||||
|
bootstrap(options?: Readonly<BootstrapOptions>): Promise<void>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/ws/#readme | @discordjs/ws} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { BootstrapOptions, CloseCodes, CompressionMethod, DefaultDeviceProperty, DefaultWebSocketManagerOptions, Encoding, FetchingStrategyOptions, IContextFetchingStrategy, IIdentifyThrottler, IShardingStrategy, IdentifyState, ImportantGatewayOpcodes, ManagerShardEventsMap, OptionalWebSocketManagerOptions, RequiredWebSocketManagerOptions, SendRateLimitState, SessionInfo, ShardRange, SimpleContextFetchingStrategy, SimpleIdentifyThrottler, SimpleShardingStrategy, WebSocketManager, WebSocketManagerOptions, WebSocketShard, WebSocketShardDestroyOptions, WebSocketShardDestroyRecovery, WebSocketShardEvents, WebSocketShardEventsMap, WebSocketShardStatus, WorkerBootstrapper, WorkerContextFetchingStrategy, WorkerData, WorkerReceivePayload, WorkerReceivePayloadOp, WorkerSendPayload, WorkerSendPayloadOp, WorkerShardingStrategy, WorkerShardingStrategyOptions, getInitialSendRateLimitState, managerToFetchingStrategyOptions, version };
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
191
node_modules/@discordjs/ws/node_modules/@discordjs/collection/LICENSE
generated
vendored
Normal file
191
node_modules/@discordjs/ws/node_modules/@discordjs/collection/LICENSE
generated
vendored
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
Copyright 2021 Noel Buechler
|
||||||
|
Copyright 2015 Amish Shah
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
67
node_modules/@discordjs/ws/node_modules/@discordjs/collection/README.md
generated
vendored
Normal file
67
node_modules/@discordjs/ws/node_modules/@discordjs/collection/README.md
generated
vendored
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
<div align="center">
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.js.org"><img src="https://discord.js.org/static/logo.svg" width="546" alt="discord.js" /></a>
|
||||||
|
</p>
|
||||||
|
<br />
|
||||||
|
<p>
|
||||||
|
<a href="https://discord.gg/djs"><img src="https://img.shields.io/discord/222078108977594368?color=5865F2&logo=discord&logoColor=white" alt="Discord server" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/v/@discordjs/collection.svg?maxAge=3600" alt="npm version" /></a>
|
||||||
|
<a href="https://www.npmjs.com/package/@discordjs/collection"><img src="https://img.shields.io/npm/dt/@discordjs/collection.svg?maxAge=3600" alt="npm downloads" /></a>
|
||||||
|
<a href="https://github.com/discordjs/discord.js/actions"><img src="https://github.com/discordjs/discord.js/actions/workflows/test.yml/badge.svg" alt="Build status" /></a>
|
||||||
|
<a href="https://codecov.io/gh/discordjs/discord.js" ><img src="https://codecov.io/gh/discordjs/discord.js/branch/main/graph/badge.svg?precision=2&flag=collection" alt="Code coverage" /></a>
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
<a href="https://vercel.com/?utm_source=discordjs&utm_campaign=oss"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-vercel.svg" alt="Vercel" /></a>
|
||||||
|
<a href="https://www.cloudflare.com"><img src="https://raw.githubusercontent.com/discordjs/discord.js/main/.github/powered-by-workers.png" alt="Cloudflare Workers" height="44" /></a>
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
## About
|
||||||
|
|
||||||
|
`@discordjs/collection` is a powerful utility data structure used in discord.js.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
**Node.js 18 or newer is required.**
|
||||||
|
|
||||||
|
```sh
|
||||||
|
npm install @discordjs/collection
|
||||||
|
yarn add @discordjs/collection
|
||||||
|
pnpm add @discordjs/collection
|
||||||
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
- [Website][website] ([source][website-source])
|
||||||
|
- [Documentation][documentation]
|
||||||
|
- [Guide][guide] ([source][guide-source])
|
||||||
|
Also see the v13 to v14 [Update Guide][guide-update], which includes updated and removed items from the library.
|
||||||
|
- [discord.js Discord server][discord]
|
||||||
|
- [Discord API Discord server][discord-api]
|
||||||
|
- [GitHub][source]
|
||||||
|
- [npm][npm]
|
||||||
|
- [Related libraries][related-libs]
|
||||||
|
|
||||||
|
## Contributing
|
||||||
|
|
||||||
|
Before creating an issue, please ensure that it hasn't already been reported/suggested, and double-check the
|
||||||
|
[documentation][documentation].
|
||||||
|
See [the contribution guide][contributing] if you'd like to submit a PR.
|
||||||
|
|
||||||
|
## Help
|
||||||
|
|
||||||
|
If you don't understand something in the documentation, you are experiencing problems, or you just need a gentle nudge in the right direction, please don't hesitate to join our official [discord.js Server][discord].
|
||||||
|
|
||||||
|
[website]: https://discord.js.org
|
||||||
|
[website-source]: https://github.com/discordjs/discord.js/tree/main/apps/website
|
||||||
|
[documentation]: https://discord.js.org/docs/packages/collection/stable
|
||||||
|
[guide]: https://discordjs.guide/
|
||||||
|
[guide-source]: https://github.com/discordjs/guide
|
||||||
|
[guide-update]: https://discordjs.guide/additional-info/changes-in-v14.html
|
||||||
|
[discord]: https://discord.gg/djs
|
||||||
|
[discord-api]: https://discord.gg/discord-api
|
||||||
|
[source]: https://github.com/discordjs/discord.js/tree/main/packages/collection
|
||||||
|
[npm]: https://www.npmjs.com/package/@discordjs/collection
|
||||||
|
[related-libs]: https://discord.com/developers/docs/topics/community-resources#libraries
|
||||||
|
[contributing]: https://github.com/discordjs/discord.js/blob/main/.github/CONTRIBUTING.md
|
535
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.d.mts
generated
vendored
Normal file
535
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.d.mts
generated
vendored
Normal file
|
@ -0,0 +1,535 @@
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface CollectionConstructor {
|
||||||
|
new (): Collection<unknown, unknown>;
|
||||||
|
new <Key, Value>(entries?: readonly (readonly [Key, Value])[] | null): Collection<Key, Value>;
|
||||||
|
new <Key, Value>(iterable: Iterable<readonly [Key, Value]>): Collection<Key, Value>;
|
||||||
|
readonly prototype: Collection<unknown, unknown>;
|
||||||
|
readonly [Symbol.species]: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an immutable version of a collection
|
||||||
|
*/
|
||||||
|
type ReadonlyCollection<Key, Value> = Omit<Collection<Key, Value>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
constructor: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||||
|
* an ID, for significantly improved performance and ease-of-use.
|
||||||
|
*
|
||||||
|
* @typeParam Key - The key type this collection holds
|
||||||
|
* @typeParam Value - The value type this collection holds
|
||||||
|
*/
|
||||||
|
declare class Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key: Key, defaultValueGenerator: (key: Key, collection: this) => Value): Value;
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Obtains the first value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the beginning
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||||
|
*/
|
||||||
|
first(): Value | undefined;
|
||||||
|
first(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the first key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the beginning
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
firstKey(): Key | undefined;
|
||||||
|
firstKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Obtains the last value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the end
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
last(): Value | undefined;
|
||||||
|
last(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the last key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the end
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
lastKey(): Key | undefined;
|
||||||
|
lastKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index: number): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index: number): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Obtains unique random value(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain randomly
|
||||||
|
* @returns A single value if no amount is provided or an array of values
|
||||||
|
*/
|
||||||
|
random(): Value | undefined;
|
||||||
|
random(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains unique random key(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain randomly
|
||||||
|
* @returns A single key if no amount is provided or an array
|
||||||
|
*/
|
||||||
|
randomKey(): Key | undefined;
|
||||||
|
randomKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse(): this;
|
||||||
|
/**
|
||||||
|
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
|
||||||
|
* All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||||
|
* should use the `get` method. See
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.find(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
find<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
find(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
find<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
find<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.findKey(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
findKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast | Array.findLast()}.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLast<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
findLast(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
findLast<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
findLast<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex | Array.findLastIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLastKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findLastKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findLastKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findLastKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Removes items that satisfy the provided filter function.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @returns The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn: (value: Value, key: Key, collection: this) => unknown): number;
|
||||||
|
sweep<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): number;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.filter(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): Collection<NewKey, Value>;
|
||||||
|
filter<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): Collection<Key, NewValue>;
|
||||||
|
filter(fn: (value: Value, key: Key, collection: this) => unknown): Collection<Key, Value>;
|
||||||
|
filter<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): Collection<NewKey, Value>;
|
||||||
|
filter<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
filter<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
partition<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition(fn: (value: Value, key: Key, collection: this) => unknown): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
partition<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
/**
|
||||||
|
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces a new Collection
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.flatMap(guild => guild.members.cache);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
flatMap<NewValue>(fn: (value: Value, key: Key, collection: this) => Collection<Key, NewValue>): Collection<Key, NewValue>;
|
||||||
|
flatMap<NewValue, This>(fn: (this: This, value: Value, key: Key, collection: this) => Collection<Key, NewValue>, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into an array. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new array, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.map(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
map<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): NewValue[];
|
||||||
|
map<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): NewValue[];
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into a collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new collection, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.mapValues(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
mapValues<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): Collection<Key, NewValue>;
|
||||||
|
mapValues<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.some(user => user.discriminator === '0000');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
some(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
some<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if all items passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.every(user => !user.bot);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
every<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): this is Collection<NewKey, Value>;
|
||||||
|
every<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): this is Collection<Key, NewValue>;
|
||||||
|
every(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
every<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): this is Collection<NewKey, Value>;
|
||||||
|
every<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): this is Collection<Key, NewValue>;
|
||||||
|
every<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce<InitialValue = Value>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight<InitialValue>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
|
||||||
|
* but returns the collection instead of undefined.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute for each element
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .each(user => console.log(user.username))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .each(user => console.log(user.username));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
each(fn: (value: Value, key: Key, collection: this) => void): this;
|
||||||
|
each<This>(fn: (this: This, value: Value, key: Key, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Runs a function on the collection and returns the collection.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
tap(fn: (collection: this) => void): this;
|
||||||
|
tap<This>(fn: (this: This, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections: ReadonlyCollection<Key, Value>[]): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection: ReadonlyCollection<Key, Value>): boolean;
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction?: Comparator<Key, Value>): this;
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge<OtherValue, ResultValue>(other: ReadonlyCollection<Key, OtherValue>, whenInSelf: (value: Value, key: Key) => Keep<ResultValue>, whenInOther: (valueOther: OtherValue, key: Key) => Keep<ResultValue>, whenInBoth: (value: Value, valueOther: OtherValue, key: Key) => Keep<ResultValue>): Collection<Key, ResultValue>;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction?: Comparator<Key, Value>): Collection<Key, Value>;
|
||||||
|
toJSON(): [Key, Value][];
|
||||||
|
private static defaultSort;
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries<Key, Value>(entries: Iterable<[Key, Value]>, combine: (firstValue: Value, secondValue: Value, key: Key) => Value): Collection<Key, Value>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Keep<Value> = {
|
||||||
|
keep: false;
|
||||||
|
} | {
|
||||||
|
keep: true;
|
||||||
|
value: Value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Comparator<Key, Value> = (firstValue: Value, secondValue: Value, firstKey: Key, secondKey: Key) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
|
535
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.d.ts
generated
vendored
Normal file
535
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.d.ts
generated
vendored
Normal file
|
@ -0,0 +1,535 @@
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface CollectionConstructor {
|
||||||
|
new (): Collection<unknown, unknown>;
|
||||||
|
new <Key, Value>(entries?: readonly (readonly [Key, Value])[] | null): Collection<Key, Value>;
|
||||||
|
new <Key, Value>(iterable: Iterable<readonly [Key, Value]>): Collection<Key, Value>;
|
||||||
|
readonly prototype: Collection<unknown, unknown>;
|
||||||
|
readonly [Symbol.species]: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Represents an immutable version of a collection
|
||||||
|
*/
|
||||||
|
type ReadonlyCollection<Key, Value> = Omit<Collection<Key, Value>, 'delete' | 'ensure' | 'forEach' | 'get' | 'reverse' | 'set' | 'sort' | 'sweep'> & ReadonlyMap<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
interface Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
constructor: CollectionConstructor;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* A Map with additional utility methods. This is used throughout discord.js rather than Arrays for anything that has
|
||||||
|
* an ID, for significantly improved performance and ease-of-use.
|
||||||
|
*
|
||||||
|
* @typeParam Key - The key type this collection holds
|
||||||
|
* @typeParam Value - The value type this collection holds
|
||||||
|
*/
|
||||||
|
declare class Collection<Key, Value> extends Map<Key, Value> {
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key: Key, defaultValueGenerator: (key: Key, collection: this) => Value): Value;
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys: Key[]): boolean;
|
||||||
|
/**
|
||||||
|
* Obtains the first value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the beginning
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the end if amount is negative
|
||||||
|
*/
|
||||||
|
first(): Value | undefined;
|
||||||
|
first(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the first key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the beginning
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the end if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
firstKey(): Key | undefined;
|
||||||
|
firstKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Obtains the last value(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain from the end
|
||||||
|
* @returns A single value if no amount is provided or an array of values, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
last(): Value | undefined;
|
||||||
|
last(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains the last key(s) in this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain from the end
|
||||||
|
* @returns A single key if no amount is provided or an array of keys, starting from the start if
|
||||||
|
* amount is negative
|
||||||
|
*/
|
||||||
|
lastKey(): Key | undefined;
|
||||||
|
lastKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index: number): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index: number): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Obtains unique random value(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of values to obtain randomly
|
||||||
|
* @returns A single value if no amount is provided or an array of values
|
||||||
|
*/
|
||||||
|
random(): Value | undefined;
|
||||||
|
random(amount: number): Value[];
|
||||||
|
/**
|
||||||
|
* Obtains unique random key(s) from this collection.
|
||||||
|
*
|
||||||
|
* @param amount - Amount of keys to obtain randomly
|
||||||
|
* @returns A single key if no amount is provided or an array
|
||||||
|
*/
|
||||||
|
randomKey(): Key | undefined;
|
||||||
|
randomKey(amount: number): Key[];
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse(): this;
|
||||||
|
/**
|
||||||
|
* Searches for a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find | Array.find()}.
|
||||||
|
* All collections used in Discord.js are mapped using their `id` property, and if you want to find by id you
|
||||||
|
* should use the `get` method. See
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get | MDN} for details.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.find(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
find<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
find(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
find<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
find<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a single item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex | Array.findIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.findKey(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
findKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLast | Array.findLast()}.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLast<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): NewValue | undefined;
|
||||||
|
findLast(fn: (value: Value, key: Key, collection: this) => unknown): Value | undefined;
|
||||||
|
findLast<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): NewValue | undefined;
|
||||||
|
findLast<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Value | undefined;
|
||||||
|
/**
|
||||||
|
* Searches for the key of a last item where the given function returns a truthy value. This behaves like
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findLastIndex | Array.findLastIndex()},
|
||||||
|
* but returns the key rather than the positional index.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
*/
|
||||||
|
findLastKey<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): NewKey | undefined;
|
||||||
|
findLastKey(fn: (value: Value, key: Key, collection: this) => unknown): Key | undefined;
|
||||||
|
findLastKey<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): NewKey | undefined;
|
||||||
|
findLastKey<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Key | undefined;
|
||||||
|
/**
|
||||||
|
* Removes items that satisfy the provided filter function.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @returns The number of removed entries
|
||||||
|
*/
|
||||||
|
sweep(fn: (value: Value, key: Key, collection: this) => unknown): number;
|
||||||
|
sweep<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): number;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter | Array.filter()},
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*
|
||||||
|
* @param fn - The function to test with (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.filter(user => user.username === 'Bob');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
filter<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): Collection<NewKey, Value>;
|
||||||
|
filter<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): Collection<Key, NewValue>;
|
||||||
|
filter(fn: (value: Value, key: Key, collection: this) => unknown): Collection<Key, Value>;
|
||||||
|
filter<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): Collection<NewKey, Value>;
|
||||||
|
filter<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
filter<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Partitions the collection into two collections where the first collection
|
||||||
|
* contains the items that passed and the second contains the items that failed.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const [big, small] = collection.partition(guild => guild.memberCount > 250);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
partition<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition(fn: (value: Value, key: Key, collection: this) => unknown): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
partition<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): [Collection<NewKey, Value>, Collection<Exclude<Key, NewKey>, Value>];
|
||||||
|
partition<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): [Collection<Key, NewValue>, Collection<Key, Exclude<Value, NewValue>>];
|
||||||
|
partition<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): [Collection<Key, Value>, Collection<Key, Value>];
|
||||||
|
/**
|
||||||
|
* Maps each item into a Collection, then joins the results into a single Collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap | Array.flatMap()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces a new Collection
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.flatMap(guild => guild.members.cache);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
flatMap<NewValue>(fn: (value: Value, key: Key, collection: this) => Collection<Key, NewValue>): Collection<Key, NewValue>;
|
||||||
|
flatMap<NewValue, This>(fn: (this: This, value: Value, key: Key, collection: this) => Collection<Key, NewValue>, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into an array. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new array, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.map(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
map<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): NewValue[];
|
||||||
|
map<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): NewValue[];
|
||||||
|
/**
|
||||||
|
* Maps each item to another value into a collection. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map | Array.map()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function that produces an element of the new collection, taking three arguments
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.mapValues(user => user.tag);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
mapValues<NewValue>(fn: (value: Value, key: Key, collection: this) => NewValue): Collection<Key, NewValue>;
|
||||||
|
mapValues<This, NewValue>(fn: (this: This, value: Value, key: Key, collection: this) => NewValue, thisArg: This): Collection<Key, NewValue>;
|
||||||
|
/**
|
||||||
|
* Checks if there exists an item that passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some | Array.some()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.some(user => user.discriminator === '0000');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
some(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
some<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Checks if all items passes a test. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every | Array.every()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to test (should return a boolean)
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.every(user => !user.bot);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
every<NewKey extends Key>(fn: (value: Value, key: Key, collection: this) => key is NewKey): this is Collection<NewKey, Value>;
|
||||||
|
every<NewValue extends Value>(fn: (value: Value, key: Key, collection: this) => value is NewValue): this is Collection<Key, NewValue>;
|
||||||
|
every(fn: (value: Value, key: Key, collection: this) => unknown): boolean;
|
||||||
|
every<This, NewKey extends Key>(fn: (this: This, value: Value, key: Key, collection: this) => key is NewKey, thisArg: This): this is Collection<NewKey, Value>;
|
||||||
|
every<This, NewValue extends Value>(fn: (this: This, value: Value, key: Key, collection: this) => value is NewValue, thisArg: This): this is Collection<Key, NewValue>;
|
||||||
|
every<This>(fn: (this: This, value: Value, key: Key, collection: this) => unknown, thisArg: This): boolean;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce<InitialValue = Value>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight<InitialValue>(fn: (accumulator: InitialValue, value: Value, key: Key, collection: this) => InitialValue, initialValue?: InitialValue): InitialValue;
|
||||||
|
/**
|
||||||
|
* Identical to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach | Map.forEach()},
|
||||||
|
* but returns the collection instead of undefined.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute for each element
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .each(user => console.log(user.username))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .each(user => console.log(user.username));
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
each(fn: (value: Value, key: Key, collection: this) => void): this;
|
||||||
|
each<This>(fn: (this: This, value: Value, key: Key, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Runs a function on the collection and returns the collection.
|
||||||
|
*
|
||||||
|
* @param fn - Function to execute
|
||||||
|
* @param thisArg - Value to use as `this` when executing the function
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* .filter(user => user.bot)
|
||||||
|
* .tap(coll => console.log(coll.size))
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
tap(fn: (collection: this) => void): this;
|
||||||
|
tap<This>(fn: (this: This, collection: this) => void, thisArg: This): this;
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections: ReadonlyCollection<Key, Value>[]): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection: ReadonlyCollection<Key, Value>): boolean;
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction?: Comparator<Key, Value>): this;
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other: ReadonlyCollection<Key, any>): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference<OtherValue>(other: ReadonlyCollection<Key, OtherValue>): Collection<Key, OtherValue | Value>;
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge<OtherValue, ResultValue>(other: ReadonlyCollection<Key, OtherValue>, whenInSelf: (value: Value, key: Key) => Keep<ResultValue>, whenInOther: (valueOther: OtherValue, key: Key) => Keep<ResultValue>, whenInBoth: (value: Value, valueOther: OtherValue, key: Key) => Keep<ResultValue>): Collection<Key, ResultValue>;
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed(): Collection<Key, Value>;
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction?: Comparator<Key, Value>): Collection<Key, Value>;
|
||||||
|
toJSON(): [Key, Value][];
|
||||||
|
private static defaultSort;
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries<Key, Value>(entries: Iterable<[Key, Value]>, combine: (firstValue: Value, secondValue: Value, key: Key) => Value): Collection<Key, Value>;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Keep<Value> = {
|
||||||
|
keep: false;
|
||||||
|
} | {
|
||||||
|
keep: true;
|
||||||
|
value: Value;
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
type Comparator<Key, Value> = (firstValue: Value, secondValue: Value, firstKey: Key, secondKey: Key) => number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link https://github.com/discordjs/discord.js/blob/main/packages/collection/#readme | @discordjs/collection} version
|
||||||
|
* that you are currently using.
|
||||||
|
*/
|
||||||
|
declare const version: string;
|
||||||
|
|
||||||
|
export { Collection, CollectionConstructor, Comparator, Keep, ReadonlyCollection, version };
|
654
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.js
generated
vendored
Normal file
654
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.js
generated
vendored
Normal file
|
@ -0,0 +1,654 @@
|
||||||
|
"use strict";
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
||||||
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
||||||
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
var __export = (target, all) => {
|
||||||
|
for (var name in all)
|
||||||
|
__defProp(target, name, { get: all[name], enumerable: true });
|
||||||
|
};
|
||||||
|
var __copyProps = (to, from, except, desc) => {
|
||||||
|
if (from && typeof from === "object" || typeof from === "function") {
|
||||||
|
for (let key of __getOwnPropNames(from))
|
||||||
|
if (!__hasOwnProp.call(to, key) && key !== except)
|
||||||
|
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
||||||
|
}
|
||||||
|
return to;
|
||||||
|
};
|
||||||
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var src_exports = {};
|
||||||
|
__export(src_exports, {
|
||||||
|
Collection: () => Collection,
|
||||||
|
version: () => version
|
||||||
|
});
|
||||||
|
module.exports = __toCommonJS(src_exports);
|
||||||
|
|
||||||
|
// src/collection.ts
|
||||||
|
var Collection = class _Collection extends Map {
|
||||||
|
static {
|
||||||
|
__name(this, "Collection");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key, defaultValueGenerator) {
|
||||||
|
if (this.has(key))
|
||||||
|
return this.get(key);
|
||||||
|
if (typeof defaultValueGenerator !== "function")
|
||||||
|
throw new TypeError(`${defaultValueGenerator} is not a function`);
|
||||||
|
const defaultValue = defaultValueGenerator(key, this);
|
||||||
|
this.set(key, defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys) {
|
||||||
|
return keys.every((key) => super.has(key));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys) {
|
||||||
|
return keys.some((key) => super.has(key));
|
||||||
|
}
|
||||||
|
first(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.values().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.last(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.values();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
firstKey(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.keys().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.lastKey(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.keys();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
last(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.first(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
lastKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.firstKey(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.values()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
random(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
randomKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse() {
|
||||||
|
const entries = [...this.entries()].reverse();
|
||||||
|
this.clear();
|
||||||
|
for (const [key, value] of entries)
|
||||||
|
this.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
find(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLast(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const val = entries[index][1];
|
||||||
|
const key = entries[index][0];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLastKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
filter(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
results.set(key, val);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = [
|
||||||
|
new this.constructor[Symbol.species](),
|
||||||
|
new this.constructor[Symbol.species]()
|
||||||
|
];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
flatMap(fn, thisArg) {
|
||||||
|
const collections = this.map(fn, thisArg);
|
||||||
|
return new this.constructor[Symbol.species]().concat(...collections);
|
||||||
|
}
|
||||||
|
map(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const iter = this.entries();
|
||||||
|
return Array.from({ length: this.size }, () => {
|
||||||
|
const [key, value] = iter.next().value;
|
||||||
|
return fn(value, key, this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mapValues(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this)
|
||||||
|
coll.set(key, fn(val, key, this));
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
some(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
every(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (!fn(val, key, this))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
let accumulator;
|
||||||
|
const iterator = this.entries();
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (this.size === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
}
|
||||||
|
for (const [key, value] of iterator) {
|
||||||
|
accumulator = fn(accumulator, value, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
let accumulator;
|
||||||
|
let index;
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (entries.length === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = entries[entries.length - 1][1];
|
||||||
|
index = entries.length - 1;
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
index = entries.length;
|
||||||
|
}
|
||||||
|
while (--index >= 0) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
accumulator = fn(accumulator, val, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
each(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
fn(value, key, this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
tap(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
fn(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone() {
|
||||||
|
return new this.constructor[Symbol.species](this);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections) {
|
||||||
|
const newColl = this.clone();
|
||||||
|
for (const coll of collections) {
|
||||||
|
for (const [key, val] of coll)
|
||||||
|
newColl.set(key, val);
|
||||||
|
}
|
||||||
|
return newColl;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection) {
|
||||||
|
if (!collection)
|
||||||
|
return false;
|
||||||
|
if (this === collection)
|
||||||
|
return true;
|
||||||
|
if (this.size !== collection.size)
|
||||||
|
return false;
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!collection.has(key) || value !== collection.get(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction = _Collection.defaultSort) {
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
||||||
|
super.clear();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
super.set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species](this);
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!coll.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!this.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge(other, whenInSelf, whenInOther, whenInBoth) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
|
||||||
|
for (const key of keys) {
|
||||||
|
const hasInSelf = this.has(key);
|
||||||
|
const hasInOther = other.has(key);
|
||||||
|
if (hasInSelf && hasInOther) {
|
||||||
|
const result = whenInBoth(this.get(key), other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInSelf) {
|
||||||
|
const result = whenInSelf(this.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInOther) {
|
||||||
|
const result = whenInOther(other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed() {
|
||||||
|
return new this.constructor[Symbol.species](this).reverse();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction = _Collection.defaultSort) {
|
||||||
|
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||||
|
}
|
||||||
|
toJSON() {
|
||||||
|
return [...this.entries()];
|
||||||
|
}
|
||||||
|
static defaultSort(firstValue, secondValue) {
|
||||||
|
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries(entries, combine) {
|
||||||
|
const coll = new _Collection();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
if (coll.has(key)) {
|
||||||
|
coll.set(key, combine(coll.get(key), value, key));
|
||||||
|
} else {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var version = "2.0.0";
|
||||||
|
// Annotate the CommonJS export names for ESM import in node:
|
||||||
|
0 && (module.exports = {
|
||||||
|
Collection,
|
||||||
|
version
|
||||||
|
});
|
||||||
|
//# sourceMappingURL=index.js.map
|
1
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.js.map
generated
vendored
Normal file
1
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
628
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.mjs
generated
vendored
Normal file
628
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.mjs
generated
vendored
Normal file
|
@ -0,0 +1,628 @@
|
||||||
|
var __defProp = Object.defineProperty;
|
||||||
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
||||||
|
|
||||||
|
// src/collection.ts
|
||||||
|
var Collection = class _Collection extends Map {
|
||||||
|
static {
|
||||||
|
__name(this, "Collection");
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Obtains the value of the given key if it exists, otherwise sets and returns the value provided by the default value generator.
|
||||||
|
*
|
||||||
|
* @param key - The key to get if it exists, or set otherwise
|
||||||
|
* @param defaultValueGenerator - A function that generates the default value
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.ensure(guildId, () => defaultGuildConfig);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
ensure(key, defaultValueGenerator) {
|
||||||
|
if (this.has(key))
|
||||||
|
return this.get(key);
|
||||||
|
if (typeof defaultValueGenerator !== "function")
|
||||||
|
throw new TypeError(`${defaultValueGenerator} is not a function`);
|
||||||
|
const defaultValue = defaultValueGenerator(key, this);
|
||||||
|
this.set(key, defaultValue);
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if all of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if all of the elements exist, `false` if at least one does not exist.
|
||||||
|
*/
|
||||||
|
hasAll(...keys) {
|
||||||
|
return keys.every((key) => super.has(key));
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if any of the elements exist in the collection.
|
||||||
|
*
|
||||||
|
* @param keys - The keys of the elements to check for
|
||||||
|
* @returns `true` if any of the elements exist, `false` if none exist.
|
||||||
|
*/
|
||||||
|
hasAny(...keys) {
|
||||||
|
return keys.some((key) => super.has(key));
|
||||||
|
}
|
||||||
|
first(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.values().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.last(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.values();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
firstKey(amount) {
|
||||||
|
if (amount === void 0)
|
||||||
|
return this.keys().next().value;
|
||||||
|
if (amount < 0)
|
||||||
|
return this.lastKey(amount * -1);
|
||||||
|
amount = Math.min(this.size, amount);
|
||||||
|
const iter = this.keys();
|
||||||
|
return Array.from({ length: amount }, () => iter.next().value);
|
||||||
|
}
|
||||||
|
last(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.first(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
lastKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
if (amount < 0)
|
||||||
|
return this.firstKey(amount * -1);
|
||||||
|
if (!amount)
|
||||||
|
return [];
|
||||||
|
return arr.slice(-amount);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the item at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the element to obtain
|
||||||
|
*/
|
||||||
|
at(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.values()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/at | Array.at()}.
|
||||||
|
* Returns the key at a given index, allowing for positive and negative integers.
|
||||||
|
* Negative integers count back from the last item in the collection.
|
||||||
|
*
|
||||||
|
* @param index - The index of the key to obtain
|
||||||
|
*/
|
||||||
|
keyAt(index) {
|
||||||
|
index = Math.floor(index);
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
return arr.at(index);
|
||||||
|
}
|
||||||
|
random(amount) {
|
||||||
|
const arr = [...this.values()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
randomKey(amount) {
|
||||||
|
const arr = [...this.keys()];
|
||||||
|
if (amount === void 0)
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
if (!arr.length || !amount)
|
||||||
|
return [];
|
||||||
|
return Array.from(
|
||||||
|
{ length: Math.min(amount, arr.length) },
|
||||||
|
() => arr.splice(Math.floor(Math.random() * arr.length), 1)[0]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse | Array.reverse()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
reverse() {
|
||||||
|
const entries = [...this.entries()].reverse();
|
||||||
|
this.clear();
|
||||||
|
for (const [key, value] of entries)
|
||||||
|
this.set(key, value);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
find(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLast(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const val = entries[index][1];
|
||||||
|
const key = entries[index][0];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
findLastKey(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
for (let index = entries.length - 1; index >= 0; index--) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
return void 0;
|
||||||
|
}
|
||||||
|
sweep(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const previousSize = this.size;
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
this.delete(key);
|
||||||
|
}
|
||||||
|
return previousSize - this.size;
|
||||||
|
}
|
||||||
|
filter(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
results.set(key, val);
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
partition(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const results = [
|
||||||
|
new this.constructor[Symbol.species](),
|
||||||
|
new this.constructor[Symbol.species]()
|
||||||
|
];
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this)) {
|
||||||
|
results[0].set(key, val);
|
||||||
|
} else {
|
||||||
|
results[1].set(key, val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
flatMap(fn, thisArg) {
|
||||||
|
const collections = this.map(fn, thisArg);
|
||||||
|
return new this.constructor[Symbol.species]().concat(...collections);
|
||||||
|
}
|
||||||
|
map(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const iter = this.entries();
|
||||||
|
return Array.from({ length: this.size }, () => {
|
||||||
|
const [key, value] = iter.next().value;
|
||||||
|
return fn(value, key, this);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
mapValues(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, val] of this)
|
||||||
|
coll.set(key, fn(val, key, this));
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
some(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (fn(val, key, this))
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
every(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, val] of this) {
|
||||||
|
if (!fn(val, key, this))
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce | Array.reduce()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
|
||||||
|
* and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
reduce(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
let accumulator;
|
||||||
|
const iterator = this.entries();
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (this.size === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = iterator.next().value[1];
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
}
|
||||||
|
for (const [key, value] of iterator) {
|
||||||
|
accumulator = fn(accumulator, value, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Applies a function to produce a single value. Identical in behavior to
|
||||||
|
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight | Array.reduceRight()}.
|
||||||
|
*
|
||||||
|
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `value`, `key`, and `collection`
|
||||||
|
* @param initialValue - Starting value for the accumulator
|
||||||
|
*/
|
||||||
|
reduceRight(fn, initialValue) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
let accumulator;
|
||||||
|
let index;
|
||||||
|
if (initialValue === void 0) {
|
||||||
|
if (entries.length === 0)
|
||||||
|
throw new TypeError("Reduce of empty collection with no initial value");
|
||||||
|
accumulator = entries[entries.length - 1][1];
|
||||||
|
index = entries.length - 1;
|
||||||
|
} else {
|
||||||
|
accumulator = initialValue;
|
||||||
|
index = entries.length;
|
||||||
|
}
|
||||||
|
while (--index >= 0) {
|
||||||
|
const key = entries[index][0];
|
||||||
|
const val = entries[index][1];
|
||||||
|
accumulator = fn(accumulator, val, key, this);
|
||||||
|
}
|
||||||
|
return accumulator;
|
||||||
|
}
|
||||||
|
each(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
fn(value, key, this);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
tap(fn, thisArg) {
|
||||||
|
if (typeof fn !== "function")
|
||||||
|
throw new TypeError(`${fn} is not a function`);
|
||||||
|
if (thisArg !== void 0)
|
||||||
|
fn = fn.bind(thisArg);
|
||||||
|
fn(this);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates an identical shallow copy of this collection.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.clone();
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
clone() {
|
||||||
|
return new this.constructor[Symbol.species](this);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Combines this collection with others into a new collection. None of the source collections are modified.
|
||||||
|
*
|
||||||
|
* @param collections - Collections to merge
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
concat(...collections) {
|
||||||
|
const newColl = this.clone();
|
||||||
|
for (const coll of collections) {
|
||||||
|
for (const [key, val] of coll)
|
||||||
|
newColl.set(key, val);
|
||||||
|
}
|
||||||
|
return newColl;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Checks if this collection shares identical items with another.
|
||||||
|
* This is different to checking for equality using equal-signs, because
|
||||||
|
* the collections may be different objects, but contain the same data.
|
||||||
|
*
|
||||||
|
* @param collection - Collection to compare with
|
||||||
|
* @returns Whether the collections have identical contents
|
||||||
|
*/
|
||||||
|
equals(collection) {
|
||||||
|
if (!collection)
|
||||||
|
return false;
|
||||||
|
if (this === collection)
|
||||||
|
return true;
|
||||||
|
if (this.size !== collection.size)
|
||||||
|
return false;
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!collection.has(key) || value !== collection.get(key)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sort method sorts the items of a collection in place and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value, according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sort((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
sort(compareFunction = _Collection.defaultSort) {
|
||||||
|
const entries = [...this.entries()];
|
||||||
|
entries.sort((a, b) => compareFunction(a[1], b[1], a[0], b[0]));
|
||||||
|
super.clear();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
super.set(key, value);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The intersection method returns a new collection containing the items where the key is present in both collections.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const intersection = col1.intersection(col2);
|
||||||
|
* console.log(col1.intersection(col2));
|
||||||
|
* // => Collection { 'a' => 1 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
intersection(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in either of the collections.
|
||||||
|
*
|
||||||
|
* @remarks
|
||||||
|
*
|
||||||
|
* If the collections have any items with the same key, the value from the first collection will be used.
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['b', 3], ['c', 3]]);
|
||||||
|
* const union = col1.union(col2);
|
||||||
|
* console.log(union);
|
||||||
|
* // => Collection { 'a' => 1, 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
union(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species](this);
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!coll.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing the items where the key is present in this collection but not the other.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* console.log(col1.difference(col2));
|
||||||
|
* // => Collection { 'b' => 2 }
|
||||||
|
* console.log(col2.difference(col1));
|
||||||
|
* // => Collection { 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
difference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Returns a new collection containing only the items where the keys are present in either collection, but not both.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to filter against
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* const col1 = new Collection([['a', 1], ['b', 2]]);
|
||||||
|
* const col2 = new Collection([['a', 1], ['c', 3]]);
|
||||||
|
* const symmetricDifference = col1.symmetricDifference(col2);
|
||||||
|
* console.log(col1.symmetricDifference(col2));
|
||||||
|
* // => Collection { 'b' => 2, 'c' => 3 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
symmetricDifference(other) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
for (const [key, value] of this) {
|
||||||
|
if (!other.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
for (const [key, value] of other) {
|
||||||
|
if (!this.has(key))
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Merges two Collections together into a new Collection.
|
||||||
|
*
|
||||||
|
* @param other - The other Collection to merge with
|
||||||
|
* @param whenInSelf - Function getting the result if the entry only exists in this Collection
|
||||||
|
* @param whenInOther - Function getting the result if the entry only exists in the other Collection
|
||||||
|
* @param whenInBoth - Function getting the result if the entry exists in both Collections
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Sums up the entries in two collections.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: true, value: x }),
|
||||||
|
* y => ({ keep: true, value: y }),
|
||||||
|
* (x, y) => ({ keep: true, value: x + y }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* // Intersects two collections in a left-biased manner.
|
||||||
|
* coll.merge(
|
||||||
|
* other,
|
||||||
|
* x => ({ keep: false }),
|
||||||
|
* y => ({ keep: false }),
|
||||||
|
* (x, _) => ({ keep: true, value: x }),
|
||||||
|
* );
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
merge(other, whenInSelf, whenInOther, whenInBoth) {
|
||||||
|
const coll = new this.constructor[Symbol.species]();
|
||||||
|
const keys = /* @__PURE__ */ new Set([...this.keys(), ...other.keys()]);
|
||||||
|
for (const key of keys) {
|
||||||
|
const hasInSelf = this.has(key);
|
||||||
|
const hasInOther = other.has(key);
|
||||||
|
if (hasInSelf && hasInOther) {
|
||||||
|
const result = whenInBoth(this.get(key), other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInSelf) {
|
||||||
|
const result = whenInSelf(this.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
} else if (hasInOther) {
|
||||||
|
const result = whenInOther(other.get(key), key);
|
||||||
|
if (result.keep)
|
||||||
|
coll.set(key, result.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toReversed | Array.toReversed()}
|
||||||
|
* but returns a Collection instead of an Array.
|
||||||
|
*/
|
||||||
|
toReversed() {
|
||||||
|
return new this.constructor[Symbol.species](this).reverse();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* The sorted method sorts the items of a collection and returns it.
|
||||||
|
* The sort is not necessarily stable in Node 10 or older.
|
||||||
|
* The default sort order is according to string Unicode code points.
|
||||||
|
*
|
||||||
|
* @param compareFunction - Specifies a function that defines the sort order.
|
||||||
|
* If omitted, the collection is sorted according to each character's Unicode code point value,
|
||||||
|
* according to the string conversion of each element.
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* collection.sorted((userA, userB) => userA.createdTimestamp - userB.createdTimestamp);
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
toSorted(compareFunction = _Collection.defaultSort) {
|
||||||
|
return new this.constructor[Symbol.species](this).sort((av, bv, ak, bk) => compareFunction(av, bv, ak, bk));
|
||||||
|
}
|
||||||
|
toJSON() {
|
||||||
|
return [...this.entries()];
|
||||||
|
}
|
||||||
|
static defaultSort(firstValue, secondValue) {
|
||||||
|
return Number(firstValue > secondValue) || Number(firstValue === secondValue) - 1;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates a Collection from a list of entries.
|
||||||
|
*
|
||||||
|
* @param entries - The list of entries
|
||||||
|
* @param combine - Function to combine an existing entry with a new one
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* Collection.combineEntries([["a", 1], ["b", 2], ["a", 2]], (x, y) => x + y);
|
||||||
|
* // returns Collection { "a" => 3, "b" => 2 }
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static combineEntries(entries, combine) {
|
||||||
|
const coll = new _Collection();
|
||||||
|
for (const [key, value] of entries) {
|
||||||
|
if (coll.has(key)) {
|
||||||
|
coll.set(key, combine(coll.get(key), value, key));
|
||||||
|
} else {
|
||||||
|
coll.set(key, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return coll;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// src/index.ts
|
||||||
|
var version = "2.0.0";
|
||||||
|
export {
|
||||||
|
Collection,
|
||||||
|
version
|
||||||
|
};
|
||||||
|
//# sourceMappingURL=index.mjs.map
|
1
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.mjs.map
generated
vendored
Normal file
1
node_modules/@discordjs/ws/node_modules/@discordjs/collection/dist/index.mjs.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
83
node_modules/@discordjs/ws/node_modules/@discordjs/collection/package.json
generated
vendored
Normal file
83
node_modules/@discordjs/ws/node_modules/@discordjs/collection/package.json
generated
vendored
Normal file
|
@ -0,0 +1,83 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/collection",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"description": "Utility data structure used in discord.js",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"map",
|
||||||
|
"collection",
|
||||||
|
"utility"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/collection"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "18.18.8",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"esbuild-plugin-version-injector": "^1.2.1",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"prettier": "^3.0.3",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=18"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src __tests__",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src __tests__",
|
||||||
|
"fmt": "pnpm run format",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/collection/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,109 @@
|
||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/package.json",
|
||||||
|
"name": "@discordjs/ws",
|
||||||
|
"version": "1.0.2",
|
||||||
|
"description": "Wrapper around Discord's gateway",
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"require": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"default": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": "./dist/index.d.mts",
|
||||||
|
"default": "./dist/index.mjs"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"./defaultWorker": {
|
||||||
|
"require": {
|
||||||
|
"types": null,
|
||||||
|
"default": "./dist/defaultWorker.js"
|
||||||
|
},
|
||||||
|
"import": {
|
||||||
|
"types": null,
|
||||||
|
"default": "./dist/defaultWorker.mjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"module": "./dist/index.mjs",
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"directories": {
|
||||||
|
"lib": "src",
|
||||||
|
"test": "__tests__"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"contributors": [
|
||||||
|
"Crawl <icrawltogo@gmail.com>",
|
||||||
|
"Amish Shah <amishshah.2k@gmail.com>",
|
||||||
|
"SpaceEEC <spaceeec@yahoo.com>",
|
||||||
|
"Vlad Frangu <kingdgrizzle@gmail.com>",
|
||||||
|
"Aura Román <kyradiscord@gmail.com>",
|
||||||
|
"DD <didinele.dev@gmail.com>"
|
||||||
|
],
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"keywords": [
|
||||||
|
"discord",
|
||||||
|
"api",
|
||||||
|
"gateway",
|
||||||
|
"discordapp",
|
||||||
|
"discordjs"
|
||||||
|
],
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/discordjs/discord.js.git",
|
||||||
|
"directory": "packages/ws"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/discordjs/discord.js/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://discord.js.org",
|
||||||
|
"dependencies": {
|
||||||
|
"@sapphire/async-queue": "^1.5.0",
|
||||||
|
"@types/ws": "^8.5.9",
|
||||||
|
"@vladfrangu/async_event_emitter": "^2.2.2",
|
||||||
|
"discord-api-types": "0.37.61",
|
||||||
|
"tslib": "^2.6.2",
|
||||||
|
"ws": "^8.14.2",
|
||||||
|
"@discordjs/collection": "^2.0.0",
|
||||||
|
"@discordjs/rest": "^2.1.0",
|
||||||
|
"@discordjs/util": "^1.0.2"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@favware/cliff-jumper": "^2.2.1",
|
||||||
|
"@types/node": "18.17.9",
|
||||||
|
"@vitest/coverage-v8": "^0.34.6",
|
||||||
|
"cross-env": "^7.0.3",
|
||||||
|
"esbuild-plugin-version-injector": "^1.2.1",
|
||||||
|
"eslint": "^8.53.0",
|
||||||
|
"eslint-config-neon": "^0.1.57",
|
||||||
|
"eslint-formatter-pretty": "^5.0.0",
|
||||||
|
"mock-socket": "^9.3.1",
|
||||||
|
"prettier": "^3.0.3",
|
||||||
|
"tsup": "^7.2.0",
|
||||||
|
"turbo": "^1.10.17-canary.0",
|
||||||
|
"typescript": "^5.2.2",
|
||||||
|
"undici": "5.27.2",
|
||||||
|
"vitest": "^0.34.6",
|
||||||
|
"zlib-sync": "^0.1.9",
|
||||||
|
"@discordjs/api-extractor": "^7.38.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=16.11.0"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "vitest run",
|
||||||
|
"build": "tsc --noEmit && tsup",
|
||||||
|
"build:docs": "tsc -p tsconfig.docs.json",
|
||||||
|
"lint": "prettier --check . && cross-env TIMING=1 eslint --format=pretty src __tests__",
|
||||||
|
"format": "prettier --write . && cross-env TIMING=1 eslint --fix --format=pretty src __tests__",
|
||||||
|
"docs": "pnpm run build:docs && api-extractor run --local",
|
||||||
|
"changelog": "git cliff --prepend ./CHANGELOG.md -u -c ./cliff.toml -r ../../ --include-path 'packages/ws/*'",
|
||||||
|
"release": "cliff-jumper"
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright Brian White. All rights reserved.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to
|
||||||
|
deal in the Software without restriction, including without limitation the
|
||||||
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||||
|
sell copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
IN THE SOFTWARE.
|
|
@ -0,0 +1,271 @@
|
||||||
|
# busboy
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|
[![Build Status](https://github.com/fastify/busboy/workflows/ci/badge.svg)](https://github.com/fastify/busboy/actions)
|
||||||
|
[![Coverage Status](https://coveralls.io/repos/fastify/busboy/badge.svg?branch=master)](https://coveralls.io/r/fastify/busboy?branch=master)
|
||||||
|
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/)
|
||||||
|
[![Security Responsible Disclosure](https://img.shields.io/badge/Security-Responsible%20Disclosure-yellow.svg)](https://github.com/nodejs/security-wg/blob/HEAD/processes/responsible_disclosure_template.md)
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div align="center">
|
||||||
|
|
||||||
|
[![NPM version](https://img.shields.io/npm/v/@fastify/busboy.svg?style=flat)](https://www.npmjs.com/package/@fastify/busboy)
|
||||||
|
[![NPM downloads](https://img.shields.io/npm/dm/@fastify/busboy.svg?style=flat)](https://www.npmjs.com/package/@fastify/busboy)
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
Description
|
||||||
|
===========
|
||||||
|
|
||||||
|
A Node.js module for parsing incoming HTML form data.
|
||||||
|
|
||||||
|
This is an officially supported fork by [fastify](https://github.com/fastify/) organization of the amazing library [originally created](https://github.com/mscdex/busboy) by Brian White,
|
||||||
|
aimed at addressing long-standing issues with it.
|
||||||
|
|
||||||
|
Benchmark (Mean time for 500 Kb payload, 2000 cycles, 1000 cycle warmup):
|
||||||
|
|
||||||
|
| Library | Version | Mean time in nanoseconds (less is better) |
|
||||||
|
|-----------------------|---------|-------------------------------------------|
|
||||||
|
| busboy | 0.3.1 | `340114` |
|
||||||
|
| @fastify/busboy | 1.0.0 | `270984` |
|
||||||
|
|
||||||
|
[Changelog](https://github.com/fastify/busboy/blob/master/CHANGELOG.md) since busboy 0.31.
|
||||||
|
|
||||||
|
Requirements
|
||||||
|
============
|
||||||
|
|
||||||
|
* [Node.js](http://nodejs.org/) 10+
|
||||||
|
|
||||||
|
|
||||||
|
Install
|
||||||
|
=======
|
||||||
|
|
||||||
|
npm i @fastify/busboy
|
||||||
|
|
||||||
|
|
||||||
|
Examples
|
||||||
|
========
|
||||||
|
|
||||||
|
* Parsing (multipart) with default options:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const http = require('node:http');
|
||||||
|
const { inspect } = require('node:util');
|
||||||
|
const Busboy = require('busboy');
|
||||||
|
|
||||||
|
http.createServer((req, res) => {
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
const busboy = new Busboy({ headers: req.headers });
|
||||||
|
busboy.on('file', (fieldname, file, filename, encoding, mimetype) => {
|
||||||
|
console.log(`File [${fieldname}]: filename: ${filename}, encoding: ${encoding}, mimetype: ${mimetype}`);
|
||||||
|
file.on('data', data => {
|
||||||
|
console.log(`File [${fieldname}] got ${data.length} bytes`);
|
||||||
|
});
|
||||||
|
file.on('end', () => {
|
||||||
|
console.log(`File [${fieldname}] Finished`);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
busboy.on('field', (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) => {
|
||||||
|
console.log(`Field [${fieldname}]: value: ${inspect(val)}`);
|
||||||
|
});
|
||||||
|
busboy.on('finish', () => {
|
||||||
|
console.log('Done parsing form!');
|
||||||
|
res.writeHead(303, { Connection: 'close', Location: '/' });
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
req.pipe(busboy);
|
||||||
|
} else if (req.method === 'GET') {
|
||||||
|
res.writeHead(200, { Connection: 'close' });
|
||||||
|
res.end(`<html><head></head><body>
|
||||||
|
<form method="POST" enctype="multipart/form-data">
|
||||||
|
<input type="text" name="textfield"><br>
|
||||||
|
<input type="file" name="filefield"><br>
|
||||||
|
<input type="submit">
|
||||||
|
</form>
|
||||||
|
</body></html>`);
|
||||||
|
}
|
||||||
|
}).listen(8000, () => {
|
||||||
|
console.log('Listening for requests');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Example output, using http://nodejs.org/images/ryan-speaker.jpg as the file:
|
||||||
|
//
|
||||||
|
// Listening for requests
|
||||||
|
// File [filefield]: filename: ryan-speaker.jpg, encoding: binary
|
||||||
|
// File [filefield] got 11971 bytes
|
||||||
|
// Field [textfield]: value: 'testing! :-)'
|
||||||
|
// File [filefield] Finished
|
||||||
|
// Done parsing form!
|
||||||
|
```
|
||||||
|
|
||||||
|
* Save all incoming files to disk:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const http = require('node:http');
|
||||||
|
const path = require('node:path');
|
||||||
|
const os = require('node:os');
|
||||||
|
const fs = require('node:fs');
|
||||||
|
|
||||||
|
const Busboy = require('busboy');
|
||||||
|
|
||||||
|
http.createServer(function(req, res) {
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
const busboy = new Busboy({ headers: req.headers });
|
||||||
|
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
||||||
|
var saveTo = path.join(os.tmpdir(), path.basename(fieldname));
|
||||||
|
file.pipe(fs.createWriteStream(saveTo));
|
||||||
|
});
|
||||||
|
busboy.on('finish', function() {
|
||||||
|
res.writeHead(200, { 'Connection': 'close' });
|
||||||
|
res.end("That's all folks!");
|
||||||
|
});
|
||||||
|
return req.pipe(busboy);
|
||||||
|
}
|
||||||
|
res.writeHead(404);
|
||||||
|
res.end();
|
||||||
|
}).listen(8000, function() {
|
||||||
|
console.log('Listening for requests');
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
* Parsing (urlencoded) with default options:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const http = require('node:http');
|
||||||
|
const { inspect } = require('node:util');
|
||||||
|
|
||||||
|
const Busboy = require('busboy');
|
||||||
|
|
||||||
|
http.createServer(function(req, res) {
|
||||||
|
if (req.method === 'POST') {
|
||||||
|
const busboy = new Busboy({ headers: req.headers });
|
||||||
|
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
|
||||||
|
console.log('File [' + fieldname + ']: filename: ' + filename);
|
||||||
|
file.on('data', function(data) {
|
||||||
|
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
|
||||||
|
});
|
||||||
|
file.on('end', function() {
|
||||||
|
console.log('File [' + fieldname + '] Finished');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
busboy.on('field', function(fieldname, val, fieldnameTruncated, valTruncated) {
|
||||||
|
console.log('Field [' + fieldname + ']: value: ' + inspect(val));
|
||||||
|
});
|
||||||
|
busboy.on('finish', function() {
|
||||||
|
console.log('Done parsing form!');
|
||||||
|
res.writeHead(303, { Connection: 'close', Location: '/' });
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
req.pipe(busboy);
|
||||||
|
} else if (req.method === 'GET') {
|
||||||
|
res.writeHead(200, { Connection: 'close' });
|
||||||
|
res.end('<html><head></head><body>\
|
||||||
|
<form method="POST">\
|
||||||
|
<input type="text" name="textfield"><br />\
|
||||||
|
<select name="selectfield">\
|
||||||
|
<option value="1">1</option>\
|
||||||
|
<option value="10">10</option>\
|
||||||
|
<option value="100">100</option>\
|
||||||
|
<option value="9001">9001</option>\
|
||||||
|
</select><br />\
|
||||||
|
<input type="checkbox" name="checkfield">Node.js rules!<br />\
|
||||||
|
<input type="submit">\
|
||||||
|
</form>\
|
||||||
|
</body></html>');
|
||||||
|
}
|
||||||
|
}).listen(8000, function() {
|
||||||
|
console.log('Listening for requests');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Example output:
|
||||||
|
//
|
||||||
|
// Listening for requests
|
||||||
|
// Field [textfield]: value: 'testing! :-)'
|
||||||
|
// Field [selectfield]: value: '9001'
|
||||||
|
// Field [checkfield]: value: 'on'
|
||||||
|
// Done parsing form!
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
API
|
||||||
|
===
|
||||||
|
|
||||||
|
_Busboy_ is a _Writable_ stream
|
||||||
|
|
||||||
|
Busboy (special) events
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
* **file**(< _string_ >fieldname, < _ReadableStream_ >stream, < _string_ >filename, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new file form field found. `transferEncoding` contains the 'Content-Transfer-Encoding' value for the file stream. `mimeType` contains the 'Content-Type' value for the file stream.
|
||||||
|
* Note: if you listen for this event, you should always handle the `stream` no matter if you care about the file contents or not (e.g. you can simply just do `stream.resume();` if you want to discard the contents), otherwise the 'finish' event will never fire on the Busboy instance. However, if you don't care about **any** incoming files, you can simply not listen for the 'file' event at all and any/all files will be automatically and safely discarded (these discarded files do still count towards `files` and `parts` limits).
|
||||||
|
* If a configured file size limit was reached, `stream` will both have a boolean property `truncated` (best checked at the end of the stream) and emit a 'limit' event to notify you when this happens.
|
||||||
|
* The property `bytesRead` informs about the number of bytes that have been read so far.
|
||||||
|
|
||||||
|
* **field**(< _string_ >fieldname, < _string_ >value, < _boolean_ >fieldnameTruncated, < _boolean_ >valueTruncated, < _string_ >transferEncoding, < _string_ >mimeType) - Emitted for each new non-file field found.
|
||||||
|
|
||||||
|
* **partsLimit**() - Emitted when specified `parts` limit has been reached. No more 'file' or 'field' events will be emitted.
|
||||||
|
|
||||||
|
* **filesLimit**() - Emitted when specified `files` limit has been reached. No more 'file' events will be emitted.
|
||||||
|
|
||||||
|
* **fieldsLimit**() - Emitted when specified `fields` limit has been reached. No more 'field' events will be emitted.
|
||||||
|
|
||||||
|
|
||||||
|
Busboy methods
|
||||||
|
--------------
|
||||||
|
|
||||||
|
* **(constructor)**(< _object_ >config) - Creates and returns a new Busboy instance.
|
||||||
|
|
||||||
|
* The constructor takes the following valid `config` settings:
|
||||||
|
|
||||||
|
* **headers** - _object_ - These are the HTTP headers of the incoming request, which are used by individual parsers.
|
||||||
|
|
||||||
|
* **autoDestroy** - _boolean_ - Whether this stream should automatically call .destroy() on itself after ending. (Default: false).
|
||||||
|
|
||||||
|
* **highWaterMark** - _integer_ - highWaterMark to use for this Busboy instance (Default: WritableStream default).
|
||||||
|
|
||||||
|
* **fileHwm** - _integer_ - highWaterMark to use for file streams (Default: ReadableStream default).
|
||||||
|
|
||||||
|
* **defCharset** - _string_ - Default character set to use when one isn't defined (Default: 'utf8').
|
||||||
|
|
||||||
|
* **preservePath** - _boolean_ - If paths in the multipart 'filename' field shall be preserved. (Default: false).
|
||||||
|
|
||||||
|
* **isPartAFile** - __function__ - Use this function to override the default file detection functionality. It has following parameters:
|
||||||
|
|
||||||
|
* fieldName - __string__ The name of the field.
|
||||||
|
|
||||||
|
* contentType - __string__ The content-type of the part, e.g. `text/plain`, `image/jpeg`, `application/octet-stream`
|
||||||
|
|
||||||
|
* fileName - __string__ The name of a file supplied by the part.
|
||||||
|
|
||||||
|
(Default: `(fieldName, contentType, fileName) => (contentType === 'application/octet-stream' || fileName !== undefined)`)
|
||||||
|
|
||||||
|
* **limits** - _object_ - Various limits on incoming data. Valid properties are:
|
||||||
|
|
||||||
|
* **fieldNameSize** - _integer_ - Max field name size (in bytes) (Default: 100 bytes).
|
||||||
|
|
||||||
|
* **fieldSize** - _integer_ - Max field value size (in bytes) (Default: 1 MiB, which is 1024 x 1024 bytes).
|
||||||
|
|
||||||
|
* **fields** - _integer_ - Max number of non-file fields (Default: Infinity).
|
||||||
|
|
||||||
|
* **fileSize** - _integer_ - For multipart forms, the max file size (in bytes) (Default: Infinity).
|
||||||
|
|
||||||
|
* **files** - _integer_ - For multipart forms, the max number of file fields (Default: Infinity).
|
||||||
|
|
||||||
|
* **parts** - _integer_ - For multipart forms, the max number of parts (fields + files) (Default: Infinity).
|
||||||
|
|
||||||
|
* **headerPairs** - _integer_ - For multipart forms, the max number of header key=>value pairs to parse **Default:** 2000
|
||||||
|
|
||||||
|
* **headerSize** - _integer_ - For multipart forms, the max size of a multipart header **Default:** 81920.
|
||||||
|
|
||||||
|
* The constructor can throw errors:
|
||||||
|
|
||||||
|
* **Busboy expected an options-Object.** - Busboy expected an Object as first parameters.
|
||||||
|
|
||||||
|
* **Busboy expected an options-Object with headers-attribute.** - The first parameter is lacking of a headers-attribute.
|
||||||
|
|
||||||
|
* **Limit $limit is not a valid number** - Busboy expected the desired limit to be of type number. Busboy throws this Error to prevent a potential security issue by falling silently back to the Busboy-defaults. Potential source for this Error can be the direct use of environment variables without transforming them to the type number.
|
||||||
|
|
||||||
|
* **Unsupported Content-Type.** - The `Content-Type` isn't one Busboy can parse.
|
||||||
|
|
||||||
|
* **Missing Content-Type-header.** - The provided headers don't include `Content-Type` at all.
|
|
@ -0,0 +1,19 @@
|
||||||
|
Copyright Brian White. All rights reserved.
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to
|
||||||
|
deal in the Software without restriction, including without limitation the
|
||||||
|
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||||
|
sell copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
IN THE SOFTWARE.
|
|
@ -0,0 +1,207 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const WritableStream = require('node:stream').Writable
|
||||||
|
const inherits = require('node:util').inherits
|
||||||
|
|
||||||
|
const StreamSearch = require('../../streamsearch/sbmh')
|
||||||
|
|
||||||
|
const PartStream = require('./PartStream')
|
||||||
|
const HeaderParser = require('./HeaderParser')
|
||||||
|
|
||||||
|
const DASH = 45
|
||||||
|
const B_ONEDASH = Buffer.from('-')
|
||||||
|
const B_CRLF = Buffer.from('\r\n')
|
||||||
|
const EMPTY_FN = function () {}
|
||||||
|
|
||||||
|
function Dicer (cfg) {
|
||||||
|
if (!(this instanceof Dicer)) { return new Dicer(cfg) }
|
||||||
|
WritableStream.call(this, cfg)
|
||||||
|
|
||||||
|
if (!cfg || (!cfg.headerFirst && typeof cfg.boundary !== 'string')) { throw new TypeError('Boundary required') }
|
||||||
|
|
||||||
|
if (typeof cfg.boundary === 'string') { this.setBoundary(cfg.boundary) } else { this._bparser = undefined }
|
||||||
|
|
||||||
|
this._headerFirst = cfg.headerFirst
|
||||||
|
|
||||||
|
this._dashes = 0
|
||||||
|
this._parts = 0
|
||||||
|
this._finished = false
|
||||||
|
this._realFinish = false
|
||||||
|
this._isPreamble = true
|
||||||
|
this._justMatched = false
|
||||||
|
this._firstWrite = true
|
||||||
|
this._inHeader = true
|
||||||
|
this._part = undefined
|
||||||
|
this._cb = undefined
|
||||||
|
this._ignoreData = false
|
||||||
|
this._partOpts = { highWaterMark: cfg.partHwm }
|
||||||
|
this._pause = false
|
||||||
|
|
||||||
|
const self = this
|
||||||
|
this._hparser = new HeaderParser(cfg)
|
||||||
|
this._hparser.on('header', function (header) {
|
||||||
|
self._inHeader = false
|
||||||
|
self._part.emit('header', header)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
inherits(Dicer, WritableStream)
|
||||||
|
|
||||||
|
Dicer.prototype.emit = function (ev) {
|
||||||
|
if (ev === 'finish' && !this._realFinish) {
|
||||||
|
if (!this._finished) {
|
||||||
|
const self = this
|
||||||
|
process.nextTick(function () {
|
||||||
|
self.emit('error', new Error('Unexpected end of multipart data'))
|
||||||
|
if (self._part && !self._ignoreData) {
|
||||||
|
const type = (self._isPreamble ? 'Preamble' : 'Part')
|
||||||
|
self._part.emit('error', new Error(type + ' terminated early due to unexpected end of multipart data'))
|
||||||
|
self._part.push(null)
|
||||||
|
process.nextTick(function () {
|
||||||
|
self._realFinish = true
|
||||||
|
self.emit('finish')
|
||||||
|
self._realFinish = false
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
self._realFinish = true
|
||||||
|
self.emit('finish')
|
||||||
|
self._realFinish = false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else { WritableStream.prototype.emit.apply(this, arguments) }
|
||||||
|
}
|
||||||
|
|
||||||
|
Dicer.prototype._write = function (data, encoding, cb) {
|
||||||
|
// ignore unexpected data (e.g. extra trailer data after finished)
|
||||||
|
if (!this._hparser && !this._bparser) { return cb() }
|
||||||
|
|
||||||
|
if (this._headerFirst && this._isPreamble) {
|
||||||
|
if (!this._part) {
|
||||||
|
this._part = new PartStream(this._partOpts)
|
||||||
|
if (this._events.preamble) { this.emit('preamble', this._part) } else { this._ignore() }
|
||||||
|
}
|
||||||
|
const r = this._hparser.push(data)
|
||||||
|
if (!this._inHeader && r !== undefined && r < data.length) { data = data.slice(r) } else { return cb() }
|
||||||
|
}
|
||||||
|
|
||||||
|
// allows for "easier" testing
|
||||||
|
if (this._firstWrite) {
|
||||||
|
this._bparser.push(B_CRLF)
|
||||||
|
this._firstWrite = false
|
||||||
|
}
|
||||||
|
|
||||||
|
this._bparser.push(data)
|
||||||
|
|
||||||
|
if (this._pause) { this._cb = cb } else { cb() }
|
||||||
|
}
|
||||||
|
|
||||||
|
Dicer.prototype.reset = function () {
|
||||||
|
this._part = undefined
|
||||||
|
this._bparser = undefined
|
||||||
|
this._hparser = undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
Dicer.prototype.setBoundary = function (boundary) {
|
||||||
|
const self = this
|
||||||
|
this._bparser = new StreamSearch('\r\n--' + boundary)
|
||||||
|
this._bparser.on('info', function (isMatch, data, start, end) {
|
||||||
|
self._oninfo(isMatch, data, start, end)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
Dicer.prototype._ignore = function () {
|
||||||
|
if (this._part && !this._ignoreData) {
|
||||||
|
this._ignoreData = true
|
||||||
|
this._part.on('error', EMPTY_FN)
|
||||||
|
// we must perform some kind of read on the stream even though we are
|
||||||
|
// ignoring the data, otherwise node's Readable stream will not emit 'end'
|
||||||
|
// after pushing null to the stream
|
||||||
|
this._part.resume()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Dicer.prototype._oninfo = function (isMatch, data, start, end) {
|
||||||
|
let buf; const self = this; let i = 0; let r; let shouldWriteMore = true
|
||||||
|
|
||||||
|
if (!this._part && this._justMatched && data) {
|
||||||
|
while (this._dashes < 2 && (start + i) < end) {
|
||||||
|
if (data[start + i] === DASH) {
|
||||||
|
++i
|
||||||
|
++this._dashes
|
||||||
|
} else {
|
||||||
|
if (this._dashes) { buf = B_ONEDASH }
|
||||||
|
this._dashes = 0
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this._dashes === 2) {
|
||||||
|
if ((start + i) < end && this._events.trailer) { this.emit('trailer', data.slice(start + i, end)) }
|
||||||
|
this.reset()
|
||||||
|
this._finished = true
|
||||||
|
// no more parts will be added
|
||||||
|
if (self._parts === 0) {
|
||||||
|
self._realFinish = true
|
||||||
|
self.emit('finish')
|
||||||
|
self._realFinish = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (this._dashes) { return }
|
||||||
|
}
|
||||||
|
if (this._justMatched) { this._justMatched = false }
|
||||||
|
if (!this._part) {
|
||||||
|
this._part = new PartStream(this._partOpts)
|
||||||
|
this._part._read = function (n) {
|
||||||
|
self._unpause()
|
||||||
|
}
|
||||||
|
if (this._isPreamble && this._events.preamble) { this.emit('preamble', this._part) } else if (this._isPreamble !== true && this._events.part) { this.emit('part', this._part) } else { this._ignore() }
|
||||||
|
if (!this._isPreamble) { this._inHeader = true }
|
||||||
|
}
|
||||||
|
if (data && start < end && !this._ignoreData) {
|
||||||
|
if (this._isPreamble || !this._inHeader) {
|
||||||
|
if (buf) { shouldWriteMore = this._part.push(buf) }
|
||||||
|
shouldWriteMore = this._part.push(data.slice(start, end))
|
||||||
|
if (!shouldWriteMore) { this._pause = true }
|
||||||
|
} else if (!this._isPreamble && this._inHeader) {
|
||||||
|
if (buf) { this._hparser.push(buf) }
|
||||||
|
r = this._hparser.push(data.slice(start, end))
|
||||||
|
if (!this._inHeader && r !== undefined && r < end) { this._oninfo(false, data, start + r, end) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (isMatch) {
|
||||||
|
this._hparser.reset()
|
||||||
|
if (this._isPreamble) { this._isPreamble = false } else {
|
||||||
|
if (start !== end) {
|
||||||
|
++this._parts
|
||||||
|
this._part.on('end', function () {
|
||||||
|
if (--self._parts === 0) {
|
||||||
|
if (self._finished) {
|
||||||
|
self._realFinish = true
|
||||||
|
self.emit('finish')
|
||||||
|
self._realFinish = false
|
||||||
|
} else {
|
||||||
|
self._unpause()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this._part.push(null)
|
||||||
|
this._part = undefined
|
||||||
|
this._ignoreData = false
|
||||||
|
this._justMatched = true
|
||||||
|
this._dashes = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Dicer.prototype._unpause = function () {
|
||||||
|
if (!this._pause) { return }
|
||||||
|
|
||||||
|
this._pause = false
|
||||||
|
if (this._cb) {
|
||||||
|
const cb = this._cb
|
||||||
|
this._cb = undefined
|
||||||
|
cb()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Dicer
|
|
@ -0,0 +1,100 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const EventEmitter = require('node:events').EventEmitter
|
||||||
|
const inherits = require('node:util').inherits
|
||||||
|
const getLimit = require('../../../lib/utils/getLimit')
|
||||||
|
|
||||||
|
const StreamSearch = require('../../streamsearch/sbmh')
|
||||||
|
|
||||||
|
const B_DCRLF = Buffer.from('\r\n\r\n')
|
||||||
|
const RE_CRLF = /\r\n/g
|
||||||
|
const RE_HDR = /^([^:]+):[ \t]?([\x00-\xFF]+)?$/ // eslint-disable-line no-control-regex
|
||||||
|
|
||||||
|
function HeaderParser (cfg) {
|
||||||
|
EventEmitter.call(this)
|
||||||
|
|
||||||
|
cfg = cfg || {}
|
||||||
|
const self = this
|
||||||
|
this.nread = 0
|
||||||
|
this.maxed = false
|
||||||
|
this.npairs = 0
|
||||||
|
this.maxHeaderPairs = getLimit(cfg, 'maxHeaderPairs', 2000)
|
||||||
|
this.maxHeaderSize = getLimit(cfg, 'maxHeaderSize', 80 * 1024)
|
||||||
|
this.buffer = ''
|
||||||
|
this.header = {}
|
||||||
|
this.finished = false
|
||||||
|
this.ss = new StreamSearch(B_DCRLF)
|
||||||
|
this.ss.on('info', function (isMatch, data, start, end) {
|
||||||
|
if (data && !self.maxed) {
|
||||||
|
if (self.nread + end - start >= self.maxHeaderSize) {
|
||||||
|
end = self.maxHeaderSize - self.nread + start
|
||||||
|
self.nread = self.maxHeaderSize
|
||||||
|
self.maxed = true
|
||||||
|
} else { self.nread += (end - start) }
|
||||||
|
|
||||||
|
self.buffer += data.toString('binary', start, end)
|
||||||
|
}
|
||||||
|
if (isMatch) { self._finish() }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
inherits(HeaderParser, EventEmitter)
|
||||||
|
|
||||||
|
HeaderParser.prototype.push = function (data) {
|
||||||
|
const r = this.ss.push(data)
|
||||||
|
if (this.finished) { return r }
|
||||||
|
}
|
||||||
|
|
||||||
|
HeaderParser.prototype.reset = function () {
|
||||||
|
this.finished = false
|
||||||
|
this.buffer = ''
|
||||||
|
this.header = {}
|
||||||
|
this.ss.reset()
|
||||||
|
}
|
||||||
|
|
||||||
|
HeaderParser.prototype._finish = function () {
|
||||||
|
if (this.buffer) { this._parseHeader() }
|
||||||
|
this.ss.matches = this.ss.maxMatches
|
||||||
|
const header = this.header
|
||||||
|
this.header = {}
|
||||||
|
this.buffer = ''
|
||||||
|
this.finished = true
|
||||||
|
this.nread = this.npairs = 0
|
||||||
|
this.maxed = false
|
||||||
|
this.emit('header', header)
|
||||||
|
}
|
||||||
|
|
||||||
|
HeaderParser.prototype._parseHeader = function () {
|
||||||
|
if (this.npairs === this.maxHeaderPairs) { return }
|
||||||
|
|
||||||
|
const lines = this.buffer.split(RE_CRLF)
|
||||||
|
const len = lines.length
|
||||||
|
let m, h
|
||||||
|
|
||||||
|
for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
|
||||||
|
if (lines[i].length === 0) { continue }
|
||||||
|
if (lines[i][0] === '\t' || lines[i][0] === ' ') {
|
||||||
|
// folded header content
|
||||||
|
// RFC2822 says to just remove the CRLF and not the whitespace following
|
||||||
|
// it, so we follow the RFC and include the leading whitespace ...
|
||||||
|
if (h) {
|
||||||
|
this.header[h][this.header[h].length - 1] += lines[i]
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const posColon = lines[i].indexOf(':')
|
||||||
|
if (
|
||||||
|
posColon === -1 ||
|
||||||
|
posColon === 0
|
||||||
|
) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
m = RE_HDR.exec(lines[i])
|
||||||
|
h = m[1].toLowerCase()
|
||||||
|
this.header[h] = this.header[h] || []
|
||||||
|
this.header[h].push((m[2] || ''))
|
||||||
|
if (++this.npairs === this.maxHeaderPairs) { break }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = HeaderParser
|
|
@ -0,0 +1,13 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
const inherits = require('node:util').inherits
|
||||||
|
const ReadableStream = require('node:stream').Readable
|
||||||
|
|
||||||
|
function PartStream (opts) {
|
||||||
|
ReadableStream.call(this, opts)
|
||||||
|
}
|
||||||
|
inherits(PartStream, ReadableStream)
|
||||||
|
|
||||||
|
PartStream.prototype._read = function (n) {}
|
||||||
|
|
||||||
|
module.exports = PartStream
|
|
@ -0,0 +1,164 @@
|
||||||
|
// Type definitions for dicer 0.2
|
||||||
|
// Project: https://github.com/mscdex/dicer
|
||||||
|
// Definitions by: BendingBender <https://github.com/BendingBender>
|
||||||
|
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||||
|
// TypeScript Version: 2.2
|
||||||
|
/// <reference types="node" />
|
||||||
|
|
||||||
|
import stream = require("stream");
|
||||||
|
|
||||||
|
// tslint:disable:unified-signatures
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A very fast streaming multipart parser for node.js.
|
||||||
|
* Dicer is a WritableStream
|
||||||
|
*
|
||||||
|
* Dicer (special) events:
|
||||||
|
* - on('finish', ()) - Emitted when all parts have been parsed and the Dicer instance has been ended.
|
||||||
|
* - on('part', (stream: PartStream)) - Emitted when a new part has been found.
|
||||||
|
* - on('preamble', (stream: PartStream)) - Emitted for preamble if you should happen to need it (can usually be ignored).
|
||||||
|
* - on('trailer', (data: Buffer)) - Emitted when trailing data was found after the terminating boundary (as with the preamble, this can usually be ignored too).
|
||||||
|
*/
|
||||||
|
export class Dicer extends stream.Writable {
|
||||||
|
/**
|
||||||
|
* Creates and returns a new Dicer instance with the following valid config settings:
|
||||||
|
*
|
||||||
|
* @param config The configuration to use
|
||||||
|
*/
|
||||||
|
constructor(config: Dicer.Config);
|
||||||
|
/**
|
||||||
|
* Sets the boundary to use for parsing and performs some initialization needed for parsing.
|
||||||
|
* You should only need to use this if you set headerFirst to true in the constructor and are parsing the boundary from the preamble header.
|
||||||
|
*
|
||||||
|
* @param boundary The boundary to use
|
||||||
|
*/
|
||||||
|
setBoundary(boundary: string): void;
|
||||||
|
addListener(event: "finish", listener: () => void): this;
|
||||||
|
addListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
addListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
addListener(event: "trailer", listener: (data: Buffer) => void): this;
|
||||||
|
addListener(event: "close", listener: () => void): this;
|
||||||
|
addListener(event: "drain", listener: () => void): this;
|
||||||
|
addListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
addListener(event: "pipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
addListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
addListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
on(event: "finish", listener: () => void): this;
|
||||||
|
on(event: "part", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
on(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
on(event: "trailer", listener: (data: Buffer) => void): this;
|
||||||
|
on(event: "close", listener: () => void): this;
|
||||||
|
on(event: "drain", listener: () => void): this;
|
||||||
|
on(event: "error", listener: (err: Error) => void): this;
|
||||||
|
on(event: "pipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
on(event: "unpipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
on(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
once(event: "finish", listener: () => void): this;
|
||||||
|
once(event: "part", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
once(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
once(event: "trailer", listener: (data: Buffer) => void): this;
|
||||||
|
once(event: "close", listener: () => void): this;
|
||||||
|
once(event: "drain", listener: () => void): this;
|
||||||
|
once(event: "error", listener: (err: Error) => void): this;
|
||||||
|
once(event: "pipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
once(event: "unpipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
once(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
prependListener(event: "finish", listener: () => void): this;
|
||||||
|
prependListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
prependListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
prependListener(event: "trailer", listener: (data: Buffer) => void): this;
|
||||||
|
prependListener(event: "close", listener: () => void): this;
|
||||||
|
prependListener(event: "drain", listener: () => void): this;
|
||||||
|
prependListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
prependListener(event: "pipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
prependListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
prependListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
prependOnceListener(event: "finish", listener: () => void): this;
|
||||||
|
prependOnceListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
prependOnceListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
prependOnceListener(event: "trailer", listener: (data: Buffer) => void): this;
|
||||||
|
prependOnceListener(event: "close", listener: () => void): this;
|
||||||
|
prependOnceListener(event: "drain", listener: () => void): this;
|
||||||
|
prependOnceListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
prependOnceListener(event: "pipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
prependOnceListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
removeListener(event: "finish", listener: () => void): this;
|
||||||
|
removeListener(event: "part", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
removeListener(event: "preamble", listener: (stream: Dicer.PartStream) => void): this;
|
||||||
|
removeListener(event: "trailer", listener: (data: Buffer) => void): this;
|
||||||
|
removeListener(event: "close", listener: () => void): this;
|
||||||
|
removeListener(event: "drain", listener: () => void): this;
|
||||||
|
removeListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
removeListener(event: "pipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
removeListener(event: "unpipe", listener: (src: stream.Readable) => void): this;
|
||||||
|
removeListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
}
|
||||||
|
|
||||||
|
declare namespace Dicer {
|
||||||
|
interface Config {
|
||||||
|
/**
|
||||||
|
* This is the boundary used to detect the beginning of a new part.
|
||||||
|
*/
|
||||||
|
boundary?: string | undefined;
|
||||||
|
/**
|
||||||
|
* If true, preamble header parsing will be performed first.
|
||||||
|
*/
|
||||||
|
headerFirst?: boolean | undefined;
|
||||||
|
/**
|
||||||
|
* The maximum number of header key=>value pairs to parse Default: 2000 (same as node's http).
|
||||||
|
*/
|
||||||
|
maxHeaderPairs?: number | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PartStream is a _ReadableStream_
|
||||||
|
*
|
||||||
|
* PartStream (special) events:
|
||||||
|
* - on('header', (header: object)) - An object containing the header for this particular part. Each property value is an array of one or more string values.
|
||||||
|
*/
|
||||||
|
interface PartStream extends stream.Readable {
|
||||||
|
addListener(event: "header", listener: (header: object) => void): this;
|
||||||
|
addListener(event: "close", listener: () => void): this;
|
||||||
|
addListener(event: "data", listener: (chunk: Buffer | string) => void): this;
|
||||||
|
addListener(event: "end", listener: () => void): this;
|
||||||
|
addListener(event: "readable", listener: () => void): this;
|
||||||
|
addListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
addListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
on(event: "header", listener: (header: object) => void): this;
|
||||||
|
on(event: "close", listener: () => void): this;
|
||||||
|
on(event: "data", listener: (chunk: Buffer | string) => void): this;
|
||||||
|
on(event: "end", listener: () => void): this;
|
||||||
|
on(event: "readable", listener: () => void): this;
|
||||||
|
on(event: "error", listener: (err: Error) => void): this;
|
||||||
|
on(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
once(event: "header", listener: (header: object) => void): this;
|
||||||
|
once(event: "close", listener: () => void): this;
|
||||||
|
once(event: "data", listener: (chunk: Buffer | string) => void): this;
|
||||||
|
once(event: "end", listener: () => void): this;
|
||||||
|
once(event: "readable", listener: () => void): this;
|
||||||
|
once(event: "error", listener: (err: Error) => void): this;
|
||||||
|
once(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
prependListener(event: "header", listener: (header: object) => void): this;
|
||||||
|
prependListener(event: "close", listener: () => void): this;
|
||||||
|
prependListener(event: "data", listener: (chunk: Buffer | string) => void): this;
|
||||||
|
prependListener(event: "end", listener: () => void): this;
|
||||||
|
prependListener(event: "readable", listener: () => void): this;
|
||||||
|
prependListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
prependListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
prependOnceListener(event: "header", listener: (header: object) => void): this;
|
||||||
|
prependOnceListener(event: "close", listener: () => void): this;
|
||||||
|
prependOnceListener(event: "data", listener: (chunk: Buffer | string) => void): this;
|
||||||
|
prependOnceListener(event: "end", listener: () => void): this;
|
||||||
|
prependOnceListener(event: "readable", listener: () => void): this;
|
||||||
|
prependOnceListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
removeListener(event: "header", listener: (header: object) => void): this;
|
||||||
|
removeListener(event: "close", listener: () => void): this;
|
||||||
|
removeListener(event: "data", listener: (chunk: Buffer | string) => void): this;
|
||||||
|
removeListener(event: "end", listener: () => void): this;
|
||||||
|
removeListener(event: "readable", listener: () => void): this;
|
||||||
|
removeListener(event: "error", listener: (err: Error) => void): this;
|
||||||
|
removeListener(event: string, listener: (...args: any[]) => void): this;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,228 @@
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Copyright Brian White. All rights reserved.
|
||||||
|
*
|
||||||
|
* @see https://github.com/mscdex/streamsearch
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to
|
||||||
|
* deal in the Software without restriction, including without limitation the
|
||||||
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||||
|
* sell copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
|
* IN THE SOFTWARE.
|
||||||
|
*
|
||||||
|
* Based heavily on the Streaming Boyer-Moore-Horspool C++ implementation
|
||||||
|
* by Hongli Lai at: https://github.com/FooBarWidget/boyer-moore-horspool
|
||||||
|
*/
|
||||||
|
const EventEmitter = require('node:events').EventEmitter
|
||||||
|
const inherits = require('node:util').inherits
|
||||||
|
|
||||||
|
function SBMH (needle) {
|
||||||
|
if (typeof needle === 'string') {
|
||||||
|
needle = Buffer.from(needle)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Buffer.isBuffer(needle)) {
|
||||||
|
throw new TypeError('The needle has to be a String or a Buffer.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const needleLength = needle.length
|
||||||
|
|
||||||
|
if (needleLength === 0) {
|
||||||
|
throw new Error('The needle cannot be an empty String/Buffer.')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needleLength > 256) {
|
||||||
|
throw new Error('The needle cannot have a length bigger than 256.')
|
||||||
|
}
|
||||||
|
|
||||||
|
this.maxMatches = Infinity
|
||||||
|
this.matches = 0
|
||||||
|
|
||||||
|
this._occ = new Array(256)
|
||||||
|
.fill(needleLength) // Initialize occurrence table.
|
||||||
|
this._lookbehind_size = 0
|
||||||
|
this._needle = needle
|
||||||
|
this._bufpos = 0
|
||||||
|
|
||||||
|
this._lookbehind = Buffer.alloc(needleLength)
|
||||||
|
|
||||||
|
// Populate occurrence table with analysis of the needle,
|
||||||
|
// ignoring last letter.
|
||||||
|
for (var i = 0; i < needleLength - 1; ++i) { // eslint-disable-line no-var
|
||||||
|
this._occ[needle[i]] = needleLength - 1 - i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
inherits(SBMH, EventEmitter)
|
||||||
|
|
||||||
|
SBMH.prototype.reset = function () {
|
||||||
|
this._lookbehind_size = 0
|
||||||
|
this.matches = 0
|
||||||
|
this._bufpos = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
SBMH.prototype.push = function (chunk, pos) {
|
||||||
|
if (!Buffer.isBuffer(chunk)) {
|
||||||
|
chunk = Buffer.from(chunk, 'binary')
|
||||||
|
}
|
||||||
|
const chlen = chunk.length
|
||||||
|
this._bufpos = pos || 0
|
||||||
|
let r
|
||||||
|
while (r !== chlen && this.matches < this.maxMatches) { r = this._sbmh_feed(chunk) }
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
SBMH.prototype._sbmh_feed = function (data) {
|
||||||
|
const len = data.length
|
||||||
|
const needle = this._needle
|
||||||
|
const needleLength = needle.length
|
||||||
|
const lastNeedleChar = needle[needleLength - 1]
|
||||||
|
|
||||||
|
// Positive: points to a position in `data`
|
||||||
|
// pos == 3 points to data[3]
|
||||||
|
// Negative: points to a position in the lookbehind buffer
|
||||||
|
// pos == -2 points to lookbehind[lookbehind_size - 2]
|
||||||
|
let pos = -this._lookbehind_size
|
||||||
|
let ch
|
||||||
|
|
||||||
|
if (pos < 0) {
|
||||||
|
// Lookbehind buffer is not empty. Perform Boyer-Moore-Horspool
|
||||||
|
// search with character lookup code that considers both the
|
||||||
|
// lookbehind buffer and the current round's haystack data.
|
||||||
|
//
|
||||||
|
// Loop until
|
||||||
|
// there is a match.
|
||||||
|
// or until
|
||||||
|
// we've moved past the position that requires the
|
||||||
|
// lookbehind buffer. In this case we switch to the
|
||||||
|
// optimized loop.
|
||||||
|
// or until
|
||||||
|
// the character to look at lies outside the haystack.
|
||||||
|
while (pos < 0 && pos <= len - needleLength) {
|
||||||
|
ch = this._sbmh_lookup_char(data, pos + needleLength - 1)
|
||||||
|
|
||||||
|
if (
|
||||||
|
ch === lastNeedleChar &&
|
||||||
|
this._sbmh_memcmp(data, pos, needleLength - 1)
|
||||||
|
) {
|
||||||
|
this._lookbehind_size = 0
|
||||||
|
++this.matches
|
||||||
|
this.emit('info', true)
|
||||||
|
|
||||||
|
return (this._bufpos = pos + needleLength)
|
||||||
|
}
|
||||||
|
pos += this._occ[ch]
|
||||||
|
}
|
||||||
|
|
||||||
|
// No match.
|
||||||
|
|
||||||
|
if (pos < 0) {
|
||||||
|
// There's too few data for Boyer-Moore-Horspool to run,
|
||||||
|
// so let's use a different algorithm to skip as much as
|
||||||
|
// we can.
|
||||||
|
// Forward pos until
|
||||||
|
// the trailing part of lookbehind + data
|
||||||
|
// looks like the beginning of the needle
|
||||||
|
// or until
|
||||||
|
// pos == 0
|
||||||
|
while (pos < 0 && !this._sbmh_memcmp(data, pos, len - pos)) { ++pos }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pos >= 0) {
|
||||||
|
// Discard lookbehind buffer.
|
||||||
|
this.emit('info', false, this._lookbehind, 0, this._lookbehind_size)
|
||||||
|
this._lookbehind_size = 0
|
||||||
|
} else {
|
||||||
|
// Cut off part of the lookbehind buffer that has
|
||||||
|
// been processed and append the entire haystack
|
||||||
|
// into it.
|
||||||
|
const bytesToCutOff = this._lookbehind_size + pos
|
||||||
|
if (bytesToCutOff > 0) {
|
||||||
|
// The cut off data is guaranteed not to contain the needle.
|
||||||
|
this.emit('info', false, this._lookbehind, 0, bytesToCutOff)
|
||||||
|
}
|
||||||
|
|
||||||
|
this._lookbehind.copy(this._lookbehind, 0, bytesToCutOff,
|
||||||
|
this._lookbehind_size - bytesToCutOff)
|
||||||
|
this._lookbehind_size -= bytesToCutOff
|
||||||
|
|
||||||
|
data.copy(this._lookbehind, this._lookbehind_size)
|
||||||
|
this._lookbehind_size += len
|
||||||
|
|
||||||
|
this._bufpos = len
|
||||||
|
return len
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pos += (pos >= 0) * this._bufpos
|
||||||
|
|
||||||
|
// Lookbehind buffer is now empty. We only need to check if the
|
||||||
|
// needle is in the haystack.
|
||||||
|
if (data.indexOf(needle, pos) !== -1) {
|
||||||
|
pos = data.indexOf(needle, pos)
|
||||||
|
++this.matches
|
||||||
|
if (pos > 0) { this.emit('info', true, data, this._bufpos, pos) } else { this.emit('info', true) }
|
||||||
|
|
||||||
|
return (this._bufpos = pos + needleLength)
|
||||||
|
} else {
|
||||||
|
pos = len - needleLength
|
||||||
|
}
|
||||||
|
|
||||||
|
// There was no match. If there's trailing haystack data that we cannot
|
||||||
|
// match yet using the Boyer-Moore-Horspool algorithm (because the trailing
|
||||||
|
// data is less than the needle size) then match using a modified
|
||||||
|
// algorithm that starts matching from the beginning instead of the end.
|
||||||
|
// Whatever trailing data is left after running this algorithm is added to
|
||||||
|
// the lookbehind buffer.
|
||||||
|
while (
|
||||||
|
pos < len &&
|
||||||
|
(
|
||||||
|
data[pos] !== needle[0] ||
|
||||||
|
(
|
||||||
|
(Buffer.compare(
|
||||||
|
data.subarray(pos, pos + len - pos),
|
||||||
|
needle.subarray(0, len - pos)
|
||||||
|
) !== 0)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
++pos
|
||||||
|
}
|
||||||
|
if (pos < len) {
|
||||||
|
data.copy(this._lookbehind, 0, pos, pos + (len - pos))
|
||||||
|
this._lookbehind_size = len - pos
|
||||||
|
}
|
||||||
|
|
||||||
|
// Everything until pos is guaranteed not to contain needle data.
|
||||||
|
if (pos > 0) { this.emit('info', false, data, this._bufpos, pos < len ? pos : len) }
|
||||||
|
|
||||||
|
this._bufpos = len
|
||||||
|
return len
|
||||||
|
}
|
||||||
|
|
||||||
|
SBMH.prototype._sbmh_lookup_char = function (data, pos) {
|
||||||
|
return (pos < 0)
|
||||||
|
? this._lookbehind[this._lookbehind_size + pos]
|
||||||
|
: data[pos]
|
||||||
|
}
|
||||||
|
|
||||||
|
SBMH.prototype._sbmh_memcmp = function (data, pos, len) {
|
||||||
|
for (var i = 0; i < len; ++i) { // eslint-disable-line no-var
|
||||||
|
if (this._sbmh_lookup_char(data, pos + i) !== this._needle[i]) { return false }
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = SBMH
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue