mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-15 11:03:30 +01:00
ClientUserGuildSettings: avoid uncaught exception and a bit of refactoring (#1885)
* refactor(ClientUserGuildSettings): make client first parameter of the constructor * refactor(ClientUserChannelOverride): patch if possible rather then reinstantiating every update * fix(ClientUserGuildSettings): avoid uncaught exception when patching newly joined guilds/gdms
This commit is contained in:
@@ -1,10 +1,13 @@
|
|||||||
const AbstractHandler = require('./AbstractHandler');
|
const AbstractHandler = require('./AbstractHandler');
|
||||||
const Constants = require('../../../../util/Constants');
|
const Constants = require('../../../../util/Constants');
|
||||||
|
const ClientUserGuildSettings = require('../../../../structures/ClientUserGuildSettings');
|
||||||
|
|
||||||
class UserGuildSettingsUpdateHandler extends AbstractHandler {
|
class UserGuildSettingsUpdateHandler extends AbstractHandler {
|
||||||
handle(packet) {
|
handle(packet) {
|
||||||
const client = this.packetManager.client;
|
const client = this.packetManager.client;
|
||||||
client.user.guildSettings.get(packet.d.guild_id).patch(packet.d);
|
const settings = client.user.guildSettings.get(packet.d.guild_id);
|
||||||
|
if (settings) settings.patch(packet.d);
|
||||||
|
else client.user.guildSettings.set(packet.d.guild_id, new ClientUserGuildSettings(this.client, packet.d));
|
||||||
client.emit(Constants.Events.USER_GUILD_SETTINGS_UPDATE, client.user.guildSettings.get(packet.d.guild_id));
|
client.emit(Constants.Events.USER_GUILD_SETTINGS_UPDATE, client.user.guildSettings.get(packet.d.guild_id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ class ClientUser extends User {
|
|||||||
this.guildSettings = new Collection();
|
this.guildSettings = new Collection();
|
||||||
if (data.user_guild_settings) {
|
if (data.user_guild_settings) {
|
||||||
for (const settings of data.user_guild_settings) {
|
for (const settings of data.user_guild_settings) {
|
||||||
this.guildSettings.set(settings.guild_id, new ClientUserGuildSettings(settings, this.client));
|
this.guildSettings.set(settings.guild_id, new ClientUserGuildSettings(this.client, settings));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const ClientUserChannelOverride = require('./ClientUserChannelOverride');
|
|||||||
* A wrapper around the ClientUser's guild settings.
|
* A wrapper around the ClientUser's guild settings.
|
||||||
*/
|
*/
|
||||||
class ClientUserGuildSettings {
|
class ClientUserGuildSettings {
|
||||||
constructor(data, client) {
|
constructor(client, data) {
|
||||||
/**
|
/**
|
||||||
* The client that created the instance of the ClientUserGuildSettings
|
* The client that created the instance of the ClientUserGuildSettings
|
||||||
* @name ClientUserGuildSettings#client
|
* @name ClientUserGuildSettings#client
|
||||||
@@ -33,8 +33,9 @@ class ClientUserGuildSettings {
|
|||||||
if (!data.hasOwnProperty(key)) continue;
|
if (!data.hasOwnProperty(key)) continue;
|
||||||
if (key === 'channel_overrides') {
|
if (key === 'channel_overrides') {
|
||||||
for (const channel of data[key]) {
|
for (const channel of data[key]) {
|
||||||
this.channelOverrides.set(channel.channel_id,
|
const override = this.channelOverrides.get(channel.channel_id);
|
||||||
new ClientUserChannelOverride(channel));
|
if (override) override.patch(channel);
|
||||||
|
else this.channelOverrides.set(channel.channel_id, new ClientUserChannelOverride(channel));
|
||||||
}
|
}
|
||||||
} else if (typeof value === 'function') {
|
} else if (typeof value === 'function') {
|
||||||
this[value.name] = value(data[key]);
|
this[value.name] = value(data[key]);
|
||||||
|
|||||||
Reference in New Issue
Block a user