From d75a3facc5fe47409dfeb1d864a9eaf9382fb55b Mon Sep 17 00:00:00 2001 From: Fleny Date: Thu, 21 Sep 2023 15:52:39 +0200 Subject: [PATCH] Use `desiredProperties` in `transformInvite` (#3127) * Use desiredProperties in transformInvite * remove not necessary payload checks * add checks for payload --- packages/bot/src/transformers/invite.ts | 81 ++++++++++++++----------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/packages/bot/src/transformers/invite.ts b/packages/bot/src/transformers/invite.ts index 7d9178ee8..c188db866 100644 --- a/packages/bot/src/transformers/invite.ts +++ b/packages/bot/src/transformers/invite.ts @@ -1,40 +1,51 @@ -import type { DiscordInviteCreate } from '@discordeno/types' -import type { Bot } from '../index.js' -import type { Optionalize } from '../optionalize.js' +import type { DiscordApplication, DiscordInviteCreate } from '@discordeno/types' +import type { Application, Bot, User } from '../index.js' // eslint-disable-next-line @typescript-eslint/explicit-function-return-type -export function transformInvite(bot: Bot, invite: DiscordInviteCreate) { - const transformedInvite = { - /** The channel the invite is for */ - channelId: bot.transformers.snowflake(invite.channel_id), - /** The unique invite code */ - code: invite.code, - /** The time at which the invite was created */ - createdAt: Date.parse(invite.created_at), - /** The guild of the invite */ - guildId: invite.guild_id ? bot.transformers.snowflake(invite.guild_id) : undefined, - /** The user that created the invite */ - inviter: invite.inviter ? bot.transformers.user(bot, invite.inviter) : undefined, - /** How long the invite is valid for (in seconds) */ - maxAge: invite.max_age, - /** The maximum number of times the invite can be used */ - maxUses: invite.max_uses, - /** The type of target for this voice channel invite */ - targetType: invite.target_type, - /** The target user for this invite */ - targetUser: invite.target_user ? bot.transformers.user(bot, invite.target_user) : undefined, - /** The embedded application to open for this voice channel embedded application invite */ - targetApplication: invite.target_application - ? // @ts-expect-error should not break anything even though its partial. if it does blame wolf :) - bot.transformers.application(bot, invite.target_application) - : undefined, - /** Whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role) */ - temporary: invite.temporary, - /** How many times the invite has been used (always will be 0) */ - uses: invite.uses, - } +export function transformInvite(bot: Bot, payload: DiscordInviteCreate) { + const props = bot.transformers.desiredProperties.invite + const invite = {} as Invite - return transformedInvite as Optionalize + if (props.channelId && payload.channel_id) invite.channelId = bot.transformers.snowflake(payload.channel_id) + if (props.code && payload.code) invite.code = payload.code + if (props.createdAt && payload.created_at) invite.createdAt = Date.parse(payload.created_at) + if (props.guildId && payload.guild_id) invite.guildId = bot.transformers.snowflake(payload.guild_id) + if (props.inviter && payload.inviter) invite.inviter = bot.transformers.user(bot, payload.inviter) + if (props.maxAge && payload.max_age) invite.maxAge = payload.max_age + if (props.maxUses && payload.max_uses) invite.maxUses = payload.max_uses + if (props.targetType && payload.target_type) invite.targetType = payload.target_type + if (props.targetUser && payload.target_user) invite.targetUser = bot.transformers.user(bot, payload.target_user) + if (props.targetApplication && payload.target_application) + invite.targetApplication = bot.transformers.application(bot, payload.target_application as DiscordApplication) + if (props.temporary && payload.temporary) invite.temporary = payload.temporary + if (props.uses && payload.uses) invite.uses = payload.uses + + return invite } -export interface Invite extends ReturnType {} +export interface Invite { + /** The channel the invite is for */ + channelId: bigint + /** The unique invite code */ + code: string + /** The time at which the invite was created */ + createdAt: number + /** The guild of the invite */ + guildId?: bigint + /** The user that created the invite */ + inviter?: User + /** How long the invite is valid for (in seconds) */ + maxAge: number + /** The maximum number of times the invite can be used */ + maxUses: number + /** The type of target for this voice channel invite */ + targetType: number + /** The target user for this invite */ + targetUser: User + /** The embedded application to open for this voice channel embedded application invite */ + targetApplication?: Application + /** Whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role) */ + temporary: boolean + /** How many times the invite has been used (always will be 0) */ + uses: number +}