UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

1 lines 10.2 kB
{"version":3,"sources":["../../../../../src/lib/utils/application-commands/normalizeInputs.ts"],"names":["SlashCommandBuilder","ApplicationCommandType","isFunction","PermissionsBitField","ApplicationCommand","ContextMenuCommandBuilder"],"mappings":";;;;;;;;;AAwBA,SAAS,UACR,OAKoE,EAAA;AACpE,EAAA,OAAO,OAAmB,YAAAA,4BAAA;AAC3B;AARS,MAAA,CAAA,SAAA,EAAA,WAAA,CAAA;AAUT,SAAS,2BAA2B,IAAwG,EAAA;AAC3I,EAAA,IAAA,CAAK,aAAkB,KAAA,IAAA;AACvB,EAAA,IAAA,CAAK,SAASC,0BAAuB,CAAA,SAAA;AACrC,EAAA,IAAA,CAAK,0BAA+B,KAAA,IAAA;AAEpC,EAAO,OAAA,IAAA;AACR;AANS,MAAA,CAAA,0BAAA,EAAA,4BAAA,CAAA;AAQT,SAAS,6BAA6B,IAA4G,EAAA;AACjJ,EAAA,IAAA,CAAK,aAAkB,KAAA,IAAA;AACvB,EAAA,IAAA,CAAK,0BAA+B,KAAA,IAAA;AAEpC,EAAO,OAAA,IAAA;AACR;AALS,MAAA,CAAA,4BAAA,EAAA,8BAAA,CAAA;AAOF,SAAS,0BACf,OAOkD,EAAA;AAClD,EAAI,IAAAC,oBAAA,CAAW,OAAO,CAAG,EAAA;AACxB,IAAM,MAAA,OAAA,GAAU,IAAIF,4BAAoB,EAAA;AACxC,IAAA,OAAA,CAAQ,OAAO,CAAA;AACf,IAAO,OAAA,0BAAA,CAA2B,OAAQ,CAAA,MAAA,EAA2D,CAAA;AAAA;AAGtG,EAAI,IAAA,SAAA,CAAU,OAAO,CAAG,EAAA;AACvB,IAAO,OAAA,0BAAA,CAA2B,OAAQ,CAAA,MAAA,EAA2D,CAAA;AAAA;AAGtG,EAAA,MAAM,WAA+D,GAAA;AAAA,IACpE,MAAM,OAAQ,CAAA,IAAA;AAAA,IACd,oBAAoB,OAAQ,CAAA,iBA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AA,CAAQ,IAAS,KAAAA,0BAAA,CAAuB,OAAS,EAAA;AAC3D,IAAA,UAAA,CAAW,OAAOA,0BAAuB,CAAA,OAAA;AAAA,GAC/B,MAAA,IAAA,OAAA,CAAQ,IAAS,KAAAA,0BAAA,CAAuB,IAAM,EAAA;AACxD,IAAA,UAAA,CAAW,OAAOA,0BAAuB,CAAA,IAAA;AAAA,GACnC,MAAA;AACN,IAAA,MAAM,IAAI,KAAA,CAAM,CAAkC,+BAAA,EAAA,OAAA,CAAQ,IAAI,CAAE,CAAA,CAAA;AAAA;AAGjE,EAAI,IAAA,OAAA,CAAQ,QAAQ,MAAQ,EAAA;AAC3B,IAAW,UAAA,CAAA,OAAA,GAAU,OAAQ,CAAA,OAAA,CAAQ,GAAI,CAAA,CAAC,WAAWG,6BAAmB,CAAA,iBAAiB,CAAE,CAAA,MAAa,CAAgC,CAAA;AAAA;AAGzI,EAAO,OAAA,UAAA;AACR;AA9BgB,MAAA,CAAA,kCAAA,EAAA,oCAAA,CAAA","file":"normalizeInputs.cjs","sourcesContent":["import {\n\tContextMenuCommandBuilder,\n\tSlashCommandBuilder,\n\ttype SlashCommandOptionsOnlyBuilder,\n\ttype SlashCommandSubcommandsOnlyBuilder\n} from '@discordjs/builders';\nimport { isFunction } from '@sapphire/utilities';\nimport {\n\tApplicationCommandType,\n\ttype APIApplicationCommandOption,\n\ttype RESTPostAPIApplicationCommandsJSONBody,\n\ttype RESTPostAPIChatInputApplicationCommandsJSONBody,\n\ttype RESTPostAPIContextMenuApplicationCommandsJSONBody\n} from 'discord-api-types/v10';\nimport {\n\tApplicationCommand,\n\tPermissionsBitField,\n\ttype ApplicationIntegrationType,\n\ttype ChatInputApplicationCommandData,\n\ttype InteractionContextType,\n\ttype MessageApplicationCommandData,\n\ttype UserApplicationCommandData\n} from 'discord.js';\n\nfunction isBuilder(\n\tcommand: unknown\n): command is\n\t| SlashCommandBuilder\n\t| SlashCommandSubcommandsOnlyBuilder\n\t| SlashCommandOptionsOnlyBuilder\n\t| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'> {\n\treturn command instanceof SlashCommandBuilder;\n}\n\nfunction addDefaultsToChatInputJSON(data: RESTPostAPIChatInputApplicationCommandsJSONBody): RESTPostAPIChatInputApplicationCommandsJSONBody {\n\tdata.dm_permission ??= true;\n\tdata.type ??= ApplicationCommandType.ChatInput;\n\tdata.default_member_permissions ??= null;\n\n\treturn data;\n}\n\nfunction addDefaultsToContextMenuJSON(data: RESTPostAPIContextMenuApplicationCommandsJSONBody): RESTPostAPIContextMenuApplicationCommandsJSONBody {\n\tdata.dm_permission ??= true;\n\tdata.default_member_permissions ??= null;\n\n\treturn data;\n}\n\nexport function normalizeChatInputCommand(\n\tcommand:\n\t\t| ChatInputApplicationCommandData\n\t\t| SlashCommandBuilder\n\t\t| SlashCommandSubcommandsOnlyBuilder\n\t\t| SlashCommandOptionsOnlyBuilder\n\t\t| Omit<SlashCommandBuilder, 'addSubcommand' | 'addSubcommandGroup'>\n\t\t| ((builder: SlashCommandBuilder) => unknown)\n): RESTPostAPIChatInputApplicationCommandsJSONBody {\n\tif (isFunction(command)) {\n\t\tconst builder = new SlashCommandBuilder();\n\t\tcommand(builder);\n\t\treturn addDefaultsToChatInputJSON(builder.toJSON() as RESTPostAPIChatInputApplicationCommandsJSONBody);\n\t}\n\n\tif (isBuilder(command)) {\n\t\treturn addDefaultsToChatInputJSON(command.toJSON() as RESTPostAPIChatInputApplicationCommandsJSONBody);\n\t}\n\n\tconst finalObject: RESTPostAPIChatInputApplicationCommandsJSONBody = {\n\t\tname: command.name,\n\t\tname_localizations: command.nameLocalizations,\n\t\tdescription: command.description,\n\t\tdescription_localizations: command.descriptionLocalizations,\n\t\ttype: ApplicationCommandType.ChatInput,\n\t\tdm_permission: command.dmPermission,\n\t\tnsfw: command.nsfw,\n\t\tintegration_types: command.integrationTypes as ApplicationIntegrationType[] | undefined,\n\t\tcontexts: command.contexts as InteractionContextType[] | undefined\n\t};\n\n\tif (typeof command.defaultMemberPermissions !== 'undefined') {\n\t\tfinalObject.default_member_permissions =\n\t\t\tcommand.defaultMemberPermissions === null ? null : new PermissionsBitField(command.defaultMemberPermissions).bitfield.toString();\n\t}\n\n\tif (command.options?.length) {\n\t\tfinalObject.options = command.options.map((option) => ApplicationCommand['transformOption'](option) as APIApplicationCommandOption);\n\t}\n\n\treturn addDefaultsToChatInputJSON(finalObject);\n}\n\nexport function normalizeContextMenuCommand(\n\tcommand:\n\t\t| UserApplicationCommandData\n\t\t| MessageApplicationCommandData\n\t\t| ContextMenuCommandBuilder\n\t\t| ((builder: ContextMenuCommandBuilder) => unknown)\n): RESTPostAPIContextMenuApplicationCommandsJSONBody {\n\tif (isFunction(command)) {\n\t\tconst builder = new ContextMenuCommandBuilder();\n\t\tcommand(builder);\n\t\treturn addDefaultsToContextMenuJSON(builder.toJSON() as RESTPostAPIContextMenuApplicationCommandsJSONBody);\n\t}\n\n\tif (command instanceof ContextMenuCommandBuilder) {\n\t\treturn addDefaultsToContextMenuJSON(command.toJSON() as RESTPostAPIContextMenuApplicationCommandsJSONBody);\n\t}\n\n\tconst finalObject: RESTPostAPIContextMenuApplicationCommandsJSONBody = {\n\t\tname: command.name,\n\t\tname_localizations: command.nameLocalizations,\n\t\t// TODO (favna 2024-10-19): Remove this line after discord.js has been updated past 14.16.3\n\t\t// @ts-ignore temporarily ignore the error because discord.js broke types\n\t\ttype: command.type,\n\t\tdm_permission: command.dmPermission,\n\t\tnsfw: command.nsfw,\n\t\tintegration_types: command.integrationTypes as ApplicationIntegrationType[] | undefined,\n\t\tcontexts: command.contexts as InteractionContextType[] | undefined\n\t};\n\n\tif (typeof command.defaultMemberPermissions !== 'undefined') {\n\t\tfinalObject.default_member_permissions =\n\t\t\tcommand.defaultMemberPermissions === null ? null : new PermissionsBitField(command.defaultMemberPermissions).bitfield.toString();\n\t}\n\n\treturn addDefaultsToContextMenuJSON(finalObject);\n}\n\nexport function convertApplicationCommandToApiData(command: ApplicationCommand): RESTPostAPIApplicationCommandsJSONBody {\n\tconst returnData = {\n\t\tname: command.name,\n\t\tname_localizations: command.nameLocalizations,\n\t\tdm_permission: command.dmPermission,\n\t\tnsfw: command.nsfw,\n\t\tdefault_member_permissions: command.defaultMemberPermissions?.bitfield.toString() ?? null,\n\t\tintegration_types: command.integrationTypes,\n\t\tcontexts: command.contexts\n\t} as RESTPostAPIApplicationCommandsJSONBody;\n\n\tif (command.type === ApplicationCommandType.ChatInput) {\n\t\treturnData.type = ApplicationCommandType.ChatInput;\n\t\t(returnData as RESTPostAPIChatInputApplicationCommandsJSONBody).description = command.description;\n\t\t// TODO (favna): Remove this line after website rewrite is done\n\t\t// @ts-ignore this is currently ignored for the website\n\t\t(returnData as RESTPostAPIChatInputApplicationCommandsJSONBody).description_localizations = command.descriptionLocalizations;\n\t} else if (command.type === ApplicationCommandType.Message) {\n\t\treturnData.type = ApplicationCommandType.Message;\n\t} else if (command.type === ApplicationCommandType.User) {\n\t\treturnData.type = ApplicationCommandType.User;\n\t} else {\n\t\tthrow new Error(`Unknown command type received: ${command.type}`);\n\t}\n\n\tif (command.options.length) {\n\t\treturnData.options = command.options.map((option) => ApplicationCommand['transformOption'](option as any) as APIApplicationCommandOption);\n\t}\n\n\treturn returnData;\n}\n"]}