-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change logs: + New Event: > relationshipAdd: user.id, type > relationshipRemove: user.id > client.relationships: RelationshipsManager > User.relationships > Update Document .-. - DEPRECATED > client.blocked > client.friends > clientUser.findFriend > User.blocked > User.friend > some console.log()
- Loading branch information
1 parent
89d8b08
commit f73525f
Showing
14 changed files
with
297 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict'; | ||
|
||
const { Events } = require('../../../util/Constants'); | ||
|
||
module.exports = (client, { d: data }) => { | ||
data.user ? client.users._add(data.user) : null; | ||
client.relationships.cache.set(data.id, data.type); | ||
/** | ||
* Emitted whenever a relationship is updated. | ||
* @event Client#relationshipUpdate | ||
* @param {UserID} user The userID that was updated | ||
* @param {Number} type The new relationship type | ||
*/ | ||
client.emit(Events.RELATIONSHIP_ADD, data.id, data.type); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
|
||
const { Events } = require('../../../util/Constants'); | ||
|
||
module.exports = (client, { d: data }) => { | ||
client.relationships.cache.delete(data.id); | ||
/** | ||
* Emitted whenever a relationship is updated. | ||
* @event Client#relationshipUpdate | ||
* @param {UserID} user The userID that was updated | ||
*/ | ||
client.emit(Events.RELATIONSHIP_REMOVE, data.id); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
'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'); | ||
|
||
/** | ||
* Manages API methods for users and stores their cache. | ||
*/ | ||
class RelationshipsManager { | ||
constructor(client, users) { | ||
this.client = client; | ||
this.cache = new Collection(); | ||
this._setup(users); | ||
} | ||
|
||
_setup(users) { | ||
if (!Array.isArray(users)) return; | ||
for (const relationShip of users) { | ||
this.cache.set(relationShip.id, relationShip.type); | ||
} | ||
} | ||
|
||
/** | ||
* 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; | ||
} | ||
|
||
/** | ||
* 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>} | ||
*/ | ||
async fetch(user, { cache = true, force = false } = {}) { | ||
const id = this.resolveId(user); | ||
if (!force) { | ||
const existing = this.cache.get(id); | ||
if (existing && !existing.partial) return existing; | ||
} | ||
|
||
const data = await this.client.api.users['@me'].relationships.get(); | ||
await this._setup(data); | ||
return this.cache.get(id); | ||
} | ||
|
||
// some option .-. | ||
|
||
async deleteFriend(user) { | ||
const id = this.resolveId(user); | ||
// check if already friends | ||
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; | ||
} | ||
|
||
async deleteBlocked(user) { | ||
const id = this.resolveId(user); | ||
// check if already blocked | ||
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; | ||
} | ||
|
||
async sendFriendRequest(username, discriminator) { | ||
await this.client.api.users('@me').relationships.post({ | ||
data: { | ||
username, | ||
discriminator: parseInt(discriminator), | ||
}, | ||
}); | ||
return true; | ||
} | ||
|
||
async addFriend(user) { | ||
const id = this.resolveId(user); | ||
// check if already friends | ||
if (this.cache.get(id) === RelationshipTypes.FRIEND) return false; | ||
// check if outgoing request | ||
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; | ||
} | ||
|
||
async addBlocked(user) { | ||
const id = this.resolveId(user); | ||
// check | ||
if (this.cache.get(id) === RelationshipTypes.BLOCKED) return false; | ||
await this.client.api | ||
.users['@me'].relationships[id].put({ | ||
data: { | ||
type: RelationshipTypes.BLOCKED, | ||
}, | ||
}); | ||
return true; | ||
} | ||
} | ||
|
||
module.exports = RelationshipsManager; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.