1 |
rakin |
397 |
import { formatDuration, intervalToDuration } from "date-fns"; |
2 |
|
|
import { Message } from "discord.js"; |
3 |
|
|
import MessageEmbed from "../client/MessageEmbed"; |
4 |
|
|
import KeyValuePair from "../types/KeyValuePair"; |
5 |
|
|
import BaseCommand from "../utils/structures/BaseCommand"; |
6 |
|
|
import Service from "../utils/structures/Service"; |
7 |
|
|
|
8 |
|
|
export default class Cooldown extends Service { |
9 |
|
|
cooldowns = new Map<string, (KeyValuePair<NodeJS.Timeout>)>(); |
10 |
|
|
|
11 |
|
|
async onMessageCreate(message: Message, command: BaseCommand) { |
12 |
|
|
if (!command.coolDown) |
13 |
|
|
return true; |
14 |
|
|
|
15 |
|
|
const commandName = command.getName(); |
16 |
|
|
|
17 |
|
|
if (!this.cooldowns.has(message.guild!.id)) { |
18 |
|
|
this.cooldowns.set(message.guild!.id, { |
19 |
|
|
[`${commandName}-${message.author.id}-${Date.now()}`]: setTimeout(() => { |
20 |
|
|
const cooldowns = this.cooldowns.get(message.guild!.id); |
21 |
|
|
|
22 |
|
|
if (cooldowns) { |
23 |
|
|
for (const cooldown in cooldowns) { |
24 |
|
|
if (!cooldowns[cooldown]) { |
25 |
|
|
continue; |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
if (cooldown.startsWith(`${commandName}-${message.author.id}`)) { |
29 |
|
|
delete cooldowns[cooldown]; |
30 |
|
|
console.log('Cooldown expired', `${commandName}-${message.author.tag}`); |
31 |
|
|
} |
32 |
|
|
} |
33 |
|
|
} |
34 |
|
|
}, command.coolDown) |
35 |
|
|
}); |
36 |
|
|
} |
37 |
|
|
else { |
38 |
|
|
const cooldowns = this.cooldowns.get(message.guild!.id)!; |
39 |
|
|
|
40 |
|
|
console.log("Cooldowns", cooldowns); |
41 |
|
|
|
42 |
|
|
for (const cooldown in cooldowns) { |
43 |
|
|
if (cooldown.startsWith(`${commandName}-${message.author.id}`) && cooldowns[cooldown]) { |
44 |
|
|
console.log('Cooldown triggered', `${commandName}-${message.author.tag}`); |
45 |
|
|
const [,, time] = cooldown.split('-'); |
46 |
|
|
const end = parseInt(time) + command.coolDown; |
47 |
|
|
console.log(end); |
48 |
|
|
const timetext = formatDuration(intervalToDuration({ start: Date.now(), end: parseInt(time) + command.coolDown })); |
49 |
|
|
|
50 |
|
|
await message.reply({ |
51 |
|
|
embeds: [ |
52 |
|
|
new MessageEmbed({ |
53 |
|
|
description: `:clock: Please wait, you're doing that too fast!`, |
54 |
|
|
footer: { text: 'Cooldown • ' + (timetext.trim() === '' ? '1 second' : timetext) }, |
55 |
|
|
color: 0xf14a60 |
56 |
|
|
}) |
57 |
|
|
] |
58 |
|
|
}); |
59 |
|
|
|
60 |
|
|
return false; |
61 |
|
|
} |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
cooldowns[`${commandName}-${message.author.id}-${Date.now()}`] = setTimeout(() => { |
65 |
|
|
const cooldowns = this.cooldowns.get(message.guild!.id); |
66 |
|
|
|
67 |
|
|
if (cooldowns) { |
68 |
|
|
for (const cooldown in cooldowns) { |
69 |
|
|
if (!cooldowns[cooldown]) { |
70 |
|
|
continue; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
if (cooldown.startsWith(`${commandName}-${message.author.id}`)) { |
74 |
|
|
delete cooldowns[cooldown]; |
75 |
|
|
console.log('Cooldown expired', `${commandName}-${message.author.tag}`); |
76 |
|
|
} |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
}, command.coolDown); |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
return true; |
83 |
|
|
} |
84 |
|
|
} |