Skip to content

Commit e745e19

Browse files
committed
Cooldowns Remastered
Added types to cooldown, for example "channel", "user" or "guild".
1 parent bf2dfd7 commit e745e19

File tree

5 files changed

+72
-21
lines changed

5 files changed

+72
-21
lines changed

config.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,30 @@ module.exports = new (require("./types/Config"))({
2929
// Kullanıcı hatalarındaki uyarı mesajları/olayları.
3030
userErrors: {
3131
// Arka arkaya interaksiyon kullanma limiti aşıldığında.
32-
coolDown(interaction, uInteraction, coolDown, other) {
33-
interaction.reply({ ephemeral: true, content: `Bu interaksiyonu tekrardan ${(coolDown / 1000).toFixed(2)} saniye içerisinde kullanabilirsin.` })
32+
coolDown(interaction, uInteraction, coolDown, other, type) {
33+
switch (type) {
34+
case "user": {
35+
interaction.reply({ ephemeral: true, content: `Bu interaksiyonu sen tekrardan ${(coolDown / 1000).toFixed(2)} saniye içerisinde kullanabilirsin.` })
36+
break;
37+
}
38+
case "member": {
39+
interaction.reply({ ephemeral: true, content: `Bu interaksiyonu bu sunucuda tekrardan senin kullanabilmen için ${(coolDown / 1000).toFixed(2)} saniye beklemen lazım.` })
40+
break;
41+
}
42+
case "guild": {
43+
interaction.reply({ ephemeral: true, content: `Bu interaksiyonu bu sunucuda tekrardan ${(coolDown / 1000).toFixed(2)} saniye içerisinde kullanabilirsin.` })
44+
break;
45+
}
46+
case "channel": {
47+
interaction.reply({ ephemeral: true, content: `Bu interaksiyonu bu kanalda tekrardan ${(coolDown / 1000).toFixed(2)} saniye içerisinde kullanabilirsin.` })
48+
break;
49+
}
50+
case "any": {
51+
interaction.reply({ ephemeral: true, content: `Bu interaksiyonu tekrardan ${(coolDown / 1000).toFixed(2)} saniye içerisinde kullanabilirsin.` })
52+
break;
53+
}
54+
}
55+
3456
},
3557
// interaksiyon kapalı olduğunda
3658
disabled(interaction, uInteraction, other) {

index.js

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ let eventListeners = [];
5555
async function load() {
5656
let loadStart = Date.now();
5757
console.debug(`[HATA AYIKLAMA] Yüklemeye başlandı!`);
58-
58+
5959
let interactionFiles = await getInteractionFilePaths();
6060
await chillout.forEach(interactionFiles, (interactionFile) => {
6161
let start = Date.now();
@@ -163,7 +163,7 @@ async function load() {
163163
*/
164164
([eventName, events]) => {
165165
console.info(`[BİLGİ] Event "${eventName}" için ${events.length} dinleyici yüklendi!`);
166-
166+
167167
let listener = (...args) => {
168168

169169
setTimeout(async () => {
@@ -199,7 +199,7 @@ async function unloadModule(modulePath) {
199199
if (nodeModule) {
200200
if (nodeModule.children.length) await chillout.forEach(nodeModule.children, async (child) => {
201201
if (child.filename) unloadModule(child.filename);
202-
});
202+
});
203203
}
204204
delete require.cache[modulePath];
205205
}
@@ -297,31 +297,52 @@ client.on("interactionCreate", async (interaction) => {
297297
return;
298298
}
299299

300-
let userCooldown = uInter.coolDowns.get(interaction.user.id) || 0;
301-
if (Date.now() < userCooldown) {
302-
config.userErrors.coolDown(interaction, uInter, userCooldown - Date.now(), other);
303-
return;
300+
if (typeof uInter.coolDown == "number") uInter.coolDown = {
301+
type: "user",
302+
amount: uInter.coolDown,
303+
}
304+
305+
let converter = {
306+
"user": interaction.user.id,
307+
"member": interaction.user.id + "_" + interaction.guild?.id,
308+
"channel": interaction.channel?.id || interaction.user.id + " _c",
309+
"guild": interaction.guild?.id || interaction.user.id + "_g",
310+
"any": "any"
311+
}
312+
313+
if (uInter.coolDown && !uInter.coolDown.amount) uInter.coolDown.amount = 0;
314+
let now = Date.now();
315+
316+
for (let k in converter) {
317+
let key = converter[k];
318+
let keyCooldown = uInter.coolDowns.get(key);
319+
if (uInter.coolDown?.amount && (now < keyCooldown)) {
320+
config.userErrors.coolDown(interaction, uInter, keyCooldown - now, other, k);
321+
return;
322+
}
304323
}
305324

306-
function setCoolDown(duration = 0) {
325+
function setCoolDown(duration = 0, type = "user") {
326+
let ckey = converter[type] || interaction.user.id;
307327
if (typeof duration == "number" && duration > 0) {
308-
return uInter.coolDowns.set(interaction.user.id, Date.now() + duration);
328+
return uInter.coolDowns.set(ckey, Date.now() + duration);
309329
} else {
310-
return uInter.coolDowns.delete(interaction.user.id);
330+
return uInter.coolDowns.delete(ckey);
311331
}
312332
}
333+
313334
other.setCoolDown = setCoolDown;
314335

315-
if (uInter.coolDown > 0) {
316-
setCoolDown(uInter.coolDown);
336+
if (uInter.coolDown?.amount > 0) {
337+
setCoolDown(uInter.coolDown?.amount, uInter.coolDown?.type);
317338
}
318339

319340
if (uInter.guildOnly && uInter.perms.bot.length != 0 && !uInter.perms.bot.every(perm => interaction.guild.me.permissions.has(perm))) {
320341
config.userErrors.botPermsRequired(interaction, uInter, uInter.perms.bot, other);
321342
return;
322343
}
323344

324-
if (uInter.guildOnly && uInter.perms.user.length != 0 && !uInter.perms.user.every(perm => interaction.member.permissions.has(perm))) {
345+
if (uInter.guildOnly && (!config.developers.has(interaction.user.id)) && uInter.perms.user.length != 0 && !uInter.perms.user.every(perm => interaction.member.permissions.has(perm))) {
325346
config.userErrors.userPermsRequired(interaction, uInter, uInter.perms.user, other);
326347
return;
327348
}
@@ -350,7 +371,7 @@ client.on("interactionCreate", async (interaction) => {
350371
await config.onBeforeLoad(client);
351372
await load();
352373
await config.onAfterLoad(client);
353-
374+
354375
await client.login(config.clientToken);
355376

356377
config.onReady(client);

interactions/autocomplateUnban.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module.exports = new Underline.SlashCommand({
22
description: "Banlı bir kullanıcının banını açmanızı sağlar.",
33
name: ["unban"],
44
async onInteraction(inter, other) {
5+
other.setCoolDown(30000, "channel")
6+
other.setCoolDown(10000, "guild")
57
let targetId = inter.options.getString("id", false)
68
await inter.guild.bans.fetch({ cache: false });
79
if (!inter.guild.bans.cache.has(targetId)) return inter.reply("Yasağını açacak kişiyi bulamadım!");
@@ -25,7 +27,10 @@ module.exports = new Underline.SlashCommand({
2527
}
2628
],
2729
guildOnly: true,
28-
coolDown: 2000,
30+
coolDown: {
31+
type: "guild",
32+
amount: 5000
33+
},
2934
perms: {
3035
bot: ["BAN_MEMBERS"],
3136
user: ["BAN_MEMBERS"]

types/Config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Config {
1212
/** @type {Discord.ClientOptions} */
1313
clientOptions = {};
1414

15-
/** @type {{coolDown(interaction: Discord.CommandInteraction, interaction: Interaction, timeout: number, other: {[key:string|number]: any}): void, disabled(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void, blocked(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void, botPermsRequired(interaction: Discord.CommandInteraction, interaction: Interaction, perms: string[], other: {setCoolDown(duration:number): void, [key:string|number]: any}): void, userPermsRequired(interaction: Discord.CommandInteraction, interaction: Interaction, perms: string[], other: {setCoolDown(duration:number): void, [key:string|number]: any}): void, developerOnly(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void, guildOnly(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void}} */
15+
/** @type {{coolDown(interaction: Discord.CommandInteraction, interaction: Interaction, timeout: number, other: {[key:string|number]: any}, type: "user" | "member" | "channel" | "guild" | "any"): void, disabled(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void, blocked(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void, botPermsRequired(interaction: Discord.CommandInteraction, interaction: Interaction, perms: string[], other: {setCoolDown(duration:number): void, [key:string|number]: any}): void, userPermsRequired(interaction: Discord.CommandInteraction, interaction: Interaction, perms: string[], other: {setCoolDown(duration:number): void, [key:string|number]: any}): void, developerOnly(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void, guildOnly(interaction: Discord.CommandInteraction, interaction: Interaction, other: {[key:string|number]: any}): void}} */
1616
userErrors = {};
1717

1818
/** @type {{[key: string|number]: any}} */

types/Interaction.d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ export class BaseInteraction {
5353
disabled?: boolean;
5454
developerOnly?: boolean;
5555
other?: { [key: string | number]: any };
56-
coolDown?: number;
56+
coolDown?: {
57+
type: cooldownType;
58+
amount: number;
59+
};
5760
guildOnly?: boolean;
5861
options?: CustomApplicationCommandOptionData[];
5962
defaultPermission?: boolean;
@@ -68,9 +71,9 @@ export class BaseInteraction {
6871

6972
export type TOmittedInteraction = Omit<BaseInteraction, "_type" | "coolDowns" | "name" | "onInteraction" | "actionType" | "options" | "toJSON">;
7073
export type TInteractionConstructor = TOmittedInteraction & ((ActionChatCommand | ActionRightClickCommand | SelectMenu | Button));
71-
74+
type cooldownType = "user" | "member" | "channel" | "guild" | "any";
7275
export interface IOther {
73-
setCoolDown(durations: number): void,
76+
setCoolDown(durations: number, type: cooldownType): void,
7477
[key: string | number]: any
7578
}
7679

0 commit comments

Comments
 (0)