/[sudobot]/branches/4.x/src/services/Cooldown.ts
ViewVC logotype

Annotation of /branches/4.x/src/services/Cooldown.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 4217 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /**
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2022 OSN Inc.
5     *
6     * SudoBot is free software; you can redistribute it and/or modify it
7     * under the terms of the GNU Affero General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10     *
11     * SudoBot is distributed in the hope that it will be useful, but
12     * WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU Affero General Public License for more details.
15     *
16     * You should have received a copy of the GNU Affero General Public License
17     * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18     */
19    
20     import { formatDuration, intervalToDuration } from "date-fns";
21     import { Message } from "discord.js";
22     import MessageEmbed from "../client/MessageEmbed";
23     import KeyValuePair from "../types/KeyValuePair";
24     import BaseCommand from "../utils/structures/BaseCommand";
25     import Service from "../utils/structures/Service";
26    
27     export default class Cooldown extends Service {
28     cooldowns = new Map<string, (KeyValuePair<NodeJS.Timeout>)>();
29    
30     async onMessageCreate(message: Message, command: BaseCommand) {
31     if (!command.coolDown)
32     return true;
33    
34     const commandName = command.getName();
35    
36     if (!this.cooldowns.has(message.guild!.id)) {
37     this.cooldowns.set(message.guild!.id, {
38     [`${commandName}-${message.author.id}-${Date.now()}`]: setTimeout(() => {
39     const cooldowns = this.cooldowns.get(message.guild!.id);
40    
41     if (cooldowns) {
42     for (const cooldown in cooldowns) {
43     if (!cooldowns[cooldown]) {
44     continue;
45     }
46    
47     if (cooldown.startsWith(`${commandName}-${message.author.id}`)) {
48     delete cooldowns[cooldown];
49     console.log('Cooldown expired', `${commandName}-${message.author.tag}`);
50     }
51     }
52     }
53     }, command.coolDown)
54     });
55     }
56     else {
57     const cooldowns = this.cooldowns.get(message.guild!.id)!;
58    
59     console.log("Cooldowns", cooldowns);
60    
61     for (const cooldown in cooldowns) {
62     if (cooldown.startsWith(`${commandName}-${message.author.id}`) && cooldowns[cooldown]) {
63     console.log('Cooldown triggered', `${commandName}-${message.author.tag}`);
64     const [,, time] = cooldown.split('-');
65     const end = parseInt(time) + command.coolDown;
66     console.log(end);
67     const timetext = formatDuration(intervalToDuration({ start: Date.now(), end: parseInt(time) + command.coolDown }));
68    
69     await message.reply({
70     embeds: [
71     new MessageEmbed({
72     description: `:clock: Please wait, you're doing that too fast!`,
73     footer: { text: 'Cooldown • ' + (timetext.trim() === '' ? '1 second' : timetext) },
74     color: 0xf14a60
75     })
76     ]
77     });
78    
79     return false;
80     }
81     }
82    
83     cooldowns[`${commandName}-${message.author.id}-${Date.now()}`] = setTimeout(() => {
84     const cooldowns = this.cooldowns.get(message.guild!.id);
85    
86     if (cooldowns) {
87     for (const cooldown in cooldowns) {
88     if (!cooldowns[cooldown]) {
89     continue;
90     }
91    
92     if (cooldown.startsWith(`${commandName}-${message.author.id}`)) {
93     delete cooldowns[cooldown];
94     console.log('Cooldown expired', `${commandName}-${message.author.tag}`);
95     }
96     }
97     }
98     }, command.coolDown);
99     }
100    
101     return true;
102     }
103     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26