com
@discord-nestjs/common
npm i @discord-nestjs/common
com

@discord-nestjs/common

👾 NestJS package for discord.js

by fjodor-rybakov

4.0.8 (see all)License:MITTypeScript:Built-In
npm i @discord-nestjs/common
Readme

Common module

🧾 Description

This package contains some ready-made templates

👨🏻‍💻 Installation

$ npm install @discord-nestjs/common

Or via yarn

$ yarn add @discord-nestjs/common

📑 Overview

ℹ️ Pipes template

TransformPipe fills in the fields DTO from CommandInteraction.

For prefix command use PrefixCommandTransformPipe.

💡 Example

/* registration-email.dto.ts */

import { City } from '../definitions/city';
import { Param, ParamType, Choice } from '@discord-nestjs/core';

export class RegistrationEmailDto {
  @Param({
    name: 'email',
    description: 'Base user email',
    required: true,
  })
  email: string;

  @Param({ description: 'User nickname', required: true })
  name: string;

  @Param({ description: 'User age', required: true, type: ParamType.INTEGER })
  age: number;

  @Choice(City)
  @Param({ description: 'City of residence', type: ParamType.INTEGER })
  city: City;
}
/* play.command.ts */

import { RegistrationDto } from './registration.dto';
import { Command, UsePipes, Payload, DiscordTransformedCommand } from '@discord-nestjs/core';
import { TransformPipe } from '@discord-nestjs/common';
import { CommandInteraction } from 'discord.js';

@Command({
  name: 'reg',
  description: 'User registration',
})
@UsePipes(TransformPipe)
export class BaseInfoCommand implements DiscordTransformedCommand<RegistrationEmailDto>
{
  handler(@Payload() dto: RegistrationEmailDto): string {
    // dto instance must have the following fields: email, name, age, city
  }
}

ValidationPipe validate the resulting DTO based on class-validator. If the DTO is invalid then an exception will be thrown, which can be caught by the filter from the package @discord-nestjs/core.

Also suitable for prefix commands.

for validation, you need to install package class-validator

💡 Example

/* registration-email.dto.ts */

import { Param, ParamType } from '@discord-nestjs/core';
import { IsEmail, Length, Max, Min } from 'class-validator';

export class RegistrationEmailDto {
  @IsEmail()
  @Param({
    name: 'email',
    description: 'Base user email',
    required: true,
  })
  email: string;

  @Param({ description: 'User nickname', required: true })
  @Length(3, 100)
  name: string;

  @Param({ description: 'User age', required: true, type: ParamType.INTEGER })
  @Max(150)
  @Min(18)
  age: number;
}
/* play.command.ts */

import { RegistrationDto } from './registration.dto';
import { Command, UsePipes, Payload, DiscordTransformedCommand } from '@discord-nestjs/core';
import { TransformPipe, ValidationPipe } from '@discord-nestjs/common';
import { CommandInteraction } from 'discord.js';

@Command({
  name: 'reg',
  description: 'User registration',
})
@UsePipes(TransformPipe, ValidationPipe)
export class BaseInfoCommand implements DiscordTransformedCommand<RegistrationEmailDto> {
  handler(@Payload() dto: RegistrationEmailDto): string {
    // dto instance must be valid
  }
}

ℹ️ Mapped types

Intersection type allows you to infer a new type

💡 Example

import { Param } from '@discord-nestjs/core';

class FullName {
  @Param({
    description: 'Your full name'
  })
  name: string;
}
import { Param } from '@discord-nestjs/core';

class ResidencePlace {
  @Param({
    description: 'City of residence'
  })
  city: string;

  @Param({
    description: 'The street where your house is located'
  })
  street: string;
}
import { DiscordIntersectionType } from '@discord-nestjs/common';

// Must have properties name, city and street
class Profile extends DiscordIntersectionType(FullName, ResidencePlace) {
}

Downloads/wk

339

GitHub Stars

159

LAST COMMIT

9mos ago

MAINTAINERS

1

CONTRIBUTORS

13

OPEN ISSUES

3

OPEN PRs

0
VersionTagPublished
4.0.8
latest
2mos ago
0.0.2-beta.872
canary
1yr ago
No alternatives found
No tutorials found
Add a tutorial
No dependencies found

Rate & Review

100
No reviews found
Be the first to rate