mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-19 04:38:17 +00:00
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:
+51
-48
@@ -144,36 +144,59 @@ export async function sendMessage(
|
|||||||
content: string | MessageContent,
|
content: string | MessageContent,
|
||||||
) {
|
) {
|
||||||
if (typeof content === "string") content = { content };
|
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(
|
const channel = await cacheHandlers.get("channels", channelID);
|
||||||
channelID,
|
// If the channel is cached, we can do extra checks/safety
|
||||||
["SEND_TTS_MESSAGES"],
|
if (channel) {
|
||||||
);
|
if (
|
||||||
if (
|
![ChannelTypes.DM, ChannelTypes.GUILD_NEWS, ChannelTypes.GUILD_TEXT]
|
||||||
content.tts &&
|
.includes(channel.type)
|
||||||
!hasSendTtsMessagesPerm
|
) {
|
||||||
) {
|
throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
|
||||||
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const hasEmbedLinksPerm = await botHasChannelPermissions(
|
const hasSendMessagesPerm = await botHasChannelPermissions(
|
||||||
channelID,
|
channelID,
|
||||||
["EMBED_LINKS"],
|
["SEND_MESSAGES"],
|
||||||
);
|
);
|
||||||
if (
|
if (
|
||||||
content.embed &&
|
!hasSendMessagesPerm
|
||||||
!hasEmbedLinksPerm
|
) {
|
||||||
) {
|
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||||
throw new Error(Errors.MISSING_EMBED_LINKS);
|
}
|
||||||
|
|
||||||
|
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
|
// 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);
|
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(
|
||||||
|
|||||||
@@ -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,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user