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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 124 - (show annotations)
Mon Jul 29 17:28:41 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 2854 byte(s)
Added hardmuting support
1 import { CommandInteraction, GuildMember, Message, User } from "discord.js";
2 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 cooldownCommands: Array <string> = [];
20 cooldownTimes: Array <Date> = [];
21
22 constructor(protected client: DiscordClient) {
23 this.config = {} as CooldownConfig;
24 }
25
26 async start(msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions): Promise <boolean> {
27 this.config = this.client.config.get('cooldown');
28
29 const { cmdName } = options;
30 const command = this.client.commands.get(cmdName)!;
31
32 if (!this.config.enabled && !command.coolDown) {
33 return true;
34 }
35
36 if (!command.coolDown && (msg.member as GuildMember).roles.cache.has(this.client.config.get('mod_role')))
37 return true;
38
39 let time = null;
40
41 if (command.coolDown) {
42 time = command.coolDown;
43 }
44 else if (this.config.cmds[cmdName]) {
45 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 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())) + 1) / 1000))
64 ]
65 });
66
67 return false;
68 }
69
70 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
77 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 }
82
83 return true;
84 });
85
86 console.log(this.cooldownCommands, this.cooldownTimes);
87
88 }, time);
89
90 return true;
91 }
92 };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26