|
| 1 | +import { |
| 2 | + APIApplicationCommandInteractionDataChannelOption, |
| 3 | + APIApplicationCommandInteractionDataRoleOption, |
| 4 | + APIApplicationCommandInteractionDataSubcommandOption, |
| 5 | + APIApplicationCommandInteractionDataUserOption, |
| 6 | + APIChatInputApplicationCommandGuildInteraction, |
| 7 | + ApplicationCommandOptionType, |
| 8 | + InteractionResponseType, |
| 9 | + MessageFlags, |
| 10 | +} from "discord-api-types/v9"; |
| 11 | + |
| 12 | +import { |
| 13 | + InteractionOrRequestFinalStatus, |
| 14 | + UnexpectedFailure, |
| 15 | +} from "../../../errors"; |
| 16 | +import { InternalInteractionType } from "../../interaction"; |
| 17 | +import { InteractionReturnData } from "../../types"; |
| 18 | + |
| 19 | +// eslint-disable-next-line @typescript-eslint/require-await |
| 20 | +export default async function handleRawFormatCommand( |
| 21 | + internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction> |
| 22 | +): Promise<InteractionReturnData> { |
| 23 | + const interaction = internalInteraction.interaction; |
| 24 | + const subcommand = interaction.data.options?.[0]; |
| 25 | + if ( |
| 26 | + !subcommand || |
| 27 | + subcommand.type !== ApplicationCommandOptionType.Subcommand |
| 28 | + ) { |
| 29 | + throw new UnexpectedFailure( |
| 30 | + InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION, |
| 31 | + "Missing subcommand" |
| 32 | + ); |
| 33 | + } |
| 34 | + switch (subcommand.name) { |
| 35 | + case "user": |
| 36 | + return handleRawFormatUserSubcommand(internalInteraction, subcommand); |
| 37 | + |
| 38 | + case "role": |
| 39 | + return handleRawFormatRoleSubcommand(internalInteraction, subcommand); |
| 40 | + |
| 41 | + case "channel": |
| 42 | + return handleRawFormatChannelSubcommand(internalInteraction, subcommand); |
| 43 | + |
| 44 | + default: |
| 45 | + throw new UnexpectedFailure( |
| 46 | + InteractionOrRequestFinalStatus.APPLICATION_COMMAND_UNEXPECTED_SUBCOMMAND, |
| 47 | + `Invalid subcommand: \`${subcommand.name}\`` |
| 48 | + ); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function handleRawFormatUserSubcommand( |
| 53 | + internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>, |
| 54 | + subcommand: APIApplicationCommandInteractionDataSubcommandOption |
| 55 | +): InteractionReturnData { |
| 56 | + const targetId: string | undefined = ( |
| 57 | + subcommand.options?.find( |
| 58 | + (option) => |
| 59 | + option.name === "user" && |
| 60 | + option.type === ApplicationCommandOptionType.User |
| 61 | + ) as APIApplicationCommandInteractionDataUserOption | undefined |
| 62 | + )?.value; |
| 63 | + if (targetId === undefined) { |
| 64 | + throw new UnexpectedFailure( |
| 65 | + InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION, |
| 66 | + "Missing target option" |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + return { |
| 71 | + type: InteractionResponseType.ChannelMessageWithSource, |
| 72 | + data: { |
| 73 | + content: `\`<@${targetId}>\``, |
| 74 | + flags: MessageFlags.Ephemeral, |
| 75 | + }, |
| 76 | + }; |
| 77 | +} |
| 78 | +function handleRawFormatRoleSubcommand( |
| 79 | + internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>, |
| 80 | + subcommand: APIApplicationCommandInteractionDataSubcommandOption |
| 81 | +): InteractionReturnData { |
| 82 | + const targetId: string | undefined = ( |
| 83 | + subcommand.options?.find( |
| 84 | + (option) => |
| 85 | + option.name === "role" && |
| 86 | + option.type === ApplicationCommandOptionType.Role |
| 87 | + ) as APIApplicationCommandInteractionDataRoleOption | undefined |
| 88 | + )?.value; |
| 89 | + if (targetId === undefined) { |
| 90 | + throw new UnexpectedFailure( |
| 91 | + InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION, |
| 92 | + "Missing target option" |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + type: InteractionResponseType.ChannelMessageWithSource, |
| 98 | + data: { |
| 99 | + content: `\`<@&${targetId}>\``, |
| 100 | + flags: MessageFlags.Ephemeral, |
| 101 | + }, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | +function handleRawFormatChannelSubcommand( |
| 106 | + internalInteraction: InternalInteractionType<APIChatInputApplicationCommandGuildInteraction>, |
| 107 | + subcommand: APIApplicationCommandInteractionDataSubcommandOption |
| 108 | +): InteractionReturnData { |
| 109 | + const targetId: string | undefined = ( |
| 110 | + subcommand.options?.find( |
| 111 | + (option) => |
| 112 | + option.name === "channel" && |
| 113 | + option.type === ApplicationCommandOptionType.Channel |
| 114 | + ) as APIApplicationCommandInteractionDataChannelOption | undefined |
| 115 | + )?.value; |
| 116 | + if (targetId === undefined) { |
| 117 | + throw new UnexpectedFailure( |
| 118 | + InteractionOrRequestFinalStatus.APPLICATION_COMMAND_MISSING_EXPECTED_OPTION, |
| 119 | + "Missing target option" |
| 120 | + ); |
| 121 | + } |
| 122 | + return { |
| 123 | + type: InteractionResponseType.ChannelMessageWithSource, |
| 124 | + data: { |
| 125 | + content: `\`<#${targetId}>\``, |
| 126 | + flags: MessageFlags.Ephemeral, |
| 127 | + }, |
| 128 | + }; |
| 129 | +} |
0 commit comments