mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-03 17:30:07 +00:00
fix(bot)!: fix type errors when using commandOptionsParser (#3994)
* Fix type errors when using commandOptionsParser * remove export from TransformProperty * use TransformProperty in InteractionResolvedData * Fix CI * Remove the union on GetErrorWhenUndesired
This commit is contained in:
@@ -1,52 +1,85 @@
|
||||
import { ApplicationCommandOptionTypes } from '@discordeno/types'
|
||||
import type { Attachment, Channel, Interaction, InteractionDataOption, Member, Role, User } from './index.js'
|
||||
import type {
|
||||
Attachment,
|
||||
Channel,
|
||||
DesiredPropertiesBehavior,
|
||||
Interaction,
|
||||
InteractionDataOption,
|
||||
Member,
|
||||
Role,
|
||||
SetupDesiredProps,
|
||||
TransformProperty,
|
||||
TransformersDesiredProperties,
|
||||
User,
|
||||
WithAtLeast,
|
||||
} from './index.js'
|
||||
|
||||
export function commandOptionsParser<
|
||||
TProps extends WithAtLeast<TransformersDesiredProperties, { interaction: { data: true } }>,
|
||||
TBehavior extends DesiredPropertiesBehavior,
|
||||
>(__interaction: SetupDesiredProps<Interaction, TProps, TBehavior>, options?: InteractionDataOption[]): ParsedInteractionOption<TProps, TBehavior> {
|
||||
// This is necessary as typescript gets really confused when using __interaction alone, as it will say that 'data' does not exist despite it surely exist since we have the WithAtLeast
|
||||
const interaction = __interaction as SetupDesiredProps<
|
||||
Interaction,
|
||||
WithAtLeast<TransformersDesiredProperties, { interaction: { data: true } }>,
|
||||
DesiredPropertiesBehavior.RemoveKey
|
||||
>
|
||||
|
||||
export function commandOptionsParser(interaction: Interaction, options?: InteractionDataOption[]): ParsedInteractionOption {
|
||||
if (!interaction.data) return {}
|
||||
if (!options) options = interaction.data.options ?? []
|
||||
|
||||
const args: ParsedInteractionOption = {}
|
||||
const args: ParsedInteractionOption<TProps, TBehavior> = {}
|
||||
|
||||
for (const option of options) {
|
||||
switch (option.type) {
|
||||
case ApplicationCommandOptionTypes.SubCommandGroup:
|
||||
case ApplicationCommandOptionTypes.SubCommand:
|
||||
args[option.name] = commandOptionsParser(interaction, option.options)
|
||||
args[option.name] = commandOptionsParser(interaction, option.options) as InteractionResolvedData<TProps, TBehavior>
|
||||
break
|
||||
case ApplicationCommandOptionTypes.Channel:
|
||||
args[option.name] = interaction.data.resolved?.channels?.get(BigInt(option.value!)) as InteractionResolvedChannel
|
||||
args[option.name] = interaction.data.resolved?.channels?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>
|
||||
break
|
||||
case ApplicationCommandOptionTypes.Role:
|
||||
args[option.name] = interaction.data.resolved?.roles?.get(BigInt(option.value!)) as Role
|
||||
args[option.name] = interaction.data.resolved?.roles?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>
|
||||
break
|
||||
case ApplicationCommandOptionTypes.User:
|
||||
args[option.name] = {
|
||||
user: interaction.data.resolved?.users?.get(BigInt(option.value!)) as User,
|
||||
member: interaction.data.resolved?.members?.get(BigInt(option.value!)) as InteractionResolvedMember,
|
||||
user: interaction.data.resolved?.users?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>,
|
||||
member: interaction.data.resolved?.members?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>,
|
||||
}
|
||||
break
|
||||
case ApplicationCommandOptionTypes.Attachment:
|
||||
args[option.name] = interaction.data.resolved?.attachments?.get(BigInt(option.value!)) as Attachment
|
||||
args[option.name] = interaction.data.resolved?.attachments?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>
|
||||
break
|
||||
case ApplicationCommandOptionTypes.Mentionable:
|
||||
// Mentionable are roles or users
|
||||
args[option.name] = (interaction.data.resolved?.roles?.get(BigInt(option.value!)) as Role) ?? {
|
||||
user: interaction.data.resolved?.users?.get(BigInt(option.value!)) as User,
|
||||
member: interaction.data.resolved?.members?.get(BigInt(option.value!)) as InteractionResolvedMember,
|
||||
args[option.name] = (interaction.data.resolved?.roles?.get(BigInt(option.value!)) as ParsedInteractionOption<TProps, TBehavior>[string]) ?? {
|
||||
user: interaction.data.resolved?.users?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>,
|
||||
member: interaction.data.resolved?.members?.get(BigInt(option.value!)) as InteractionResolvedData<TProps, TBehavior>,
|
||||
}
|
||||
break
|
||||
default:
|
||||
args[option.name] = option.value as ParsedInteractionOption[string]
|
||||
args[option.name] = option.value as InteractionResolvedData<TProps, TBehavior>
|
||||
}
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
export interface ParsedInteractionOption {
|
||||
[key: string]: string | number | boolean | InteractionResolvedUser | InteractionResolvedChannel | Role | Attachment | ParsedInteractionOption
|
||||
export interface ParsedInteractionOption<TProps extends TransformersDesiredProperties, TBehavior extends DesiredPropertiesBehavior> {
|
||||
[key: string]: InteractionResolvedData<TProps, TBehavior>
|
||||
}
|
||||
|
||||
export type InteractionResolvedData<TProps extends TransformersDesiredProperties, TBehavior extends DesiredPropertiesBehavior> =
|
||||
| string
|
||||
| number
|
||||
| boolean
|
||||
| TransformProperty<InteractionResolvedUser, TProps, TBehavior>
|
||||
| TransformProperty<InteractionResolvedChannel, TProps, TBehavior>
|
||||
| TransformProperty<Role, TProps, TBehavior>
|
||||
| TransformProperty<Attachment, TProps, TBehavior>
|
||||
| ParsedInteractionOption<TProps, TBehavior>
|
||||
|
||||
export interface InteractionResolvedUser {
|
||||
user: User
|
||||
member: InteractionResolvedMember
|
||||
|
||||
Reference in New Issue
Block a user