Skip to content

Commit

Permalink
refactor: Update code to newer TS, fix typos.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayfri committed Oct 15, 2022
1 parent 484c2b1 commit e7f657f
Showing 1 changed file with 47 additions and 40 deletions.
87 changes: 47 additions & 40 deletions src/Fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import {Client, Collection, Guild, Message, NewsChannel, Permissions, Snowflake, TextChannel, ThreadChannel} from 'discord.js';
//noinspection JSUnusedGlobalSymbols

import {AnyChannel, Channel, Client, Collection, Guild, Message, NewsChannel, Permissions, Snowflake, TextChannel, ThreadChannel} from 'discord.js';
import {EventEmitter} from 'events';

export interface Events {
fetch: [size: number, messages: Collection<Snowflake, Message>];
fetchChannel: [channel: FetchChannel];
fetchChannel: [channel: FetchableChannel];
fetchGuild: [guild: Guild];
fetchThread: [thread: ThreadChannel, parentChannel: FetchChannel | null];
fetchThread: [thread: ThreadChannel, parentChannel: FetchableChannel | null];
}

/**
* A fetchable TextChannel.
*/
export type FetchChannel = NewsChannel | TextChannel;
export type FetchableChannel = NewsChannel | TextChannel;

type Nullable<T> = T | null;

