Skip to content

Commit

Permalink
Moved in many ways files to have proper exports.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayfri committed Jan 24, 2021
1 parent e3352cf commit e51b9fc
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 89 deletions.
82 changes: 1 addition & 81 deletions index.js
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');
34 changes: 29 additions & 5 deletions src/Fetcher.ts
Original file line number Diff line number Diff line change
@@ -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<Snowflake, Message>;
/**
* 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<Collection<Snowflake, Message>> {
const channel = channelID instanceof TextChannel ? channelID : await this.client.channels.fetch(channelID);
let messages = new Collection<Snowflake, Message>();
Expand All @@ -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<Collection<Snowflake, Message>> {
const guild = guildID instanceof Guild ? guildID : await this.client.guilds.fetch(guildID);
let messages = new Collection<Snowflake, Message>();
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { fetchChannelMessages, fetchGuildMessages } from './main.js';
export { Fetcher } from './Fetcher';
79 changes: 79 additions & 0 deletions src/main.js
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,
};
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"alwaysStrict": true,
"allowJs": true,
"declaration": true,
"declarationDir": "types",
"lib": ["ESNext"],
Expand All @@ -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"]
}
7 changes: 6 additions & 1 deletion types/Fetcher.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/// <reference types="node" />
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<Snowflake, Message>;
/**
* 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<Collection<Snowflake, Message>>;
Expand Down
2 changes: 2 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { fetchChannelMessages, fetchGuildMessages } from './main.js';
export { Fetcher } from './Fetcher';

0 comments on commit e51b9fc

Please sign in to comment.