fix(handlers/channel): rewrite permission handler for send*Message() (#535)

* v8 stopped sending this

* better safety handling

* husky o husky where art though husky
This commit is contained in:
Skillz4Killz
2021-02-21 12:21:56 -05:00
committed by GitHub
parent c81d28acfe
commit f481962b9c
2 changed files with 51 additions and 50 deletions
+23 -20
View File
@@ -144,6 +144,17 @@ export async function sendMessage(
content: string | MessageContent, content: string | MessageContent,
) { ) {
if (typeof content === "string") content = { content }; if (typeof content === "string") content = { content };
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 hasSendMessagesPerm = await botHasChannelPermissions( const hasSendMessagesPerm = await botHasChannelPermissions(
channelID, channelID,
["SEND_MESSAGES"], ["SEND_MESSAGES"],
@@ -176,6 +187,18 @@ export async function sendMessage(
throw new Error(Errors.MISSING_EMBED_LINKS); 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 // Use ... for content length due to unicode characters and js .length handling
if (content.content && [...content.content].length > 2000) { if (content.content && [...content.content].length > 2000) {
throw new Error(Errors.MESSAGE_MAX_LENGTH); throw new Error(Errors.MESSAGE_MAX_LENGTH);
@@ -205,26 +228,6 @@ export async function sendMessage(
content.mentions.roles = content.mentions.roles.slice(0, 100); 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( const result = await RequestManager.post(
-2
View File
@@ -131,8 +131,6 @@ export async function sendDirectMessage(
endpoints.USER_DM, endpoints.USER_DM,
{ recipient_id: memberID }, { recipient_id: memberID },
) as DMChannelCreatePayload; ) as DMChannelCreatePayload;
// Channel create event will have added this channel to the cache
await cacheHandlers.delete("channels", dmChannelData.id);
const channelStruct = await structures.createChannel( const channelStruct = await structures.createChannel(
dmChannelData as unknown as ChannelCreatePayload, dmChannelData as unknown as ChannelCreatePayload,
); );