반응형
한번에 많은 명령어를 도배하듯이
사용한다면 봇이 버티기 힘들수있습니다.
그래서 setTimeout() 함수를 사용하여 간단한 쿨타임을 만들겠습니다!
전역 범위 코드
client.cooldowns = new Collection();
client.COOLDOWN_SECONDS = 1;
쿨타임 적용 코드
Client.on(Event.IntractionCreate, (interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`명령어 [ ${interaction.commandName} ] 를 찾을 수 없습니다! `);
return;
}
if (client.cooldowns.has(interaction.user.id)) {
return await interaction.reply({ content: "명령어를 천천히 사용해주세요!", ephemeral: true });
}
await command.execute(interaction);
client.cooldowns.set(interaction.user.id, true);
setTimeout(() => {
client.cooldowns.delete(interaction.user.id);
}, client.COOLDOWN_SECONDS * 1000);
})
간단히 설명하자면
client 에 cooldowns 이 적용된 서버를 기록할 Map() 과
쿨다운이 지속될 시간(초)을 COOLDOWN_SECONDS에 입력합니다.
명령어를 실행할때 실행한 사람의 id로 쿨다운 Map에 입력 합니다.
Client에 입력된 COOLDOWN_SECONDS를 setTimeout의 기다리는 시간으로 설정합니다.
(setTimeout은 시간단위가 ms 이기에 1000을 곱하였습니다.)
해당 쿨다운 시간이 종료되면 명령어를 사용한 유저를 client cooldowns Map에서 삭제합니다.
만약 유저가 삭제되지않은 상태에서 입력할시 "쿨다운이 끝나지 않았습니다." 라는 메시지를 보내게 합니다.
읽어주셔서 감사합니다!
'discord developer' 카테고리의 다른 글
interaction & message 올바른 type(Interface) 지정하는 법 (discord.js / typescript) (0) | 2023.07.28 |
---|---|
how to fix 'node-pre-gyp error' in linux or ubuntu (@discordjs/opu installing issue) (0) | 2023.07.16 |
Slash command 디스코드 봇 제작 [discord.js] (+ interaction 오류 처리 & 안정적인 interaction 반응) (0) | 2023.06.27 |
디스코드 봇 생성하기(봇 생성하기) (0) | 2023.06.27 |
ytdl-core를 이용하여 디스코드 음악봇 만들기 discord.js (0) | 2023.06.20 |