Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ThreadMemberManager): allow individual members to be fetched #6889

Merged
merged 23 commits into from Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
36271b0
feat(ThreadMemberManager): allow individual members to be fetched
suneettipirneni Oct 25, 2021
1cfe884
chore: clean up definitions
suneettipirneni Oct 26, 2021
34fc819
fix: boolean bug
suneettipirneni Oct 26, 2021
04d67a8
fix: reduce fetch many lines
suneettipirneni Oct 26, 2021
f7690b0
Update src/managers/ThreadMemberManager.js
suneettipirneni Oct 26, 2021
1162b5c
docs: improve docs for member
suneettipirneni Oct 26, 2021
ca49f04
Update src/managers/ThreadMemberManager.js
suneettipirneni Oct 26, 2021
efa2001
fix: code suggestion
suneettipirneni Oct 26, 2021
48b0219
types: use UserResolvable as parameter type
suneettipirneni Oct 26, 2021
900dde2
types: fix optional type
suneettipirneni Oct 26, 2021
704e705
docs: mark old fetch as deprecated
suneettipirneni Oct 26, 2021
e1b653c
docs: use one line jsdoc
suneettipirneni Oct 26, 2021
652fcfa
Update src/managers/ThreadMemberManager.js
suneettipirneni Oct 26, 2021
5c4a43e
Update typings/index.d.ts
suneettipirneni Oct 26, 2021
b76b1e3
Update src/managers/ThreadMemberManager.js
suneettipirneni Oct 26, 2021
62a25f9
chore: fix linting issues
suneettipirneni Oct 26, 2021
9c54d05
fix: typeof check
suneettipirneni Oct 26, 2021
3d8ebff
chore: bump builders
suneettipirneni Oct 26, 2021
841803e
Update src/managers/ThreadMemberManager.js
suneettipirneni Oct 27, 2021
9754fbb
Merge branch 'main' of https://github.com/discordjs/discord.js into f…
suneettipirneni Oct 29, 2021
699ad1a
Merge branch 'feat/thread-member-fetching' of https://github.com/sune…
suneettipirneni Oct 29, 2021
780b9df
fix: make requested changes
suneettipirneni Oct 29, 2021
4c17082
Update src/managers/ThreadMemberManager.js
suneettipirneni Oct 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/managers/ThreadMemberManager.js
Expand Up @@ -92,17 +92,38 @@ class ThreadMemberManager extends CachedManager {
return id;
}

async _fetchId(memberId, cache, force) {
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
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.id, this._add(member, cache)), new Collection());
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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 } = {}) {
// TODO: Replace `member` usages as `cache` with correct respective parameter.
if (typeof member === 'string') {
return this._fetchId(member, cache, force);
} else if (member && typeof member === 'object') {
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
return this._fetchId(member.id, cache, force);
}

return this._fetchMany(member ?? cache);
}
}

Expand Down
2 changes: 2 additions & 0 deletions typings/index.d.ts
Expand Up @@ -2897,6 +2897,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>>;
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
public remove(id: Snowflake | '@me', reason?: string): Promise<Snowflake>;
}
Expand Down
10 changes: 9 additions & 1 deletion typings/tests.ts
Expand Up @@ -70,6 +70,7 @@ import {
TextBasedChannels,
TextChannel,
ThreadChannel,
ThreadMember,
Typing,
User,
VoiceChannel,
Expand Down Expand Up @@ -434,10 +435,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