function isFetchChannel(channel: any): channel is TextChannel | NewsChannel {
function isFetchChannel(channel: Nullable<AnyChannel>): channel is TextChannel | NewsChannel {
return channel instanceof TextChannel || channel instanceof NewsChannel;
}

Expand All @@ -36,24 +40,24 @@ export class Fetcher extends EventEmitter {
this.fetching = false;
}

public on<K extends keyof Events>(event: K, listener: (...args: Events[K]) => void) {
return super.on(event, listener as (...args: any[]) => void);
public override emit<K extends keyof Events>(event: K, ...args: Events[K]) {
return super.emit(event, args);
}

public once<K extends keyof Events>(event: K, listener: (...args: Events[K]) => void) {
return super.on(event, listener as (...args: any[]) => void);
public override eventNames() {
return super.eventNames() as Array<keyof Events>;
}

public emit<K extends keyof Events>(event: K, ...args: Events[K]) {
return super.emit(event, args);
public override off<K extends keyof Events>(event: K, listener: (...args: Events[K]) => void) {
return super.off(event, listener as (...args: any[]) => void);
}

public eventNames() {
return super.eventNames() as Array<keyof Events>;
public override on<K extends keyof Events>(event: K, listener: (...args: Events[K]) => void) {
return super.on(event, listener as (...args: any[]) => void);
}

public off<K extends keyof Events>(event: K, listener: (...args: Events[K]) => void) {
return super.off(event, listener as (...args: any[]) => void);
public override once<K extends keyof Events>(event: K, listener: (...args: Events[K]) => void) {
return super.on(event, listener as (...args: any[]) => void);
}

/**
Expand All @@ -63,7 +67,7 @@ export class Fetcher extends EventEmitter {
* @param threads - If set to `true` it will fetch its threads, for now it will only fetch the active threads.
* @returns - The messages fetched.
*/
public async fetchChannel(channelID: Snowflake | FetchChannel, threads: boolean = false) {
public async fetchChannel(channelID: Snowflake | FetchableChannel, threads: boolean = false) {
const channel = typeof channelID === 'string' ? await this.client.channels.fetch(channelID) : channelID;
let messages = new Collection<Snowflake, Message>();

Expand Down Expand Up @@ -101,7 +105,7 @@ export class Fetcher extends EventEmitter {
* @param threads - If set to `true` it will fetch all the threads of all the channels, for now it will only fetch the active threads.
* @returns - The messages fetched.
*/
public async fetchChannels(channels: Array<Snowflake | FetchChannel> | Collection<Snowflake, FetchChannel>, threads: boolean = false) {
public async fetchChannels(channels: Array<Snowflake | FetchableChannel> | Collection<Snowflake, FetchableChannel>, threads: boolean = false) {
if (channels instanceof Collection) channels = [...channels.values()];
let messages = new Collection<Snowflake, Message>();

Expand All @@ -119,7 +123,7 @@ export class Fetcher extends EventEmitter {
* 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.
* Can be quite long, you should instead use events than waiting for it to finish.
*
* @param guildID - The guild to fetch, can be an ID or a Guild.
* @param threads - If set to `true` it will fetch all the threads of the guild, for now it will only fetch the active threads.
Expand All @@ -129,7 +133,7 @@ export class Fetcher extends EventEmitter {
const guild = guildID instanceof Guild ? guildID : await this.client.guilds.fetch(guildID);
let messages = new Collection<Snowflake, Message>();
if (guild) {
const channels = guild.channels.cache.filter(c => c.isText() && !c.isThread()) as Collection<Snowflake, FetchChannel>;
const channels = guild.channels.cache.filter(c => c.isText() && !c.isThread()) as Collection<Snowflake, FetchableChannel>;
this.emit('fetchGuild', guild);
messages = await this.fetchChannels(channels, threads);
}
Expand All @@ -143,7 +147,7 @@ export class Fetcher extends EventEmitter {
* Fetch all the guilds provided, if not all the guilds in the cache of the {@link client}.
*
* @remarks
* Can be really long and you should prefer using events than waiting for it to finish.
* Can be quite long, you should instead use events than waiting for it to finish.
*
* @param guilds - The guilds to fetch, if omitted it will fetch all the guilds cached by the client.
* @param threads - If set to `true` it will fetch all the threads of the guilds, for now it will only fetch the active threads.
Expand All @@ -162,24 +166,25 @@ export class Fetcher extends EventEmitter {
}

public async fetchThread(thread: ThreadChannel): Promise<Collection<Snowflake, Message>>;
public async fetchThread(threadID: Snowflake, channelID: Snowflake | FetchChannel): Promise<Collection<Snowflake, Message>>;
public async fetchThread(threadID: Snowflake, channelID: Snowflake | FetchableChannel): Promise<Collection<Snowflake, Message>>;
/**
* Fetch the entire list of messages from a Thread.
*
* @remarks
* If the thread is private, the client need the `MANAGE_THREADS` permissions.
*
* @param threadID - The thread ID or Thread itself, if an ID is provided you have to set the second paramter.
* @param threadID - The thread ID or Thread itself, if an ID is provided you have to set the second parameter.
* @param channelID - The channel ID or Channel itself of the Thread, only necessary if you provide a Thread ID, else it is not used.
* @returns The messages fetched.
*/
public async fetchThread(threadID: Snowflake | ThreadChannel, channelID?: Snowflake | FetchChannel) {
public async fetchThread(threadID: Snowflake | ThreadChannel, channelID?: Snowflake | FetchableChannel) {
let messages = new Collection<Snowflake, Message>();

if (typeof threadID === 'string' && !channelID) throw Error('channelID is required when using ThreadID.');
let thread: ThreadChannel | null = null;
if (threadID instanceof ThreadChannel) thread = threadID;
else if (channelID) {
let thread: Nullable<ThreadChannel> = null;
if (threadID instanceof ThreadChannel) {
thread = threadID;
} else if (channelID) {
const channel = typeof channelID === 'string' ? await this.client.channels.fetch(channelID) : channelID;
if (isFetchChannel(channel)) thread = await channel.threads.fetch(threadID);
else return messages;
Expand Down Expand Up @@ -212,25 +217,25 @@ export class Fetcher extends EventEmitter {
}

public async fetchThreads(channel: Guild): Promise<Collection<Snowflake, Message>>;
public async fetchThreads(channel: FetchChannel): Promise<Collection<Snowflake, Message>>;
public async fetchThreads(channel: FetchableChannel): Promise<Collection<Snowflake, Message>>;
public async fetchThreads(threadsIDs: Array<ThreadChannel> | Collection<Snowflake, ThreadChannel>): Promise<Collection<Snowflake, Message>>;
public async fetchThreads(threadsIDs: Array<Snowflake> | Collection<Snowflake, Snowflake>, channelID: Snowflake | FetchChannel): Promise<Collection<Snowflake, Message>>;
public async fetchThreads(threadsIDs: Array<Snowflake> | Collection<Snowflake, Snowflake>, channelID: Snowflake | FetchableChannel): Promise<Collection<Snowflake, Message>>;
/**
* Fetch the entire list of messages from multiple threads or all the threads of a channel or all threads of a guild.
* For now it will only fetch the active threads.
* For now, it will only fetch the active threads.
*
* @remarks
* If one of the thread is private, it will need the `MANAGE_THREADS` permission to be able to fetch its messages.
* Can be really long and you should prefer using events than waiting for it to finish.
* Can be quite long, you should instead use events than waiting for it to finish.
*
* @param threadsIDs - A list or a collection of threads or snowflakes to fetch messages from, if snowflakes are provided, you will need the second argument, or a channel where it will fetch all its channels, or a guild where it will fetch all its threads from all its channels.
* @param channelID - The channel ID or the Channel itself parent to all the threads passed as snowflakes, it will fetch the threads from this channel.
* @returns - All the messages fetched.
*/
public async fetchThreads(threadsIDs: Array<Snowflake | ThreadChannel> | Collection<Snowflake, Snowflake | ThreadChannel> | FetchChannel | Guild, channelID?: Snowflake | FetchChannel) {
public async fetchThreads(threadsIDs: Array<Snowflake | ThreadChannel> | Collection<Snowflake, Snowflake | ThreadChannel> | FetchableChannel | Guild, channelID?: Snowflake | FetchableChannel) {
let messages = new Collection<Snowflake, Message>();
let threads: Array<ThreadChannel> = [];
let channel: FetchChannel | null = null;
let channel: Nullable<FetchableChannel> = null;
if (channelID) {
const c = typeof channelID === 'string' ? await this.client.channels.fetch(channelID) : channelID;
if (isFetchChannel(c)) channel = c;
Expand All @@ -239,19 +244,21 @@ export class Fetcher extends EventEmitter {
async function resolveThread(thread: Snowflake | ThreadChannel) {
if (thread instanceof ThreadChannel) {
threads.push(thread);
} else {
if (channel) {
const t = await channel.threads.fetch(thread);
if (t) threads.push(t);
}
} else if (channel) {
const t = await channel.threads.fetch(thread);
if (t) threads.push(t);
}
}

if (threadsIDs instanceof Guild) {
threads = (await Promise.all((await threadsIDs.channels.fetch()).filter(isFetchChannel).map(async c => [...(await c.threads.fetch()).threads.values()]))).flat();
} else if (isFetchChannel(threadsIDs)) threads = [...(await threadsIDs.threads.fetch()).threads.values()];
else if (threadsIDs instanceof Collection) [...threadsIDs.values()].forEach(resolveThread);
else threadsIDs.forEach(resolveThread);
} else if (threadsIDs instanceof Channel && isFetchChannel(threadsIDs)) {
threads = [...(await threadsIDs.threads.fetch()).threads.values()];
} else if (threadsIDs instanceof Collection) {
[...threadsIDs.values()].forEach(resolveThread);
} else {
threadsIDs.forEach(resolveThread);
}

for (const thread of threads) {
const threadMessages = await this.fetchThread(thread);
Expand Down

0 comments on commit e7f657f

Please sign in to comment.