2022-04-12 05:46:08 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Collection } = require('@discordjs/collection');
|
|
|
|
const { GuildMember } = require('../structures/GuildMember');
|
|
|
|
const { Message } = require('../structures/Message');
|
|
|
|
const ThreadMember = require('../structures/ThreadMember');
|
|
|
|
const User = require('../structures/User');
|
|
|
|
const { RelationshipTypes } = require('../util/Constants');
|
|
|
|
|
|
|
|
/**
|
2022-04-26 05:41:31 +00:00
|
|
|
* Manages API methods for Relationships and stores their cache.
|
2022-04-12 05:46:08 +00:00
|
|
|
*/
|
|
|
|
class RelationshipsManager {
|
2022-04-16 10:44:43 +00:00
|
|
|
constructor(client, users) {
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
|
|
|
* The client that instantiated this manager.
|
|
|
|
* @type {Client}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
this.client = client;
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
2022-06-11 13:17:27 +00:00
|
|
|
* A collection of users this manager is caching. (Type: Number)
|
2022-06-11 13:13:52 +00:00
|
|
|
* @type {Collection<Snowflake, RelationshipTypes>}
|
|
|
|
* @readonly
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
this.cache = new Collection();
|
|
|
|
this._setup(users);
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-04-26 05:41:31 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {Array<User>} users An array of users to add to the cache
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
_setup(users) {
|
|
|
|
if (!Array.isArray(users)) return;
|
|
|
|
for (const relationShip of users) {
|
|
|
|
this.cache.set(relationShip.id, relationShip.type);
|
2022-04-12 05:46:08 +00:00
|
|
|
}
|
2022-04-16 10:44:43 +00:00
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-04-16 10:44:43 +00:00
|
|
|
/**
|
|
|
|
* Resolves a {@link UserResolvable} to a {@link User} id.
|
|
|
|
* @param {UserResolvable} user The UserResolvable to identify
|
|
|
|
* @returns {?Snowflake}
|
|
|
|
*/
|
|
|
|
resolveId(user) {
|
|
|
|
if (user instanceof ThreadMember) return user.id;
|
|
|
|
if (user instanceof GuildMember) return user.user.id;
|
|
|
|
if (user instanceof Message) return user.author.id;
|
|
|
|
if (user instanceof User) return user.id;
|
|
|
|
return user;
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-04-16 10:44:43 +00:00
|
|
|
/**
|
|
|
|
* Obtains a user from Discord, or the user cache if it's already available.
|
|
|
|
* @param {UserResolvable} user The user to fetch
|
|
|
|
* @param {BaseFetchOptions} [options] Additional options for this fetch
|
|
|
|
* @returns {Promise<User>}
|
|
|
|
*/
|
2022-04-16 12:01:05 +00:00
|
|
|
async fetch(user, { force = false } = {}) {
|
2022-04-16 10:44:43 +00:00
|
|
|
const id = this.resolveId(user);
|
|
|
|
if (!force) {
|
|
|
|
const existing = this.cache.get(id);
|
|
|
|
if (existing && !existing.partial) return existing;
|
2022-04-12 05:46:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-16 10:44:43 +00:00
|
|
|
const data = await this.client.api.users['@me'].relationships.get();
|
|
|
|
await this._setup(data);
|
|
|
|
return this.cache.get(id);
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-04-16 12:01:05 +00:00
|
|
|
// Some option .-.
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
|
|
|
* Deletes a friend relationship with a client user.
|
|
|
|
* @param {User} user Target
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
async deleteFriend(user) {
|
|
|
|
const id = this.resolveId(user);
|
2022-04-16 12:01:05 +00:00
|
|
|
// Check if already friends
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.cache.get(id) !== RelationshipTypes.FRIEND) return false;
|
|
|
|
await this.client.api.users['@me'].relationships[id].delete(); // 204 status and no data
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
|
|
|
* Deletes a blocked relationship with a client user.
|
|
|
|
* @param {User} user Target
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
async deleteBlocked(user) {
|
|
|
|
const id = this.resolveId(user);
|
2022-04-16 12:01:05 +00:00
|
|
|
// Check if already blocked
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.cache.get(id) !== RelationshipTypes.BLOCKED) return false;
|
|
|
|
await this.client.api.users['@me'].relationships[id].delete(); // 204 status and no data
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
|
|
|
* Sends a friend request.
|
|
|
|
* @param {string} username Username of the user to send the request to
|
|
|
|
* @param {number} discriminator Discriminator of the user to send the request to
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
async sendFriendRequest(username, discriminator) {
|
|
|
|
await this.client.api.users('@me').relationships.post({
|
|
|
|
data: {
|
|
|
|
username,
|
|
|
|
discriminator: parseInt(discriminator),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
|
|
|
* Accepts a friend request.
|
|
|
|
* @param {UserResolvable} user The user to add as a friend
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
async addFriend(user) {
|
|
|
|
const id = this.resolveId(user);
|
2022-04-16 12:01:05 +00:00
|
|
|
// Check if already friends
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.cache.get(id) === RelationshipTypes.FRIEND) return false;
|
2022-04-16 12:01:05 +00:00
|
|
|
// Check if outgoing request
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.cache.get(id) === RelationshipTypes.OUTGOING_REQUEST) return false;
|
|
|
|
await this.client.api.users['@me'].relationships[id].put({
|
|
|
|
data: {
|
|
|
|
type: RelationshipTypes.FRIEND,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-11 13:13:52 +00:00
|
|
|
/**
|
|
|
|
* Blocks a user.
|
|
|
|
* @param {UserResolvable} user User to block
|
|
|
|
* @returns {Promise<boolean>}
|
|
|
|
*/
|
2022-04-16 10:44:43 +00:00
|
|
|
async addBlocked(user) {
|
|
|
|
const id = this.resolveId(user);
|
2022-04-16 12:01:05 +00:00
|
|
|
// Check
|
2022-04-16 10:44:43 +00:00
|
|
|
if (this.cache.get(id) === RelationshipTypes.BLOCKED) return false;
|
|
|
|
await this.client.api.users['@me'].relationships[id].put({
|
|
|
|
data: {
|
|
|
|
type: RelationshipTypes.BLOCKED,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
2022-04-12 05:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = RelationshipsManager;
|