Skip to content

Commit

Permalink
feat: Set attachment object if prop type is Attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
fjodor-rybakov committed Feb 24, 2023
1 parent aeb6efc commit 7c659d8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
32 changes: 25 additions & 7 deletions packages/common/src/pipe/slash-command/slash-command.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReflectMetadataProvider } from '@discord-nestjs/core';
import { ParamType, ReflectMetadataProvider } from '@discord-nestjs/core';
import {
ArgumentMetadata,
Inject,
Expand All @@ -8,7 +8,7 @@ import {
Type,
} from '@nestjs/common';
import { ClassTransformOptions, plainToInstance } from 'class-transformer';
import { Interaction } from 'discord.js';
import { Attachment, Interaction } from 'discord.js';

import { TRANSFORMER_OPTION } from '../../contants/transformer-options.constant';

Expand Down Expand Up @@ -40,6 +40,7 @@ export class SlashCommandPipe implements PipeTransform {
const plainObject = {};
const dtoInstance = new metadata.metatype();
const allKeys = Object.keys(dtoInstance);
const assignWithoutTransform: Record<string, any> = {};

allKeys.forEach((property: string) => {
const paramDecoratorMetadata =
Expand All @@ -50,17 +51,34 @@ export class SlashCommandPipe implements PipeTransform {

if (!paramDecoratorMetadata) return;

const { name, required } = paramDecoratorMetadata;
plainObject[property] =
interaction.options.get(name ?? property, required)?.value ??
dtoInstance[property];
const { required, type } = paramDecoratorMetadata;
const name = paramDecoratorMetadata.name ?? property;
const interactionOption = interaction.options.get(name, required);

plainObject[property] = interactionOption?.value ?? dtoInstance[property];

if (type === ParamType.ATTACHMENT) {
const propertyType = Reflect.getMetadata(
'design:type',
dtoInstance,
property,
);

if (Object.is(propertyType, Attachment)) {
assignWithoutTransform[property] =
interactionOption?.attachment ?? dtoInstance[property];
}
}
});

return plainToInstance(
// class-validator breaks classes trying to recreate an instance from a property type
const resultDto = plainToInstance(
metadata.metatype,
plainObject,
this.classTransformerOptions,
);

return Object.assign(resultDto, assignWithoutTransform);
}

private isDto(type: Type): boolean {
Expand Down
8 changes: 8 additions & 0 deletions packages/sample/sub-command/src/bot/dto/email.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Choice, Param, ParamType } from '@discord-nestjs/core';
import { Attachment } from 'discord.js';

import { City } from '../definitions/city';

Expand All @@ -15,6 +16,13 @@ export class EmailDto {
@Param({ description: 'User age', required: true, type: ParamType.INTEGER })
age: number;

@Param({
description: 'Attachment',
type: ParamType.ATTACHMENT,
required: true,
})
screenshot: Attachment;

@Choice(City)
@Param({ description: 'City of residence', type: ParamType.INTEGER })
city: City;
Expand Down

0 comments on commit 7c659d8

Please sign in to comment.