fix(GuildEmojiManager): check for guild in methods that use it (#4886)

This commit is contained in:
Jan
2020-10-17 15:40:39 +02:00
committed by GitHub
parent 7db6978012
commit 728b3f939c
3 changed files with 8 additions and 4 deletions

View File

@@ -103,6 +103,8 @@ const Messages = {
FETCH_GROUP_DM_CHANNEL: "Bots don't have access to Group DM Channels and cannot fetch them", FETCH_GROUP_DM_CHANNEL: "Bots don't have access to Group DM Channels and cannot fetch them",
MEMBER_FETCH_NONCE_LENGTH: 'Nonce length must not exceed 32 characters.', MEMBER_FETCH_NONCE_LENGTH: 'Nonce length must not exceed 32 characters.',
GUILDEMOJIMANAGER_NO_GUILD: 'Method cannot be called from a Client instance.',
}; };
for (const [name, message] of Object.entries(Messages)) register(name, message); for (const [name, message] of Object.entries(Messages)) register(name, message);

View File

@@ -1,7 +1,7 @@
'use strict'; 'use strict';
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const { TypeError } = require('../errors'); const { Error, TypeError } = require('../errors');
const GuildEmoji = require('../structures/GuildEmoji'); const GuildEmoji = require('../structures/GuildEmoji');
const ReactionEmoji = require('../structures/ReactionEmoji'); const ReactionEmoji = require('../structures/ReactionEmoji');
const Collection = require('../util/Collection'); const Collection = require('../util/Collection');
@@ -16,9 +16,9 @@ class GuildEmojiManager extends BaseManager {
super(guild.client, iterable, GuildEmoji); super(guild.client, iterable, GuildEmoji);
/** /**
* The guild this manager belongs to * The guild this manager belongs to
* @type {Guild} * @type {?Guild}
*/ */
this.guild = guild; this.guild = 'name' in guild ? guild : null;
} }
/** /**
@@ -28,6 +28,7 @@ class GuildEmojiManager extends BaseManager {
*/ */
add(data, cache) { add(data, cache) {
if (!this.guild) throw new Error('GUILDEMOJIMANAGER_NO_GUILD');
return super.add(data, cache, { extras: [this.guild] }); return super.add(data, cache, { extras: [this.guild] });
} }
@@ -51,6 +52,7 @@ class GuildEmojiManager extends BaseManager {
* .catch(console.error); * .catch(console.error);
*/ */
async create(attachment, name, { roles, reason } = {}) { async create(attachment, name, { roles, reason } = {}) {
if (!this.guild) throw new Error('GUILDEMOJIMANAGER_NO_GUILD');
attachment = await DataResolver.resolveImage(attachment); attachment = await DataResolver.resolveImage(attachment);
if (!attachment) throw new TypeError('REQ_RESOURCE_TYPE'); if (!attachment) throw new TypeError('REQ_RESOURCE_TYPE');

2
typings/index.d.ts vendored
View File

@@ -1917,7 +1917,7 @@ declare module 'discord.js' {
export class GuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> { export class GuildEmojiManager extends BaseManager<Snowflake, GuildEmoji, EmojiResolvable> {
constructor(guild: Guild, iterable?: Iterable<any>); constructor(guild: Guild, iterable?: Iterable<any>);
public guild: Guild; public guild: Guild | null;
public create( public create(
attachment: BufferResolvable | Base64Resolvable, attachment: BufferResolvable | Base64Resolvable,
name: string, name: string,