/[sudobot]/trunk/src/automod/Cooldown.ts
ViewVC logotype

Annotation of /trunk/src/automod/Cooldown.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 125 - (hide annotations)
Mon Jul 29 17:28:41 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2903 byte(s)
Added support of context menu commands
1 rakin 125 import { CommandInteraction, ContextMenuInteraction, GuildMember, Message, User } from "discord.js";
2 rakin 54 import DiscordClient from "../client/Client";
3     import MessageEmbed from "../client/MessageEmbed";
4     import CommandOptions from "../types/CommandOptions";
5     import InteractionOptions from "../types/InteractionOptions";
6     import { timeProcess, timeSince } from "../utils/util";
7    
8     export type CooldownConfig = {
9     enabled: boolean;
10     global: number;
11     cmds: {
12     [cmd: string]: number;
13     }
14     };
15    
16    
17     export default class Cooldown {
18     config: CooldownConfig;
19 rakin 56 cooldownCommands: Array <string> = [];
20     cooldownTimes: Array <Date> = [];
21 rakin 54
22     constructor(protected client: DiscordClient) {
23     this.config = {} as CooldownConfig;
24     }
25    
26 rakin 125 async start(msg: Message | CommandInteraction | ContextMenuInteraction, options: CommandOptions | InteractionOptions): Promise <boolean> {
27 rakin 54 this.config = this.client.config.get('cooldown');
28    
29 rakin 56 const { cmdName } = options;
30     const command = this.client.commands.get(cmdName)!;
31    
32     if (!this.config.enabled && !command.coolDown) {
33 rakin 54 return true;
34     }
35    
36 rakin 56 if (!command.coolDown && (msg.member as GuildMember).roles.cache.has(this.client.config.get('mod_role')))
37 rakin 54 return true;
38    
39     let time = null;
40    
41 rakin 56 if (command.coolDown) {
42     time = command.coolDown;
43     }
44     else if (this.config.cmds[cmdName]) {
45 rakin 54 time = this.config.cmds[cmdName];
46     }
47     else {
48     time = this.config.global;
49     }
50    
51     if (time === null) {
52     return true;
53     }
54    
55     console.log(time);
56 rakin 56 const index = this.cooldownCommands.indexOf(`${msg.member!.user.id}/${msg.guild!.id}/${cmdName}`);
57 rakin 54
58 rakin 56 if (index !== -1) {
59     await msg.reply({
60     embeds: [
61     new MessageEmbed()
62     .setColor('#f14a60')
63 rakin 124 .setDescription(':clock: Please try again in ' + timeProcess(((time - ((new Date()).getTime() - this.cooldownTimes[index].getTime())) + 1) / 1000))
64 rakin 56 ]
65     });
66 rakin 54
67 rakin 56 return false;
68     }
69 rakin 54
70 rakin 56 this.cooldownCommands.push(`${msg.member!.user.id}/${msg.guild!.id}/${cmdName}`);
71     this.cooldownTimes.push(new Date());
72    
73     setTimeout(() => {
74     console.log('Clearing...');
75     console.log(this.cooldownCommands, this.cooldownTimes);
76 rakin 54
77 rakin 56 this.cooldownCommands = this.cooldownCommands.filter((val, index) => {
78     if (val === `${msg.member!.user.id}/${msg.guild!.id}/${cmdName}`) {
79     this.cooldownTimes.splice(index, 1);
80     return false;
81 rakin 54 }
82 rakin 56
83     return true;
84 rakin 54 });
85    
86 rakin 56 console.log(this.cooldownCommands, this.cooldownTimes);
87 rakin 54
88 rakin 56 }, time);
89 rakin 54
90     return true;
91     }
92     };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26