Skip to content

Commit

Permalink
feat(ThreadMemberManager): allow individual members to be fetched (#6889
Browse files Browse the repository at this point in the history
)

Co-authored-by: ckohen <chaikohen@gmail.com>
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
  • Loading branch information
5 people committed Oct 29, 2021
1 parent aa4d055 commit 14716df
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/publish-dev.yml
Expand Up @@ -17,7 +17,7 @@ jobs:
node-version: 16
registry-url: https://registry.npmjs.org/
cache: npm

- name: pre-release
id: pre-release
run: |
Expand Down
14 changes: 14 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 23 additions & 8 deletions src/managers/ThreadMemberManager.js
Expand Up @@ -92,17 +92,32 @@ class ThreadMemberManager extends CachedManager {
return id;
}

async _fetchOne(memberId, cache, force) {
if (!force) {
const existing = this.cache.get(memberId);
if (existing) return existing;
}

const data = await this.client.api.channels(this.thread.id, 'thread-members', memberId).get();
return this._add(data, cache);
}

async _fetchMany(cache) {
const raw = await this.client.api.channels(this.thread.id, 'thread-members').get();
return raw.reduce((col, member) => col.set(member.user_id, this._add(member, cache)), new Collection());
}

/**
* Fetches member(s) for the thread from Discord, requires access to the `GUILD_MEMBERS` gateway intent.
* @param {boolean} [cache=true] Whether or not to cache the fetched members
* @returns {Promise<Collection<Snowflake, ThreadMember>>}
* @param {UserResolvable|boolean} [member] The member to fetch. If `undefined`, all members
* in the thread are fetched, and will be cached based on `options.cache`. If boolean, this serves
* the purpose of `options.cache`.
* @param {BaseFetchOptions} [options] Additional options for this fetch
* @returns {Promise<ThreadMember|Collection<Snowflake, ThreadMember>>}
*/
async fetch(cache = true) {
const raw = await this.client.api.channels(this.thread.id, 'thread-members').get();
return raw.reduce((col, rawMember) => {
const member = this._add(rawMember, cache);
return col.set(member.id, member);
}, new Collection());
fetch(member, { cache = true, force = false } = {}) {
const id = this.resolveId(member);
return id ? this._fetchOne(id, cache, force) : this._fetchMany(member ?? cache);
}
}

Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Expand Up @@ -2946,6 +2946,8 @@ export class ThreadMemberManager extends CachedManager<Snowflake, ThreadMember,
private constructor(thread: ThreadChannel, iterable?: Iterable<RawThreadMemberData>);
public thread: ThreadChannel;
public add(member: UserResolvable | '@me', reason?: string): Promise<Snowflake>;
public fetch(member?: UserResolvable, options?: BaseFetchOptions): Promise<ThreadMember>;
/** @deprecated Use `fetch(member, options)` instead. */
public fetch(cache?: boolean): Promise<Collection<Snowflake, ThreadMember>>;
public remove(id: Snowflake | '@me', reason?: string): Promise<Snowflake>;
}
Expand Down
10 changes: 9 additions & 1 deletion typings/tests.ts
Expand Up @@ -75,6 +75,7 @@ import {
TextBasedChannels,
TextChannel,
ThreadChannel,
ThreadMember,
Typing,
User,
VoiceChannel,
Expand Down Expand Up @@ -446,10 +447,17 @@ client.on('ready', async () => {
// This is to check that stuff is the right type
declare const assertIsPromiseMember: (m: Promise<GuildMember>) => void;

client.on('guildCreate', g => {
client.on('guildCreate', async g => {
const channel = g.channels.cache.random();
if (!channel) return;

if (channel.isThread()) {
const fetchedMember = await channel.members.fetch('12345678');
assertType<ThreadMember>(fetchedMember);
const fetchedMemberCol = await channel.members.fetch(true);
assertType<Collection<Snowflake, ThreadMember>>(fetchedMemberCol);
}

channel.setName('foo').then(updatedChannel => {
console.log(`New channel name: ${updatedChannel.name}`);
});
Expand Down

0 comments on commit 14716df

Please sign in to comment.