fix: 🗃️ update instead of create new cooldowns if one already exists

This should solve problems related to bot creating new cooldowns when they are expired instead of updating their "expiresAt" date
This commit is contained in:
Axel Olausson Holtenäs 2023-05-31 13:26:36 +02:00
parent 6ad1de8d6f
commit 3875c9e136
3 changed files with 17 additions and 13 deletions

View file

@ -27,7 +27,11 @@ export default async function handleCommandInteraction(
const { guildCooldown, userCooldown, guildMemberCooldown } =
await cooldownManager.checkCooldowns(cooldownItem, guild, user);
if (guildCooldown || userCooldown || guildMemberCooldown) {
if (
(guildCooldown && guildCooldown.expiresAt > new Date()) ||
(userCooldown && userCooldown.expiresAt > new Date()) ||
(guildMemberCooldown && guildMemberCooldown.expiresAt > new Date())
) {
await handleCooldown(
interaction,
guildCooldown,

View file

@ -56,7 +56,10 @@ async function isUserOnCooldown(guild: Guild, author: User): Promise<boolean> {
guild,
author
);
return cooldownActive !== null;
if (!cooldownActive) return false;
return cooldownActive.expiresAt > new Date();
}
async function setCooldown(guild: Guild, user: User) {

View file

@ -18,21 +18,19 @@ class CooldownManager {
user: user ? { connect: { id: user.id } } : undefined,
};
const { guildCooldown, guildMemberCooldown, userCooldown } =
await this.checkCooldowns(cooldownItem, guild, user);
if (guildCooldown || guildMemberCooldown || userCooldown) {
await prisma.cooldown.updateMany({
where: {
const existingCooldown = await this.checkCooldown(
cooldownItem,
guild: guild ? { id: guild.id } : undefined,
user: user ? { id: user.id } : undefined,
guild,
user
);
if (existingCooldown) {
await prisma.cooldown.update({
where: {
id: existingCooldown.id,
},
data: {
cooldownItem,
expiresAt,
guildId: guild ? guild.id : undefined,
userId: user ? user.id : undefined,
},
});
} else {
@ -62,7 +60,6 @@ class CooldownManager {
cooldownItem,
guild: guild ? { id: guild.id } : null,
user: user ? { id: user.id } : null,
expiresAt: { gte: new Date() },
};
const cooldown = await prisma.cooldown.findFirst({ where });
const duration = Date.now() - start;