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:
parent
6ad1de8d6f
commit
3875c9e136
3 changed files with 17 additions and 13 deletions
|
@ -27,7 +27,11 @@ export default async function handleCommandInteraction(
|
||||||
const { guildCooldown, userCooldown, guildMemberCooldown } =
|
const { guildCooldown, userCooldown, guildMemberCooldown } =
|
||||||
await cooldownManager.checkCooldowns(cooldownItem, guild, user);
|
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(
|
await handleCooldown(
|
||||||
interaction,
|
interaction,
|
||||||
guildCooldown,
|
guildCooldown,
|
||||||
|
|
|
@ -56,7 +56,10 @@ async function isUserOnCooldown(guild: Guild, author: User): Promise<boolean> {
|
||||||
guild,
|
guild,
|
||||||
author
|
author
|
||||||
);
|
);
|
||||||
return cooldownActive !== null;
|
|
||||||
|
if (!cooldownActive) return false;
|
||||||
|
|
||||||
|
return cooldownActive.expiresAt > new Date();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function setCooldown(guild: Guild, user: User) {
|
async function setCooldown(guild: Guild, user: User) {
|
||||||
|
|
|
@ -18,21 +18,19 @@ class CooldownManager {
|
||||||
user: user ? { connect: { id: user.id } } : undefined,
|
user: user ? { connect: { id: user.id } } : undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
const { guildCooldown, guildMemberCooldown, userCooldown } =
|
const existingCooldown = await this.checkCooldown(
|
||||||
await this.checkCooldowns(cooldownItem, guild, user);
|
cooldownItem,
|
||||||
|
guild,
|
||||||
|
user
|
||||||
|
);
|
||||||
|
|
||||||
if (guildCooldown || guildMemberCooldown || userCooldown) {
|
if (existingCooldown) {
|
||||||
await prisma.cooldown.updateMany({
|
await prisma.cooldown.update({
|
||||||
where: {
|
where: {
|
||||||
cooldownItem,
|
id: existingCooldown.id,
|
||||||
guild: guild ? { id: guild.id } : undefined,
|
|
||||||
user: user ? { id: user.id } : undefined,
|
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
cooldownItem,
|
|
||||||
expiresAt,
|
expiresAt,
|
||||||
guildId: guild ? guild.id : undefined,
|
|
||||||
userId: user ? user.id : undefined,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
@ -62,7 +60,6 @@ class CooldownManager {
|
||||||
cooldownItem,
|
cooldownItem,
|
||||||
guild: guild ? { id: guild.id } : null,
|
guild: guild ? { id: guild.id } : null,
|
||||||
user: user ? { id: user.id } : null,
|
user: user ? { id: user.id } : null,
|
||||||
expiresAt: { gte: new Date() },
|
|
||||||
};
|
};
|
||||||
const cooldown = await prisma.cooldown.findFirst({ where });
|
const cooldown = await prisma.cooldown.findFirst({ where });
|
||||||
const duration = Date.now() - start;
|
const duration = Date.now() - start;
|
||||||
|
|
Loading…
Add table
Reference in a new issue