feat(bot,types): Type commandOptionsParser, remove any (#3786)

* Type commandOptionsParser, remove any

* fix ts errors
This commit is contained in:
Fleny
2024-07-23 07:28:36 +02:00
committed by GitHub
parent ba23cbebde
commit 0819d2be9b
5 changed files with 36 additions and 34 deletions

View File

@@ -1,11 +1,11 @@
import { ApplicationCommandOptionTypes } from '@discordeno/types'
import type { Interaction, InteractionDataOption } from './index.js'
import type { Attachment, Channel, Interaction, InteractionDataOption, Member, Role, User } from './index.js'
export function commandOptionsParser(interaction: Interaction, options?: InteractionDataOption[]): Record<string, any> {
export function commandOptionsParser(interaction: Interaction, options?: InteractionDataOption[]): ParsedInteractionOption {
if (!interaction.data) return {}
if (!options) options = interaction.data.options ?? []
const args: Record<string, any> = {}
const args: ParsedInteractionOption = {}
for (const option of options) {
switch (option.type) {
@@ -14,31 +14,44 @@ export function commandOptionsParser(interaction: Interaction, options?: Interac
args[option.name] = commandOptionsParser(interaction, option.options)
break
case ApplicationCommandOptionTypes.Channel:
args[option.name] = interaction.data.resolved?.channels?.get(BigInt(option.value!))
args[option.name] = interaction.data.resolved?.channels?.get(BigInt(option.value!)) as InteractionResolvedChannel
break
case ApplicationCommandOptionTypes.Role:
args[option.name] = interaction.data.resolved?.roles?.get(BigInt(option.value!))
args[option.name] = interaction.data.resolved?.roles?.get(BigInt(option.value!)) as Role
break
case ApplicationCommandOptionTypes.User:
args[option.name] = {
user: interaction.data.resolved?.users?.get(BigInt(option.value!)),
member: interaction.data.resolved?.members?.get(BigInt(option.value!)),
user: interaction.data.resolved?.users?.get(BigInt(option.value!)) as User,
member: interaction.data.resolved?.members?.get(BigInt(option.value!)) as InteractionResolvedMember,
}
break
case ApplicationCommandOptionTypes.Attachment:
args[option.name] = interaction.data.resolved?.attachments?.get(BigInt(option.value!))
args[option.name] = interaction.data.resolved?.attachments?.get(BigInt(option.value!)) as Attachment
break
case ApplicationCommandOptionTypes.Mentionable:
// Mentionable are roles or users
args[option.name] = interaction.data.resolved?.roles?.get(BigInt(option.value!)) ?? {
user: interaction.data.resolved?.users?.get(BigInt(option.value!)),
member: interaction.data.resolved?.members?.get(BigInt(option.value!)),
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,
}
break
default:
args[option.name] = option.value
args[option.name] = option.value as ParsedInteractionOption[string]
}
}
return args
}
export interface ParsedInteractionOption {
[key: string]: string | number | boolean | InteractionResolvedUser | InteractionResolvedChannel | Role | Attachment | ParsedInteractionOption
}
export interface InteractionResolvedUser {
user: User
member: InteractionResolvedMember
}
export type InteractionResolvedChannel = Pick<Channel, 'id' | 'name' | 'type' | 'permissions' | 'threadMetadata' | 'parentId'>
export type InteractionResolvedMember = Omit<Member, 'user' | 'deaf' | 'mute'>

View File

@@ -14,6 +14,7 @@ import {
type Interaction,
type InteractionDataOption,
type InteractionDataResolved,
type InteractionResolvedChannel,
type Member,
type Message,
} from '../index.js'
@@ -205,23 +206,9 @@ export function transformInteractionDataResolved(bot: Bot, resolved: DiscordInte
if (resolved.channels) {
transformed.channels = new Collection(
Object.entries(resolved.channels).map(([key, value]) => {
const id = bot.transformers.snowflake(key)
const channel = value as {
id: string
name: string
type: ChannelTypes
permissions: string
}
return [
id,
{
id,
name: channel.name,
type: channel.type,
permissions: bot.transformers.snowflake(channel.permissions),
},
]
Object.entries(resolved.channels).map(([_id, value]) => {
const channel = bot.transformers.channel(bot, { channel: value }) as InteractionResolvedChannel
return [channel.id, channel]
}),
)
}

View File

@@ -66,6 +66,8 @@ import type {
EmojiToggles,
GuildFeatureKeys,
GuildToggles,
InteractionResolvedChannel,
InteractionResolvedMember,
MemberToggles,
Permissions,
RoleToggles,
@@ -932,9 +934,9 @@ export interface InteractionData {
export interface InteractionDataResolved {
messages?: Collection<bigint, Message>
users?: Collection<bigint, User>
members?: Collection<bigint, Member>
members?: Collection<bigint, InteractionResolvedMember>
roles?: Collection<bigint, Role>
channels?: Collection<bigint, { id: bigint; name: string; type: ChannelTypes; permissions: bigint }>
channels?: Collection<bigint, InteractionResolvedChannel>
attachments?: Collection<bigint, Attachment>
}

View File

@@ -130,7 +130,7 @@ export interface DiscordInteractionDataResolved {
/** The Ids and Role objects */
roles?: Record<string, DiscordRole>
/** The Ids and partial Channel objects */
channels?: Record<string, Pick<DiscordChannel, 'id' | 'name' | 'type' | 'permissions'>>
channels?: Record<string, Pick<DiscordChannel, 'id' | 'name' | 'type' | 'permissions' | 'thread_metadata' | 'parent_id'>>
/** The Ids and attachments objects */
attachments?: Record<string, DiscordAttachment>
}

View File

@@ -1064,11 +1064,11 @@ export interface DiscordChannel {
/** The set of tags that can be used in a GUILD_FORUM channel */
available_tags?: DiscordForumTag[]
/** The IDs of the set of tags that have been applied to a thread in a GUILD_FORUM channel */
applied_tags: string[]
applied_tags?: string[]
/** the emoji to show in the add reaction button on a thread in a GUILD_FORUM channel */
default_reaction_emoji?: DiscordDefaultReactionEmoji | null
/** the initial rate_limit_per_user to set on newly created threads in a channel. this field is copied to the thread at creation time and does not live update. */
default_thread_rate_limit_per_user: number
default_thread_rate_limit_per_user?: number
/** the default sort order type used to order posts in GUILD_FORUM channels. Defaults to null, which indicates a preferred sort order hasn't been set by a channel admin */
default_sort_order?: SortOrderTypes | null
/** the default forum layout view used to display posts in `GUILD_FORUM` channels. Defaults to `0`, which indicates a layout view has not been set by a channel admin */