Set Position of Guild

This commit is contained in:
March 7th 2022-03-20 14:25:53 +07:00
parent 1fc5510a4a
commit 9c625a9bf5
7 changed files with 164 additions and 9 deletions

View File

@ -67,3 +67,6 @@ client.login('token');
## Need help? ## Need help?
Contact me in Discord [Shiraori#1782] (UserID: 721746046543331449) Contact me in Discord [Shiraori#1782] (UserID: 721746046543331449)
## <strong><img src="https://cdn.discordapp.com/attachments/820557032016969751/952436539118456882/flag-vietnam_1f1fb-1f1f3.png" alt="." width="20" height="20"/> Vietnamese</strong>
- Tóm lại là module này dùng Discord.js v13, API v9 nên chưa chết sớm đâu, cứ dùng đi =))

View File

@ -1,6 +1,6 @@
{ {
"name": "discord.js-selfbot-v13", "name": "discord.js-selfbot-v13",
"version": "0.0.2", "version": "0.0.3",
"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",
@ -46,6 +46,7 @@
"@types/ws": "^8.5.2", "@types/ws": "^8.5.2",
"discord-api-types": "^0.27.3", "discord-api-types": "^0.27.3",
"form-data": "^4.0.0", "form-data": "^4.0.0",
"lodash": "^4.17.21",
"lodash.snakecase": "^4.1.1", "lodash.snakecase": "^4.1.1",
"node-fetch": "^3.2.2", "node-fetch": "^3.2.2",
"undici": "^4.15.0", "undici": "^4.15.0",

View File

@ -18,6 +18,8 @@ module.exports = (client, { d: data }, shard) => {
client.user.setAFK(true); client.user.setAFK(true);
client.setting.fetch();
for (const guild of data.guilds) { for (const guild of data.guilds) {
guild.shardId = shard.id; guild.shardId = shard.id;
client.guilds._add(guild); client.guilds._add(guild);

View File

@ -186,6 +186,8 @@ const Messages = {
INVALID_BOT_METHOD: `Bot accounts cannot use this method`, INVALID_BOT_METHOD: `Bot accounts cannot use this method`,
INVALID_USER_METHOD: `User accounts cannot use this method`, INVALID_USER_METHOD: `User accounts cannot use this method`,
INVALID_LOCALE: 'Unable to select this location', INVALID_LOCALE: 'Unable to select this location',
FOLDER_NOT_FOUND: 'Server directory not found',
FOLDER_POSITION_INVALID: 'The server index in the directory is invalid',
}; };
Messages.AuthenticationFailed = Messages.TOKEN_INVALID; Messages.AuthenticationFailed = Messages.TOKEN_INVALID;

View File

@ -3,6 +3,7 @@
const CachedManager = require('./CachedManager'); const CachedManager = require('./CachedManager');
const { default: Collection } = require('@discordjs/collection'); const { default: Collection } = require('@discordjs/collection');
const { Error, TypeError } = require('../errors/DJSError'); const { Error, TypeError } = require('../errors/DJSError');
const { remove } = require('lodash');
/** /**
* Manages API methods for users and stores their cache. * Manages API methods for users and stores their cache.
* @extends {CachedManager} * @extends {CachedManager}
@ -67,6 +68,11 @@ class ClientUserSettingManager extends CachedManager {
// Guild folder and position // Guild folder and position
this.guildMetadata = new Collection(); this.guildMetadata = new Collection();
} }
/**
*
* @param {Object} data Raw Data to patch
* @private
*/
_patch(data) { _patch(data) {
this.rawSetting = data; this.rawSetting = data;
if ('locale' in data) { if ('locale' in data) {
@ -177,7 +183,17 @@ class ClientUserSettingManager extends CachedManager {
async setTheme(value) { async setTheme(value) {
if (this.client.bot) throw new Error('INVALID_BOT_METHOD'); if (this.client.bot) throw new Error('INVALID_BOT_METHOD');
const validValues = ['dark', 'light']; const validValues = ['dark', 'light'];
if (typeof value !== 'string' && typeof value !== 'null' && typeof value !== 'undefined') throw new TypeError('INVALID_TYPE', 'value', 'string | null | undefined', true); if (
typeof value !== 'string' &&
typeof value !== 'null' &&
typeof value !== 'undefined'
)
throw new TypeError(
'INVALID_TYPE',
'value',
'string | null | undefined',
true,
);
if (!validValues.includes(value)) { if (!validValues.includes(value)) {
value == validValues[0] value == validValues[0]
? (value = validValues[1]) ? (value = validValues[1])
@ -225,7 +241,8 @@ class ClientUserSettingManager extends CachedManager {
*/ */
async setLocale(value) { async setLocale(value) {
if (this.client.bot) throw new Error('INVALID_BOT_METHOD'); if (this.client.bot) throw new Error('INVALID_BOT_METHOD');
if (typeof value !== 'string') throw new TypeError('INVALID_TYPE', 'value', 'string', true); if (typeof value !== 'string')
throw new TypeError('INVALID_TYPE', 'value', 'string', true);
if (!localeObject[value]) throw new Error('INVALID_LOCALE'); if (!localeObject[value]) throw new Error('INVALID_LOCALE');
if (localeObject[value] !== this.locale) { if (localeObject[value] !== this.locale) {
await this.edit({ locale: localeObject[value] }); await this.edit({ locale: localeObject[value] });
@ -233,6 +250,81 @@ class ClientUserSettingManager extends CachedManager {
return this.locale; return this.locale;
} }
// TODO: Guild positions & folders // TODO: Guild positions & folders
// Change Index in Array [Hidden]
/**
*
* @param {Array} array Array
* @param {Number} from Index1
* @param {Number} to Index2
* @returns {Array}
* @private
*/
_move(array, from, to) {
array.splice(to, 0, array.splice(from, 1)[0]);
return array;
}
// TODO: Move Guild
// folder to folder
// folder to home
// home to home
// home to folder
/**
* Change Guild Position (from * to Folder or Home)
* @param {GuildIDResolve} guildId guild.id
* @param {Number} newPosition Guild Position
* * **WARNING**: Type = `FOLDER`, newPosition is the guild's index in the Folder.
* @param {number} type Move to folder or home
* * `FOLDER`: 1
* * `HOME`: 2
* @param {FolderID} folderId If you want to move to folder
* @private
*/
async guildChangePosition(guildId, newPosition, type, folderId) {
// get Guild default position
// Escape
const oldGuildFolderPosition = this.rawSetting.guild_folders.findIndex(
(value) => value.guild_ids.includes(guildId),
);
const newGuildFolderPosition = this.rawSetting.guild_folders.findIndex((value) =>
value.guild_ids.includes(this.rawSetting.guild_positions[newPosition]),
);
if (type == 2 || `${type}`.toUpperCase() == 'HOME') {
// Delete GuildID from Folder and create new Folder
// Check it is folder
const folder = this.rawSetting.guild_folders[oldGuildFolderPosition];
if (folder.id) {
this.rawSetting.guild_folders[oldGuildFolderPosition].guild_ids =
this.rawSetting.guild_folders[
oldGuildFolderPosition
].guild_ids.filter((v) => v !== guildId);
}
this.rawSetting.guild_folders = this._move(
this.rawSetting.guild_folders,
oldGuildFolderPosition,
newGuildFolderPosition,
);
this.rawSetting.guild_folders[newGuildFolderPosition].id = null;
} else if (type == 1 || `${type}`.toUpperCase() == 'FOLDER') {
// Delete GuildID from oldFolder
this.rawSetting.guild_folders[oldGuildFolderPosition].guild_ids =
this.rawSetting.guild_folders[oldGuildFolderPosition].guild_ids.filter(
(v) => v !== guildId,
);
// Index new Folder
const folderIndex = this.rawSetting.guild_folders.findIndex(
(value) => value.id == folderId,
);
const folder = this.rawSetting.guild_folders[folderIndex];
folder.guild_ids.push(guildId);
folder.guild_ids = [...new Set(folder.guild_ids)];
folder.guild_ids = this._move(
folder.guild_ids,
folder.guild_ids.findIndex((v) => v == guildId),
newPosition,
);
}
this.edit({ guild_folders: this.rawSetting.guild_folders });
}
} }
module.exports = ClientUserSettingManager; module.exports = ClientUserSettingManager;

View File

@ -446,7 +446,8 @@ class Guild extends AnonymousGuild {
*/ */
get position() { get position() {
return ( return (
this.client.setting.guildMetadata.get(this.id.toString())?.guildIndex || null this.client.setting.guildMetadata.get(this.id.toString())?.guildIndex ||
null
); );
} }
@ -456,9 +457,7 @@ class Guild extends AnonymousGuild {
* @readonly * @readonly
*/ */
get folder() { get folder() {
return ( return this.client.setting.guildMetadata.get(this.id.toString()) || {};
this.client.setting.guildMetadata.get(this.id.toString()) || {}
);
} }
/** /**
@ -1162,6 +1161,61 @@ class Guild extends AnonymousGuild {
return this.edit({ rulesChannel }, reason); return this.edit({ rulesChannel }, reason);
} }
/**
* Change Guild Position (from * to Folder or Home)
* @param {number} position Guild Position
* * **WARNING**: Type = `FOLDER`, newPosition is the guild's index in the Folder.
* @param {String|Number} type Move to folder or home
* * `FOLDER`: 1
* * `HOME`: 2
* @param {String|Number|void|null} folderID If you want to move to folder
* @returns {Promise<Guild>}
* @example
* // Move guild to folderID 123456, index 1
* guild.setPosition(1, 'FOLDER', 123456)
* .then(guild => console.log(`Guild moved to folderID ${guild.folder.folderId}`));
*/
async setPosition(position, type, folderID) {
if (type == 1 || `${type}`.toUpperCase() === 'FOLDER') {
folderID = folderID || this.folder.folderId;
if (!['number', 'string'].includes(typeof folderID))
throw new TypeError(
'INVALID_TYPE',
'folderID',
'String | Number',
);
// Get Data from Folder ID
const folder = await this.client.setting.rawSetting.guild_folders.find(
(obj) => obj.id == folderID,
);
if (!folder) throw new Error('FOLDER_NOT_FOUND');
if (folder.guild_ids.length - 1 < position || position < 0)
throw new Error('FOLDER_POSITION_INVALID');
if (position !== folder.guild_ids.indexOf(this.id)) {
await this.client.setting.guildChangePosition(
this.id,
position,
1,
folderID,
);
}
} else if (type == 2 || `${type}`.toUpperCase() === 'HOME') {
if (this.client.setting.guild_positions - 1 < position || position < 0)
throw new Error('FOLDER_POSITION_INVALID');
if (position !== this.position) {
await this.client.setting.guildChangePosition(
this.id,
position,
2,
null,
);
}
} else {
throw new TypeError('INVALID_TYPE', 'type', '`Folder`| `Home`');
}
return this;
}
/** /**
* Edits the community updates channel of the guild. * Edits the community updates channel of the guild.
* @param {TextChannelResolvable} publicUpdatesChannel The new community updates channel * @param {TextChannelResolvable} publicUpdatesChannel The new community updates channel

1
typings/index.d.ts vendored
View File

@ -968,6 +968,7 @@ export class Guild extends AnonymousGuild {
public setIcon(icon: BufferResolvable | Base64Resolvable | null, reason?: string): Promise<Guild>; public setIcon(icon: BufferResolvable | Base64Resolvable | null, reason?: string): Promise<Guild>;
public setName(name: string, reason?: string): Promise<Guild>; public setName(name: string, reason?: string): Promise<Guild>;
public setOwner(owner: GuildMemberResolvable, reason?: string): Promise<Guild>; public setOwner(owner: GuildMemberResolvable, reason?: string): Promise<Guild>;
public setPosition(position: number, type: FOLDER | HOME, folderID?: FolderID): Promise<Guild>;
public setPreferredLocale(preferredLocale: string, reason?: string): Promise<Guild>; public setPreferredLocale(preferredLocale: string, reason?: string): Promise<Guild>;
public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>; public setPublicUpdatesChannel(publicUpdatesChannel: TextChannelResolvable | null, reason?: string): Promise<Guild>;
/** @deprecated Use {@link RoleManager.setPositions} instead */ /** @deprecated Use {@link RoleManager.setPositions} instead */