반응형
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> |
클래스 Message는 class 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 처리 */
}
}
'discord developer' 카테고리의 다른 글
디스코드 레니 봇 개발 일지_ ( 로그 편 ) (5) | 2023.11.20 |
---|---|
discord.js 를 이용한 디스코드 음악 봇 만들기 (ytdl-core 심화편) (0) | 2023.07.30 |
how to fix 'node-pre-gyp error' in linux or ubuntu (@discordjs/opu installing issue) (0) | 2023.07.16 |
slash commands에 쿨타임 적용하기 [discord.js] (0) | 2023.06.30 |
Slash command 디스코드 봇 제작 [discord.js] (+ interaction 오류 처리 & 안정적인 interaction 반응) (0) | 2023.06.27 |