Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
fix: cooldown msg
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlolDev committed Nov 15, 2023
1 parent 3721ecf commit bfb738b
Showing 1 changed file with 80 additions and 65 deletions.
145 changes: 80 additions & 65 deletions src/bot/utils/cooldown.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
BigString,
ButtonComponent,
ButtonStyles,
Camelize,
CreateMessageOptions,
DiscordEmbed,
MessageComponentTypes,
MessageComponents,
BigString,
ButtonComponent,
ButtonStyles,
Camelize,
CreateMessageOptions,
DiscordEmbed,
MessageComponentTypes,
MessageComponents,
} from "@discordeno/bot";
import { CollectionNames } from "../../types/collections.js";
import { Command, CommandCooldown } from "../types/index.js";
Expand All @@ -15,68 +15,83 @@ import { generateCampaignEmbed } from "./campaigns.js";
import config from "../../config.js";

export async function checkCooldown(
userId: BigString,
command: Command,
type: keyof CommandCooldown,
userId: BigString,
command: Command,
type: keyof CommandCooldown
): Promise<CreateMessageOptions | undefined> {
const has = await getCooldown(userId, command.body.name);
if (has) {
const embeds: DiscordEmbed[] = [];
const components: MessageComponents = [];
const user = await env(userId.toString());
if (!user) return {};
const premiumInfo = await premium(user);
if (!premiumInfo) {
const campaign = await generateCampaignEmbed();
if (campaign) {
components.push({
type: MessageComponentTypes.ActionRow,
components: [
{
type: MessageComponentTypes.Button,
label: "Click here",
customId: `campaign_${campaign.id}`,
style: ButtonStyles.Secondary,
},
] as [ButtonComponent],
});
embeds.push(campaign.embed);
const has = await getCooldown(userId, command.body.name);
if (has) {
const embeds: DiscordEmbed[] = [];
const components: MessageComponents = [];
const user = await env(userId.toString());
if (!user) return {};
const premiumInfo = await premium(user);

return {
embeds,
components,
};
}
}
embeds.push({
description: "You have to wait a bit before using this command again.",
color: config.brand.color,
});
return {
embeds,
};
} else {
await setCooldown(userId, command.body.name, command.cooldown[type]);
}
const cooldownEmbed = {
description: `This is a cooldown message. You can use this command again <t:${has}:R>. You can reduce/remove this cooldown by donating to the project. [Click here](https://turing.sh/pay)`,
};

if (!premiumInfo) {
const campaign = await generateCampaignEmbed();
if (campaign) {
components.push({
type: MessageComponentTypes.ActionRow,
components: [
{
type: MessageComponentTypes.Button,
label: "Click here",
customId: `campaign_${campaign.id}`,
style: ButtonStyles.Secondary,
},
] as [ButtonComponent],
});
embeds.push(campaign.embed);
embeds.push(cooldownEmbed);
return {
embeds,
components,
};
}
}
embeds.push(cooldownEmbed);
return {
embeds,
};
} else {
await setCooldown(userId, command.body.name, command.cooldown[type]);
}
}

export async function getCooldown(userId: BigString, command: string) {
const collection = getCollectionKey(CollectionNames.cooldowns, userId.toString(), command);
const cooldown = await getCache<{
expires: number;
}>(collection);
if (!cooldown) return false;
return Date.now() < cooldown.expires;
const collection = getCollectionKey(
CollectionNames.cooldowns,
userId.toString(),
command
);
const cooldown = await getCache<{
expires: number;
}>(collection);
if (!cooldown) return false;
// calculate the time left
return cooldown.expires;
}

export async function setCooldown(userId: BigString, command: string, time: number) {
if (time <= 0) return;
const collection = getCollectionKey(CollectionNames.cooldowns, userId.toString(), command);
await setCache(
collection,
{
expires: Date.now() + time,
},
time,
);
export async function setCooldown(
userId: BigString,
command: string,
time: number
) {
if (time <= 0) return;
const collection = getCollectionKey(
CollectionNames.cooldowns,
userId.toString(),
command
);
await setCache(
collection,
{
expires: Date.now() + time,
},
time
);
}

0 comments on commit bfb738b

Please sign in to comment.