* 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

@@ -878,7 +878,8 @@ class RESTMethods {
} }
resetApplication(id) { resetApplication(id) {
return this.rest.makeRequest('post', Endpoints.OAUTH2.Application(id).reset, true) return this.rest.makeRequest('post', Endpoints.OAUTH2.Application(id).resetToken, true)
.then(() => this.rest.makeRequest('post', Endpoints.OAUTH2.Application(id).resetSecret, true))
.then(app => new OAuth2Application(this.client, app)); .then(app => new OAuth2Application(this.client, app));
} }
@@ -908,6 +909,10 @@ class RESTMethods {
patchUserSettings(data) { patchUserSettings(data) {
return this.rest.makeRequest('patch', Constants.Endpoints.User('@me').settings, true, data); return this.rest.makeRequest('patch', Constants.Endpoints.User('@me').settings, true, data);
} }
patchClientUserGuildSettings(guildID, data) {
return this.rest.makeRequest('patch', Constants.Endpoints.User('@me').Guild(guildID).settings, true, data);
}
} }
module.exports = RESTMethods; module.exports = RESTMethods;

View File

@@ -40,6 +40,12 @@ class BurstRequestHandler extends RequestHandler {
this.handle(); this.handle();
this.resetTimeout = null; this.resetTimeout = null;
}, Number(res.headers['retry-after']) + this.client.options.restTimeOffset); }, Number(res.headers['retry-after']) + this.client.options.restTimeOffset);
} else if (err.status >= 500 && err.status < 600) {
this.queue.unshift(item);
this.resetTimeout = this.client.setTimeout(() => {
this.handle();
this.resetTimeout = null;
}, 1e3 + this.client.options.restTimeOffset);
} else { } else {
item.reject(err.status === 400 ? new DiscordAPIError(res.body) : err); item.reject(err.status === 400 ? new DiscordAPIError(res.body) : err);
this.handle(); this.handle();

View File

@@ -64,6 +64,9 @@ class SequentialRequestHandler extends RequestHandler {
resolve(); resolve();
}, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset); }, Number(res.headers['retry-after']) + this.restManager.client.options.restTimeOffset);
if (res.headers['x-ratelimit-global']) this.globalLimit = true; if (res.headers['x-ratelimit-global']) this.globalLimit = true;
} else if (err.status >= 500 && err.status < 600) {
this.queue.unshift(item);
this.restManager.client.setTimeout(resolve, 1e3 + this.client.options.restTimeOffset);
} else { } else {
item.reject(err.status >= 400 && err.status < 500 ? new DiscordAPIError(res.body) : err); item.reject(err.status >= 400 && err.status < 500 ? new DiscordAPIError(res.body) : err);
resolve(err); resolve(err);

View File

@@ -39,6 +39,7 @@ class WebSocketPacketManager {
this.register(Constants.WSEvents.USER_UPDATE, require('./handlers/UserUpdate')); this.register(Constants.WSEvents.USER_UPDATE, require('./handlers/UserUpdate'));
this.register(Constants.WSEvents.USER_NOTE_UPDATE, require('./handlers/UserNoteUpdate')); this.register(Constants.WSEvents.USER_NOTE_UPDATE, require('./handlers/UserNoteUpdate'));
this.register(Constants.WSEvents.USER_SETTINGS_UPDATE, require('./handlers/UserSettingsUpdate')); this.register(Constants.WSEvents.USER_SETTINGS_UPDATE, require('./handlers/UserSettingsUpdate'));
this.register(Constants.WSEvents.USER_GUILD_SETTINGS_UPDATE, require('./handlers/UserGuildSettingsUpdate'));
this.register(Constants.WSEvents.VOICE_STATE_UPDATE, require('./handlers/VoiceStateUpdate')); this.register(Constants.WSEvents.VOICE_STATE_UPDATE, require('./handlers/VoiceStateUpdate'));
this.register(Constants.WSEvents.TYPING_START, require('./handlers/TypingStart')); this.register(Constants.WSEvents.TYPING_START, require('./handlers/TypingStart'));
this.register(Constants.WSEvents.MESSAGE_CREATE, require('./handlers/MessageCreate')); this.register(Constants.WSEvents.MESSAGE_CREATE, require('./handlers/MessageCreate'));

View File

@@ -10,6 +10,7 @@ class ReadyHandler extends AbstractHandler {
client.ws.heartbeat(); client.ws.heartbeat();
data.user.user_settings = data.user_settings; data.user.user_settings = data.user_settings;
data.user.user_guild_settings = data.user_guild_settings;
const clientUser = new ClientUser(client, data.user); const clientUser = new ClientUser(client, data.user);
client.user = clientUser; client.user = clientUser;

View File

@@ -0,0 +1,18 @@
const AbstractHandler = require('./AbstractHandler');
const Constants = require('../../../../util/Constants');
class UserGuildSettingsUpdateHandler extends AbstractHandler {
handle(packet) {
const client = this.packetManager.client;
client.user.guildSettings.get(packet.d.guild_id).patch(packet.d);
client.emit(Constants.Events.USER_GUILD_SETTINGS_UPDATE, client.user.guildSettings.get(packet.d.guild_id));
}
}
/**
* Emitted whenever the client user's settings update.
* @event Client#clientUserGuildSettingsUpdate
* @param {ClientUserGuildSettings} clientUserGuildSettings The new client user guild settings
*/
module.exports = UserGuildSettingsUpdateHandler;

View File

@@ -1,6 +1,7 @@
const User = require('./User'); const User = require('./User');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const ClientUserSettings = require('./ClientUserSettings'); const ClientUserSettings = require('./ClientUserSettings');
const ClientUserGuildSettings = require('./ClientUserGuildSettings');
const Constants = require('../util/Constants'); const Constants = require('../util/Constants');
/** /**
@@ -73,6 +74,18 @@ class ClientUser extends User {
* @type {?ClientUserSettings} * @type {?ClientUserSettings}
*/ */
this.settings = data.user_settings ? new ClientUserSettings(this, data.user_settings) : null; 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) { 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 * The position of this guild
* <warn>This is only available when using a user account.</warn> * <warn>This is only available when using a user account.</warn>
* @type {?number} * @type {?number}
* @readonly
*/ */
get position() { get position() {
if (this.client.user.bot) return null; if (this.client.user.bot) return null;
@@ -337,6 +338,66 @@ class Guild {
return this.client.user.settings.guildPositions.indexOf(this.id); 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 * The `@everyone` role of the guild
* @type {Role} * @type {Role}

View File

@@ -3,6 +3,7 @@ const Role = require('./Role');
const PermissionOverwrites = require('./PermissionOverwrites'); const PermissionOverwrites = require('./PermissionOverwrites');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
const Constants = require('../util/Constants');
/** /**
* Represents a guild channel (i.e. text channels and voice channels). * 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); 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. * When concatenated with a string, this automatically returns the channel's mention instead of the Channel object.
* @returns {string} * @returns {string}

View File

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

View File

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

View File

@@ -102,7 +102,10 @@ const Endpoints = exports.Endpoints = {
relationships: `${base}/relationships`, relationships: `${base}/relationships`,
settings: `${base}/settings`, settings: `${base}/settings`,
Relationship: uID => `${base}/relationships/${uID}`, Relationship: uID => `${base}/relationships/${uID}`,
Guild: guildID => `${base}/guilds/${guildID}`, Guild: guildID => ({
toString: () => `${base}/guilds/${guildID}`,
settings: `${base}/guilds/${guildID}/settings`,
}),
Note: id => `${base}/notes/${id}`, Note: id => `${base}/notes/${id}`,
Mentions: (limit, roles, everyone, guildID) => Mentions: (limit, roles, everyone, guildID) =>
`${base}/mentions?limit=${limit}&roles=${roles}&everyone=${everyone}${guildID ? `&guild_id=${guildID}` : ''}`, `${base}/mentions?limit=${limit}&roles=${roles}&everyone=${everyone}${guildID ? `&guild_id=${guildID}` : ''}`,
@@ -200,7 +203,8 @@ const Endpoints = exports.Endpoints = {
const base = `/oauth2/applications/${appID}`; const base = `/oauth2/applications/${appID}`;
return { return {
toString: () => base, toString: () => base,
reset: `${base}/reset`, resetSecret: `${base}/reset`,
resetToken: `${base}/bot/reset`,
}; };
}, },
App: appID => `/oauth2/authorize?client_id=${appID}`, App: appID => `/oauth2/authorize?client_id=${appID}`,
@@ -320,6 +324,7 @@ exports.Events = {
USER_UPDATE: 'userUpdate', USER_UPDATE: 'userUpdate',
USER_NOTE_UPDATE: 'userNoteUpdate', USER_NOTE_UPDATE: 'userNoteUpdate',
USER_SETTINGS_UPDATE: 'clientUserSettingsUpdate', USER_SETTINGS_UPDATE: 'clientUserSettingsUpdate',
USER_GUILD_SETTINGS_UPDATE: 'clientUserGuildSettingsUpdate',
PRESENCE_UPDATE: 'presenceUpdate', PRESENCE_UPDATE: 'presenceUpdate',
VOICE_STATE_UPDATE: 'voiceStateUpdate', VOICE_STATE_UPDATE: 'voiceStateUpdate',
TYPING_START: 'typingStart', TYPING_START: 'typingStart',
@@ -401,6 +406,7 @@ exports.WSEvents = {
USER_UPDATE: 'USER_UPDATE', USER_UPDATE: 'USER_UPDATE',
USER_NOTE_UPDATE: 'USER_NOTE_UPDATE', USER_NOTE_UPDATE: 'USER_NOTE_UPDATE',
USER_SETTINGS_UPDATE: 'USER_SETTINGS_UPDATE', USER_SETTINGS_UPDATE: 'USER_SETTINGS_UPDATE',
USER_GUILD_SETTINGS_UPDATE: 'USER_GUILD_SETTINGS_UPDATE',
PRESENCE_UPDATE: 'PRESENCE_UPDATE', PRESENCE_UPDATE: 'PRESENCE_UPDATE',
VOICE_STATE_UPDATE: 'VOICE_STATE_UPDATE', VOICE_STATE_UPDATE: 'VOICE_STATE_UPDATE',
TYPING_START: 'TYPING_START', TYPING_START: 'TYPING_START',
@@ -432,6 +438,21 @@ exports.MessageTypes = [
'GUILD_MEMBER_JOIN', 'GUILD_MEMBER_JOIN',
]; ];
/**
* The type of a message notification setting. Here are the available types:
* * EVERYTHING
* * MENTIONS
* * NOTHING
* * INHERIT (only for GuildChannel)
* @typedef {string} MessageNotificationType
*/
exports.MessageNotificationTypes = [
'EVERYTHING',
'MENTIONS',
'NOTHING',
'INHERIT',
];
exports.DefaultAvatars = { exports.DefaultAvatars = {
BLURPLE: '6debd47ed13483642cf09e832ed0bc1b', BLURPLE: '6debd47ed13483642cf09e832ed0bc1b',
GREY: '322c936a8c8be1b803cd94861bdfa868', GREY: '322c936a8c8be1b803cd94861bdfa868',
@@ -579,6 +600,58 @@ exports.UserSettingsMap = {
}, },
}; };
exports.UserGuildSettingsMap = {
message_notifications: function messageNotifications(type) { // eslint-disable-line func-name-matching
/**
* The type of message that should notify you
* @name ClientUserGuildSettings#messageNotifications
* @type {MessageNotificationType}
*/
return exports.MessageNotificationTypes[type];
},
/**
* Whether to receive mobile push notifications
* @name ClientUserGuildSettings#mobilePush
* @type {boolean}
*/
mobile_push: 'mobilePush',
/**
* Whether the guild is muted
* @name ClientUserGuildSettings#muted
* @type {boolean}
*/
muted: 'muted',
/**
* Whether to suppress everyone mention
* @name ClientUserGuildSettings#suppressEveryone
* @type {boolean}
*/
suppress_everyone: 'suppressEveryone',
/**
* A collection containing all the channel overrides
* @name ClientUserGuildSettings#channelOverrides
* @type {Collection<ClientUserChannelOverride>}
*/
channel_overrides: 'channelOverrides',
};
exports.UserChannelOverrideMap = {
message_notifications: function messageNotifications(type) { // eslint-disable-line func-name-matching
/**
* The type of message that should notify you
* @name ClientUserChannelOverride#messageNotifications
* @type {MessageNotificationType}
*/
return exports.MessageNotificationTypes[type];
},
/**
* Whether the channel is muted
* @name ClientUserChannelOverride#muted
* @type {boolean}
*/
muted: 'muted',
};
exports.Colors = { exports.Colors = {
DEFAULT: 0x000000, DEFAULT: 0x000000,
AQUA: 0x1ABC9C, AQUA: 0x1ABC9C,