-
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.
- Loading branch information
Showing
1 changed file
with
56 additions
and
2 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,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<Snowflake, Message>; | ||
public fetching: boolean; | ||
|
||
public constructor(public readonly client: Client) { | ||
super(); | ||
this.totalMessages = new Collection(); | ||
this.fetching = false; | ||
} | ||
|
||
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>(); | ||
|
||
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<Collection<Snowflake, Message>> { | ||
const guild = guildID instanceof Guild ? guildID : await this.client.guilds.fetch(guildID); | ||
let messages = new Collection<Snowflake, Message>(); | ||
if (guild) { | ||
this.fetching = true; | ||
const channels = guild.channels.cache.filter(c => c.isText()) as Collection<Snowflake, TextChannel>; | ||
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; | ||
} | ||
} |