mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 02:23:31 +01:00
feat: throw custom error for uncached Guild#me (#3271)
* handle cases where Guild#me is uncached * fix id prop * remove unnecessary checks * space's requested changes
This commit is contained in:
@@ -79,6 +79,7 @@ const Messages = {
|
|||||||
GUILD_CHANNEL_ORPHAN: 'Could not find a parent to this guild channel.',
|
GUILD_CHANNEL_ORPHAN: 'Could not find a parent to this guild channel.',
|
||||||
GUILD_OWNED: 'Guild is owned by the client.',
|
GUILD_OWNED: 'Guild is owned by the client.',
|
||||||
GUILD_MEMBERS_TIMEOUT: 'Members didn\'t arrive in time.',
|
GUILD_MEMBERS_TIMEOUT: 'Members didn\'t arrive in time.',
|
||||||
|
GUILD_UNCACHED_ME: 'The client user as a member of this guild is uncached.',
|
||||||
|
|
||||||
INVALID_TYPE: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
|
INVALID_TYPE: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
|
||||||
|
|
||||||
|
|||||||
@@ -349,19 +349,20 @@ class GuildAuditLogsEntry {
|
|||||||
guild_id: guild.id,
|
guild_id: guild.id,
|
||||||
}));
|
}));
|
||||||
} else if (targetType === Targets.INVITE) {
|
} else if (targetType === Targets.INVITE) {
|
||||||
if (guild.me.permissions.has('MANAGE_GUILD')) {
|
this.target = guild.members.fetch(guild.client.user.id).then(me => {
|
||||||
const change = this.changes.find(c => c.key === 'code');
|
if (me.permissions.has('MANAGE_GUILD')) {
|
||||||
this.target = guild.fetchInvites()
|
const change = this.changes.find(c => c.key === 'code');
|
||||||
.then(invites => {
|
return guild.fetchInvites().then(invites => {
|
||||||
this.target = invites.find(i => i.code === (change.new || change.old));
|
this.target = invites.find(i => i.code === (change.new || change.old));
|
||||||
return this.target;
|
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.target = this.changes.reduce((o, c) => {
|
this.target = this.changes.reduce((o, c) => {
|
||||||
o[c.key] = c.new || c.old;
|
o[c.key] = c.new || c.old;
|
||||||
return o;
|
return o;
|
||||||
}, {});
|
}, {});
|
||||||
}
|
return this.target;
|
||||||
|
}
|
||||||
|
});
|
||||||
} else if (targetType === Targets.MESSAGE) {
|
} else if (targetType === Targets.MESSAGE) {
|
||||||
this.target = guild.client.users.get(data.target_id);
|
this.target = guild.client.users.get(data.target_id);
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ class GuildEmoji extends Emoji {
|
|||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get deletable() {
|
get deletable() {
|
||||||
|
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
|
||||||
return !this.managed &&
|
return !this.managed &&
|
||||||
this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
|
this.guild.me.hasPermission(Permissions.FLAGS.MANAGE_EMOJIS);
|
||||||
}
|
}
|
||||||
@@ -80,8 +81,11 @@ class GuildEmoji extends Emoji {
|
|||||||
fetchAuthor() {
|
fetchAuthor() {
|
||||||
if (this.managed) {
|
if (this.managed) {
|
||||||
return Promise.reject(new Error('EMOJI_MANAGED'));
|
return Promise.reject(new Error('EMOJI_MANAGED'));
|
||||||
} else if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
|
} else {
|
||||||
return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild));
|
if (!this.guild.me) return Promise.reject(new Error('GUILD_UNCACHED_ME'));
|
||||||
|
if (!this.guild.me.permissions.has(Permissions.FLAGS.MANAGE_EMOJIS)) {
|
||||||
|
return Promise.reject(new Error('MISSING_MANAGE_EMOJIS_PERMISSION', this.guild));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return this.client.api.guilds(this.guild.id).emojis(this.id).get()
|
return this.client.api.guilds(this.guild.id).emojis(this.id).get()
|
||||||
.then(emoji => this.client.users.add(emoji.user));
|
.then(emoji => this.client.users.add(emoji.user));
|
||||||
|
|||||||
@@ -201,6 +201,7 @@ class GuildMember extends Base {
|
|||||||
get manageable() {
|
get manageable() {
|
||||||
if (this.user.id === this.guild.ownerID) return false;
|
if (this.user.id === this.guild.ownerID) return false;
|
||||||
if (this.user.id === this.client.user.id) return false;
|
if (this.user.id === this.client.user.id) return false;
|
||||||
|
if (!this.guild.me) throw new Error('GUILD_UNCACHED_ME');
|
||||||
return this.guild.me.roles.highest.comparePositionTo(this.roles.highest) > 0;
|
return this.guild.me.roles.highest.comparePositionTo(this.roles.highest) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -100,6 +100,7 @@ class Invite extends Base {
|
|||||||
get deletable() {
|
get deletable() {
|
||||||
const guild = this.guild;
|
const guild = this.guild;
|
||||||
if (!guild || !this.client.guilds.has(guild.id)) return false;
|
if (!guild || !this.client.guilds.has(guild.id)) return false;
|
||||||
|
if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
|
||||||
return this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
|
return this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
|
||||||
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD);
|
guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user