Skip to content

Commit

Permalink
fix(Message): throw error on missing channel (#6581)
Browse files Browse the repository at this point in the history
Co-authored-by: D Trombett <73136330+DTrombett@users.noreply.github.com>
  • Loading branch information
ImRodry and DTrombett committed Sep 28, 2021
1 parent 3b14883 commit 60aa9ae
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/errors/Messages.js
Expand Up @@ -91,6 +91,7 @@ const Messages = {
GUILD_OWNED: 'Guild is owned by the client.',
GUILD_MEMBERS_TIMEOUT: "Members didn't arrive in time.",
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.',

INVALID_TYPE: (name, expected, an = false) => `Supplied ${name} is not a${an ? 'n' : ''} ${expected}.`,
Expand Down
23 changes: 17 additions & 6 deletions src/structures/Message.js
Expand Up @@ -569,7 +569,7 @@ class Message extends Base {
}
return Boolean(
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),
);
}

Expand All @@ -579,12 +579,13 @@ class Message extends Base {
* @readonly
*/
get pinnable() {
const { channel } = this;
return Boolean(
!this.system &&
!this.deleted &&
(!this.guild ||
(this.channel?.viewable &&
this.channel.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_MESSAGES, false))),
(channel?.viewable &&
channel?.permissionsFor(this.client.user)?.has(Permissions.FLAGS.MANAGE_MESSAGES, false))),
);
}

Expand All @@ -610,12 +611,13 @@ class Message extends Base {
const bitfield =
Permissions.FLAGS.SEND_MESSAGES |
(this.author.id === this.client.user.id ? Permissions.defaultBit : Permissions.FLAGS.MANAGE_MESSAGES);
const { channel } = this;
return Boolean(
this.channel?.type === 'GUILD_NEWS' &&
channel?.type === 'GUILD_NEWS' &&
!this.flags.has(MessageFlags.FLAGS.CROSSPOSTED) &&
this.type === 'DEFAULT' &&
this.channel.viewable &&
this.channel.permissionsFor(this.client.user)?.has(bitfield, false) &&
channel.viewable &&
channel.permissionsFor(this.client.user)?.has(bitfield, false) &&
!this.deleted,
);
}
Expand Down Expand Up @@ -645,6 +647,7 @@ class Message extends Base {
* .catch(console.error);
*/
edit(options) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
return this.channel.messages.edit(this, options);
}

Expand All @@ -660,6 +663,7 @@ class Message extends Base {
* }
*/
crosspost() {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
return this.channel.messages.crosspost(this.id);
}

Expand All @@ -673,6 +677,7 @@ class Message extends Base {
* .catch(console.error)
*/
async pin() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
await this.channel.messages.pin(this.id);
return this;
}
Expand All @@ -687,6 +692,7 @@ class Message extends Base {
* .catch(console.error)
*/
async unpin() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
await this.channel.messages.unpin(this.id);
return this;
}
Expand All @@ -707,6 +713,7 @@ class Message extends Base {
* .catch(console.error);
*/
async react(emoji) {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
emoji = this.client.emojis.resolveIdentifier(emoji);
await this.channel.messages.react(this.id, emoji);
return this.client.actions.MessageReactionAdd.handle({
Expand All @@ -727,6 +734,7 @@ class Message extends Base {
* .catch(console.error);
*/
async delete() {
if (!this.channel) throw new Error('CHANNEL_NOT_CACHED');
await this.channel.messages.delete(this.id);
return this;
}
Expand All @@ -749,6 +757,7 @@ class Message extends Base {
* .catch(console.error);
*/
reply(options) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
let data;

if (options instanceof MessagePayload) {
Expand Down Expand Up @@ -780,6 +789,7 @@ class Message extends Base {
* @returns {Promise<ThreadChannel>}
*/
startThread(options = {}) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
if (!['GUILD_TEXT', 'GUILD_NEWS'].includes(this.channel.type)) {
return Promise.reject(new Error('MESSAGE_THREAD_PARENT'));
}
Expand All @@ -793,6 +803,7 @@ class Message extends Base {
* @returns {Promise<Message>}
*/
fetch(force = true) {
if (!this.channel) return Promise.reject(new Error('CHANNEL_NOT_CACHED'));
return this.channel.messages.fetch(this.id, { force });
}

Expand Down

0 comments on commit 60aa9ae

Please sign in to comment.