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
+51 -48
View File
@@ -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(
-2
View File
@@ -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,
);