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

@@ -446,7 +446,8 @@ class Guild extends AnonymousGuild {
*/
get position() {
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
*/
get folder() {
return (
this.client.setting.guildMetadata.get(this.id.toString()) || {}
);
return this.client.setting.guildMetadata.get(this.id.toString()) || {};
}
/**
@@ -1162,6 +1161,61 @@ class Guild extends AnonymousGuild {
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.
* @param {TextChannelResolvable} publicUpdatesChannel The new community updates channel