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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 54 - (show annotations)
Mon Jul 29 17:28:24 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3169 byte(s)
Added -emoji command and cooldown 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 export type RegistryData = {
17 user: User;
18 guilds: {
19 [id: string]: {
20 date: Date;
21 timeout: NodeJS.Timeout;
22 }
23 }
24 };
25
26 export default class Cooldown {
27 config: CooldownConfig;
28 cmdRegistry: Map <string, RegistryData>;
29
30 constructor(protected client: DiscordClient) {
31 this.config = {} as CooldownConfig;
32 this.cmdRegistry = new Map();
33 }
34
35 async start(msg: Message | CommandInteraction, options: CommandOptions | InteractionOptions): Promise <boolean> {
36 this.config = this.client.config.get('cooldown');
37
38 if (!this.config.enabled) {
39 return true;
40 }
41
42 if ((msg.member as GuildMember).roles.cache.has(this.client.config.get('mod_role')))
43 return true;
44
45 const { cmdName } = options;
46 let time = null;
47
48 if (this.config.cmds[cmdName]) {
49 time = this.config.cmds[cmdName];
50 }
51 else {
52 time = this.config.global;
53 }
54
55 if (time === null) {
56 return true;
57 }
58
59 console.log(time);
60
61
62 if (!this.cmdRegistry.has(msg.member!.user.id) || this.cmdRegistry.get(msg.member!.user.id)?.guilds[msg.guild!.id] === undefined) {
63 let prevData: any = {};
64
65 if (this.cmdRegistry.has(msg.member!.user.id)) {
66 prevData = this.cmdRegistry.get(msg.member!.user.id)?.guilds;
67 }
68
69 this.cmdRegistry.set(msg.member!.user.id, {
70 user: msg.member!.user as User,
71 guilds: {
72 ...prevData,
73 [msg.guild!.id]: {
74 date: new Date(),
75 timeout: setTimeout(() => {
76 console.log('Cleared');
77 this.cmdRegistry.delete(msg.member!.user.id);
78 }, time)
79 }
80 }
81 });
82 }
83 else {
84 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);
87
88
89 if ((registry?.guilds[msg.guild!.id].date.getTime()! + time)! > Date.now()) {
90 await msg.reply({
91 embeds: [
92 new MessageEmbed()
93 .setColor('#f14a60')
94 .setDescription(":clock: Please try again in " + timeSince(registry?.guilds[msg.guild!.id].date.getTime()!).replace(/ ago$/g, ''))
95 ]
96 });
97
98 return false;
99 }
100 }
101
102 return true;
103 }
104 };

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26