From f481962b9cd8901d69c54f5d1e4783f7c1a6ca05 Mon Sep 17 00:00:00 2001 From: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com> Date: Sun, 21 Feb 2021 12:21:56 -0500 Subject: [PATCH] fix(handlers/channel): rewrite permission handler for send*Message() (#535) * v8 stopped sending this * better safety handling * husky o husky where art though husky --- src/api/handlers/channel.ts | 99 +++++++++++++++++++------------------ src/api/handlers/member.ts | 2 - 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/api/handlers/channel.ts b/src/api/handlers/channel.ts index bb31a200d..3cf81ce6f 100644 --- a/src/api/handlers/channel.ts +++ b/src/api/handlers/channel.ts @@ -144,36 +144,59 @@ export async function sendMessage( content: string | MessageContent, ) { if (typeof content === "string") content = { content }; - const hasSendMessagesPerm = await botHasChannelPermissions( - channelID, - ["SEND_MESSAGES"], - ); - if ( - !hasSendMessagesPerm - ) { - throw new Error(Errors.MISSING_SEND_MESSAGES); - } - const hasSendTtsMessagesPerm = await botHasChannelPermissions( - channelID, - ["SEND_TTS_MESSAGES"], - ); - if ( - content.tts && - !hasSendTtsMessagesPerm - ) { - throw new Error(Errors.MISSING_SEND_TTS_MESSAGE); - } + const channel = await cacheHandlers.get("channels", channelID); + // If the channel is cached, we can do extra checks/safety + if (channel) { + if ( + ![ChannelTypes.DM, ChannelTypes.GUILD_NEWS, ChannelTypes.GUILD_TEXT] + .includes(channel.type) + ) { + throw new Error(Errors.CHANNEL_NOT_TEXT_BASED); + } - const hasEmbedLinksPerm = await botHasChannelPermissions( - channelID, - ["EMBED_LINKS"], - ); - if ( - content.embed && - !hasEmbedLinksPerm - ) { - throw new Error(Errors.MISSING_EMBED_LINKS); + const hasSendMessagesPerm = await botHasChannelPermissions( + channelID, + ["SEND_MESSAGES"], + ); + if ( + !hasSendMessagesPerm + ) { + throw new Error(Errors.MISSING_SEND_MESSAGES); + } + + const hasSendTtsMessagesPerm = await botHasChannelPermissions( + channelID, + ["SEND_TTS_MESSAGES"], + ); + if ( + content.tts && + !hasSendTtsMessagesPerm + ) { + throw new Error(Errors.MISSING_SEND_TTS_MESSAGE); + } + + const hasEmbedLinksPerm = await botHasChannelPermissions( + channelID, + ["EMBED_LINKS"], + ); + if ( + content.embed && + !hasEmbedLinksPerm + ) { + throw new Error(Errors.MISSING_EMBED_LINKS); + } + + if (content.mentions?.repliedUser) { + if ( + !(await botHasChannelPermissions( + channelID, + ["READ_MESSAGE_HISTORY"], + )) + ) { + throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY); + } + } } // Use ... for content length due to unicode characters and js .length handling @@ -205,26 +228,6 @@ export async function sendMessage( content.mentions.roles = content.mentions.roles.slice(0, 100); } } - - if (content.mentions.repliedUser) { - if ( - !(await botHasChannelPermissions( - channelID, - ["READ_MESSAGE_HISTORY"], - )) - ) { - throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY); - } - } - } - - const channel = await cacheHandlers.get("channels", channelID); - if (!channel) throw new Error(Errors.CHANNEL_NOT_FOUND); - if ( - ![ChannelTypes.DM, ChannelTypes.GUILD_NEWS, ChannelTypes.GUILD_TEXT] - .includes(channel.type) - ) { - throw new Error(Errors.CHANNEL_NOT_TEXT_BASED); } const result = await RequestManager.post( diff --git a/src/api/handlers/member.ts b/src/api/handlers/member.ts index 6ab9b6df3..143e48f9c 100644 --- a/src/api/handlers/member.ts +++ b/src/api/handlers/member.ts @@ -131,8 +131,6 @@ export async function sendDirectMessage( endpoints.USER_DM, { recipient_id: memberID }, ) as DMChannelCreatePayload; - // Channel create event will have added this channel to the cache - await cacheHandlers.delete("channels", dmChannelData.id); const channelStruct = await structures.createChannel( dmChannelData as unknown as ChannelCreatePayload, );