fix(Message): throw error on missing channel (#6581)

Co-authored-by: D Trombett <73136330+DTrombett@users.noreply.github.com>
This commit is contained in:
Rodry
2021-09-28 17:51:42 +01:00
committed by GitHub
parent 3b14883e34
commit 60aa9ae478
2 changed files with 18 additions and 6 deletions

View File

@@ -91,6 +91,7 @@ const Messages = {
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.', GUILD_UNCACHED_ME: 'The client user as a member of this guild is uncached.',
CHANNEL_NOT_CACHED: 'Could not find the channel where this message came from in the cache!',
STAGE_CHANNEL_RESOLVE: 'Could not resolve channel to a stage channel.', STAGE_CHANNEL_RESOLVE: 'Could not resolve channel to a stage channel.',
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}.`,

View File

@@ -569,7 +569,7 @@ class Message extends Base {
} }
return Boolean( return Boolean(
this.author.id === this.client.user.id || this.author.id === this.client.user.id ||
this.channel.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_MESSAGES, false), this.channel?.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_MESSAGES, false),
); );
} }
@@ -579,12 +579,13 @@ class Message extends Base {
* @readonly * @readonly
*/ */
get pinnable() { get pinnable() {
const { channel } = this;
return Boolean( return Boolean(
!this.system && !this.system &&
!this.deleted && !this.deleted &&
(!this.guild || (!this.guild ||
(this.channel?.viewable && (channel?.viewable &&
this.channel.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_MESSAGES, false))), channel?.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_MESSAGES, false))),
); );
} }
@@ -610,12 +611,13 @@ class Message extends Base {
const bitfield = const bitfield =
Permissions.FLAGS.SEND_MESSAGES | Permissions.FLAGS.SEND_MESSAGES |
(this.author.id === this.client.user.id ? Permissions.defaultBit : Permissions.FLAGS.MANAGE_MESSAGES); (this.author.id === this.client.user.id ? Permissions.defaultBit : Permissions.FLAGS.MANAGE_MESSAGES);
const { channel } = this;
return Boolean( return Boolean(
this.channel?.type === 'GUILD_NEWS' && channel?.type === 'GUILD_NEWS' &&
!this.flags.has(MessageFlags.FLAGS.CROSSPOSTED) && !this.flags.has(MessageFlags.FLAGS.CROSSPOSTED) &&
this.type === 'DEFAULT' && this.type === 'DEFAULT' &&
this.channel.viewable && channel.viewable &&
this.channel.permissionsFor(this.client.user)?.has(bitfield, false) && channel.permissionsFor(this.client.user)?.has(bitfield, false) &&
!this.deleted, !this.deleted,
); );
} }
@@ -645,6 +647,7 @@ class Message extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
edit(options) { edit(options) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
return this.channel.messages.edit(this, options); return this.channel.messages.edit(this, options);
} }
@@ -660,6 +663,7 @@ class Message extends Base {
* } * }
*/ */
crosspost() { crosspost() {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
return this.channel.messages.crosspost(this.id); return this.channel.messages.crosspost(this.id);
} }
@@ -673,6 +677,7 @@ class Message extends Base {
* .catch(console.error) * .catch(console.error)
*/ */
async pin() { async pin() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
await this.channel.messages.pin(this.id); await this.channel.messages.pin(this.id);
return this; return this;
} }
@@ -687,6 +692,7 @@ class Message extends Base {
* .catch(console.error) * .catch(console.error)
*/ */
async unpin() { async unpin() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
await this.channel.messages.unpin(this.id); await this.channel.messages.unpin(this.id);
return this; return this;
} }
@@ -707,6 +713,7 @@ class Message extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
async react(emoji) { async react(emoji) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
emoji = this.client.emojis.resolveIdentifier(emoji); emoji = this.client.emojis.resolveIdentifier(emoji);
await this.channel.messages.react(this.id, emoji); await this.channel.messages.react(this.id, emoji);
return this.client.actions.MessageReactionAdd.handle({ return this.client.actions.MessageReactionAdd.handle({
@@ -727,6 +734,7 @@ class Message extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
async delete() { async delete() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
await this.channel.messages.delete(this.id); await this.channel.messages.delete(this.id);
return this; return this;
} }
@@ -749,6 +757,7 @@ class Message extends Base {
* .catch(console.error); * .catch(console.error);
*/ */
reply(options) { reply(options) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
let data; let data;
if (options instanceof MessagePayload) { if (options instanceof MessagePayload) {
@@ -780,6 +789,7 @@ class Message extends Base {
* @returns {Promise<ThreadChannel>} * @returns {Promise<ThreadChannel>}
*/ */
startThread(options = {}) { startThread(options = {}) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!['GUILD_TEXT', 'GUILD_NEWS'].includes(this.channel.type)) { if (!['GUILD_TEXT', 'GUILD_NEWS'].includes(this.channel.type)) {
return Promise.reject(new Error('MESSAGE_THREAD_PARENT')); return Promise.reject(new Error('MESSAGE_THREAD_PARENT'));
} }
@@ -793,6 +803,7 @@ class Message extends Base {
* @returns {Promise<Message>} * @returns {Promise<Message>}
*/ */
fetch(force = true) { fetch(force = true) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
return this.channel.messages.fetch(this.id, { force }); return this.channel.messages.fetch(this.id, { force });
} }