* Backported OAuth2Application 201ecd25a2

* Backported retry on 500 57b6980313

* Backported b8034525e3 and fa5c4efa2b
This commit is contained in:
SpaceEEC
2017-08-21 22:25:21 +02:00
committed by Crawl
parent be4ccb3686
commit f7664b01a2
14 changed files with 304 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
const User = require('./User');
const Collection = require('../util/Collection');
const ClientUserSettings = require('./ClientUserSettings');
const ClientUserGuildSettings = require('./ClientUserGuildSettings');
const Constants = require('../util/Constants');
/**
@@ -73,6 +74,18 @@ class ClientUser extends User {
* @type {?ClientUserSettings}
*/
this.settings = data.user_settings ? new ClientUserSettings(this, data.user_settings) : null;
/**
* All of the user's guild settings
* <warn>This is only filled when using a user account</warn>
* @type {Collection<Snowflake, ClientUserGuildSettings>}
*/
this.guildSettings = new Collection();
if (data.user_guild_settings) {
for (const settings of data.user_guild_settings) {
this.guildSettings.set(settings.guild_id, new ClientUserGuildSettings(settings, this.client));
}
}
}
edit(data) {

View File

@@ -0,0 +1,28 @@
const Constants = require('../util/Constants');
/**
* A wrapper around the ClientUser's channel overrides.
*/
class ClientUserChannelOverride {
constructor(data) {
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.UserChannelOverrideMap)) {
const value = Constants.UserChannelOverrideMap[key];
if (!data.hasOwnProperty(key)) continue;
if (typeof value === 'function') {
this[value.name] = value(data[key]);
} else {
this[value] = data[key];
}
}
}
}
module.exports = ClientUserChannelOverride;

View File

@@ -0,0 +1,58 @@
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const ClientUserChannelOverride = require('./ClientUserChannelOverride');
/**
* A wrapper around the ClientUser's guild settings.
*/
class ClientUserGuildSettings {
constructor(data, client) {
/**
* The client that created the instance of the ClientUserGuildSettings
* @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);
}
/**
* 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.UserGuildSettingsMap)) {
const value = Constants.UserGuildSettingsMap[key];
if (!data.hasOwnProperty(key)) continue;
if (key === 'channel_overrides') {
for (const channel of data[key]) {
this.channelOverrides.set(channel.channel_id,
new ClientUserChannelOverride(channel));
}
} else if (typeof value === 'function') {
this[value.name] = value(data[key]);
} else {
this[value] = data[key];
}
}
}
/**
* Update a specific property of the guild settings.
* @param {string} name Name of property
* @param {value} value Value to patch
* @returns {Promise<Object>}
*/
update(name, value) {
return this.client.rest.methods.patchClientUserGuildSettings(this.guildID, { [name]: value });
}
}
module.exports = ClientUserGuildSettings;

View File

@@ -330,6 +330,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;
@@ -337,6 +338,66 @@ class Guild {
return this.client.user.settings.guildPositions.indexOf(this.id);
}
/**
* 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;
try {
return this.client.user.guildSettings.get(this.id).muted;
} catch (err) {
return false;
}
}
/**
* The type of message that should notify you
* <warn>This is only available when using a user account.</warn>
* @type {?MessageNotificationType}
* @readonly
*/
get messageNotifications() {
if (this.client.user.bot) return null;
try {
return this.client.user.guildSettings.get(this.id).messageNotifications;
} catch (err) {
return null;
}
}
/**
* Whether to receive mobile push notifications
* <warn>This is only available when using a user account.</warn>
* @type {?boolean}
* @readonly
*/
get mobilePush() {
if (this.client.user.bot) return null;
try {
return this.client.user.guildSettings.get(this.id).mobilePush;
} catch (err) {
return false;
}
}
/**
* Whether to suppress everyone messages
* <warn>This is only available when using a user account.</warn>
* @type {?boolean}
* @readonly
*/
get suppressEveryone() {
if (this.client.user.bot) return null;
try {
return this.client.user.guildSettings.get(this.id).suppressEveryone;
} catch (err) {
return null;
}
}
/**
* The `@everyone` role of the guild
* @type {Role}

View File

@@ -3,6 +3,7 @@ const Role = require('./Role');
const PermissionOverwrites = require('./PermissionOverwrites');
const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection');
const Constants = require('../util/Constants');
/**
* Represents a guild channel (i.e. text channels and voice channels).
@@ -328,6 +329,36 @@ class GuildChannel extends Channel {
this.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS);
}
/**
* 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 {
return this.client.user.guildSettings.get(this.guild.id).channelOverrides.get(this.id).muted;
} catch (err) {
return false;
}
}
/**
* The type of message that should notify you
* <warn>This is only available when using a user account.</warn>
* @type {?MessageNotificationType}
* @readonly
*/
get messageNotifications() {
if (this.client.user.bot) return null;
try {
return this.client.user.guildSettings.get(this.guild.id).channelOverrides.get(this.id).messageNotifications;
} catch (err) {
return Constants.MessageNotificationTypes[3];
}
}
/**
* When concatenated with a string, this automatically returns the channel's mention instead of the Channel object.
* @returns {string}

View File

@@ -37,7 +37,7 @@ class OAuth2Application {
/**
* The app's icon hash
* @type {string}
* @type {?string}
*/
this.icon = data.icon;
@@ -124,6 +124,7 @@ class OAuth2Application {
/**
* Reset the app's secret and bot token.
* <warn>This is only available when using a user account.</warn>
* @returns {OAuth2Application}
*/
reset() {

View File

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