Skip to content

Commit

Permalink
1.3.3
Browse files Browse the repository at this point in the history
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
aiko-chan-ai committed Apr 12, 2022
1 parent 89d8b08 commit f73525f
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 163 deletions.
2 changes: 2 additions & 0 deletions Document/User.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ User {

Code:
```js
// You can use client.relationships to manage your friends and blocked users.
GuildMember.user.setFriend();
User.unFriend();
Message.member.user.sendFriendRequest();
Expand Down Expand Up @@ -208,6 +209,7 @@ And you can change the status 5 times every 20 seconds!
await client.user.setHypeSquad('HOUSE_BRAVERY');
await client.user.setHypeSquad('HOUSE_BRILLIANCE');
await client.user.setHypeSquad('HOUSE_BALANCE');
await client.user.setHypeSquad('LEAVE');
// Set Note to User
await user.setNote('Hello World');
// Set Username
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "discord.js-selfbot-v13",
"version": "1.3.2",
"version": "1.3.3",
"description": "A unofficial discord.js fork for creating selfbots [Based on discord.js v13]",
"main": "./src/index.js",
"types": "./typings/index.d.ts",
Expand Down
6 changes: 2 additions & 4 deletions src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ const Options = require('../util/Options');
const Permissions = require('../util/Permissions');
const Sweepers = require('../util/Sweepers');
// Patch
const FriendsManager = require('../managers/FriendsManager');
const BlockedManager = require('../managers/BlockedManager');
const RelationshipsManager = require('../managers/RelationshipsManager');
const ClientUserSettingManager = require('../managers/ClientUserSettingManager');

/**
Expand Down Expand Up @@ -129,8 +128,7 @@ class Client extends BaseClient {
/** Patch
*
*/
this.friends = new FriendsManager(this);
this.blocked = new BlockedManager(this);
this.relationships = new RelationshipsManager(this);
this.setting = new ClientUserSettingManager(this);
/**
* All of the guilds the client is currently handling, mapped by their ids -
Expand Down
17 changes: 3 additions & 14 deletions src/client/websocket/handlers/READY.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ module.exports = (client, { d: data }, shard) => {
// console.log(data);
if (client.options.checkUpdate) {
try {
checkUpdate()
checkUpdate();
} catch (e) {
console.log(e)
console.log(e);
}
};
client.session_id = data.session_id;
Expand All @@ -52,11 +52,6 @@ module.exports = (client, { d: data }, shard) => {
client.users.cache.set(client.user.id, client.user);
}

console.log(`
${chalk.yellow(
`Can you take a look at this notice and give me your opinion?\nhttps://github.com/aiko-chan-ai/discord.js-selfbot-v13/issues/29`,
)}`);

client.user.setAFK(false);

client.setting.fetch().then(async (res) => {
Expand Down Expand Up @@ -88,13 +83,7 @@ ${chalk.yellow(
client.guilds._add(guild);
}

for (const r of data.relationships) {
if (r.type == 1) {
client.friends.cache.set(r.id, new User(client, r.user));
} else if (r.type == 2) {
client.blocked.cache.set(r.id, new User(client, r.user));
}
}
client.relationships._setup(data.relationships);

shard.checkReady();
};
15 changes: 15 additions & 0 deletions src/client/websocket/handlers/RELATIONSHIP_ADD.js
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);
};
13 changes: 13 additions & 0 deletions src/client/websocket/handlers/RELATIONSHIP_REMOVE.js
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);
};
4 changes: 3 additions & 1 deletion src/client/websocket/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const handlers = Object.fromEntries([
['READY', require('./READY')],
['RESUMED', require('./RESUMED')],
['RELATIONSHIP_ADD', require('./RELATIONSHIP_ADD')],
['RELATIONSHIP_REMOVE', require('./RELATIONSHIP_REMOVE')],
['APPLICATION_COMMAND_CREATE', require('./APPLICATION_COMMAND_CREATE')],
['APPLICATION_COMMAND_DELETE', require('./APPLICATION_COMMAND_DELETE')],
['APPLICATION_COMMAND_UPDATE', require('./APPLICATION_COMMAND_UPDATE')],
Expand All @@ -16,7 +18,7 @@ const handlers = Object.fromEntries([
['GUILD_MEMBER_UPDATE', require('./GUILD_MEMBER_UPDATE')],
['GUILD_MEMBERS_CHUNK', require('./GUILD_MEMBERS_CHUNK')],
['GUILD_MEMBER_LIST_UPDATE', require('./GUILD_MEMBER_LIST_UPDATE.js')],
['GUILD_INTEGRATIONS_UPDATE', require('./GUILD_INTEGRATIONS_UPDATE')],
['GUILD_INTEGRATIONS_UPDATE', require('./GUILD_INTEGRATIONS_UPDATE')],
['GUILD_ROLE_CREATE', require('./GUILD_ROLE_CREATE')],
['GUILD_ROLE_DELETE', require('./GUILD_ROLE_DELETE')],
['GUILD_ROLE_UPDATE', require('./GUILD_ROLE_UPDATE')],
Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ exports.UserManager = require('./managers/UserManager');
exports.VoiceStateManager = require('./managers/VoiceStateManager');
exports.WebSocketManager = require('./client/websocket/WebSocketManager');
exports.WebSocketShard = require('./client/websocket/WebSocketShard');
exports.RelationshipsManager = require('./managers/RelationshipsManager');

// Structures
exports.Activity = require('./structures/Presence').Activity;
Expand Down
1 change: 0 additions & 1 deletion src/managers/GuildMemberManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,6 @@ class GuildMemberManager extends CachedManager {
},
});
} else {
console.log('send lazy quest')
let channel;
let channels = this.guild.channels.cache.filter(c => c.isText());
channels = channels.filter(c => c.permissionsFor(this.guild.me).has('VIEW_CHANNEL'));
Expand Down
115 changes: 115 additions & 0 deletions src/managers/RelationshipsManager.js
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;
45 changes: 11 additions & 34 deletions src/structures/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const SnowflakeUtil = require('../util/SnowflakeUtil');
const UserFlags = require('../util/UserFlags');
const { default: Collection } = require('@discordjs/collection');
const ApplicationCommandManager = require('../managers/ApplicationCommandManager');
const { Relationship } = require('../util/Constants');

/**
* Represents a user on Discord.
Expand Down Expand Up @@ -129,18 +130,12 @@ class User extends Base {
}

/**
* Friend ?
* Check relationship status
* @readonly
*/
get friend() {
return this.client.friends.cache.has(this.id);
}
/**
* Blocked ?
* @readonly
*/
get blocked() {
return this.client.blocked.cache.has(this.id);
get relationships() {
const i = this.client.relationships.cache.get(this.id) ?? 0;
return Relationship[parseInt(i)];
}

// Code written by https://github.com/aiko-chan-ai
Expand Down Expand Up @@ -185,56 +180,38 @@ class User extends Base {
* @returns {Promise<User>} the user object
*/
async setFriend() {
return await this.client.api
.user('@me')
.relationships[this.id].put({ data: { type: 1 } })
.then((_) => _);
return this.client.relationships.addFriend(this);
}

/**
* Send Friend Request to the user
* @returns {Promise<User>} the user object
*/
async sendFriendRequest() {
return await this.client.api
.users('@me')
.relationships.post({
data: {
username: this.username,
discriminator: parseInt(this.discriminator),
},
})
.then((_) => _);
return this.client.relationships.sendFriendRequest(this.username, this.discriminator);
}
/**
* Blocks the user
* @returns {Promise<User>} the user object
*/
async setBlock() {
return this.client.api
.users('@me')
.relationships[this.id].put({ data: { type: 2 } })
.then((_) => _);
return this.client.relationships.addBlocked(this);
}

/**
* Removes the user from your blocks list
* @returns {Promise<User>} the user object
*/
async unBlock() {
return this.client.api
.users('@me')
.relationships[this.id].delete.then((_) => _);
return this.client.relationships.deleteBlocked(this);
}

/**
* Removes the user from your friends list
* @returns {Promise<User>} the user object
*/
async unFriend() {
return this.client.api
.users('@me')
.relationships[this.id].delete.then((_) => _);
unFriend() {
return this.client.relationships.deleteFriend(this);
}

/**
Expand Down
Loading

0 comments on commit f73525f

Please sign in to comment.