Fixed a bunch of ClientUserGuildSettings stuff and its docs (#1758)

This commit is contained in:
SpaceEEC
2017-08-10 01:25:24 +02:00
committed by Crawl
parent 87cdad332c
commit fa5c4efa2b
7 changed files with 38 additions and 25 deletions

View File

@@ -88,9 +88,7 @@ class ClientUser extends User {
this.guildSettings = new Collection();
if (data.user_guild_settings) {
for (const settings of data.user_guild_settings) {
settings.client = this.client;
const guild = this.client.guilds.get(settings.guild_id);
this.guildSettings.set(settings.guild_id, new ClientUserGuildSettings(settings, guild));
this.guildSettings.set(settings.guild_id, new ClientUserGuildSettings(settings, this.client));
}
}
}

View File

@@ -4,8 +4,7 @@ const Constants = require('../util/Constants');
* A wrapper around the ClientUser's channel overrides.
*/
class ClientUserChannelOverride {
constructor(user, data) {
this.user = user;
constructor(data) {
this.patch(data);
}
@@ -14,8 +13,7 @@ class ClientUserChannelOverride {
* @param {Object} data Data to patch this with
*/
patch(data) {
for (const key of Object.keys(Constants.UserChannelOverrideMap)) {
const value = Constants.UserChannelOverrideMap[key];
for (const [key, value] of Object.entries(Constants.UserChannelOverrideMap)) {
if (!data.hasOwnProperty(key)) continue;
if (typeof value === 'function') {
this[value.name] = value(data[key]);

View File

@@ -6,9 +6,19 @@ const ClientUserChannelOverride = require('./ClientUserChannelOverride');
* A wrapper around the ClientUser's guild settings.
*/
class ClientUserGuildSettings {
constructor(data, guild) {
this.guild = guild;
this.client = data.client;
constructor(data, client) {
/**
* The client that created the instance of the the user
* @name ClientUserGuildSettings#client
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });
/**
* The ID of the guild this settings are for
* @type {Snowflake}
*/
this.guildID = data.guild_id;
this.channelOverrides = new Collection();
this.patch(data);
}
@@ -18,13 +28,12 @@ class ClientUserGuildSettings {
* @param {Object} data Data to patch this with
*/
patch(data) {
for (const key of Object.keys(Constants.UserGuildSettingsMap)) {
const value = Constants.UserGuildSettingsMap[key];
for (const [key, value] of Object.entries(Constants.UserGuildSettingsMap)) {
if (!data.hasOwnProperty(key)) continue;
if (key === 'channel_overrides') {
for (const channel of data[key]) {
this.channelOverrides.set(channel.channel_id,
new ClientUserChannelOverride(this.client.user, channel));
new ClientUserChannelOverride(channel));
}
} else if (typeof value === 'function') {
this[value.name] = value(data[key]);
@@ -41,7 +50,7 @@ class ClientUserGuildSettings {
* @returns {Promise<Object>}
*/
update(name, value) {
return this.guild.client.api.guilds(this.guild.id).settings.patch({ data: { [name]: value } });
return this.client.api.users('@me').guilds(this.guildID).settings.patch({ data: { [name]: value } });
}
}

View File

@@ -316,6 +316,7 @@ class Guild {
* The position of this guild
* <warn>This is only available when using a user account.</warn>
* @type {?number}
* @readonly
*/
get position() {
if (this.client.user.bot) return null;
@@ -327,6 +328,7 @@ class Guild {
* Whether the guild is muted
* <warn>This is only available when using a user account.</warn>
* @type {?boolean}
* @readonly
*/
get muted() {
if (this.client.user.bot) return null;
@@ -341,7 +343,8 @@ class Guild {
* The type of message that should notify you
* one of `EVERYTHING`, `MENTIONS`, `NOTHING`
* <warn>This is only available when using a user account.</warn>
* @type {string}
* @type {?string}
* @readonly
*/
get messageNotifications() {
if (this.client.user.bot) return null;
@@ -355,7 +358,8 @@ class Guild {
/**
* Whether to receive mobile push notifications
* <warn>This is only available when using a user account.</warn>
* @type {boolean}
* @type {?boolean}
* @readonly
*/
get mobilePush() {
if (this.client.user.bot) return null;
@@ -369,9 +373,11 @@ class Guild {
/**
* Whether to suppress everyone messages
* <warn>This is only available when using a user account.</warn>
* @type {boolean}
* @type {?boolean}
* @readonly
*/
get suppressEveryone() {
if (this.client.user.bot) return null;
try {
return this.client.user.guildSettings.get(this.id).suppressEveryone;
} catch (err) {

View File

@@ -365,10 +365,11 @@ class GuildChannel extends Channel {
}
/**
* Whether the channel is muted
* <warn>This is only available when using a user account.</warn>
* @type {boolean}
*/
* Whether the channel is muted
* <warn>This is only available when using a user account.</warn>
* @type {?boolean}
* @readonly
*/
get muted() {
if (this.client.user.bot) return null;
try {
@@ -382,7 +383,8 @@ class GuildChannel extends Channel {
* The type of message that should notify you
* one of `EVERYTHING`, `MENTIONS`, `NOTHING`, `INHERIT`
* <warn>This is only available when using a user account.</warn>
* @type {string}
* @type {?string}
* @readonly
*/
get messageNotifications() {
if (this.client.user.bot) return null;

View File

@@ -14,7 +14,7 @@ class User {
/**
* The client that created the instance of the the user
* @name User#client
* @type {}
* @type {Client}
* @readonly
*/
Object.defineProperty(this, 'client', { value: client });