Files
discordeno/plugins/permissions/src/messages/create.ts
T
7caab52b68 Forum channel (#2160)
* [WIP] Forum Channels

* Delete package-lock.json

🤕

* Add Message typings and fix createForumPost

* Remove unnecessary comment

* Update helpers/channels/forums/createForumPost.ts

Use transformer

Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>

* Update helpers/channels/forums/createForumPost.ts

Use Pascal case for `CreateForumPostWithMessage`

Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>

* Update helpers/channels/forums/createForumPost.ts

Use Pascal case for `CreateForumPostWithMessage`

Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>

* Remove unneeded types

* Revert Review and Edit it

* Add reason

Co-authored-by: meister03 <root@_HOSTNAME_>
Co-authored-by: LTS20050703 <87189679+lts20050703@users.noreply.github.com>
Co-authored-by: Skillz4Killz <23035000+Skillz4Killz@users.noreply.github.com>
2022-05-13 10:28:53 -04:00

196 lines
5.0 KiB
TypeScript

import { AllowedMentionsTypes, BotWithCache, ChannelTypes, PermissionStrings } from "../../deps.ts";
import { validateComponents } from "../components.ts";
import { requireBotChannelPermissions } from "../permissions.ts";
export function sendMessage(bot: BotWithCache) {
const sendMessageOld = bot.helpers.sendMessage;
bot.helpers.sendMessage = async function (
channelId,
content,
) {
if (typeof content === "string") {
throw new Error("TODO");
}
const channel = bot.channels.get(channelId);
if (
channel &&
[
ChannelTypes.GuildCategory,
ChannelTypes.GuildStageVoice,
ChannelTypes.GuildForum,
].includes(channel.type)
) {
throw new Error(
`Can not send message to a channel of this type. Channel ID: ${channelId}`,
);
}
if (
content.content &&
!bot.utils.validateLength(content.content, { max: 2000 })
) {
throw new Error("The content should not exceed 2000 characters.");
}
if (content.allowedMentions) {
if (content.allowedMentions.users?.length) {
if (
content.allowedMentions.parse?.includes(
AllowedMentionsTypes.UserMentions,
)
) {
content.allowedMentions.parse = content.allowedMentions.parse.filter((
p,
) => p !== "users");
}
if (content.allowedMentions.users.length > 100) {
content.allowedMentions.users = content.allowedMentions.users.slice(
0,
100,
);
}
}
if (content.allowedMentions.roles?.length) {
if (
content.allowedMentions.parse?.includes(
AllowedMentionsTypes.RoleMentions,
)
) {
content.allowedMentions.parse = content.allowedMentions.parse.filter((
p,
) => p !== "roles");
}
if (content.allowedMentions.roles.length > 100) {
content.allowedMentions.roles = content.allowedMentions.roles.slice(
0,
100,
);
}
}
}
if (content.components) {
validateComponents(bot, content.components);
}
if (channel) {
const requiredPerms: PermissionStrings[] = [];
if (channel.guildId) {
requiredPerms.push("SEND_MESSAGES");
}
if (content.tts) requiredPerms.push("SEND_TTS_MESSAGES");
if (content.messageReference) requiredPerms.push("READ_MESSAGE_HISTORY");
if (requiredPerms.length) {
requireBotChannelPermissions(bot, channel, requiredPerms);
}
}
return await sendMessageOld(channelId, content);
};
}
export function editMessage(bot: BotWithCache) {
const editMessageOld = bot.helpers.editMessage;
bot.helpers.editMessage = function (
channelId,
messageId,
content,
) {
if (typeof content === "string") {
throw new Error("TODO");
}
const message = bot.messages.get(messageId);
if (message) {
if (message.authorId !== bot.id) {
content = { flags: content.flags };
requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]);
}
}
if (content.allowedMentions) {
if (content.allowedMentions.users?.length) {
if (
content.allowedMentions.parse?.includes(
AllowedMentionsTypes.UserMentions,
)
) {
content.allowedMentions.parse = content.allowedMentions.parse.filter((
p,
) => p !== "users");
}
if (content.allowedMentions.users.length > 100) {
content.allowedMentions.users = content.allowedMentions.users.slice(
0,
100,
);
}
}
if (content.allowedMentions.roles?.length) {
if (
content.allowedMentions.parse?.includes(
AllowedMentionsTypes.RoleMentions,
)
) {
content.allowedMentions.parse = content.allowedMentions.parse.filter((
p,
) => p !== "roles");
}
if (content.allowedMentions.roles.length > 100) {
content.allowedMentions.roles = content.allowedMentions.roles.slice(
0,
100,
);
}
}
}
content.embeds?.splice(10);
if (
content.content &&
!bot.utils.validateLength(content.content, { max: 2000 })
) {
throw new Error(
"A message content can not contain more than 2000 characters.",
);
}
return editMessageOld(channelId, messageId, content);
};
}
export function publishMessage(bot: BotWithCache) {
const publishMessageOld = bot.helpers.publishMessage;
bot.helpers.publishMessage = function (
channelId,
messageId,
) {
const message = bot.messages.get(messageId);
requireBotChannelPermissions(
bot,
channelId,
message?.authorId === bot.id ? ["SEND_MESSAGES"] : ["MANAGE_MESSAGES"],
);
return publishMessageOld(channelId, messageId);
};
}
export default function setupCreateMessagePermChecks(bot: BotWithCache) {
sendMessage(bot);
editMessage(bot);
publishMessage(bot);
}