Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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