-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved in many ways files to have proper exports.
- Loading branch information
Showing
7 changed files
with
122 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<module:"discord.js".Message[]>} - 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<module:"discord.js".Message[]>} - 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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { fetchChannelMessages, fetchGuildMessages } from './main.js'; | ||
export { Fetcher } from './Fetcher'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<module:"discord.js".Message[]>} - 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<module:"discord.js".Message[]>} - 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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { fetchChannelMessages, fetchGuildMessages } from './main.js'; | ||
export { Fetcher } from './Fetcher'; |