Group DM channel

Feature list: Create, Add member, Remove member, fetchInvite, createInvite, setName, setIcon, send Message, leave .-.
This commit is contained in:
March 7th 2022-04-03 11:53:09 +07:00
parent ed996371b0
commit 54d0e9d272
8 changed files with 561 additions and 307 deletions

View File

@ -117,8 +117,6 @@ User {
bot: false, bot: false,
system: false, system: false,
flags: UserFlagsBitField { bitfield: 256 }, flags: UserFlagsBitField { bitfield: 256 },
friend: false,
blocked: false,
note: null, note: null,
connectedAccounts: [], connectedAccounts: [],
premiumSince: 1623357181151, premiumSince: 1623357181151,
@ -156,6 +154,40 @@ Guild {}
``` ```
</details> </details>
## Group DM
<details>
<summary><strong>Click to show</strong></summary>
Code:
```js
/* Create */
const memberAdd = [
client.users.cache.get('id1'),
client.users.cache.get('id2'),
...
client.users.cache.get('id9')
]
// Max member add to Group: 9, Min: 2
await client.channels.createGroupDM(memberAdd);
/* Edit */
const groupDM = client.channels.cache.get('id');
await groupDM.setName('New Name');
await groupDM.setIcon('iconURL');
await groupDM.getInvite();
await groupDM.fetchInvite();
await groupDM.removeInvite(invite);
await groupDM.addMember(user);
await groupDM.removeMember(user);
/* Text Channel not Bulk delete */
await groupDM.send('Hello World');
await groupDM.delete(); // Leave
```
Response
```js
Guild {}
```
</details>
## Custom Status and RPC ## Custom Status and RPC
<details> <details>

View File

@ -1,6 +1,6 @@
{ {
"name": "discord.js-selfbot-v13", "name": "discord.js-selfbot-v13",
"version": "1.2.6", "version": "1.2.7",
"description": "A unofficial discord.js fork for creating selfbots [Based on discord.js v13]", "description": "A unofficial discord.js fork for creating selfbots [Based on discord.js v13]",
"main": "./src/index.js", "main": "./src/index.js",
"types": "./typings/index.d.ts", "types": "./typings/index.d.ts",
@ -47,6 +47,7 @@
"@types/ws": "^8.5.2", "@types/ws": "^8.5.2",
"axios": "^0.26.1", "axios": "^0.26.1",
"bignumber.js": "^9.0.2", "bignumber.js": "^9.0.2",
"bufferutil": "^4.0.6",
"chalk": "^4.1.2", "chalk": "^4.1.2",
"discord-api-types": "^0.27.3", "discord-api-types": "^0.27.3",
"discord-bettermarkdown": "^1.1.0", "discord-bettermarkdown": "^1.1.0",
@ -59,6 +60,7 @@
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"string-similarity": "^4.0.4", "string-similarity": "^4.0.4",
"undici": "^4.15.0", "undici": "^4.15.0",
"utf-8-validate": "^5.0.9",
"ws": "^8.5.0" "ws": "^8.5.0"
}, },
"engines": { "engines": {

View File

@ -152,6 +152,10 @@ const Messages = {
INVITE_NOT_FOUND: 'Could not find the requested invite.', INVITE_NOT_FOUND: 'Could not find the requested invite.',
NOT_OWNER_GROUP_DM_CHANNEL: "You can't do this action [Missing Permission]",
USER_ALREADY_IN_GROUP_DM_CHANNEL: 'User is already in the channel.',
USER_NOT_IN_GROUP_DM_CHANNEL: 'User is not in the channel.',
DELETE_GROUP_DM_CHANNEL: DELETE_GROUP_DM_CHANNEL:
"Bots don't have access to Group DM Channels and cannot delete them", "Bots don't have access to Group DM Channels and cannot delete them",
FETCH_GROUP_DM_CHANNEL: FETCH_GROUP_DM_CHANNEL:
@ -200,7 +204,8 @@ const Messages = {
APPLICATION_ID_INVALID: "The application isn't BOT", APPLICATION_ID_INVALID: "The application isn't BOT",
INVALID_NITRO: 'Invalid Nitro Code', INVALID_NITRO: 'Invalid Nitro Code',
MESSAGE_ID_NOT_FOUND: 'Message ID not found', MESSAGE_ID_NOT_FOUND: 'Message ID not found',
MESSAGE_EMBED_LINK_LENGTH: 'Message content with embed link length is too long', MESSAGE_EMBED_LINK_LENGTH:
'Message content with embed link length is too long',
}; };
for (const [name, message] of Object.entries(Messages)) register(name, message); for (const [name, message] of Object.entries(Messages)) register(name, message);

View File

@ -4,6 +4,8 @@ const process = require('node:process');
const CachedManager = require('./CachedManager'); const CachedManager = require('./CachedManager');
const { Channel } = require('../structures/Channel'); const { Channel } = require('../structures/Channel');
const { Events, ThreadChannelTypes } = require('../util/Constants'); const { Events, ThreadChannelTypes } = require('../util/Constants');
const User = require('../structures/User');
const PartialGroupDMChannel = require('../structures/PartialGroupDMChannel');
let cacheWarningEmitted = false; let cacheWarningEmitted = false;
@ -113,8 +115,27 @@ class ChannelManager extends CachedManager {
} }
const data = await this.client.api.channels(id).get(); const data = await this.client.api.channels(id).get();
// delete in cache
this._remove(id);
return this._add(data, null, { cache, allowUnknownGuild }); return this._add(data, null, { cache, allowUnknownGuild });
} }
// Create Group DM
/**
* Create Group DM
* @param {Array<Discord.User>} recipients Array of recipients
* @returns {PartialGroupDMChannel} Channel
*/
async createGroupDM(recipients) {
// Check
if (!recipients || !Array.isArray(recipients)) throw new Error('No recipients || Invalid Type (Array)');
recipients = recipients.filter(r => r instanceof User && r.id && r.friend);
console.log(recipients);
if (recipients.length < 2 || recipients.length > 9) throw new Error('Invalid Users length (2 - 9)');
const data = await this.client.api.users['@me'].channels.post({
data: { recipients: recipients.map((r) => r.id) },
});
return this._add(data, null, { cache: true, allowUnknownGuild: true });
}
} }
module.exports = ChannelManager; module.exports = ChannelManager;

