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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 54 by rakin, Mon Jul 29 17:28:24 2024 UTC revision 56 by rakin, Mon Jul 29 17:28:24 2024 UTC
# Line 13  export type CooldownConfig = { Line 13  export type CooldownConfig = {
13      }      }
14  };  };
15    
 export type RegistryData = {  
     user: User;  
     guilds: {  
         [id: string]: {  
             date: Date;  
             timeout: NodeJS.Timeout;  
         }  
     }  
 };  
16    
17  export default class Cooldown {  export default class Cooldown {
18      config: CooldownConfig;      config: CooldownConfig;
19      cmdRegistry: Map <string, RegistryData>;      cooldownCommands: Array <string> = [];
20        cooldownTimes: Array <Date> = [];
21    
22      constructor(protected client: DiscordClient) {      constructor(protected client: DiscordClient) {
23          this.config = {} as CooldownConfig;          this.config = {} as CooldownConfig;
         this.cmdRegistry = new Map();  
24      }      }
25    
26      async start(msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions): Promise <boolean> {      async start(msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions): Promise <boolean> {
27          this.config = this.client.config.get('cooldown');          this.config = this.client.config.get('cooldown');
28    
29          if (!this.config.enabled) {          const { cmdName } = options;
30            const command = this.client.commands.get(cmdName)!;
31    
32            if (!this.config.enabled && !command.coolDown) {
33              return true;              return true;
34          }          }
35    
36          if ((msg.member as GuildMember).roles.cache.has(this.client.config.get('mod_role')))          if (!command.coolDown && (msg.member as GuildMember).roles.cache.has(this.client.config.get('mod_role')))
37              return true;              return true;
38    
         const { cmdName } = options;  
39          let time = null;          let time = null;
40    
41          if (this.config.cmds[cmdName]) {          if (command.coolDown) {
42                time = command.coolDown;
43            }
44            else if (this.config.cmds[cmdName]) {
45              time = this.config.cmds[cmdName];              time = this.config.cmds[cmdName];
46          }          }
47          else {          else {
# Line 57  export default class Cooldown { Line 53  export default class Cooldown {
53          }          }
54    
55          console.log(time);          console.log(time);
56            const index = this.cooldownCommands.indexOf(`${msg.member!.user.id}/${msg.guild!.id}/${cmdName}`);
57                    
58            if (index !== -1) {
59                await msg.reply({
60                    embeds: [
61                        new MessageEmbed()
62                        .setColor('#f14a60')
63                        .setDescription(':clock: Please try again in ' + timeProcess((time - ((new Date()).getTime() - this.cooldownTimes[index].getTime())) / 1000))
64                    ]
65                });
66    
67          if (!this.cmdRegistry.has(msg.member!.user.id) || this.cmdRegistry.get(msg.member!.user.id)?.guilds[msg.guild!.id] === undefined) {              return false;
68              let prevData: any = {};          }
69    
70              if (this.cmdRegistry.has(msg.member!.user.id)) {          this.cooldownCommands.push(`${msg.member!.user.id}/${msg.guild!.id}/${cmdName}`);
71                  prevData = this.cmdRegistry.get(msg.member!.user.id)?.guilds;          this.cooldownTimes.push(new Date());
72              }          
73            setTimeout(() => {
74                console.log('Clearing...');
75                console.log(this.cooldownCommands, this.cooldownTimes);
76                            
77              this.cmdRegistry.set(msg.member!.user.id, {              this.cooldownCommands = this.cooldownCommands.filter((val, index) => {
78                  user: msg.member!.user as User,                  if (val === `${msg.member!.user.id}/${msg.guild!.id}/${cmdName}`) {
79                  guilds: {                      this.cooldownTimes.splice(index, 1);
80                      ...prevData,                      return false;
                     [msg.guild!.id]: {  
                         date: new Date(),  
                         timeout: setTimeout(() => {  
                             console.log('Cleared');  
                             this.cmdRegistry.delete(msg.member!.user.id);  
                         }, time)  
                     }  
81                  }                  }
82    
83                    return true;
84              });              });
         }  
         else {  
             const registry = this.cmdRegistry.get(msg.member!.user.id);  
85    
86              console.log(new Date(registry?.guilds[msg.guild!.id].date.getTime()! + time), new Date);              console.log(this.cooldownCommands, this.cooldownTimes);
87                            
88            }, time);
             if ((registry?.guilds[msg.guild!.id].date.getTime()! + time)! > Date.now()) {  
                 await msg.reply({  
                     embeds: [  
                         new MessageEmbed()  
                         .setColor('#f14a60')  
                         .setDescription(":clock: Please try again in " + timeSince(registry?.guilds[msg.guild!.id].date.getTime()!).replace(/ ago$/g, ''))  
                     ]  
                 });  
   
                 return false;  
             }  
         }  
89    
90          return true;          return true;
91      }      }

Legend:
Removed from v.54  
changed lines
  Added in v.56

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26