necordjs/necord

Options from slash command are undefined if you use TypeORM

iAmArbuzik opened this issue · 2 comments

Issue description

Steps to reproduce:

  1. Create Nest project using following command nest new necord_issue
  2. Remove unnecessary files (leave only app.module.ts and main.ts under src folder)
  3. Install Necord and discordjs npm install necord discord.js
  4. Under src create folder discord which will represent discord module of nest application
  5. Under discord folder create discord.module.ts file with following content and register this module inside app.module:
import { GatewayIntentBits } from 'discord.js';
import { NecordModule } from 'necord';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot(),
    NecordModule.forRoot({
      token: process.env.DISCORD_BOT_TOKEN,
      intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMessageReactions,
      ],
      development: [process.env.DISCORD_DEVELOPMENT_GUILD_ID],
    }),
  ],
  providers: [],
})
export class DiscordModule {}
  1. Create discord.service.ts with following content (to create slash command) and define it in discord.module.ts inside providers array
import {
  Context,
  Options,
  SlashCommand,
  SlashCommandContext,
  StringOption,
} from 'necord';
import { Injectable } from '@nestjs/common';

class TextDto {
  @StringOption({
    name: 'text',
    description: 'Your text',
    required: true,
  })
  text: string;
}

@Injectable()
export class DiscordService {
  @SlashCommand({
    name: 'length',
    description: 'Get length of text',
  })
  public async onLength(
    @Context() [interaction]: SlashCommandContext,
    @Options() { text }: TextDto,
  ) {
    return interaction.reply({ content: `Length of your text ${text.length}` });
  }
}
  1. If you run the bot and try to use command then everything is okay. But let's install typeorm
  2. Install TypeOrm npm i @nestjs/typeorm typeorm pg config @nestjs/config
  3. In the app.module.ts configure TypeOrmModule
@Module({
  imports: [
    DiscordModule,
    TypeOrmModule.forRoot({
      type: 'postgres',
      host: process.env.PG_HOST,
      username: process.env.PG_USER,
      password: process.env.PG_PASS,
      database: process.env.PG_DATABASE,
      ssl: true,
      autoLoadEntities: true,
      synchronize: true,
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}
  1. If you run the bot and try to use command then you will get an error
E:\Learning\necord_issue\src\discord\discord.service.ts:29
    return interaction.reply({ content: `Length of your text ${text.length}` });
                                                                    ^
TypeError: Cannot read properties of undefined (reading 'length')
    at DiscordService.onLength (E:\Learning\necord_issue\src\discord\discord.service.ts:29:69)
    at E:\Learning\necord_issue\node_modules\@nestjs\core\helpers\external-context-creator.js:69:29
    at InterceptorsConsumer.intercept (E:\Learning\necord_issue\node_modules\@nestjs\core\interceptors\interceptors-consumer.js:12:20)
    at target (E:\Learning\necord_issue\node_modules\@nestjs\core\helpers\external-context-creator.js:74:60)
    at SlashCommandDiscovery.contextCallback (E:\Learning\necord_issue\node_modules\@nestjs\core\helpers\external-proxy.js:9:30)
    at SlashCommandDiscovery.execute (E:\Learning\necord_issue\node_modules\necord\dist\context\necord-base.discovery.js:25:21)
    at SlashCommandDiscovery.execute (E:\Learning\necord_issue\node_modules\necord\dist\commands\slash-commands\slash-command.discovery.js:45:22)
    at Client.<anonymous> (E:\Learning\necord_issue\node_modules\necord\dist\commands\slash-commands\slash-commands.module.js:37:112)
    at Client.emit (node:events:526:35)
    at InteractionCreateAction.handle (E:\Learning\necord_issue\node_modules\discord.js\src\client\actions\InteractionCreate.js:97:12)

Code sample

Link to repo with reproduction - https://github.com/iAmArbuzik/necord_issue

discord.js version

14.14.1

nest.js version

10.3.2

Node.js version

nodejs version - v20.10.0 typescript version - v5.3.3

Operating system

OS Windows 11 (Version 10.0.22621)

Priority this issue should have

High (immediate attention needed)

Which partials do you have configured?

No Partials

Which gateway intents are you subscribing to?

GUILDS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS

Thank you for issue! You need to update reflect-metadata to 0.2.*

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.