View File

@ -1 +0,0 @@
// Todo: create, add, remove, update

View File

@ -2,6 +2,14 @@
const { Channel } = require('./Channel'); const { Channel } = require('./Channel');
const { Error } = require('../errors'); const { Error } = require('../errors');
const { Collection } = require('discord.js');
const { Message } = require('./Message');
const MessageManager = require('../managers/MessageManager');
const User = require('./User');
const DataResolver = require('../util/DataResolver');
const TextBasedChannel = require('./interfaces/TextBasedChannel');
const Invite = require('./Invite');
/** /**
* Represents a Partial Group DM Channel on Discord. * Represents a Partial Group DM Channel on Discord.
@ -10,7 +18,6 @@ const { Error } = require('../errors');
class PartialGroupDMChannel extends Channel { class PartialGroupDMChannel extends Channel {
constructor(client, data) { constructor(client, data) {
super(client, data); super(client, data);
/** /**
* The name of this Group DM Channel * The name of this Group DM Channel
* @type {?string} * @type {?string}
@ -33,7 +40,84 @@ class PartialGroupDMChannel extends Channel {
* The recipients of this Group DM Channel. * The recipients of this Group DM Channel.
* @type {PartialRecipient[]} * @type {PartialRecipient[]}
*/ */
this.recipients = data.recipients; this.recipients = new Collection();
/**
* Messages data
* @type {Collection}
*/
this.messages = new MessageManager(this);
/**
* Last Message ID
* @type {?snowflake<Message.id>}
*/
this.lastMessageId = null;
/**
* Last Pin Timestamp
* @type {UnixTimestamp}
*/
this.lastPinTimestamp = null;
/**
* The owner of this Group DM Channel
* @type {?User}
* @readonly
*/
this.owner = client.users.cache.get(data.owner_id);
this.ownerId = data.owner_id;
/**
* Invites fetch
*/
this.invites = new Collection();
this._setup(client, data);
}
/**
*
* @param {Discord.Client} client
* @param {object} data
* @private
*/
_setup(client, data) {
if ('recipients' in data) {
Promise.all(
data.recipients.map((recipient) => {
this.recipients.set(
recipient.id,
client.users.cache.get(data.owner_id) || recipient,
);
}),
);
}
if ('last_pin_timestamp' in data) {
const date = new Date(data.last_pin_timestamp);
this.lastPinTimestamp = date.getTime();
}
if ('last_message_id' in data) {
this.lastMessageId = data.last_message_id;
}
}
/**
*
* @param {Object} data name, icon
* @returns
* @private
*/
async edit(data) {
const _data = {};
if ('name' in data) _data.name = data.name?.trim() ?? null;
if (typeof data.icon !== 'undefined')
_data.icon = await DataResolver.resolveImage(data.icon);
const newData = await this.client.api.channels(this.id).patch({
data: _data,
});
return this.client.actions.ChannelUpdate.handle(newData).updated;
} }
/** /**
@ -42,16 +126,85 @@ class PartialGroupDMChannel extends Channel {
* @returns {?string} * @returns {?string}
*/ */
iconURL({ format, size } = {}) { iconURL({ format, size } = {}) {
return this.icon && this.client.rest.cdn.GDMIcon(this.id, this.icon, format, size); return (
this.icon &&
this.client.rest.cdn.GDMIcon(this.id, this.icon, format, size)
);
} }
delete() { async addMember(user) {
return Promise.reject(new Error('DELETE_GROUP_DM_CHANNEL')); if (this.ownerId !== this.client.user.id)
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
if (!user instanceof User)
return Promise.reject(
new TypeError('User is not an instance of Discord.User'),
);
if (this.recipients.get(user.id)) return Promise.reject(new Error('USER_ALREADY_IN_GROUP_DM_CHANNEL'));
//
await this.client.api.channels[this.id].recipients[user.id].put();
this.recipients.set(user.id, user);
return this;
} }
fetch() { async removeMember(user) {
return Promise.reject(new Error('FETCH_GROUP_DM_CHANNEL')); if (this.ownerId !== this.client.user.id)
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
if (!user instanceof User)
return Promise.reject(
new TypeError('User is not an instance of Discord.User'),
);
if (!this.recipients.get(user.id)) return Promise.reject(new Error('USER_NOT_IN_GROUP_DM_CHANNEL'));
await this.client.api.channels[this.id].recipients[user.id].delete();
this.recipients.delete(user.id);
return this;
} }
setName(name) {
return this.edit({ name });
}
setIcon(icon) {
return this.edit({ icon });
}
async getInvite() {
const inviteCode = await this.client.api.channels(this.id).invites.post({
data: {
max_age: 86400,
},
});
const invite = new Invite(this.client, inviteCode);
this.invites.set(invite.code, invite);
return invite;
}
async fetchInvite(force = false) {
if (this.ownerId !== this.client.user.id)
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
if (!force && this.invites.size) return this.invites;
const invites = await this.client.api.channels(this.id).invites.get();
await Promise.all(invites.map(invite => this.invites.set(invite.code, new Invite(this.client, invite))));
return this.invites;
}
async removeInvite(invite) {
if (this.ownerId !== this.client.user.id)
return Promise.reject(new Error('NOT_OWNER_GROUP_DM_CHANNEL'));
if (!invite instanceof Invite)
return Promise.reject(new TypeError('Invite is not an instance of Discord.Invite'));
await this.client.api.channels(this.id).invites[invite.code].delete();
this.invites.delete(invite.code);
return this;
}
// These are here only for documentation purposes - they are implemented by TextBasedChannel
/* eslint-disable no-empty-function */
get lastMessage() { }
get lastPinAt() { }
send() { }
sendTyping() { }
} }
TextBasedChannel.applyToClass(PartialGroupDMChannel, false);
module.exports = PartialGroupDMChannel; module.exports = PartialGroupDMChannel;

