Files
discord.js/src/structures/ClientUserSettings.js
SpaceEEC 11556c0b3b Moved all error strings to src/errors/messages and a few other things (#1695)
* Added missing error messages
As well as `Guild#setRolePosition` and `Guild#setChannelPosition`'s first arg validation
And fixed a typo in `Guild#setChannelPosition`
`roles` -> `channels`

* Reverted collection and Util constructors

* Removed leftover messages
Should have been in the second commit.

* It's a single invalid permission and removed unused flag error

* Fix INVALID_TOKEN -> TOKEN_INVALID as of #1703
2017-07-21 02:27:19 +02:00

78 lines
2.3 KiB
JavaScript

const Constants = require('../util/Constants');
const Util = require('../util/Util');
const { Error } = require('../errors');
/**
* A wrapper around the ClientUser's settings.
*/
class ClientUserSettings {
constructor(user, data) {
this.user = user;
this.patch(data);
}
/**
* Patch the data contained in this class with new partial data.
* @param {Object} data Data to patch this with
*/
patch(data) {
for (const [key, value] of Object.entries(Constants.UserSettingsMap)) {
if (!data.hasOwnProperty(key)) continue;
if (typeof value === 'function') {
this[value.name] = value(data[key]);
} else {
this[value] = data[key];
}
}
}
/**
* Update a specific property of of user settings.
* @param {string} name Name of property
* @param {value} value Value to patch
* @returns {Promise<Object>}
*/
update(name, value) {
return this.user.client.api.users['@me'].settings.patch({ data: { [name]: value } });
}
/**
* @param {Guild} guild The guild to move
* @param {number} position Absolute or relative position
* @param {boolean} [relative=false] Whether to position relatively or absolutely
* @returns {Promise<Guild>}
*/
setGuildPosition(guild, position, relative) {
const temp = Object.assign([], this.guildPositions);
Util.moveElementInArray(temp, guild.id, position, relative);
return this.update('guild_positions', temp).then(() => guild);
}
/**
* Add a guild to the list of restricted guilds.
* @param {Guild} guild The guild to add
* @returns {Promise<Guild>}
*/
addRestrictedGuild(guild) {
const temp = Object.assign([], this.restrictedGuilds);
if (temp.includes(guild.id)) return Promise.reject(new Error('GUILD_RESTRICTED', true));
temp.push(guild.id);
return this.update('restricted_guilds', temp).then(() => guild);
}
/**
* Remove a guild from the list of restricted guilds.
* @param {Guild} guild The guild to remove
* @returns {Promise<Guild>}
*/
removeRestrictedGuild(guild) {
const temp = Object.assign([], this.restrictedGuilds);
const index = temp.indexOf(guild.id);
if (index < 0) return Promise.reject(new Error('GUILD_RESTRICTED'));
temp.splice(index, 1);
return this.update('restricted_guilds', temp).then(() => guild);
}
}
module.exports = ClientUserSettings;