From 4b0fae652a716a6a4226915b549b4665a7829931 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 14:51:09 +0100 Subject: [PATCH 01/18] Updated dependencies. --- package-lock.json | 188 +++++++++++++++++++++++++++++++++++++++------- package.json | 6 +- 2 files changed, 162 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4d3d436..adee5ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,9 +8,9 @@ "name": "discord-fetch-messages", "version": "1.1.2", "devDependencies": { - "discord.js": "^12.5.0", - "prettier": "^2.2.0", - "typescript": "^4.1.2" + "discord.js": "^12.5.1", + "prettier": "^2.2.1", + "typescript": "^4.1.3" }, "engines": { "discord.js": ">=12.0.0", @@ -77,9 +77,9 @@ } }, "node_modules/discord.js": { - "version": "12.5.0", - "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.0.tgz", - "integrity": "sha512-MXZcnBIosHEOX26ipWEcZdUrTyfTbb4sDYYp0Go5N05PyI78LR8Ds7yAfMu0zUDmxFHYLSYX0pCdiO2pd4CP6w==", + "version": "12.5.1", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz", + "integrity": "sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w==", "dev": true, "dependencies": { "@discordjs/collection": "^0.1.6", @@ -105,21 +105,21 @@ } }, "node_modules/mime-db": { - "version": "1.44.0", - "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.44.0.tgz", - "integrity": "sha512-/NOTfLrsPBVeH7YtFPgsVWveuL+4SjjYxaQ1xtM1KMFj7HdxlBlxeyNLzhyJVx7r4rZGJAZ/6lkKCitSc/Nmpg==", + "version": "1.45.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", + "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==", "dev": true, "engines": { "node": ">= 0.6" } }, "node_modules/mime-types": { - "version": "2.1.27", - "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.27.tgz", - "integrity": "sha512-JIhqnCasI9yD+SsmkquHBxTSEuZdQX5BuQnS2Vc7puQQQ+8yiP5AY5uWhpdv4YL4VM5c6iliiYWPgJ/nJQLp7w==", + "version": "2.1.28", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", + "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", "dev": true, "dependencies": { - "mime-db": "1.44.0" + "mime-db": "1.45.0" }, "engines": { "node": ">= 0.6" @@ -135,9 +135,9 @@ } }, "node_modules/prettier": { - "version": "2.2.0", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.0.tgz", - "integrity": "sha512-yYerpkvseM4iKD/BXLYUkQV5aKt4tQPqaGW6EsZjzyu0r7sVZZNPJW4Y8MyKmicp6t42XUPcBVA+H6sB3gqndw==", + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz", + "integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==", "dev": true, "bin": { "prettier": "bin-prettier.js" @@ -147,15 +147,15 @@ } }, "node_modules/prism-media": { - "version": "1.2.2", - "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.2.tgz", - "integrity": "sha512-I+nkWY212lJ500jLe4tN9tWO7nRiBAVdMv76P9kffZjYhw20raMlW1HSSvS+MLXC9MmbNZCazMrAr+5jEEgTuw==", + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.3.tgz", + "integrity": "sha512-fSrR66n0l6roW9Rx4rSLMyTPTjRTiXy5RVqDOurACQ6si1rKHHKDU5gwBJoCsIV0R3o9gi+K50akl/qyw1C74A==", "dev": true, "peerDependencies": { - "@discordjs/opus": "^0.1.0", - "ffmpeg-static": "^2.4.0 || ^3.0.0", - "node-opus": "^0.3.1", - "opusscript": "^0.0.6" + "@discordjs/opus": "^0.3.3", + "ffmpeg-static": "^4.2.7 || ^3.0.0 || ^2.4.0", + "node-opus": "^0.3.3", + "opusscript": "^0.0.7" }, "peerDependenciesMeta": { "@discordjs/opus": { @@ -185,9 +185,9 @@ "dev": true }, "node_modules/typescript": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.2.tgz", - "integrity": "sha512-thGloWsGH3SOxv1SoY7QojKi0tc+8FnOmiarEGMbd/lar7QOEd3hvlx3Fp5y6FlDUGl9L+pd4n2e+oToGMmhRQ==", + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", + "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", "dev": true, "bin": { "tsc": "bin/tsc", @@ -198,9 +198,9 @@ } }, "node_modules/ws": { - "version": "7.4.0", - "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.0.tgz", - "integrity": "sha512-kyFwXuV/5ymf+IXhS6f0+eAFvydbaBW3zjpT6hUdAh/hbVjTIB5EHBGi0bPoCLSK2wcuz3BrEkB9LrYv1Nm4NQ==", + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.2.tgz", + "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==", "dev": true, "engines": { "node": ">=8.3.0" @@ -218,5 +218,135 @@ } } } + }, + "dependencies": { + "@discordjs/collection": { + "version": "0.1.6", + "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz", + "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==", + "dev": true + }, + "@discordjs/form-data": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", + "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", + "dev": true, + "requires": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + } + }, + "abort-controller": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", + "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", + "dev": true, + "requires": { + "event-target-shim": "^5.0.0" + } + }, + "asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", + "dev": true + }, + "combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "requires": { + "delayed-stream": "~1.0.0" + } + }, + "delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", + "dev": true + }, + "discord.js": { + "version": "12.5.1", + "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz", + "integrity": "sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w==", + "dev": true, + "requires": { + "@discordjs/collection": "^0.1.6", + "@discordjs/form-data": "^3.0.1", + "abort-controller": "^3.0.0", + "node-fetch": "^2.6.1", + "prism-media": "^1.2.2", + "setimmediate": "^1.0.5", + "tweetnacl": "^1.0.3", + "ws": "^7.3.1" + } + }, + "event-target-shim": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", + "dev": true + }, + "mime-db": { + "version": "1.45.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", + "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==", + "dev": true + }, + "mime-types": { + "version": "2.1.28", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", + "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", + "dev": true, + "requires": { + "mime-db": "1.45.0" + } + }, + "node-fetch": { + "version": "2.6.1", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", + "dev": true + }, + "prettier": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.2.1.tgz", + "integrity": "sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==", + "dev": true + }, + "prism-media": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.3.tgz", + "integrity": "sha512-fSrR66n0l6roW9Rx4rSLMyTPTjRTiXy5RVqDOurACQ6si1rKHHKDU5gwBJoCsIV0R3o9gi+K50akl/qyw1C74A==", + "dev": true, + "requires": {} + }, + "setimmediate": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=", + "dev": true + }, + "tweetnacl": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==", + "dev": true + }, + "typescript": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.3.tgz", + "integrity": "sha512-B3ZIOf1IKeH2ixgHhj6la6xdwR9QrLC5d1VKeCSY4tvkqhF2eqd9O7txNlS0PO3GrBAFIdr3L1ndNwteUbZLYg==", + "dev": true + }, + "ws": { + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.2.tgz", + "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==", + "dev": true, + "requires": {} + } } } diff --git a/package.json b/package.json index 643c8b2..365b32c 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "description": "A npm package for fetching messages from a Discord Guild or a Discord Channel.", "devDependencies": { - "discord.js": "^12.5.0", - "prettier": "^2.2.0", - "typescript": "^4.1.2" + "discord.js": "^12.5.1", + "prettier": "^2.2.1", + "typescript": "^4.1.3" }, "engineStrict": true, "engines": { From 5749bbf30c58f8a11ea5caf3ec96ae621d5d7694 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 14:51:47 +0100 Subject: [PATCH 02/18] Added build & prepare scripts. --- package.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/package.json b/package.json index 365b32c..94011a8 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,8 @@ "url": "https://github.com/Ayfri/discord-fetch" }, "scripts": { + "build": "npm run prettier && tsc", + "prepare": "npm run build", "prettier": "prettier --write main.js package.json README.md" }, "version": "1.1.2" From 2a7b7eea4b631d833dafa9e9e718f6efb216931c Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:00:05 +0100 Subject: [PATCH 03/18] Added tsconfig.json. --- tsconfig.json | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tsconfig.json diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..79f8df0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "alwaysStrict": true, + "declaration": true, + "declarationDir": "types", + "lib": [ + "ESNext" + ], + "module": "commonjs", + "noImplicitAny": true, + "outDir": "dist", + "pretty": true, + "rootDir": "src", + "strict": true, + "target": "esNext" + }, + "exclude": [ + "node_modules" + ] +} From d89f9c37d32b2791980667601f80646f174610ae Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:00:22 +0100 Subject: [PATCH 04/18] Simplified prettier script. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 94011a8..63b6302 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "scripts": { "build": "npm run prettier && tsc", "prepare": "npm run build", - "prettier": "prettier --write main.js package.json README.md" + "prettier": "prettier . --write" }, "version": "1.1.2" } From e71e2e31f6b610efbcabc25cb48d254f39e3388a Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:24:46 +0100 Subject: [PATCH 05/18] Improved main.js chore variables names. --- main.js | 20 ++++++++++---------- src/Fetcher.ts | 4 ++++ 2 files changed, 14 insertions(+), 10 deletions(-) create mode 100644 src/Fetcher.ts diff --git a/main.js b/main.js index c0130f9..fcd0200 100644 --- a/main.js +++ b/main.js @@ -40,7 +40,7 @@ async function fetchChannelMessages(client, channel) { * @returns {Promise} - All the messages fetched. */ async function fetchGuildMessages(client, guildID) { - const m = []; + const total = []; const channels = client.guilds.cache.get(guildID).channels.cache.filter(c => c.isText()); console.log( `Getting the messages from these channels : ${channels @@ -49,20 +49,20 @@ async function fetchGuildMessages(client, guildID) { .join('\n')}` ); - for (const channel of channels.array()) { - console.log(`Getting messages from : #${channel.name}.`); - const messages = await fetchChannelMessages(client, channel); + for (const textChannel of channels.array()) { + console.log(`Getting messages from : #${textChannel.name}.`); + const messages = await fetchChannelMessages(client, textChannel); - if (!m.find(c => c.id === channel.id)) - m.push({ - id: channel.id, + if (!total.find(channel => channel.id === textChannel.id)) + total.push({ + id: textChannel.id, messages: [], }); - m.find(c => c.id === channel.id).messages.push(...messages.map(m => m.cleanContent)); + total.find(channel => channel.id === textChannel.id).messages.push(...messages.map(m => m.cleanContent)); } - console.log(`Finished fetching messages, messages count: ${m.length}`); - return m; + console.log(`Finished fetching messages, messages count: ${total.length}`); + return total; } module.exports = { diff --git a/src/Fetcher.ts b/src/Fetcher.ts new file mode 100644 index 0000000..846b624 --- /dev/null +++ b/src/Fetcher.ts @@ -0,0 +1,4 @@ +export default class Fetcher { + public constructor() { + } +} From 6837676fee010fd77c35abaf0d5a024251bad1ad Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:25:08 +0100 Subject: [PATCH 06/18] Added @types/node. --- package-lock.json | 71 +++++++++++++++++++---------------------------- package.json | 5 +++- 2 files changed, 32 insertions(+), 44 deletions(-) diff --git a/package-lock.json b/package-lock.json index adee5ae..9f9246d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,8 +7,11 @@ "": { "name": "discord-fetch-messages", "version": "1.1.2", + "dependencies": { + "discord.js": "^12.5.1" + }, "devDependencies": { - "discord.js": "^12.5.1", + "@types/node": "^14.14.22", "prettier": "^2.2.1", "typescript": "^4.1.3" }, @@ -20,14 +23,12 @@ "node_modules/@discordjs/collection": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz", - "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==", - "dev": true + "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==" }, "node_modules/@discordjs/form-data": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", - "dev": true, "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -37,11 +38,16 @@ "node": ">= 6" } }, + "node_modules/@types/node": { + "version": "14.14.22", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.22.tgz", + "integrity": "sha512-g+f/qj/cNcqKkc3tFqlXOYjrmZA+jNBiDzbP3kH+B+otKFqAdPgVTGP1IeKRdMml/aE69as5S4FqtxAbl+LaMw==", + "dev": true + }, "node_modules/abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", - "dev": true, "dependencies": { "event-target-shim": "^5.0.0" }, @@ -52,14 +58,12 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "dev": true + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "node_modules/combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "dependencies": { "delayed-stream": "~1.0.0" }, @@ -71,7 +75,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "dev": true, "engines": { "node": ">=0.4.0" } @@ -80,7 +83,6 @@ "version": "12.5.1", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz", "integrity": "sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w==", - "dev": true, "dependencies": { "@discordjs/collection": "^0.1.6", "@discordjs/form-data": "^3.0.1", @@ -99,7 +101,6 @@ "version": "5.0.1", "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", - "dev": true, "engines": { "node": ">=6" } @@ -108,7 +109,6 @@ "version": "1.45.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==", - "dev": true, "engines": { "node": ">= 0.6" } @@ -117,7 +117,6 @@ "version": "2.1.28", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", - "dev": true, "dependencies": { "mime-db": "1.45.0" }, @@ -129,7 +128,6 @@ "version": "2.6.1", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", - "dev": true, "engines": { "node": "4.x || >=6.0.0" } @@ -150,7 +148,6 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.3.tgz", "integrity": "sha512-fSrR66n0l6roW9Rx4rSLMyTPTjRTiXy5RVqDOurACQ6si1rKHHKDU5gwBJoCsIV0R3o9gi+K50akl/qyw1C74A==", - "dev": true, "peerDependencies": { "@discordjs/opus": "^0.3.3", "ffmpeg-static": "^4.2.7 || ^3.0.0 || ^2.4.0", @@ -175,14 +172,12 @@ "node_modules/setimmediate": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", - "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=", - "dev": true + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=" }, "node_modules/tweetnacl": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", - "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==", - "dev": true + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" }, "node_modules/typescript": { "version": "4.1.3", @@ -201,7 +196,6 @@ "version": "7.4.2", "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.2.tgz", "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==", - "dev": true, "engines": { "node": ">=8.3.0" }, @@ -223,25 +217,28 @@ "@discordjs/collection": { "version": "0.1.6", "resolved": "https://registry.npmjs.org/@discordjs/collection/-/collection-0.1.6.tgz", - "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==", - "dev": true + "integrity": "sha512-utRNxnd9kSS2qhyivo9lMlt5qgAUasH2gb7BEOn6p0efFh24gjGomHzWKMAPn2hEReOPQZCJaRKoURwRotKucQ==" }, "@discordjs/form-data": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/@discordjs/form-data/-/form-data-3.0.1.tgz", "integrity": "sha512-ZfFsbgEXW71Rw/6EtBdrP5VxBJy4dthyC0tpQKGKmYFImlmmrykO14Za+BiIVduwjte0jXEBlhSKf0MWbFp9Eg==", - "dev": true, "requires": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", "mime-types": "^2.1.12" } }, + "@types/node": { + "version": "14.14.22", + "resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.22.tgz", + "integrity": "sha512-g+f/qj/cNcqKkc3tFqlXOYjrmZA+jNBiDzbP3kH+B+otKFqAdPgVTGP1IeKRdMml/aE69as5S4FqtxAbl+LaMw==", + "dev": true + }, "abort-controller": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/abort-controller/-/abort-controller-3.0.0.tgz", "integrity": "sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==", - "dev": true, "requires": { "event-target-shim": "^5.0.0" } @@ -249,14 +246,12 @@ "asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=", - "dev": true + "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=" }, "combined-stream": { "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", - "dev": true, "requires": { "delayed-stream": "~1.0.0" } @@ -264,14 +259,12 @@ "delayed-stream": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", - "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=", - "dev": true + "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, "discord.js": { "version": "12.5.1", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-12.5.1.tgz", "integrity": "sha512-VwZkVaUAIOB9mKdca0I5MefPMTQJTNg0qdgi1huF3iwsFwJ0L5s/Y69AQe+iPmjuV6j9rtKoG0Ta0n9vgEIL6w==", - "dev": true, "requires": { "@discordjs/collection": "^0.1.6", "@discordjs/form-data": "^3.0.1", @@ -286,20 +279,17 @@ "event-target-shim": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/event-target-shim/-/event-target-shim-5.0.1.tgz", - "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==", - "dev": true + "integrity": "sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==" }, "mime-db": { "version": "1.45.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.45.0.tgz", - "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==", - "dev": true + "integrity": "sha512-CkqLUxUk15hofLoLyljJSrukZi8mAtgd+yE5uO4tqRZsdsAJKv0O+rFMhVDRJgozy+yG6md5KwuXhD4ocIoP+w==" }, "mime-types": { "version": "2.1.28", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.28.tgz", "integrity": "sha512-0TO2yJ5YHYr7M2zzT7gDU1tbwHxEUWBCLt0lscSNpcdAfFyJOVEpRYNS7EXVcTLNj/25QO8gulHC5JtTzSE2UQ==", - "dev": true, "requires": { "mime-db": "1.45.0" } @@ -307,8 +297,7 @@ "node-fetch": { "version": "2.6.1", "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz", - "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==", - "dev": true + "integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==" }, "prettier": { "version": "2.2.1", @@ -320,20 +309,17 @@ "version": "1.2.3", "resolved": "https://registry.npmjs.org/prism-media/-/prism-media-1.2.3.tgz", "integrity": "sha512-fSrR66n0l6roW9Rx4rSLMyTPTjRTiXy5RVqDOurACQ6si1rKHHKDU5gwBJoCsIV0R3o9gi+K50akl/qyw1C74A==", - "dev": true, "requires": {} }, "setimmediate": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz", - "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=", - "dev": true + "integrity": "sha1-KQy7Iy4waULX1+qbg3Mqt4VvgoU=" }, "tweetnacl": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/tweetnacl/-/tweetnacl-1.0.3.tgz", - "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==", - "dev": true + "integrity": "sha512-6rt+RN7aOi1nGMyC4Xa5DdYiukl2UWCbcJft7YhxReBGQD7OAM8Pbxw6YMo4r2diNEA8FEmu32YOn9rhaiE5yw==" }, "typescript": { "version": "4.1.3", @@ -345,7 +331,6 @@ "version": "7.4.2", "resolved": "https://registry.npmjs.org/ws/-/ws-7.4.2.tgz", "integrity": "sha512-T4tewALS3+qsrpGI/8dqNMLIVdq/g/85U98HPMa6F0m6xTbvhXU6RCQLqPH3+SlomNV/LdY6RXEbBpMH6EOJnA==", - "dev": true, "requires": {} } } diff --git a/package.json b/package.json index 63b6302..c3cbf92 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,13 @@ { "description": "A npm package for fetching messages from a Discord Guild or a Discord Channel.", "devDependencies": { - "discord.js": "^12.5.1", + "@types/node": "^14.14.22", "prettier": "^2.2.1", "typescript": "^4.1.3" }, + "dependencies": { + "discord.js": "^12.5.1" + }, "engineStrict": true, "engines": { "node": ">=14.0.0", From cd48399be3fefb7e99b2b121b703996122073f7f Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:29:10 +0100 Subject: [PATCH 07/18] Added rules in tsconfig to ignore libs and use Node module resolution. --- main.js => index.js | 3 +++ tsconfig.json | 10 ++++------ 2 files changed, 7 insertions(+), 6 deletions(-) rename main.js => index.js (97%) diff --git a/main.js b/index.js similarity index 97% rename from main.js rename to index.js index fcd0200..0bc62c4 100644 --- a/main.js +++ b/index.js @@ -1,3 +1,5 @@ +const { Fetcher } = require('./dist/Fetcher.js'); + /** * Fetch all the messages from a Discord TextChannel. * @param {module:"discord.js".Client} client - Your Discord.js Client. @@ -66,6 +68,7 @@ async function fetchGuildMessages(client, guildID) { } module.exports = { + Fetcher, fetchGuildMessages, fetchChannelMessages, }; diff --git a/tsconfig.json b/tsconfig.json index 79f8df0..c1578a5 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,18 +3,16 @@ "alwaysStrict": true, "declaration": true, "declarationDir": "types", - "lib": [ - "ESNext" - ], + "lib": ["ESNext"], "module": "commonjs", + "moduleResolution": "node", "noImplicitAny": true, "outDir": "dist", "pretty": true, "rootDir": "src", + "skipLibCheck": true, "strict": true, "target": "esNext" }, - "exclude": [ - "node_modules" - ] + "exclude": ["node_modules"] } From 3800d40e9f62872eb56f60974e29ed44a3859eb3 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:29:32 +0100 Subject: [PATCH 08/18] Renamed main.js to index.js. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c3cbf92..d1381a8 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "discord-bot", "fetch" ], - "main": "main.js", + "main": "index.js", "name": "discord-fetch-messages", "prettier": { "arrowParens": "avoid", From 8d136faf87662a771603dce9d3d6aeef77aaef49 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:30:09 +0100 Subject: [PATCH 09/18] Formatted Fetcher class. --- src/Fetcher.ts | 58 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/src/Fetcher.ts b/src/Fetcher.ts index 846b624..a4caa39 100644 --- a/src/Fetcher.ts +++ b/src/Fetcher.ts @@ -1,4 +1,58 @@ -export default class Fetcher { - public constructor() { +import { Client, Message, Collection, Snowflake, TextChannel, Guild } from 'discord.js'; +import { EventEmitter } from 'events'; + +export class Fetcher extends EventEmitter { + public totalMessages: Collection; + public fetching: boolean; + + public constructor(public readonly client: Client) { + super(); + this.totalMessages = new Collection(); + this.fetching = false; + } + + public async fetchChannel(channelID: Snowflake | TextChannel): Promise> { + const channel = channelID instanceof TextChannel ? channelID : await this.client.channels.fetch(channelID); + let messages = new Collection(); + + if (channel.isText()) { + this.emit('fetchChannel', channel); + this.fetching = true; + let channelMessages = await channel.messages.fetch({ + limit: 100, + }); + this.emit('fetch', channelMessages.size, channelMessages); + + while (channelMessages.size > 0) { + messages = messages.concat(channelMessages); + channelMessages = await channel.messages.fetch({ + limit: 100, + before: channelMessages.last()!.id, + }); + this.emit('fetch', channelMessages.size, channelMessages); + } + + this.fetching = false; + } + return messages; + } + + public async fetchGuild(guildID: Snowflake | Guild): Promise> { + const guild = guildID instanceof Guild ? guildID : await this.client.guilds.fetch(guildID); + let messages = new Collection(); + if (guild) { + this.fetching = true; + const channels = guild.channels.cache.filter(c => c.isText()) as Collection; + this.emit('fetchGuild', guild); + + for (const channel of channels.array()) { + const channelMessages = await this.fetchChannel(channel); + messages = messages.concat(channelMessages); + } + + this.fetching = false; + } + + return messages; } } From 2d0639e6be30a3e3d6d3815d7a1a9d1c7e75e18c Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:30:22 +0100 Subject: [PATCH 10/18] Added types to Fetcher. --- types/Fetcher.d.ts | 11 +++++++++++ types/main.d.ts | 36 ++++++++++++++++++------------------ 2 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 types/Fetcher.d.ts diff --git a/types/Fetcher.d.ts b/types/Fetcher.d.ts new file mode 100644 index 0000000..fb97616 --- /dev/null +++ b/types/Fetcher.d.ts @@ -0,0 +1,11 @@ +/// +import { Client, Message, Collection, Snowflake, TextChannel, Guild } from 'discord.js'; +import { EventEmitter } from 'events'; +export declare class Fetcher extends EventEmitter { + readonly client: Client; + totalMessages: Collection; + fetching: boolean; + constructor(client: Client); + fetchChannel(channelID: Snowflake | TextChannel): Promise>; + fetchGuild(guildID: Snowflake | Guild): Promise>; +} diff --git a/types/main.d.ts b/types/main.d.ts index 2ed3b78..252591e 100644 --- a/types/main.d.ts +++ b/types/main.d.ts @@ -1,18 +1,18 @@ -import * as Discord from 'discord.js'; - -declare module 'discord-fetch-messages' { - /** - * Fetch all the messages from a Discord Guild. - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {string} guildID - The ID of the Guild to be fetch. - * @returns {Promise} - All the messages fetched. - */ - export function fetchGuildMessages(client: Discord.Client, guildID: Discord.Snowflake): Promise; - /** - * Fetch all the messages from a Discord TextChannel. - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. - * @returns {Promise} - All the messages fetched. - */ - export function fetchChannelMessages(client: Discord.Client, channel: Discord.Channel): Promise; -} +import * as Discord from 'discord.js'; + +declare module 'discord-fetch-messages' { + /** + * Fetch all the messages from a Discord Guild. + * @param {module:"discord.js".Client} client - Your Discord.js Client. + * @param {string} guildID - The ID of the Guild to be fetch. + * @returns {Promise} - All the messages fetched. + */ + export function fetchGuildMessages(client: Discord.Client, guildID: Discord.Snowflake): Promise; + /** + * Fetch all the messages from a Discord TextChannel. + * @param {module:"discord.js".Client} client - Your Discord.js Client. + * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. + * @returns {Promise} - All the messages fetched. + */ + export function fetchChannelMessages(client: Discord.Client, channel: Discord.Channel): Promise; +} From 8f35dcb9e417849fb206d6b4a64ae62da703af7e Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:35:14 +0100 Subject: [PATCH 11/18] Fixed types in package.json. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d1381a8..9460c3a 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "trailingComma": "es5", "useTabs": true }, - "types": "./types/main.d.ts", + "types": "types", "readme": "https://github.com/Ayfri/discord-fetch/blob/main/README.md", "repository": { "type": "git", From e57218fa75c629599a596effe4782e564aeb4efc Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:35:25 +0100 Subject: [PATCH 12/18] Added deprecation for old functions. --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/index.js b/index.js index 0bc62c4..0dc62ff 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,13 @@ +const { deprecate } = require('util'); const { Fetcher } = require('./dist/Fetcher.js'); /** * Fetch all the messages from a Discord TextChannel. + * * @param {module:"discord.js".Client} client - Your Discord.js Client. * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. * @returns {Promise} - All the messages fetched. + * @deprecated Use Fetcher class instead. */ async function fetchChannelMessages(client, channel) { const total = []; @@ -40,6 +43,7 @@ async function fetchChannelMessages(client, channel) { * @param {module:"discord.js".Client} client - Your Discord.js Client. * @param {string} guildID - The ID of the Guild to be fetch. * @returns {Promise} - All the messages fetched. + * @deprecated Use Fetcher class instead. */ async function fetchGuildMessages(client, guildID) { const total = []; @@ -67,6 +71,9 @@ async function fetchGuildMessages(client, guildID) { return total; } +deprecate(fetchChannelMessages, 'fetchChannelMessages() is deprecated. Use Fetcher class instead.') +deprecate(fetchGuildMessages, 'fetchGuildMessages() is deprecated. Use Fetcher class instead.') + module.exports = { Fetcher, fetchGuildMessages, From e3352cfc44a2ab41ff0dc476d70f2b7330b93327 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 15:35:43 +0100 Subject: [PATCH 13/18] Reformatted github workflow. --- .github/workflows/npm-publish.yml | 74 +++++++++++++++---------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index 4b08152..ca9fd4b 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -4,44 +4,44 @@ name: Node.js Package on: - release: - types: [created] + release: + types: [created] jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 14 - - run: npm ci - - run: npm test + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 14 + - run: npm ci + - run: npm test - publish-npm: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 14 - registry-url: https://registry.npmjs.org/ - - run: npm ci - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} + publish-npm: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 14 + registry-url: https://registry.npmjs.org/ + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} - publish-gpr: - needs: build - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 - with: - node-version: 14 - registry-url: https://npm.pkg.github.com/ - - run: npm ci - - run: npm publish - env: - NODE_AUTH_TOKEN: ${{secrets.GH_TOKEN}} + publish-gpr: + needs: build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v1 + with: + node-version: 14 + registry-url: https://npm.pkg.github.com/ + - run: npm ci + - run: npm publish + env: + NODE_AUTH_TOKEN: ${{secrets.GH_TOKEN}} From e51b9fcb21522569d69c7b28bb7a79917b138e76 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 16:03:49 +0100 Subject: [PATCH 14/18] Moved in many ways files to have proper exports. --- index.js | 82 +--------------------------------------------- src/Fetcher.ts | 34 ++++++++++++++++--- src/index.ts | 2 ++ src/main.js | 79 ++++++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 5 +-- types/Fetcher.d.ts | 7 +++- types/index.d.ts | 2 ++ 7 files changed, 122 insertions(+), 89 deletions(-) create mode 100644 src/index.ts create mode 100644 src/main.js create mode 100644 types/index.d.ts diff --git a/index.js b/index.js index 0dc62ff..f843a90 100644 --- a/index.js +++ b/index.js @@ -1,81 +1 @@ -const { deprecate } = require('util'); -const { Fetcher } = require('./dist/Fetcher.js'); - -/** - * Fetch all the messages from a Discord TextChannel. - * - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. - * @returns {Promise} - All the messages fetched. - * @deprecated Use Fetcher class instead. - */ -async function fetchChannelMessages(client, channel) { - const total = []; - let lastMessageID; - let messages; - - if (!channel.isText()) return total; - - while (true) { - if (lastMessageID) { - messages = await channel.messages.fetch({ - limit: 100, - before: lastMessageID, - }); - } else { - messages = await channel.messages.fetch({ - limit: 100, - }); - } - - if (messages.size === 0) break; - - lastMessageID = messages.last().id; - console.log(`#${channel.name} : ${total.length}`); - total.push(...messages.array()); - } - - return total; -} - -/** - * Fetch all the messages from a Discord Guild. - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {string} guildID - The ID of the Guild to be fetch. - * @returns {Promise} - All the messages fetched. - * @deprecated Use Fetcher class instead. - */ -async function fetchGuildMessages(client, guildID) { - const total = []; - const channels = client.guilds.cache.get(guildID).channels.cache.filter(c => c.isText()); - console.log( - `Getting the messages from these channels : ${channels - .map(c => `#${c.name}`) - .sort() - .join('\n')}` - ); - - for (const textChannel of channels.array()) { - console.log(`Getting messages from : #${textChannel.name}.`); - const messages = await fetchChannelMessages(client, textChannel); - - if (!total.find(channel => channel.id === textChannel.id)) - total.push({ - id: textChannel.id, - messages: [], - }); - total.find(channel => channel.id === textChannel.id).messages.push(...messages.map(m => m.cleanContent)); - } - - console.log(`Finished fetching messages, messages count: ${total.length}`); - return total; -} - -deprecate(fetchChannelMessages, 'fetchChannelMessages() is deprecated. Use Fetcher class instead.') -deprecate(fetchGuildMessages, 'fetchGuildMessages() is deprecated. Use Fetcher class instead.') - -module.exports = { - Fetcher, - fetchGuildMessages, - fetchChannelMessages, -}; +module.exports = require('./dist/index.js'); diff --git a/src/Fetcher.ts b/src/Fetcher.ts index a4caa39..e333d5e 100644 --- a/src/Fetcher.ts +++ b/src/Fetcher.ts @@ -1,16 +1,31 @@ import { Client, Message, Collection, Snowflake, TextChannel, Guild } from 'discord.js'; import { EventEmitter } from 'events'; +/** + * The main class used to fetch things. + */ export class Fetcher extends EventEmitter { - public totalMessages: Collection; + /** + * A simple property set to `true` when the Fetcher is fetching a bulk of messages, then set to false. + */ public fetching: boolean; - + + /** + * Creates a new Fetcher. + * + * @param client - Needs the client to fetch things. + */ public constructor(public readonly client: Client) { super(); - this.totalMessages = new Collection(); this.fetching = false; } - + + /** + * Fetch the entire list of messages from a channel, can be long and makes you have rateLimits. + * + * @param channelID - The channel, can be an ID or a Channel. + * @returns The messages fetched. + */ public async fetchChannel(channelID: Snowflake | TextChannel): Promise> { const channel = channelID instanceof TextChannel ? channelID : await this.client.channels.fetch(channelID); let messages = new Collection(); @@ -36,7 +51,16 @@ export class Fetcher extends EventEmitter { } return messages; } - + + /** + * Fetch an entire guild, fetching every TextChannels one by one because there is no other way. + * + * @remarks + * Can be really long and you should prefer using events than waiting for it to finish. + * + * @param guildID - The guild to fetch, can be an ID or a Guild. + * @returns The messages fetched. + */ public async fetchGuild(guildID: Snowflake | Guild): Promise> { const guild = guildID instanceof Guild ? guildID : await this.client.guilds.fetch(guildID); let messages = new Collection(); diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..c4e3dd8 --- /dev/null +++ b/src/index.ts @@ -0,0 +1,2 @@ +export { fetchChannelMessages, fetchGuildMessages } from './main.js'; +export { Fetcher } from './Fetcher'; diff --git a/src/main.js b/src/main.js new file mode 100644 index 0000000..3e8cf16 --- /dev/null +++ b/src/main.js @@ -0,0 +1,79 @@ +const { deprecate } = require('util'); + +/** + * Fetch all the messages from a Discord TextChannel. + * + * @param {module:"discord.js".Client} client - Your Discord.js Client. + * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. + * @returns {Promise} - All the messages fetched. + * @deprecated Use {@link Fetcher} class instead. + */ +async function fetchChannelMessages(client, channel) { + const total = []; + let lastMessageID; + let messages; + + if (!channel.isText()) return total; + + while (true) { + if (lastMessageID) { + messages = await channel.messages.fetch({ + limit: 100, + before: lastMessageID, + }); + } else { + messages = await channel.messages.fetch({ + limit: 100, + }); + } + + if (messages.size === 0) break; + + lastMessageID = messages.last().id; + console.log(`#${channel.name} : ${total.length}`); + total.push(...messages.array()); + } + + return total; +} + +/** + * Fetch all the messages from a Discord Guild. + * @param {module:"discord.js".Client} client - Your Discord.js Client. + * @param {string} guildID - The ID of the Guild to be fetch. + * @returns {Promise} - All the messages fetched. + * @deprecated Use {@link Fetcher} class instead. + */ +async function fetchGuildMessages(client, guildID) { + const total = []; + const channels = client.guilds.cache.get(guildID).channels.cache.filter(c => c.isText()); + console.log( + `Getting the messages from these channels : ${channels + .map(c => `#${c.name}`) + .sort() + .join('\n')}` + ); + + for (const textChannel of channels.array()) { + console.log(`Getting messages from : #${textChannel.name}.`); + const messages = await fetchChannelMessages(client, textChannel); + + if (!total.find(channel => channel.id === textChannel.id)) + total.push({ + id: textChannel.id, + messages: [], + }); + total.find(channel => channel.id === textChannel.id).messages.push(...messages.map(m => m.cleanContent)); + } + + console.log(`Finished fetching messages, messages count: ${total.length}`); + return total; +} + +deprecate(fetchChannelMessages, 'fetchChannelMessages() is deprecated. Use Fetcher class instead.'); +deprecate(fetchGuildMessages, 'fetchGuildMessages() is deprecated. Use Fetcher class instead.'); + +module.exports = { + fetchGuildMessages, + fetchChannelMessages, +}; diff --git a/tsconfig.json b/tsconfig.json index c1578a5..7e7ee71 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,7 @@ { "compilerOptions": { "alwaysStrict": true, + "allowJs": true, "declaration": true, "declarationDir": "types", "lib": ["ESNext"], @@ -9,10 +10,10 @@ "noImplicitAny": true, "outDir": "dist", "pretty": true, - "rootDir": "src", "skipLibCheck": true, "strict": true, "target": "esNext" }, - "exclude": ["node_modules"] + "exclude": ["node_modules"], + "files": ["src/main.js", "src/index.ts", "src/Fetcher.ts"] } diff --git a/types/Fetcher.d.ts b/types/Fetcher.d.ts index fb97616..63c4b62 100644 --- a/types/Fetcher.d.ts +++ b/types/Fetcher.d.ts @@ -1,9 +1,14 @@ /// import { Client, Message, Collection, Snowflake, TextChannel, Guild } from 'discord.js'; import { EventEmitter } from 'events'; +/** + * The main class used to fetch things. + */ export declare class Fetcher extends EventEmitter { readonly client: Client; - totalMessages: Collection; + /** + * A simple property set to `true` when the Fetcher is fetching a bulk of messages, then set to false. + */ fetching: boolean; constructor(client: Client); fetchChannel(channelID: Snowflake | TextChannel): Promise>; diff --git a/types/index.d.ts b/types/index.d.ts new file mode 100644 index 0000000..65706bf --- /dev/null +++ b/types/index.d.ts @@ -0,0 +1,2 @@ +export { fetchChannelMessages, fetchGuildMessages } from './main.js'; +export { Fetcher } from './Fetcher'; From f72f382d47a9ede9d1cd5bc14a35e4da2d8433c4 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 16:04:09 +0100 Subject: [PATCH 15/18] Added documentation for Fetcher & remade README. --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cb7ad24..1f23eef 100644 --- a/README.md +++ b/README.md @@ -15,11 +15,28 @@ These operations took a long time to process. Be patient and avoid doing this of ```js -const fetchedGuildMessages = await require('discord-fetch-messages').fetchGuildMessages(client, id); +const fetcher = new Fetcher(client); -const fetchedChannelMessages = await require('discord-fetch-messages').fetchChannelMessages(client, id); +fetcher.on('fetchChannel', async channel => { + await message.channel.send(`Fetching <#${channel.id}>.`); +}); + +await fetcher.fetchGuild(guildID); +await fetcher.fetchChannel(channel); ``` -Note : +## + +## Event list : + +| Event Name | Description | Arguments | +| -------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------ | +| `fetchChannel` | Emitter when fetching a Channel. | `channel`: The channel fetched. | +| `fetchGuild` | Emitted when fetching a Guild. | `guild`: The guild fetched. | +| `fetch` | Emitted after fetched a bulk a of messages, can fetch 0 to 100 messages. | `length`: The number of messages fetched.
`messages`: The messages fetched, as a Collection. | + +You can also use a `fetching` boolean property that is set to true when fetching a bulk of message, then to false. +> Note : +> > Node.js > 14 is required. From 193f2bb67750b3dbd70f43b5ca20639811226cf5 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 16:05:20 +0100 Subject: [PATCH 16/18] Remade .gitignore. --- .gitignore | 110 +++--------------------------------------------- types/main.d.ts | 41 ++++++++++-------- 2 files changed, 29 insertions(+), 122 deletions(-) diff --git a/.gitignore b/.gitignore index 4ae68c0..63629cf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,107 +1,9 @@ -# Logs -logs -*.log -npm-debug.log* -yarn-debug.log* -yarn-error.log* -lerna-debug.log* - -# Diagnostic reports (https://nodejs.org/api/report.html) -report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json - -# Runtime data -pids -*.pid -*.seed -*.pid.lock - -# Directory for instrumented libs generated by jscoverage/JSCover -lib-cov - -# Coverage directory used by tools like istanbul -coverage -*.lcov - -# nyc test coverage -.nyc_output - -# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) -.grunt - -# Bower dependency directory (https://bower.io/) -bower_components - -# node-waf configuration -.lock-wscript - -# Compiled binary addons (https://nodejs.org/api/addons.html) -build/Release - -# Dependency directories -node_modules/ -jspm_packages/ - -# TypeScript v1 declaration files -typings/ - -# TypeScript cache -*.tsbuildinfo - -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Microbundle cache -.rpt2_cache/ -.rts2_cache_cjs/ -.rts2_cache_es/ -.rts2_cache_umd/ - -# Optional REPL history -.node_repl_history - -# Output of 'npm pack' -*.tgz - -# Yarn Integrity file -.yarn-integrity - -# dotenv environment variables file -.env -.env.test - -# parcel-bundler cache (https://parceljs.org/) -.cache - -# Next.js build output -.next +# JetBrains +.idea -# Nuxt.js build / generate output -.nuxt +# Compilation dist +types -# Gatsby files -.cache/ -# Comment in the public line in if your project uses Gatsby and *not* Next.js -# https://nextjs.org/blog/next-9-1#public-directory-support -# public - -# vuepress build output -.vuepress/dist - -# Serverless directories -.serverless/ - -# FuseBox cache -.fusebox/ - -# DynamoDB Local files -.dynamodb/ - -# TernJS port file -.tern-port - -# JetBrains -.idea +# Node +node_modules diff --git a/types/main.d.ts b/types/main.d.ts index 252591e..541f028 100644 --- a/types/main.d.ts +++ b/types/main.d.ts @@ -1,18 +1,23 @@ -import * as Discord from 'discord.js'; - -declare module 'discord-fetch-messages' { - /** - * Fetch all the messages from a Discord Guild. - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {string} guildID - The ID of the Guild to be fetch. - * @returns {Promise} - All the messages fetched. - */ - export function fetchGuildMessages(client: Discord.Client, guildID: Discord.Snowflake): Promise; - /** - * Fetch all the messages from a Discord TextChannel. - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. - * @returns {Promise} - All the messages fetched. - */ - export function fetchChannelMessages(client: Discord.Client, channel: Discord.Channel): Promise; -} +/** + * Fetch all the messages from a Discord Guild. + * @param {module:"discord.js".Client} client - Your Discord.js Client. + * @param {string} guildID - The ID of the Guild to be fetch. + * @returns {Promise} - All the messages fetched. + * @deprecated Use {@link Fetcher} class instead. + */ +export function fetchGuildMessages(client: any, guildID: string): Promise<{ + "\"C:/Users/Pierre/WebstormProjects/libs/discord-fetch/src/main\"": typeof import("./main"); +}>; +/** + * Fetch all the messages from a Discord TextChannel. + * + * @param {module:"discord.js".Client} client - Your Discord.js Client. + * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. + * @returns {Promise} - All the messages fetched. + * @deprecated Use {@link Fetcher} class instead. + */ +export function fetchChannelMessages(client: any, channel: any): Promise<{ + "\"C:/Users/Pierre/WebstormProjects/libs/discord-fetch/src/main\"": typeof import("./main"); +}>; +declare namespace __C__Users_Pierre_WebstormProjects_libs_discord_fetch_src_main_ { } +export {}; From 86472a05991f5d487ae84be0c35d2d51cfc2ba75 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 16:05:40 +0100 Subject: [PATCH 17/18] Removed types form git. --- types/Fetcher.d.ts | 16 ---------------- types/index.d.ts | 2 -- types/main.d.ts | 23 ----------------------- 3 files changed, 41 deletions(-) delete mode 100644 types/Fetcher.d.ts delete mode 100644 types/index.d.ts delete mode 100644 types/main.d.ts diff --git a/types/Fetcher.d.ts b/types/Fetcher.d.ts deleted file mode 100644 index 63c4b62..0000000 --- a/types/Fetcher.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -/// -import { Client, Message, Collection, Snowflake, TextChannel, Guild } from 'discord.js'; -import { EventEmitter } from 'events'; -/** - * The main class used to fetch things. - */ -export declare class Fetcher extends EventEmitter { - readonly client: Client; - /** - * A simple property set to `true` when the Fetcher is fetching a bulk of messages, then set to false. - */ - fetching: boolean; - constructor(client: Client); - fetchChannel(channelID: Snowflake | TextChannel): Promise>; - fetchGuild(guildID: Snowflake | Guild): Promise>; -} diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index 65706bf..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,2 +0,0 @@ -export { fetchChannelMessages, fetchGuildMessages } from './main.js'; -export { Fetcher } from './Fetcher'; diff --git a/types/main.d.ts b/types/main.d.ts deleted file mode 100644 index 541f028..0000000 --- a/types/main.d.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Fetch all the messages from a Discord Guild. - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {string} guildID - The ID of the Guild to be fetch. - * @returns {Promise} - All the messages fetched. - * @deprecated Use {@link Fetcher} class instead. - */ -export function fetchGuildMessages(client: any, guildID: string): Promise<{ - "\"C:/Users/Pierre/WebstormProjects/libs/discord-fetch/src/main\"": typeof import("./main"); -}>; -/** - * Fetch all the messages from a Discord TextChannel. - * - * @param {module:"discord.js".Client} client - Your Discord.js Client. - * @param {module:"discord.js".TextChannel | module:"discord.js".NewsChannel} channel - The ID of the Discord TextChannel. - * @returns {Promise} - All the messages fetched. - * @deprecated Use {@link Fetcher} class instead. - */ -export function fetchChannelMessages(client: any, channel: any): Promise<{ - "\"C:/Users/Pierre/WebstormProjects/libs/discord-fetch/src/main\"": typeof import("./main"); -}>; -declare namespace __C__Users_Pierre_WebstormProjects_libs_discord_fetch_src_main_ { } -export {}; From 65ee7f2b1f5470ef28aa8c045242cea690709604 Mon Sep 17 00:00:00 2001 From: Ayfri Date: Sun, 24 Jan 2021 16:06:03 +0100 Subject: [PATCH 18/18] Added src to .npmignore. --- .npmignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.npmignore b/.npmignore index ba1bce0..40f872f 100644 --- a/.npmignore +++ b/.npmignore @@ -1,2 +1,3 @@ .github .idea +src