feat: api v9 and threads (#5570)

Co-authored-by: Noel <icrawltogo@gmail.com>
Co-authored-by: Amish Shah <dev@shah.gg>
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
Co-authored-by: SynthGhost <60333233+synthghost@users.noreply.github.com>
Co-authored-by: SpaceEEC <24881032+SpaceEEC@users.noreply.github.com>
Co-authored-by: Elliot <elliot@maisl.fr>
Co-authored-by: Antonio Román <kyradiscord@gmail.com>
Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com>
This commit is contained in:
ckohen
2021-06-24 12:48:29 -07:00
committed by GitHub
parent ea49f7ca74
commit 7346621d15
34 changed files with 1461 additions and 24 deletions

View File

@@ -0,0 +1,112 @@
'use strict';
const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors');
const ThreadMember = require('../structures/ThreadMember');
const Collection = require('../util/Collection');
/**
* Manages API methods for GuildMembers and stores their cache.
* @extends {BaseManager}
*/
class ThreadMemberManager extends BaseManager {
constructor(thread, iterable) {
super(thread.client, iterable, ThreadMember);
/**
* The thread this manager belongs to
* @type {ThreadChannel}
*/
this.thread = thread;
}
/**
* The cache of this Manager
* @type {Collection<Snowflake, ThreadMember>}
* @name ThreadMemberManager#cache
*/
_add(data, cache = true) {
const existing = this.cache.get(data.user_id);
if (cache) existing?._patch(data);
if (existing) return existing;
const member = new ThreadMember(this.thread, data);
if (cache) this.cache.set(data.user_id, member);
return member;
}
/**
* Data that resolves to give a ThreadMember object. This can be:
* * A ThreadMember object
* * A User resolvable
* @typedef {ThreadMember|UserResolvable} ThreadMemberResolvable
*/
/**
* Resolves a ThreadMemberResolvable to a ThreadMember object.
* @param {ThreadMemberResolvable} member The user that is part of the thread
* @returns {?GuildMember}
*/
resolve(member) {
const memberResolvable = super.resolve(member);
if (memberResolvable) return memberResolvable;
const userResolvable = this.client.users.resolveID(member);
if (userResolvable) return super.resolve(userResolvable);
return null;
}
/**
* Resolves a ThreadMemberResolvable to a thread member ID string.
* @param {ThreadMemberResolvable} member The user that is part of the guild
* @returns {?Snowflake}
*/
resolveID(member) {
const memberResolvable = super.resolveID(member);
if (memberResolvable) return memberResolvable;
const userResolvable = this.client.users.resolveID(member);
return this.cache.has(userResolvable) ? userResolvable : null;
}
/**
* Adds a member to the thread.
* @param {UserResolvable|'@me'} member The member to add
* @param {string} [reason] The reason for adding this member
* @returns {Promise<Snowflake>}
*/
add(member, reason) {
const id = member === '@me' ? member : this.client.users.resolveID(member);
if (!id) return Promise.reject(new TypeError('INVALID_TYPE', 'member', 'UserResolvable'));
return this.client.api
.channels(this.id, 'thread-members', id)
.put({ reason })
.then(() => id);
}
/**
* Remove a user from the thread.
* @param {Snowflake|'@me'} id The id of the member to remove
* @param {string} [reason] The reason for removing this member from the thread
* @returns {Promise<Snowflake>}
*/
remove(id, reason) {
return this.client.api
.channels(this.thread.id, 'thread-members', id)
.delete({ reason })
.then(() => id);
}
/**
* 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>>}
*/
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());
}
}
module.exports = ThreadMemberManager;