View File

@ -29,10 +29,6 @@ class User extends Base {
this.flags = null; this.flags = null;
this.friend = client.friends.cache.has(this.id);
this.blocked = client.blocked.cache.has(this.id);
// Code written by https://github.com/aiko-chan-ai // Code written by https://github.com/aiko-chan-ai
this.connectedAccounts = []; this.connectedAccounts = [];
this.premiumSince = null; this.premiumSince = null;
@ -61,7 +57,11 @@ class User extends Base {
*/ */
this.bot = Boolean(data.bot); this.bot = Boolean(data.bot);
if (this.bot == true) { if (this.bot == true) {
this.applications = new ApplicationCommandManager(this.client, undefined, this); this.applications = new ApplicationCommandManager(
this.client,
undefined,
this,
);
} }
} else if (!this.partial && typeof this.bot !== 'boolean') { } else if (!this.partial && typeof this.bot !== 'boolean') {
this.bot = false; this.bot = false;
@ -128,6 +128,20 @@ class User extends Base {
} }
} }
/**
* Friend ?
* @readonly
*/
get friend() {
return this.client.friends.cache.has(this.id);
}
/**
* Blocked ?
* @readonly
*/
get blocked() {
return this.client.blocked.cache.has(this.id);
}
// Code written by https://github.com/aiko-chan-ai // Code written by https://github.com/aiko-chan-ai
_ProfilePatch(data) { _ProfilePatch(data) {
@ -223,7 +237,6 @@ class User extends Base {
.relationships[this.id].delete.then((_) => _); .relationships[this.id].delete.then((_) => _);
} }
/** /**
* Whether this User is a partial * Whether this User is a partial
* @type {boolean} * @type {boolean}
@ -258,7 +271,13 @@ class User extends Base {
*/ */
avatarURL({ format, size, dynamic } = {}) { avatarURL({ format, size, dynamic } = {}) {
if (!this.avatar) return null; if (!this.avatar) return null;
return this.client.rest.cdn.Avatar(this.id, this.avatar, format, size, dynamic); return this.client.rest.cdn.Avatar(
this.id,
this.avatar,
format,
size,
dynamic,
);
} }
/** /**
@ -299,9 +318,16 @@ class User extends Base {
* @returns {?string} * @returns {?string}
*/ */
bannerURL({ format, size, dynamic } = {}) { bannerURL({ format, size, dynamic } = {}) {
if (typeof this.banner === 'undefined') throw new Error('USER_BANNER_NOT_FETCHED'); if (typeof this.banner === 'undefined')
throw new Error('USER_BANNER_NOT_FETCHED');
if (!this.banner) return null; if (!this.banner) return null;
return this.client.rest.cdn.Banner(this.id, this.banner, format, size, dynamic); return this.client.rest.cdn.Banner(
this.id,
this.banner,
format,
size,
dynamic,
);
} }
/** /**
@ -310,7 +336,9 @@ class User extends Base {
* @readonly * @readonly
*/ */
get tag() { get tag() {
return typeof this.username === 'string' ? `${this.username}#${this.discriminator}` : null; return typeof this.username === 'string'
? `${this.username}#${this.discriminator}`
: null;
} }
/** /**
@ -429,10 +457,8 @@ class User extends Base {
* @returns {Promise<User.note>} * @returns {Promise<User.note>}
*/ */
async setNote(note = null) { async setNote(note = null) {
await this.client.api.users['@me'] await this.client.api.users['@me'].notes(id).put({ data: { note } });
.notes(id) return (this.note = note);
.put({ data: { note } });
return this.note = note;
} }
// These are here only for documentation purposes - they are implemented by TextBasedChannel // These are here only for documentation purposes - they are implemented by TextBasedChannel

20
typings/index.d.ts vendored
View File

@ -1897,12 +1897,25 @@ export class OAuth2Guild extends BaseGuild {
public permissions: Readonly<Permissions>; public permissions: Readonly<Permissions>;
} }
export class PartialGroupDMChannel extends Channel { export class PartialGroupDMChannel extends TextBasedChannelMixin(Channel, ['bulkDelete']) {
private constructor(client: Client, data: RawPartialGroupDMChannelData); private constructor(client: Client, data: RawPartialGroupDMChannelData);
public name: string | null; public name: string | null;
public icon: string | null; public icon: string | null;
public recipients: PartialRecipient[]; public recipients: Collection<User>;
public messages: MessageManager<PartialGroupDMChannel>;
public invites: Collection<Invite.code, Invite>;
public lastMessageId: Snowflake | null;
public lastPinTimestamp: String<number> | null;
public owner: User | null;
public ownerId: Snowflake | null;
public iconURL(options?: StaticImageURLOptions): string | null; public iconURL(options?: StaticImageURLOptions): string | null;
public addMember(user: User): Promise<PartialGroupDMChannel>;
public removeMember(user: User): Promise<PartialGroupDMChannel>;
public setName(name: string): Promise<PartialGroupDMChannel>;
public setIcon(icon: Base64Resolvable | null): Promise<PartialGroupDMChannel>;
public getInvite(): Promise<Invite>;
public fetchInvite(force: boolean): Promise<PartialGroupDMChannel.invites>;
public removeInvite(invite: Invite): Promise<PartialGroupDMChannel>;
} }
export class PermissionOverwrites extends Base { export class PermissionOverwrites extends Base {
@ -2449,6 +2462,8 @@ export class User extends PartialTextBasedChannel(Base) {
public banner: string | null | undefined; public banner: string | null | undefined;
public bot: boolean; public bot: boolean;
public readonly createdAt: Date; public readonly createdAt: Date;
public readonly friend: Boolean;
public readonly blocked: Boolean;
public readonly createdTimestamp: number; public readonly createdTimestamp: number;
public discriminator: string; public discriminator: string;
public readonly defaultAvatarURL: string; public readonly defaultAvatarURL: string;
@ -3044,6 +3059,7 @@ export class BaseGuildEmojiManager extends CachedManager<Snowflake, GuildEmoji,
export class ChannelManager extends CachedManager<Snowflake, AnyChannel, ChannelResolvable> { export class ChannelManager extends CachedManager<Snowflake, AnyChannel, ChannelResolvable> {
private constructor(client: Client, iterable: Iterable<RawChannelData>); private constructor(client: Client, iterable: Iterable<RawChannelData>);
public fetch(id: Snowflake, options?: FetchChannelOptions): Promise<AnyChannel | null>; public fetch(id: Snowflake, options?: FetchChannelOptions): Promise<AnyChannel | null>;
public createGroupDM(recipients: Array<User>): Promise<PartialGroupDMChannel>;
} }
export class ClientUserSettingManager extends CachedManager<Snowflake, null> { export class ClientUserSettingManager extends CachedManager<Snowflake, null> {