discord developer

interaction & message 올바른 type(Interface) 지정하는 법 (discord.js / typescript)

yjlee06 2023. 7. 28. 20:11
반응형

typescript로 디스코드 개발을 시작한다면

가장 고민이 되는게 바로 interaction의 올바른 type이 무엇인지

찾아보지만 docs를 찾아봐도 나오지않아 난감할때가 있습니다.

 

해당 포스팅에서는 해당 interaction 또는 message type에 대해 설명하도록 하겠습니다. 

 

직접 discord.js를 뜯어본 후 확인하였으니 개발하는데 참조하시면 되겠습니다!

 

1. slash commands

선언 import { CacheType, ChatInputCommandInteractionMessage } from 'discord.js';
사용 interaction: ChatInputCommandInteraction<CacheType>

 

2. Message

선언 import { Message } from 'discord.js';
사용 message: Message<true>

클래스 Messageclass Message<InGuild extends boolean = boolean> 형태이기에

InGuild( 커뮤니티 서버에서 발생함 )이므로 true로 지정하시면 되겠습니다.

 

*. 하나의 변수에 2개이상의 type을 지정할려할때의 팁

import { CacheType, ChatInputCommandInteraction, InteractionType, Message, MessageType } from 'discord.js';

function(interaction: ChatInputCommandInteraction<CacheType> | Message<true>) {
  if (interaction.type == InteractionType.ApplicationCommand) {
  /* slash command일때 코드 */
  ...
  } else if (interaction.type == MessageType.Default) {
  /* message일때 코드 */
  ...
  } else {
  /* error 처리 */
  }
}