User settings (#1337)

* user settings bruh

* remove development dump

* emit stuff

* i am so done

* Update ClientUserSettings.js

* modularize

* Update ClientUserSettings.js

* Update Constants.js

* Update ClientUserSettings.js

* Update RESTMethods.js

* Update ClientUserSettings.js

* <.<
This commit is contained in:
Gus Caplan
2017-04-05 15:03:33 -05:00
committed by Crawl
parent 5d85de0883
commit 801633b970
11 changed files with 291 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
const User = require('./User');
const Collection = require('../util/Collection');
const ClientUserSettings = require('./ClientUserSettings');
/**
* Represents the logged in client's Discord user
* @extends {User}
@@ -44,13 +44,6 @@ class ClientUser extends User {
*/
this.notes = new Collection();
/**
* Discord client settings, such as guild positions
* <warn>This is only filled when using a user account.</warn>
* @type {Object}
*/
this.settings = {};
/**
* If the user has discord premium (nitro)
* <warn>This is only filled when using a user account.</warn>
@@ -71,6 +64,13 @@ class ClientUser extends User {
* @type {?boolean}
*/
this.mobile = typeof data.mobile === 'boolean' ? data.mobile : null;
/**
* Various settings for this user
* @type {?ClientUserSettings}
* <warn>This is only filled when using a user account</warn>
*/
if (data.user_settings) this.settings = new ClientUserSettings(this, data.user_settings);
}
edit(data) {

View File

@@ -0,0 +1,77 @@
const Constants = require('../util/Constants');
const Util = require('../util/Util');
/**
* 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 of Object.keys(Constants.UserSettingsMap)) {
const value = Constants.UserSettingsMap[key];
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.rest.methods.patchUserSettings({ [name]: value });
}
/**
* @param {Guild} guild 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 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 is already restricted'));
temp.push(guild.id);
return this.update('restricted_guilds', temp).then(() => guild);
}
/**
* Remove a guild from the list of restricted guilds
* @param {Guild} guild 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 is not restricted'));
temp.splice(index, 1);
return this.update('restricted_guilds', temp).then(() => guild);
}
}
module.exports = ClientUserSettings;

View File

@@ -301,6 +301,17 @@ class Guild {
}
/**
* Get the position of this guild
* <warn>This is only available when using a user account.</warn>
* @type {?number}
*/
get position() {
if (this.client.user.bot) return null;
if (!this.client.user.settings.guildPositions) return null;
return this.client.user.settings.guildPositions.indexOf(this.id);
}
/*
* The `@everyone` Role of the guild.
* @type {Role}
* @readonly
@@ -786,6 +797,29 @@ class Guild {
return this.client.rest.methods.ackGuild(this);
}
/**
* @param {number} position Absolute or relative position
* @param {boolean} [relative=false] Whether to position relatively or absolutely
* @returns {Promise<Guild>}
*/
setPosition(position, relative) {
if (this.client.user.bot) {
return Promise.reject(new Error('Setting guild position is only available for user accounts'));
}
return this.client.user.settings.setGuildPosition(this, position, relative);
}
/**
* Allow direct messages from guild members
* @param {boolean} allow Whether to allow direct messages
* @returns {Promise<Guild>}
*/
allowDMs(allow) {
const settings = this.client.user.settings;
if (allow) return settings.removeRestrictedGuild(this);
else return settings.addRestrictedGuild(this);
}
/**
* Whether this Guild equals another Guild. It compares all properties, so for most operations
* it is advisable to just compare `guild.id === guild2.id` as it is much faster and is often