From 4aba5ff72f9a6e67166d6da5dd4bc8541b0f9ae3 Mon Sep 17 00:00:00 2001 From: LTS20050703 Date: Sat, 10 Sep 2022 01:10:16 +0700 Subject: [PATCH] plugins: fix errors (#2446) --- plugins/helpers/mod.ts | 106 ++++---------- .../helpers/src/fetchAndRetrieveMembers.ts | 2 +- .../src/sendPrivateInteractionResponse.ts | 10 +- .../permissions/src/channels/createChannel.ts | 2 +- .../permissions/src/channels/deleteChannel.ts | 13 +- .../src/channels/deleteChannelOverwrite.ts | 16 --- .../deleteChannelPermissionOverride.ts | 16 +++ .../permissions/src/channels/editChannel.ts | 47 ++---- .../src/channels/editChannelOverwrite.ts | 15 -- .../editChannelPermissionOverrides.ts | 15 ++ .../src/channels/followAnnouncementChannel.ts | 15 ++ .../permissions/src/channels/followChannel.ts | 15 -- ...reateForumPost.ts => createForumThread.ts} | 8 +- .../permissions/src/channels/forums/mod.ts | 4 +- plugins/permissions/src/channels/mod.ts | 12 +- plugins/permissions/src/channels/stage.ts | 32 ++--- .../src/channels/threads/addThreadMember.ts | 24 ++++ .../src/channels/threads/addToThread.ts | 26 ---- .../channels/threads/getArchivedThreads.ts | 20 --- .../threads/getPrivateArchivedThreads.ts | 13 ++ .../getPrivateJoinedArchivedThreads.ts | 13 ++ .../threads/getPublicArchivedThreads.ts | 13 ++ .../permissions/src/channels/threads/mod.ts | 12 +- .../channels/threads/removeThreadMember.ts | 15 +- plugins/permissions/src/discovery.ts | 10 +- .../src/guilds/editWidgetSettings.ts | 16 +++ plugins/permissions/src/guilds/getAuditLog.ts | 12 ++ .../permissions/src/guilds/getAuditLogs.ts | 12 -- plugins/permissions/src/guilds/mod.ts | 16 +-- plugins/permissions/src/guilds/widget.ts | 16 --- .../permissions/src/interactions/commands.ts | 135 +++++++++--------- .../src/interactions/editFollowupMessage.ts | 39 +---- plugins/permissions/src/members/editBot.ts | 12 -- .../permissions/src/members/editBotMember.ts | 12 ++ plugins/permissions/src/members/mod.ts | 4 +- plugins/permissions/src/messages/reactions.ts | 74 +++------- plugins/permissions/src/webhooks/message.ts | 46 ++---- transformers/mod.ts | 4 +- 38 files changed, 357 insertions(+), 515 deletions(-) delete mode 100644 plugins/permissions/src/channels/deleteChannelOverwrite.ts create mode 100644 plugins/permissions/src/channels/deleteChannelPermissionOverride.ts delete mode 100644 plugins/permissions/src/channels/editChannelOverwrite.ts create mode 100644 plugins/permissions/src/channels/editChannelPermissionOverrides.ts create mode 100644 plugins/permissions/src/channels/followAnnouncementChannel.ts delete mode 100644 plugins/permissions/src/channels/followChannel.ts rename plugins/permissions/src/channels/forums/{createForumPost.ts => createForumThread.ts} (51%) create mode 100644 plugins/permissions/src/channels/threads/addThreadMember.ts delete mode 100644 plugins/permissions/src/channels/threads/addToThread.ts delete mode 100644 plugins/permissions/src/channels/threads/getArchivedThreads.ts create mode 100644 plugins/permissions/src/channels/threads/getPrivateArchivedThreads.ts create mode 100644 plugins/permissions/src/channels/threads/getPrivateJoinedArchivedThreads.ts create mode 100644 plugins/permissions/src/channels/threads/getPublicArchivedThreads.ts create mode 100644 plugins/permissions/src/guilds/editWidgetSettings.ts create mode 100644 plugins/permissions/src/guilds/getAuditLog.ts delete mode 100644 plugins/permissions/src/guilds/getAuditLogs.ts delete mode 100644 plugins/permissions/src/guilds/widget.ts delete mode 100644 plugins/permissions/src/members/editBot.ts create mode 100644 plugins/permissions/src/members/editBotMember.ts diff --git a/plugins/helpers/mod.ts b/plugins/helpers/mod.ts index f2e798629..41a13cd8c 100644 --- a/plugins/helpers/mod.ts +++ b/plugins/helpers/mod.ts @@ -1,3 +1,4 @@ +import { BotWithCache } from "../cache/src/addCacheCollections.ts"; import { ApplicationCommandOptionChoice, Bot, @@ -11,73 +12,44 @@ import { Message, } from "./deps.ts"; import { cloneChannel } from "./src/channels.ts"; -import { sendAutocompleteChoices } from "./src/sendAutoCompleteChoices.ts"; -import { sendDirectMessage } from "./src/sendDirectMessage.ts"; -import { suppressEmbeds } from "./src/suppressEmbeds.ts"; -import { archiveThread, editThread, lockThread, ModifyThread, unarchiveThread, unlockThread } from "./src/threads.ts"; import { disconnectMember } from "./src/disconnectMember.ts"; +import { fetchAndRetrieveMembers } from "./src/fetchAndRetrieveMembers.ts"; import { getMembersPaginated } from "./src/getMembersPaginated.ts"; import { moveMember } from "./src/moveMember.ts"; -import { fetchAndRetrieveMembers } from "./src/fetchAndRetrieveMembers.ts"; -import { BotWithCache } from "../cache/src/addCacheCollections.ts"; -import { sendTextMessage } from "./src/sendTextMessage.ts"; +import { sendAutocompleteChoices } from "./src/sendAutoCompleteChoices.ts"; +import { sendDirectMessage } from "./src/sendDirectMessage.ts"; import { sendPrivateInteractionResponse } from "./src/sendPrivateInteractionResponse.ts"; +import { sendTextMessage } from "./src/sendTextMessage.ts"; +import { suppressEmbeds } from "./src/suppressEmbeds.ts"; +import { archiveThread, editThread, lockThread, ModifyThread, unarchiveThread, unlockThread } from "./src/threads.ts"; export type BotWithHelpersPlugin = Omit & HelperFunctionsFromHelperPlugin; export interface HelperFunctionsFromHelperPlugin { helpers: FinalHelpers & { - fetchAndRetrieveMembers: ( - guildId: bigint, - ) => Promise>; - sendDirectMessage: ( - userId: bigint, - content: string | CreateMessage, - ) => Promise; - sendTextMessage: ( - channelId: bigint, - content: string | CreateMessage, - ) => Promise; + fetchAndRetrieveMembers: (guildId: bigint) => Promise>; + sendDirectMessage: (userId: bigint, content: string | CreateMessage) => Promise; + sendTextMessage: (channelId: bigint, content: string | CreateMessage) => Promise; sendPrivateInteractionResponse: ( id: bigint, token: string, options: InteractionResponse, - ) => Promise; - suppressEmbeds: ( - channelId: bigint, - messageId: bigint, - ) => Promise; + ) => Promise; + suppressEmbeds: (channelId: bigint, messageId: bigint) => Promise; archiveThread: (threadId: bigint) => Promise; unarchiveThread: (threadId: bigint) => Promise; lockThread: (threadId: bigint) => Promise; unlockThread: (threadId: bigint) => Promise; - editThread: ( - threadId: bigint, - options: ModifyThread, - reason?: string, - ) => Promise; - cloneChannel: ( - channel: Channel, - reason?: string, - ) => Promise; + editThread: (threadId: bigint, options: ModifyThread, reason?: string) => Promise; + cloneChannel: (channel: Channel, reason?: string) => Promise; sendAutocompleteChoices: ( interactionId: bigint, interactionToken: string, choices: ApplicationCommandOptionChoice[], ) => Promise; - disconnectMember: ( - guildId: bigint, - memberId: bigint, - ) => Promise; - getMembersPaginated: ( - guildId: bigint, - options: ListGuildMembers, - ) => Promise>; - moveMember: ( - guildId: bigint, - memberId: bigint, - channelId: bigint, - ) => Promise; + disconnectMember: (guildId: bigint, memberId: bigint) => Promise; + getMembersPaginated: (guildId: bigint, options: ListGuildMembers) => Promise>; + moveMember: (guildId: bigint, memberId: bigint, channelId: bigint) => Promise; }; } @@ -85,32 +57,21 @@ export function enableHelpersPlugin(rawBot: B): BotWithHelp // FORCE OVERRIDE THE TYPE SO WE CAN SETUP FUNCTIONS const bot = rawBot as unknown as BotWithHelpersPlugin; - bot.helpers.fetchAndRetrieveMembers = ( - guildId: bigint, - ) => fetchAndRetrieveMembers(bot as unknown as BotWithCache, guildId); - bot.helpers.sendDirectMessage = ( - userId: bigint, - content: string | CreateMessage, - ) => sendDirectMessage(bot, userId, content); - bot.helpers.sendTextMessage = ( - channelId: bigint, - content: string | CreateMessage, - ) => sendTextMessage(bot, channelId, content); - bot.helpers.sendPrivateInteractionResponse = ( - id: bigint, - token: string, - options: InteractionResponse, - ) => sendPrivateInteractionResponse(bot, id, token, options); + bot.helpers.fetchAndRetrieveMembers = (guildId: bigint) => + fetchAndRetrieveMembers(bot as unknown as BotWithCache, guildId); + bot.helpers.sendDirectMessage = (userId: bigint, content: string | CreateMessage) => + sendDirectMessage(bot, userId, content); + bot.helpers.sendTextMessage = (channelId: bigint, content: string | CreateMessage) => + sendTextMessage(bot, channelId, content); + bot.helpers.sendPrivateInteractionResponse = (id: bigint, token: string, options: InteractionResponse) => + sendPrivateInteractionResponse(bot, id, token, options); bot.helpers.suppressEmbeds = (channelId: bigint, messageId: bigint) => suppressEmbeds(bot, channelId, messageId); bot.helpers.archiveThread = (threadId: bigint) => archiveThread(bot, threadId); bot.helpers.unarchiveThread = (threadId: bigint) => unarchiveThread(bot, threadId); bot.helpers.lockThread = (threadId: bigint) => lockThread(bot, threadId); bot.helpers.unlockThread = (threadId: bigint) => unlockThread(bot, threadId); - bot.helpers.editThread = ( - threadId: bigint, - options: ModifyThread, - reason?: string, - ) => editThread(bot, threadId, options, reason); + bot.helpers.editThread = (threadId: bigint, options: ModifyThread, reason?: string) => + editThread(bot, threadId, options, reason); bot.helpers.cloneChannel = (channel: Channel, reason?: string) => cloneChannel(bot, channel, reason); bot.helpers.sendAutocompleteChoices = ( interactionId: bigint, @@ -118,15 +79,10 @@ export function enableHelpersPlugin(rawBot: B): BotWithHelp choices: ApplicationCommandOptionChoice[], ) => sendAutocompleteChoices(bot, interactionId, interactionToken, choices); bot.helpers.disconnectMember = (guildId: bigint, memberId: bigint) => disconnectMember(bot, guildId, memberId); - bot.helpers.getMembersPaginated = ( - guildId: bigint, - options: ListGuildMembers, - ) => getMembersPaginated(bot, guildId, options); - bot.helpers.moveMember = ( - guildId: bigint, - memberId: bigint, - channelId: bigint, - ) => moveMember(bot, guildId, memberId, channelId); + bot.helpers.getMembersPaginated = (guildId: bigint, options: ListGuildMembers) => + getMembersPaginated(bot, guildId, options); + bot.helpers.moveMember = (guildId: bigint, memberId: bigint, channelId: bigint) => + moveMember(bot, guildId, memberId, channelId); return bot as BotWithHelpersPlugin; } diff --git a/plugins/helpers/src/fetchAndRetrieveMembers.ts b/plugins/helpers/src/fetchAndRetrieveMembers.ts index 5fcd916f9..1efe5469a 100644 --- a/plugins/helpers/src/fetchAndRetrieveMembers.ts +++ b/plugins/helpers/src/fetchAndRetrieveMembers.ts @@ -11,6 +11,6 @@ export async function fetchAndRetrieveMembers(bot: BotWithCache, guildId: bigint throw new Error("The guild was not found in cache. Unable to fetch members for uncached guild."); } - await bot.helpers.fetchMembers(guildId, guild.shardId); + await bot.helpers.fetchMembers(guildId, { limit: 0 }); return bot.members.filter((member) => member.guildId === guildId); } diff --git a/plugins/helpers/src/sendPrivateInteractionResponse.ts b/plugins/helpers/src/sendPrivateInteractionResponse.ts index a34e2ae50..08324b034 100644 --- a/plugins/helpers/src/sendPrivateInteractionResponse.ts +++ b/plugins/helpers/src/sendPrivateInteractionResponse.ts @@ -1,12 +1,12 @@ -import type { Bot, InteractionResponse, Message } from "../deps.ts"; +import type { Bot, InteractionResponse } from "../deps.ts"; -/** sendInteractionResponse with ephemeral reply */ -export function sendPrivateInteractionResponse( +/** sendInteractionResponse with ephemeral reply */ +export async function sendPrivateInteractionResponse( bot: Bot, id: bigint, token: string, options: InteractionResponse, -): Promise { +) { if (options.data && !options.data?.flags) options.data.flags = 64; // private: true - return bot.helpers.sendInteractionResponse(id, token, options); + return await bot.helpers.sendInteractionResponse(id, token, options); } diff --git a/plugins/permissions/src/channels/createChannel.ts b/plugins/permissions/src/channels/createChannel.ts index 6e9cf771b..19390809c 100644 --- a/plugins/permissions/src/channels/createChannel.ts +++ b/plugins/permissions/src/channels/createChannel.ts @@ -22,7 +22,7 @@ export default function createChannel(bot: BotWithCache) { if (options?.permissionOverwrites) requiredPerms.push("MANAGE_ROLES"); - if (options?.type === ChannelTypes.GuildNews && !guild.toggles.has("news")) { + if (options?.type === ChannelTypes.GuildAnnouncement && !guild.toggles.has("news")) { throw new Error("The NEWS feature is missing in this guild to be able to modify the channel type."); } diff --git a/plugins/permissions/src/channels/deleteChannel.ts b/plugins/permissions/src/channels/deleteChannel.ts index 10677f27e..615057fd9 100644 --- a/plugins/permissions/src/channels/deleteChannel.ts +++ b/plugins/permissions/src/channels/deleteChannel.ts @@ -19,17 +19,10 @@ export default function deleteChannel(bot: BotWithCache) { throw new Error("UPDATES_CHANNEL_CANNOT_BE_DELETED"); } - const isThread = [ - ChannelTypes.GuildNewsThread, - ChannelTypes.GuildPublicThread, - ChannelTypes.GuildPrivateThread, - ].includes(channel.type); + const isThread = [ChannelTypes.AnnouncementThread, ChannelTypes.PublicThread, ChannelTypes.PrivateThread] + .includes(channel.type); - requireBotGuildPermissions( - bot, - guild, - isThread ? ["MANAGE_THREADS"] : ["MANAGE_CHANNELS"], - ); + requireBotGuildPermissions(bot, guild, isThread ? ["MANAGE_THREADS"] : ["MANAGE_CHANNELS"]); } return await deleteChannelOld(channelId, reason); diff --git a/plugins/permissions/src/channels/deleteChannelOverwrite.ts b/plugins/permissions/src/channels/deleteChannelOverwrite.ts deleted file mode 100644 index 6c57ce019..000000000 --- a/plugins/permissions/src/channels/deleteChannelOverwrite.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { BotWithCache } from "../../deps.ts"; -import { requireBotChannelPermissions } from "../permissions.ts"; - -export default function deleteChannelOverwrite(bot: BotWithCache) { - const deleteChannelOverwriteOld = bot.helpers.deleteChannelOverwrite; - - bot.helpers.deleteChannelOverwrite = async function (channelId, overwriteId) { - const channel = bot.channels.get(channelId); - - if (channel?.guildId) { - requireBotChannelPermissions(bot, channelId, ["MANAGE_ROLES"]); - } - - return await deleteChannelOverwriteOld(channelId, overwriteId); - }; -} diff --git a/plugins/permissions/src/channels/deleteChannelPermissionOverride.ts b/plugins/permissions/src/channels/deleteChannelPermissionOverride.ts new file mode 100644 index 000000000..baab11c22 --- /dev/null +++ b/plugins/permissions/src/channels/deleteChannelPermissionOverride.ts @@ -0,0 +1,16 @@ +import { BotWithCache } from "../../deps.ts"; +import { requireBotChannelPermissions } from "../permissions.ts"; + +export default function deleteChannelPermissionOverride(bot: BotWithCache) { + const deleteChannelPermissionOverrideOld = bot.helpers.deleteChannelPermissionOverride; + + bot.helpers.deleteChannelPermissionOverride = async function (channelId, overwriteId) { + const channel = bot.channels.get(channelId); + + if (channel?.guildId) { + requireBotChannelPermissions(bot, channelId, ["MANAGE_ROLES"]); + } + + return await deleteChannelPermissionOverrideOld(channelId, overwriteId); + }; +} diff --git a/plugins/permissions/src/channels/editChannel.ts b/plugins/permissions/src/channels/editChannel.ts index 37da2a285..6a06a465c 100644 --- a/plugins/permissions/src/channels/editChannel.ts +++ b/plugins/permissions/src/channels/editChannel.ts @@ -1,5 +1,4 @@ -import { PermissionStrings } from "../../deps.ts"; -import { BotWithCache, ChannelTypes, GuildFeatures } from "../../deps.ts"; +import { BotWithCache, ChannelTypes, PermissionStrings } from "../../deps.ts"; import { requireBotChannelPermissions } from "../permissions.ts"; export default function editChannel(bot: BotWithCache) { @@ -12,34 +11,22 @@ export default function editChannel(bot: BotWithCache) { const guild = bot.guilds.get(channel.guildId); if (options.rateLimitPerUser && options.rateLimitPerUser > 21600) { - throw new Error( - "Amount of seconds a user has to wait before sending another message must be between 0-21600", - ); + throw new Error("Amount of seconds a user has to wait before sending another message must be between 0-21600"); } if (options.name) { if (!bot.utils.validateLength(options.name, { min: 1, max: 100 })) { - throw new Error( - "The channel name must be between 1-100 characters.", - ); + throw new Error("The channel name must be between 1-100 characters."); } } - const isThread = [ - ChannelTypes.GuildNewsThread, - ChannelTypes.GuildPublicThread, - ChannelTypes.GuildPrivateThread, - ].includes(channel.type); + const isThread = [ChannelTypes.AnnouncementThread, ChannelTypes.PublicThread, ChannelTypes.PrivateThread] + .includes(channel.type); const requiredPerms: PermissionStrings[] = []; if (isThread) { - if ( - options.invitable !== undefined && - channel.type !== ChannelTypes.GuildPrivateThread - ) { - throw new Error( - "Invitable option is only allowed on private threads.", - ); + if (options.invitable !== undefined && channel.type !== ChannelTypes.PrivateThread) { + throw new Error("Invitable option is only allowed on private threads."); } // UNARCHIVING AN UNLOCKED CHANNEL SIMPLY REQUIRES SEND @@ -60,18 +47,12 @@ export default function editChannel(bot: BotWithCache) { } if (options.type) { - if ( - [ChannelTypes.GuildNews, ChannelTypes.GuildText].includes( - options.type, - ) - ) { + if ([ChannelTypes.GuildAnnouncement, ChannelTypes.GuildText].includes(options.type)) { throw new Error("Only news and text types can be modified."); } if (guild && !guild.toggles.has("news")) { - throw new Error( - "The NEWS feature is missing in this guild to be able to modify the channel type.", - ); + throw new Error("The NEWS feature is missing in this guild to be able to modify the channel type."); } } @@ -88,18 +69,12 @@ export default function editChannel(bot: BotWithCache) { if (options.parentId) { const category = bot.channels.get(options.parentId); if (category && category.type !== ChannelTypes.GuildCategory) { - throw new Error( - "The parent id must be for a category channel type.", - ); + throw new Error("The parent id must be for a category channel type."); } } } - requireBotChannelPermissions( - bot, - channel, - requiredPerms, - ); + requireBotChannelPermissions(bot, channel, requiredPerms); } return await editChannelOld(channelId, options, reason); diff --git a/plugins/permissions/src/channels/editChannelOverwrite.ts b/plugins/permissions/src/channels/editChannelOverwrite.ts deleted file mode 100644 index 8a6d0b539..000000000 --- a/plugins/permissions/src/channels/editChannelOverwrite.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { BotWithCache } from "../../deps.ts"; -import { requireBotChannelPermissions } from "../permissions.ts"; - -export default function editChannelOverwrite(bot: BotWithCache) { - const editChannelOverwriteOld = bot.helpers.editChannelOverwrite; - - bot.helpers.editChannelOverwrite = async function (channelId, overwrite) { - const channel = bot.channels.get(channelId); - if (channel?.guildId) { - requireBotChannelPermissions(bot, channelId, ["MANAGE_ROLES"]); - } - - return await editChannelOverwriteOld(channelId, overwrite); - }; -} diff --git a/plugins/permissions/src/channels/editChannelPermissionOverrides.ts b/plugins/permissions/src/channels/editChannelPermissionOverrides.ts new file mode 100644 index 000000000..715ba6c59 --- /dev/null +++ b/plugins/permissions/src/channels/editChannelPermissionOverrides.ts @@ -0,0 +1,15 @@ +import { BotWithCache } from "../../deps.ts"; +import { requireBotChannelPermissions } from "../permissions.ts"; + +export default function editChannelPermissionOverrides(bot: BotWithCache) { + const editChannelPermissionOverridesOld = bot.helpers.editChannelPermissionOverrides; + + bot.helpers.editChannelPermissionOverrides = async function (channelId, overwrite) { + const channel = bot.channels.get(channelId); + if (channel?.guildId) { + requireBotChannelPermissions(bot, channelId, ["MANAGE_ROLES"]); + } + + return await editChannelPermissionOverridesOld(channelId, overwrite); + }; +} diff --git a/plugins/permissions/src/channels/followAnnouncementChannel.ts b/plugins/permissions/src/channels/followAnnouncementChannel.ts new file mode 100644 index 000000000..a92c75226 --- /dev/null +++ b/plugins/permissions/src/channels/followAnnouncementChannel.ts @@ -0,0 +1,15 @@ +import { BotWithCache } from "../../deps.ts"; +import { requireBotChannelPermissions } from "../permissions.ts"; + +export default function followAnnouncementChannel(bot: BotWithCache) { + const followAnnouncementChannelOld = bot.helpers.followAnnouncementChannel; + + bot.helpers.followAnnouncementChannel = async function (sourceChannelId, targetChannelId) { + const channel = bot.channels.get(targetChannelId); + if (channel?.guildId) { + requireBotChannelPermissions(bot, channel, ["MANAGE_WEBHOOKS"]); + } + + return await followAnnouncementChannelOld(sourceChannelId, targetChannelId); + }; +} diff --git a/plugins/permissions/src/channels/followChannel.ts b/plugins/permissions/src/channels/followChannel.ts deleted file mode 100644 index 9505ea78f..000000000 --- a/plugins/permissions/src/channels/followChannel.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { BotWithCache } from "../../deps.ts"; -import { requireBotChannelPermissions } from "../permissions.ts"; - -export default function followChannel(bot: BotWithCache) { - const followChannelOld = bot.helpers.followChannel; - - bot.helpers.followChannel = async function (sourceChannelId, targetChannelId) { - const channel = bot.channels.get(targetChannelId); - if (channel?.guildId) { - requireBotChannelPermissions(bot, channel, ["MANAGE_WEBHOOKS"]); - } - - return await followChannelOld(sourceChannelId, targetChannelId); - }; -} diff --git a/plugins/permissions/src/channels/forums/createForumPost.ts b/plugins/permissions/src/channels/forums/createForumThread.ts similarity index 51% rename from plugins/permissions/src/channels/forums/createForumPost.ts rename to plugins/permissions/src/channels/forums/createForumThread.ts index 11d5182e0..40d38e5a9 100644 --- a/plugins/permissions/src/channels/forums/createForumPost.ts +++ b/plugins/permissions/src/channels/forums/createForumThread.ts @@ -1,16 +1,16 @@ import { BotWithCache } from "../../../deps.ts"; import { requireBotChannelPermissions } from "../../permissions.ts"; -export default function createForumPosts(bot: BotWithCache) { - const createForumPostsOld = bot.helpers.createForumPost; +export default function createForumThread(bot: BotWithCache) { + const createForumThreadOld = bot.helpers.createForumThread; - bot.helpers.createForumPost = async function (channelId, options) { + bot.helpers.createForumThread = async function (channelId, options) { const channel = bot.channels.get(channelId); if (channel) { requireBotChannelPermissions(bot, channel, ["SEND_MESSAGES"]); } - return await createForumPostsOld(channelId, options); + return await createForumThreadOld(channelId, options); }; } diff --git a/plugins/permissions/src/channels/forums/mod.ts b/plugins/permissions/src/channels/forums/mod.ts index 48daa34da..d8268c7ba 100644 --- a/plugins/permissions/src/channels/forums/mod.ts +++ b/plugins/permissions/src/channels/forums/mod.ts @@ -1,6 +1,6 @@ import { BotWithCache } from "../../../deps.ts"; -import createForumPost from "./createForumPost.ts"; +import createForumThread from "./createForumThread.ts"; export default function setupThreadPermChecks(bot: BotWithCache) { - createForumPost(bot); + createForumThread(bot); } diff --git a/plugins/permissions/src/channels/mod.ts b/plugins/permissions/src/channels/mod.ts index 46f3a1c90..316abf59e 100644 --- a/plugins/permissions/src/channels/mod.ts +++ b/plugins/permissions/src/channels/mod.ts @@ -1,10 +1,10 @@ import { BotWithCache } from "../../deps.ts"; import createChannel from "./createChannel.ts"; import deleteChannel from "./deleteChannel.ts"; -import deleteChannelOverwrite from "./deleteChannelOverwrite.ts"; +import deleteChannelPermissionOverride from "./deleteChannelPermissionOverride.ts"; import editChannel from "./editChannel.ts"; -import editChannelOverwrite from "./editChannelOverwrite.ts"; -import followChannel from "./followChannel.ts"; +import editChannelPermissionOverrides from "./editChannelPermissionOverrides.ts"; +import followAnnouncementChannel from "./followAnnouncementChannel.ts"; import setupForumPermChecks from "./forums/mod.ts"; import getChannelWebhooks from "./getChannelWebhooks.ts"; import setupStagePermChecks from "./stage.ts"; @@ -17,10 +17,10 @@ export default function setupChannelPermChecks(bot: BotWithCache) { setupForumPermChecks(bot); setupStagePermChecks(bot); deleteChannel(bot); - deleteChannelOverwrite(bot); + deleteChannelPermissionOverride(bot); editChannel(bot); - editChannelOverwrite(bot); - followChannel(bot); + editChannelPermissionOverrides(bot); + followAnnouncementChannel(bot); getChannelWebhooks(bot); swapChannels(bot); } diff --git a/plugins/permissions/src/channels/stage.ts b/plugins/permissions/src/channels/stage.ts index e8c9c2167..dff510951 100644 --- a/plugins/permissions/src/channels/stage.ts +++ b/plugins/permissions/src/channels/stage.ts @@ -6,16 +6,10 @@ export function createStageInstance(bot: BotWithCache) { bot.helpers.createStageInstance = async function (options) { if (!bot.utils.validateLength(options.topic, { max: 120, min: 1 })) { - throw new Error( - "The topic length for creating a stage instance must be between 1-120.", - ); + throw new Error("The topic length for creating a stage instance must be between 1-120."); } - const perms = new Set([ - "MANAGE_CHANNELS", - "MUTE_MEMBERS", - "MOVE_MEMBERS", - ]); + const perms = new Set(["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]); if (options.sendStartNotification) { perms.add("MENTION_EVERYONE"); @@ -31,32 +25,24 @@ export function deleteStageInstance(bot: BotWithCache) { const deleteStageInstanceOld = bot.helpers.deleteStageInstance; bot.helpers.deleteStageInstance = async function (channelId) { - requireBotChannelPermissions(bot, channelId, [ - "MANAGE_CHANNELS", - "MUTE_MEMBERS", - "MOVE_MEMBERS", - ]); + requireBotChannelPermissions(bot, channelId, ["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]); return await deleteStageInstanceOld(channelId); }; } -export function updateStageInstance(bot: BotWithCache) { - const updateStageInstanceOld = bot.helpers.updateStageInstance; +export function editStageInstance(bot: BotWithCache) { + const editStageInstanceOld = bot.helpers.editStageInstance; - bot.helpers.updateStageInstance = async function (channelId, data) { - requireBotChannelPermissions(bot, channelId, [ - "MANAGE_CHANNELS", - "MUTE_MEMBERS", - "MOVE_MEMBERS", - ]); + bot.helpers.editStageInstance = async function (channelId, data) { + requireBotChannelPermissions(bot, channelId, ["MANAGE_CHANNELS", "MUTE_MEMBERS", "MOVE_MEMBERS"]); - return await updateStageInstanceOld(channelId, data); + return await editStageInstanceOld(channelId, data); }; } export default function setupStagePermChecks(bot: BotWithCache) { createStageInstance(bot); deleteStageInstance(bot); - updateStageInstance(bot); + editStageInstance(bot); } diff --git a/plugins/permissions/src/channels/threads/addThreadMember.ts b/plugins/permissions/src/channels/threads/addThreadMember.ts new file mode 100644 index 000000000..599b11a96 --- /dev/null +++ b/plugins/permissions/src/channels/threads/addThreadMember.ts @@ -0,0 +1,24 @@ +import { BotWithCache } from "../../../deps.ts"; +import { requireBotChannelPermissions } from "../../permissions.ts"; + +export default function addThreadMember(bot: BotWithCache) { + const addThreadMemberOld = bot.helpers.addThreadMember; + + bot.helpers.addThreadMember = async function (threadId, userId) { + if (userId === bot.id) { + throw new Error("To add the bot to a thread, you must use bot.helpers.joinThread()"); + } + + const channel = bot.channels.get(threadId); + + if (channel) { + if (channel.archived) { + throw new Error("Cannot add user to thread if thread is archived."); + } + + requireBotChannelPermissions(bot, channel, ["SEND_MESSAGES"]); + } + + return await addThreadMemberOld(threadId, userId); + }; +} diff --git a/plugins/permissions/src/channels/threads/addToThread.ts b/plugins/permissions/src/channels/threads/addToThread.ts deleted file mode 100644 index 000d20d63..000000000 --- a/plugins/permissions/src/channels/threads/addToThread.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { BotWithCache } from "../../../deps.ts"; -import { requireBotChannelPermissions } from "../../permissions.ts"; - -export default function addToThread(bot: BotWithCache) { - const addToThreadOld = bot.helpers.addToThread; - - bot.helpers.addToThread = async function (threadId, userId) { - if (userId === bot.id) { - throw new Error( - "To add the bot to a thread, you must use bot.helpers.joinThread()", - ); - } - - const channel = bot.channels.get(threadId); - - if (channel) { - if (channel.archived) { - throw new Error("Cannot add user to thread if thread is archived."); - } - - await requireBotChannelPermissions(bot, channel, ["SEND_MESSAGES"]); - } - - return await addToThreadOld(threadId, userId); - }; -} diff --git a/plugins/permissions/src/channels/threads/getArchivedThreads.ts b/plugins/permissions/src/channels/threads/getArchivedThreads.ts deleted file mode 100644 index 48ce2aa25..000000000 --- a/plugins/permissions/src/channels/threads/getArchivedThreads.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { BotWithCache } from "../../../deps.ts"; -import { requireBotChannelPermissions } from "../../permissions.ts"; - -export default function getArchivedThreads(bot: BotWithCache) { - const getArchivedThreadsOld = bot.helpers.getArchivedThreads; - - bot.helpers.getArchivedThreads = async function (channelId, options) { - const channel = await bot.channels.get(channelId); - - if (channel) { - await requireBotChannelPermissions( - bot, - channel, - options?.type === "private" ? ["READ_MESSAGE_HISTORY", "MANAGE_THREADS"] : ["READ_MESSAGE_HISTORY"], - ); - } - - return await getArchivedThreadsOld(channelId, options); - }; -} diff --git a/plugins/permissions/src/channels/threads/getPrivateArchivedThreads.ts b/plugins/permissions/src/channels/threads/getPrivateArchivedThreads.ts new file mode 100644 index 000000000..6d4343a87 --- /dev/null +++ b/plugins/permissions/src/channels/threads/getPrivateArchivedThreads.ts @@ -0,0 +1,13 @@ +import { BotWithCache } from "../../../deps.ts"; +import { requireBotChannelPermissions } from "../../permissions.ts"; + +export function getPrivateArchivedThreads(bot: BotWithCache) { + const getPrivateArchivedThreadsOld = bot.helpers.getPrivateArchivedThreads; + bot.helpers.getPublicArchivedThreads = async function (channelId, options) { + const channel = bot.channels.get(channelId); + if (channel) { + requireBotChannelPermissions(bot, channel, ["READ_MESSAGE_HISTORY", "MANAGE_MESSAGES"]); + } + return await getPrivateArchivedThreadsOld(channelId, options); + }; +} diff --git a/plugins/permissions/src/channels/threads/getPrivateJoinedArchivedThreads.ts b/plugins/permissions/src/channels/threads/getPrivateJoinedArchivedThreads.ts new file mode 100644 index 000000000..540001c8c --- /dev/null +++ b/plugins/permissions/src/channels/threads/getPrivateJoinedArchivedThreads.ts @@ -0,0 +1,13 @@ +import { BotWithCache } from "../../../deps.ts"; +import { requireBotChannelPermissions } from "../../permissions.ts"; + +export function getPrivateJoinedArchivedThreads(bot: BotWithCache) { + const getPrivateJoinedArchivedThreadsOld = bot.helpers.getPrivateJoinedArchivedThreads; + bot.helpers.getPublicArchivedThreads = async function (channelId, options) { + const channel = bot.channels.get(channelId); + if (channel) { + requireBotChannelPermissions(bot, channel, ["READ_MESSAGE_HISTORY"]); + } + return await getPrivateJoinedArchivedThreadsOld(channelId, options); + }; +} diff --git a/plugins/permissions/src/channels/threads/getPublicArchivedThreads.ts b/plugins/permissions/src/channels/threads/getPublicArchivedThreads.ts new file mode 100644 index 000000000..7fafa864a --- /dev/null +++ b/plugins/permissions/src/channels/threads/getPublicArchivedThreads.ts @@ -0,0 +1,13 @@ +import { BotWithCache } from "../../../deps.ts"; +import { requireBotChannelPermissions } from "../../permissions.ts"; + +export function getPublicArchivedThreads(bot: BotWithCache) { + const getPublicArchivedThreadsOld = bot.helpers.getPublicArchivedThreads; + bot.helpers.getPublicArchivedThreads = async function (channelId, options) { + const channel = bot.channels.get(channelId); + if (channel) { + requireBotChannelPermissions(bot, channel, ["READ_MESSAGE_HISTORY"]); + } + return await getPublicArchivedThreadsOld(channelId, options); + }; +} diff --git a/plugins/permissions/src/channels/threads/mod.ts b/plugins/permissions/src/channels/threads/mod.ts index 8c338398a..e5d067b6d 100644 --- a/plugins/permissions/src/channels/threads/mod.ts +++ b/plugins/permissions/src/channels/threads/mod.ts @@ -1,14 +1,18 @@ import { BotWithCache } from "../../../deps.ts"; -import addToThread from "./addToThread.ts"; -import getArchivedThreads from "./getArchivedThreads.ts"; +import addThreadMember from "./addThreadMember.ts"; +import { getPrivateArchivedThreads } from "./getPrivateArchivedThreads.ts"; +import { getPrivateJoinedArchivedThreads } from "./getPrivateJoinedArchivedThreads.ts"; +import { getPublicArchivedThreads } from "./getPublicArchivedThreads.ts"; import getThreadMembers from "./getThreadMembers.ts"; import joinThread from "./joinThread.ts"; import leaveThread from "./leaveThread.ts"; import removeThreadMember from "./removeThreadMember.ts"; export default function setupThreadPermChecks(bot: BotWithCache) { - addToThread(bot); - getArchivedThreads(bot); + addThreadMember(bot); + getPublicArchivedThreads(bot); + getPrivateArchivedThreads(bot); + getPrivateJoinedArchivedThreads(bot); getThreadMembers(bot); joinThread(bot); leaveThread(bot); diff --git a/plugins/permissions/src/channels/threads/removeThreadMember.ts b/plugins/permissions/src/channels/threads/removeThreadMember.ts index fabd893ec..fad3860fe 100644 --- a/plugins/permissions/src/channels/threads/removeThreadMember.ts +++ b/plugins/permissions/src/channels/threads/removeThreadMember.ts @@ -6,25 +6,18 @@ export default function removeThreadMember(bot: BotWithCache) { bot.helpers.removeThreadMember = async function (threadId, userId) { if (userId === bot.id) { - throw new Error( - "To remove the bot from a thread, you must use bot.helpers.leaveThread()", - ); + throw new Error("To remove the bot from a thread, you must use bot.helpers.leaveThread()"); } const channel = bot.channels.get(threadId); if (channel) { if (channel.archived) { - throw new Error( - "Cannot remove user from thread if thread is archived.", - ); + throw new Error("Cannot remove user from thread if thread is archived."); } - if ( - !(bot.id === channel.ownerId && - channel.type === ChannelTypes.GuildPrivateThread) - ) { - await requireBotChannelPermissions(bot, channel, ["MANAGE_MESSAGES"]); + if (!(bot.id === channel.ownerId && channel.type === ChannelTypes.PrivateThread)) { + requireBotChannelPermissions(bot, channel, ["MANAGE_MESSAGES"]); } } diff --git a/plugins/permissions/src/discovery.ts b/plugins/permissions/src/discovery.ts index 9ae3fe6ab..a4f353117 100644 --- a/plugins/permissions/src/discovery.ts +++ b/plugins/permissions/src/discovery.ts @@ -11,13 +11,13 @@ export function addDiscoverySubcategory(bot: BotWithCache) { }; } -export function removeDiscoverySubcategory(bot: BotWithCache) { - const removeDiscoverySubcategoryOld = bot.helpers.removeDiscoverySubcategory; +export function deleteDiscoverySubcategory(bot: BotWithCache) { + const deleteDiscoverySubcategoryOld = bot.helpers.deleteDiscoverySubcategory; - bot.helpers.removeDiscoverySubcategory = async function (guildId, categoryId) { + bot.helpers.deleteDiscoverySubcategory = async function (guildId, categoryId) { requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - return await removeDiscoverySubcategoryOld(guildId, categoryId); + return await deleteDiscoverySubcategoryOld(guildId, categoryId); }; } @@ -45,5 +45,5 @@ export default function setupDiscoveryPermChecks(bot: BotWithCache) { addDiscoverySubcategory(bot); editDiscovery(bot); getDiscovery(bot); - removeDiscoverySubcategory(bot); + deleteDiscoverySubcategory(bot); } diff --git a/plugins/permissions/src/guilds/editWidgetSettings.ts b/plugins/permissions/src/guilds/editWidgetSettings.ts new file mode 100644 index 000000000..215791ce3 --- /dev/null +++ b/plugins/permissions/src/guilds/editWidgetSettings.ts @@ -0,0 +1,16 @@ +import { BotWithCache } from "../../deps.ts"; +import { requireBotGuildPermissions } from "../permissions.ts"; + +export function editWidgetSettings(bot: BotWithCache) { + const editWidgetSettingsOld = bot.helpers.editWidgetSettings; + + bot.helpers.editWidgetSettings = async function (guildId, enabled, channelId) { + requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); + + return await editWidgetSettingsOld(guildId, enabled, channelId); + }; +} + +export default function (bot: BotWithCache) { + editWidgetSettings(bot); +} diff --git a/plugins/permissions/src/guilds/getAuditLog.ts b/plugins/permissions/src/guilds/getAuditLog.ts new file mode 100644 index 000000000..121731d7b --- /dev/null +++ b/plugins/permissions/src/guilds/getAuditLog.ts @@ -0,0 +1,12 @@ +import { BotWithCache } from "../../deps.ts"; +import { requireBotGuildPermissions } from "../permissions.ts"; + +export default function getAuditLog(bot: BotWithCache) { + const getAuditLogOld = bot.helpers.getAuditLog; + + bot.helpers.getAuditLog = async function (guildId, options) { + requireBotGuildPermissions(bot, guildId, ["VIEW_AUDIT_LOG"]); + + return await getAuditLogOld(guildId, options); + }; +} diff --git a/plugins/permissions/src/guilds/getAuditLogs.ts b/plugins/permissions/src/guilds/getAuditLogs.ts deleted file mode 100644 index 219398ba4..000000000 --- a/plugins/permissions/src/guilds/getAuditLogs.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { BotWithCache } from "../../deps.ts"; -import { requireBotGuildPermissions } from "../permissions.ts"; - -export default function getAuditLogs(bot: BotWithCache) { - const getAuditLogsOld = bot.helpers.getAuditLogs; - - bot.helpers.getAuditLogs = async function (guildId, options) { - requireBotGuildPermissions(bot, guildId, ["VIEW_AUDIT_LOG"]); - - return await getAuditLogsOld(guildId, options); - }; -} diff --git a/plugins/permissions/src/guilds/mod.ts b/plugins/permissions/src/guilds/mod.ts index d87c4edcb..0cd1561e0 100644 --- a/plugins/permissions/src/guilds/mod.ts +++ b/plugins/permissions/src/guilds/mod.ts @@ -1,26 +1,26 @@ import { BotWithCache } from "../../deps.ts"; -import setupEventsPermChecks from "./events.ts"; -import setupWelcomeScreenPermChecks from "./welcomeScreen.ts"; -import setupWidgetPermChecks from "./widget.ts"; import createGuild from "./createGuild.ts"; import deleteGuild from "./deleteGuild.ts"; import editGuild from "./editGuild.ts"; -import getAuditLogs from "./getAuditLogs.ts"; +import editWidgetSettings from "./editWidgetSettings.ts"; +import setupEventsPermChecks from "./events.ts"; +import getAuditLog from "./getAuditLog.ts"; import getBan from "./getBan.ts"; import getBans from "./getBans.ts"; import getPruneCount from "./getPruneCount.ts"; import getVanityUrl from "./getVanityUrl.ts"; +import setupWelcomeScreenPermChecks from "./welcomeScreen.ts"; export default function setupGuildPermChecks(bot: BotWithCache) { - setupEventsPermChecks(bot); createGuild(bot); deleteGuild(bot); editGuild(bot); - setupWelcomeScreenPermChecks(bot); - setupWidgetPermChecks(bot); - getAuditLogs(bot); + editWidgetSettings(bot); + setupEventsPermChecks(bot); + getAuditLog(bot); getBan(bot); getBans(bot); getPruneCount(bot); getVanityUrl(bot); + setupWelcomeScreenPermChecks(bot); } diff --git a/plugins/permissions/src/guilds/widget.ts b/plugins/permissions/src/guilds/widget.ts deleted file mode 100644 index 68ba9f72d..000000000 --- a/plugins/permissions/src/guilds/widget.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { BotWithCache } from "../../deps.ts"; -import { requireBotGuildPermissions } from "../permissions.ts"; - -export function editWidget(bot: BotWithCache) { - const editWidgetOld = bot.helpers.editWidget; - - bot.helpers.editWidget = async function (guildId, enabled, channelId) { - requireBotGuildPermissions(bot, guildId, ["MANAGE_GUILD"]); - - return await editWidgetOld(guildId, enabled, channelId); - }; -} - -export default function setupWidgetPermChecks(bot: BotWithCache) { - editWidget(bot); -} diff --git a/plugins/permissions/src/interactions/commands.ts b/plugins/permissions/src/interactions/commands.ts index 999e8e0af..4021bd94d 100644 --- a/plugins/permissions/src/interactions/commands.ts +++ b/plugins/permissions/src/interactions/commands.ts @@ -7,12 +7,8 @@ import { CONTEXT_MENU_COMMANDS_NAME_REGEX, SLASH_COMMANDS_NAME_REGEX, } from "../../deps.ts"; -import { validateAttachments } from "../attachments.ts"; -export function validateApplicationCommandOptions( - bot: BotWithCache, - options: ApplicationCommandOption[], -) { +export function validateApplicationCommandOptions(bot: BotWithCache, options: ApplicationCommandOption[]) { const requiredOptions: ApplicationCommandOption[] = []; const optionalOptions: ApplicationCommandOption[] = []; @@ -25,8 +21,7 @@ export function validateApplicationCommandOptions( } if ( - option.type !== ApplicationCommandOptionTypes.String && - option.type !== ApplicationCommandOptionTypes.Integer + option.type !== ApplicationCommandOptionTypes.String && option.type !== ApplicationCommandOptionTypes.Integer ) { throw new Error("Only string or integer options can have choices."); } @@ -42,23 +37,17 @@ export function validateApplicationCommandOptions( option.choices?.every((choice) => { if (!bot.utils.validateLength(choice.name, { min: 1, max: 100 })) { - throw new Error( - "Invalid application command option choice name. Must be between 1-100 characters long.", - ); + throw new Error("Invalid application command option choice name. Must be between 1-100 characters long."); } if ( option.type === ApplicationCommandOptionTypes.String && - (typeof choice.value !== "string" || choice.value.length < 1 || - choice.value.length > 100) + (typeof choice.value !== "string" || choice.value.length < 1 || choice.value.length > 100) ) { throw new Error("Invalid slash options choice value type."); } - if ( - option.type === ApplicationCommandOptionTypes.Integer && - typeof choice.value !== "number" - ) { + if (option.type === ApplicationCommandOptionTypes.Integer && typeof choice.value !== "number") { throw new Error("A number must be set for Integer types."); } }); @@ -74,12 +63,11 @@ export function validateApplicationCommandOptions( return [...requiredOptions, ...optionalOptions]; } -export function createApplicationCommand(bot: BotWithCache) { - const createApplicationCommandOld = bot.helpers.createApplicationCommand; +export function createGuildApplicationCommand(bot: BotWithCache) { + const createGuildApplicationCommandOld = bot.helpers.createGuildApplicationCommand; - bot.helpers.createApplicationCommand = async function (options, guildId) { - const isChatInput = !options.type || - options.type === ApplicationCommandTypes.ChatInput; + bot.helpers.createGuildApplicationCommand = async function (options, guildId) { + const isChatInput = !options.type || options.type === ApplicationCommandTypes.ChatInput; if (!options.name) { throw new Error("A name is required to create a options."); @@ -87,9 +75,7 @@ export function createApplicationCommand(bot: BotWithCache) { if (isChatInput) { if (!SLASH_COMMANDS_NAME_REGEX.test(options.name)) { - throw new Error( - "The name of the slash command did not match the required regex.", - ); + throw new Error("The name of the slash command did not match the required regex."); } // Only slash need to be lowercase @@ -97,13 +83,9 @@ export function createApplicationCommand(bot: BotWithCache) { // Slash commands require description if (!options.description) { - throw new Error( - "Slash commands require some form of a description be provided.", - ); + throw new Error("Slash commands require some form of a description be provided."); } else if (!bot.utils.validateLength(options.description, { min: 1, max: 100 })) { - throw new Error( - "Application command descriptions must be between 1 and 100 characters.", - ); + throw new Error("Application command descriptions must be between 1 and 100 characters."); } if (options.options?.length) { @@ -115,20 +97,60 @@ export function createApplicationCommand(bot: BotWithCache) { } } else { if (!CONTEXT_MENU_COMMANDS_NAME_REGEX.test(options.name)) { - throw new Error( - "The name of the context menu did not match the required regex.", - ); + throw new Error("The name of the context menu did not match the required regex."); } } - return await createApplicationCommandOld(options, guildId); + return await createGuildApplicationCommandOld(options, guildId); }; } -export function editInteractionResponse(bot: BotWithCache) { - const editInteractionResponseOld = bot.helpers.editInteractionResponse; +export function createGlobalApplicationCommand(bot: BotWithCache) { + const createGlobalApplicationCommandOld = bot.helpers.createGlobalApplicationCommand; - bot.helpers.editInteractionResponse = async function (token, options) { + bot.helpers.createGlobalApplicationCommand = async function (options) { + const isChatInput = !options.type || options.type === ApplicationCommandTypes.ChatInput; + + if (!options.name) { + throw new Error("A name is required to create a options."); + } + + if (isChatInput) { + if (!SLASH_COMMANDS_NAME_REGEX.test(options.name)) { + throw new Error("The name of the slash command did not match the required regex."); + } + + // Only slash need to be lowercase + options.name = options.name.toLowerCase(); + + // Slash commands require description + if (!options.description) { + throw new Error("Slash commands require some form of a description be provided."); + } else if (!bot.utils.validateLength(options.description, { min: 1, max: 100 })) { + throw new Error("Application command descriptions must be between 1 and 100 characters."); + } + + if (options.options?.length) { + if (options.options.length > 25) { + throw new Error("Only 25 options are allowed to be provided."); + } + + options.options = validateApplicationCommandOptions(bot, options.options); + } + } else { + if (!CONTEXT_MENU_COMMANDS_NAME_REGEX.test(options.name)) { + throw new Error("The name of the context menu did not match the required regex."); + } + } + + return await createGlobalApplicationCommandOld(options); + }; +} + +export function editOriginalInteractionResponse(bot: BotWithCache) { + const editOriginalInteractionResponseOld = bot.helpers.editOriginalInteractionResponse; + + bot.helpers.editOriginalInteractionResponse = async function (token, options) { if (options.content && options.content.length > 2000) { throw Error(bot.constants.Errors.MESSAGE_MAX_LENGTH); } @@ -139,51 +161,32 @@ export function editInteractionResponse(bot: BotWithCache) { if (options.allowedMentions) { if (options.allowedMentions.users?.length) { - if ( - options.allowedMentions.parse?.includes( - AllowedMentionsTypes.UserMentions, - ) - ) { - options.allowedMentions.parse = options.allowedMentions.parse.filter(( - p, - ) => p !== "users"); + if (options.allowedMentions.parse?.includes(AllowedMentionsTypes.UserMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter((p) => p !== "users"); } if (options.allowedMentions.users.length > 100) { - options.allowedMentions.users = options.allowedMentions.users.slice( - 0, - 100, - ); + options.allowedMentions.users = options.allowedMentions.users.slice(0, 100); } } if (options.allowedMentions.roles?.length) { - if ( - options.allowedMentions.parse?.includes( - AllowedMentionsTypes.RoleMentions, - ) - ) { - options.allowedMentions.parse = options.allowedMentions.parse.filter(( - p, - ) => p !== "roles"); + if (options.allowedMentions.parse?.includes(AllowedMentionsTypes.RoleMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter((p) => p !== "roles"); } if (options.allowedMentions.roles.length > 100) { - options.allowedMentions.roles = options.allowedMentions.roles.slice( - 0, - 100, - ); + options.allowedMentions.roles = options.allowedMentions.roles.slice(0, 100); } } } - if (options.attachments) validateAttachments(bot, options.attachments); - - return await editInteractionResponseOld(token, options); + return await editOriginalInteractionResponseOld(token, options); }; } export default function setupInteractionCommandPermChecks(bot: BotWithCache) { - createApplicationCommand(bot); - editInteractionResponse(bot); + createGlobalApplicationCommand(bot); + createGuildApplicationCommand(bot); + editOriginalInteractionResponse(bot); } diff --git a/plugins/permissions/src/interactions/editFollowupMessage.ts b/plugins/permissions/src/interactions/editFollowupMessage.ts index baba1a725..d1c103f9e 100644 --- a/plugins/permissions/src/interactions/editFollowupMessage.ts +++ b/plugins/permissions/src/interactions/editFollowupMessage.ts @@ -1,14 +1,9 @@ import { AllowedMentionsTypes, BotWithCache } from "../../deps.ts"; -import { validateAttachments } from "../attachments.ts"; export default function editFollowupMessage(bot: BotWithCache) { const editFollowupMessageOld = bot.helpers.editFollowupMessage; - bot.helpers.editFollowupMessage = async function ( - token, - messageId, - options, - ) { + bot.helpers.editFollowupMessage = async function (token, messageId, options) { if (options.content && options.content.length > 2000) { throw Error("MESSAGE_MAX_LENGTH"); } @@ -19,46 +14,26 @@ export default function editFollowupMessage(bot: BotWithCache) { if (options.allowedMentions) { if (options.allowedMentions.users?.length) { - if ( - options.allowedMentions.parse?.includes( - AllowedMentionsTypes.UserMentions, - ) - ) { - options.allowedMentions.parse = options.allowedMentions.parse.filter(( - p, - ) => p !== "users"); + if (options.allowedMentions.parse?.includes(AllowedMentionsTypes.UserMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter((p) => p !== "users"); } if (options.allowedMentions.users.length > 100) { - options.allowedMentions.users = options.allowedMentions.users.slice( - 0, - 100, - ); + options.allowedMentions.users = options.allowedMentions.users.slice(0, 100); } } if (options.allowedMentions.roles?.length) { - if ( - options.allowedMentions.parse?.includes( - AllowedMentionsTypes.RoleMentions, - ) - ) { - options.allowedMentions.parse = options.allowedMentions.parse.filter(( - p, - ) => p !== "roles"); + if (options.allowedMentions.parse?.includes(AllowedMentionsTypes.RoleMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter((p) => p !== "roles"); } if (options.allowedMentions.roles.length > 100) { - options.allowedMentions.roles = options.allowedMentions.roles.slice( - 0, - 100, - ); + options.allowedMentions.roles = options.allowedMentions.roles.slice(0, 100); } } } - if (options.attachments) validateAttachments(bot, options.attachments); - return await editFollowupMessageOld(token, messageId, options); }; } diff --git a/plugins/permissions/src/members/editBot.ts b/plugins/permissions/src/members/editBot.ts deleted file mode 100644 index cd7e94706..000000000 --- a/plugins/permissions/src/members/editBot.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { BotWithCache } from "../../deps.ts"; -import { requireBotGuildPermissions } from "../permissions.ts"; - -export default function editBotNickname(bot: BotWithCache) { - const editBotNicknameOld = bot.helpers.editBotNickname; - - bot.helpers.editBotNickname = async function (guildId, options) { - requireBotGuildPermissions(bot, guildId, ["CHANGE_NICKNAME"]); - - return await editBotNicknameOld(guildId, options); - }; -} diff --git a/plugins/permissions/src/members/editBotMember.ts b/plugins/permissions/src/members/editBotMember.ts new file mode 100644 index 000000000..918d89937 --- /dev/null +++ b/plugins/permissions/src/members/editBotMember.ts @@ -0,0 +1,12 @@ +import { BotWithCache } from "../../deps.ts"; +import { requireBotGuildPermissions } from "../permissions.ts"; + +export default function editBotMember(bot: BotWithCache) { + const editBotMemberOld = bot.helpers.editBotMember; + + bot.helpers.editBotMember = async function (guildId, options) { + requireBotGuildPermissions(bot, guildId, ["CHANGE_NICKNAME"]); + + return await editBotMemberOld(guildId, options); + }; +} diff --git a/plugins/permissions/src/members/mod.ts b/plugins/permissions/src/members/mod.ts index 5f183ffae..2c4232489 100644 --- a/plugins/permissions/src/members/mod.ts +++ b/plugins/permissions/src/members/mod.ts @@ -1,13 +1,13 @@ import { BotWithCache } from "../../deps.ts"; import setupBanPermChecks from "./ban.ts"; -import editBotNickname from "./editBot.ts"; +import editBotMember from "./editBotMember.ts"; import editMember from "./editMember.ts"; import kickMember from "./kickMember.ts"; import pruneMembers from "./pruneMembers.ts"; export default function setupMemberPermChecks(bot: BotWithCache) { setupBanPermChecks(bot); - editBotNickname(bot); + editBotMember(bot); editMember(bot); kickMember(bot); pruneMembers(bot); diff --git a/plugins/permissions/src/messages/reactions.ts b/plugins/permissions/src/messages/reactions.ts index f510b8a87..42f2c89d1 100644 --- a/plugins/permissions/src/messages/reactions.ts +++ b/plugins/permissions/src/messages/reactions.ts @@ -5,10 +5,7 @@ export function addReaction(bot: BotWithCache) { const addReactionOld = bot.helpers.addReaction; bot.helpers.addReaction = async function (channelId, messageId, reaction) { - requireBotChannelPermissions(bot, channelId, [ - "READ_MESSAGE_HISTORY", - "ADD_REACTIONS", - ]); + requireBotChannelPermissions(bot, channelId, ["READ_MESSAGE_HISTORY", "ADD_REACTIONS"]); return await addReactionOld(channelId, messageId, reaction); }; @@ -17,76 +14,47 @@ export function addReaction(bot: BotWithCache) { export function addReactions(bot: BotWithCache) { const addReactionsOld = bot.helpers.addReactions; - bot.helpers.addReactions = async function ( - channelId, - messageId, - reactions, - ordered, - ) { - requireBotChannelPermissions(bot, channelId, [ - "READ_MESSAGE_HISTORY", - "ADD_REACTIONS", - ]); + bot.helpers.addReactions = async function (channelId, messageId, reactions, ordered) { + requireBotChannelPermissions(bot, channelId, ["READ_MESSAGE_HISTORY", "ADD_REACTIONS"]); return await addReactionsOld(channelId, messageId, reactions, ordered); }; } -export function removeReaction(bot: BotWithCache) { - const removeReactionOld = bot.helpers.removeReaction; +export function deleteUserReaction(bot: BotWithCache) { + const deleteUserReactionOld = bot.helpers.deleteUserReaction; - bot.helpers.removeReaction = async function ( - channelId, - messageId, - reactions, - options, - ) { - // IF REMOVING OTHER USER PERMS MANAGE MESSAGES IS REQUIRED - if (options?.userId) { - requireBotChannelPermissions(bot, channelId, [ - "MANAGE_MESSAGES", - ]); - } + bot.helpers.deleteUserReaction = async function (channelId, messageId, userId, reaction) { + requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); - return await removeReactionOld(channelId, messageId, reactions, options); + return await deleteUserReactionOld(channelId, messageId, userId, reaction); }; } -export function removeAllReactions(bot: BotWithCache) { - const removeAllReactionsOld = bot.helpers.removeAllReactions; +export function deleteReactionsAll(bot: BotWithCache) { + const DeleteReactionsAllOld = bot.helpers.deleteReactionsAll; - bot.helpers.removeAllReactions = async function ( - channelId, - messageId, - ) { - requireBotChannelPermissions(bot, channelId, [ - "MANAGE_MESSAGES", - ]); + bot.helpers.deleteReactionsAll = async function (channelId, messageId) { + requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); - return await removeAllReactionsOld(channelId, messageId); + return await DeleteReactionsAllOld(channelId, messageId); }; } -export function removeReactionEmoji(bot: BotWithCache) { - const removeReactionEmojiOld = bot.helpers.removeReactionEmoji; +export function deleteReactionsEmoji(bot: BotWithCache) { + const deleteReactionsEmojiOld = bot.helpers.deleteReactionsEmoji; - bot.helpers.removeReactionEmoji = async function ( - channelId, - messageId, - reaction, - ) { - requireBotChannelPermissions(bot, channelId, [ - "MANAGE_MESSAGES", - ]); + bot.helpers.deleteReactionsEmoji = async function (channelId, messageId, reaction) { + requireBotChannelPermissions(bot, channelId, ["MANAGE_MESSAGES"]); - return await removeReactionEmojiOld(channelId, messageId, reaction); + return await deleteReactionsEmojiOld(channelId, messageId, reaction); }; } export default function setupReactionsPermChecks(bot: BotWithCache) { addReaction(bot); addReactions(bot); - removeReaction(bot); - removeAllReactions(bot); - removeReactionEmoji(bot); + deleteUserReaction(bot); + deleteReactionsAll(bot); + deleteReactionsEmoji(bot); } diff --git a/plugins/permissions/src/webhooks/message.ts b/plugins/permissions/src/webhooks/message.ts index c96b08fc1..30a0ac59d 100644 --- a/plugins/permissions/src/webhooks/message.ts +++ b/plugins/permissions/src/webhooks/message.ts @@ -1,19 +1,11 @@ import { AllowedMentionsTypes, BotWithCache } from "../../deps.ts"; -import { validateAttachments } from "../attachments.ts"; import { validateComponents } from "../components.ts"; export function editWebhookMessage(bot: BotWithCache) { const editWebhookMessageOld = bot.helpers.editWebhookMessage; - bot.helpers.editWebhookMessage = async function ( - webhookId, - webhookToken, - options, - ) { - if ( - options.content && - !bot.utils.validateLength(options.content, { max: 2000 }) - ) { + bot.helpers.editWebhookMessage = async function (webhookId, webhookToken, messageId, options) { + if (options.content && !bot.utils.validateLength(options.content, { max: 2000 })) { throw Error("The content can not exceed 2000 characters."); } @@ -23,49 +15,29 @@ export function editWebhookMessage(bot: BotWithCache) { if (options.allowedMentions) { if (options.allowedMentions.users?.length) { - if ( - options.allowedMentions.parse?.includes( - AllowedMentionsTypes.UserMentions, - ) - ) { - options.allowedMentions.parse = options.allowedMentions.parse.filter(( - p, - ) => p !== "users"); + if (options.allowedMentions.parse?.includes(AllowedMentionsTypes.UserMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter((p) => p !== "users"); } if (options.allowedMentions.users.length > 100) { - options.allowedMentions.users = options.allowedMentions.users.slice( - 0, - 100, - ); + options.allowedMentions.users = options.allowedMentions.users.slice(0, 100); } } if (options.allowedMentions.roles?.length) { - if ( - options.allowedMentions.parse?.includes( - AllowedMentionsTypes.RoleMentions, - ) - ) { - options.allowedMentions.parse = options.allowedMentions.parse.filter(( - p, - ) => p !== "roles"); + if (options.allowedMentions.parse?.includes(AllowedMentionsTypes.RoleMentions)) { + options.allowedMentions.parse = options.allowedMentions.parse.filter((p) => p !== "roles"); } if (options.allowedMentions.roles.length > 100) { - options.allowedMentions.roles = options.allowedMentions.roles.slice( - 0, - 100, - ); + options.allowedMentions.roles = options.allowedMentions.roles.slice(0, 100); } } } if (options.components) validateComponents(bot, options.components); - if (options.attachments) validateAttachments(bot, options.attachments); - - return await editWebhookMessageOld(webhookId, webhookToken, options); + return await editWebhookMessageOld(webhookId, webhookToken, messageId, options); }; } diff --git a/transformers/mod.ts b/transformers/mod.ts index d58ec74bf..584df93ec 100644 --- a/transformers/mod.ts +++ b/transformers/mod.ts @@ -1,9 +1,11 @@ export * from "./toggles/mod.ts"; +export * from "./reverse/mod.ts"; export * from "./activity.ts"; export * from "./application.ts"; export * from "./applicationCommand.ts"; export * from "./applicationCommandOption.ts"; +export * from "./applicationCommandOptionChoice.ts"; export * from "./applicationCommandPermission.ts"; export * from "./attachment.ts"; export * from "./auditLogEntry.ts"; @@ -32,5 +34,3 @@ export * from "./webhook.ts"; export * from "./welcomeScreen.ts"; export * from "./widget.ts"; export * from "./widgetSettings.ts"; - -export * from "./reverse/mod.ts";