From 5aa373bff3a88890dfbc3c048f4da4d0c16f1b63 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 19 Oct 2022 20:58:12 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Cooldowns=20now=20in=20Pri?= =?UTF-8?q?sma?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/helpers/cooldown/index.ts | 375 ++++++++++++++++++++++------------ 1 file changed, 246 insertions(+), 129 deletions(-) diff --git a/src/helpers/cooldown/index.ts b/src/helpers/cooldown/index.ts index b6357fb..3847060 100644 --- a/src/helpers/cooldown/index.ts +++ b/src/helpers/cooldown/index.ts @@ -1,157 +1,274 @@ // Dependencies import { ButtonInteraction, CommandInteraction, Message } from "discord.js"; +import addSeconds from "../../helpers/addSeconds"; import logger from "../../middlewares/logger"; +import prisma from "../../prisma"; export const command = async (i: CommandInteraction, cooldown: number) => { - // const { guild, user, commandId } = i; + const { guild, user, commandId } = i; - // // Check if user has a timeout - // const hasTimeout = await timeoutSchema.findOne({ - // guildId: guild?.id || "0", - // userId: user.id, - // cooldown: cooldown, - // timeoutId: commandId, - // }); + if (!guild) throw new Error("Guild not found"); - // // If user is not on timeout - // if (hasTimeout) { - // const { guildId, userId, timeoutId, createdAt } = hasTimeout; - // const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); + // Check if user has a timeout + const hasTimeout = await prisma.cooldown.findUnique({ + where: { + guildId_userId_timeoutId: { + guildId: guild.id, + userId: user.id, + timeoutId: commandId, + }, + }, + }); - // if (!overDue) { - // const diff = Math.round( - // (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000 - // ); + logger.silly(hasTimeout); - // throw new Error( - // `You must wait ${diff} seconds before using this command.` - // ); - // } + // If user is not on timeout + if (hasTimeout) { + const { guildId, userId, timeoutId, createdAt } = hasTimeout; + const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); - // // Delete timeout - // await timeoutSchema - // .deleteOne({ - // guildId, - // userId, - // timeoutId, - // cooldown, - // }) - // .then(async () => { - // logger.debug( - // `Timeout document ${timeoutId} has been deleted from user ${userId}.` - // ); - // }); - // } - // // Create timeout - // await timeoutSchema.create({ - // guildId: guild?.id || "0", - // userId: user.id, - // cooldown: cooldown, - // timeoutId: commandId, - // }); - logger.warn("Command cooldown function is not working with Prisma yet!"); - return false; + if (!overDue) { + const diff = Math.round( + (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000 + ); + + throw new Error( + `You must wait ${diff} seconds before using this command.` + ); + } + + // Delete timeout + const deleteCooldown = await prisma.cooldown.delete({ + where: { + guildId_userId_timeoutId: { + guildId: guild.id, + userId: user.id, + timeoutId: commandId, + }, + }, + }); + + logger.silly(deleteCooldown); + + logger.debug( + `Timeout document ${timeoutId} has been deleted from user ${userId}.` + ); + } + + // Create timeout + const createCooldown = await prisma.cooldown.upsert({ + where: { + guildId_userId_timeoutId: { + userId: user.id, + guildId: guild.id, + timeoutId: commandId, + }, + }, + update: {}, + create: { + guild: { + connectOrCreate: { + create: { + id: guild.id, + }, + where: { + id: guild.id, + }, + }, + }, + user: { + connectOrCreate: { + create: { + id: user.id, + }, + where: { + id: user.id, + }, + }, + }, + timeoutId: commandId, + cooldown, + }, + }); + + logger.silly(createCooldown); }; export const button = async (i: ButtonInteraction, cooldown: number) => { - // const { guild, user, customId } = i; + const { guild, user, customId } = i; - // // Check if user has a timeout - // const hasTimeout = await timeoutSchema.findOne({ - // guildId: guild?.id || "0", - // userId: user.id, - // cooldown: cooldown, - // timeoutId: customId, - // }); + if (!guild) throw new Error("Guild not found"); - // // If user is not on timeout - // if (hasTimeout) { - // const { guildId, userId, timeoutId, createdAt } = hasTimeout; - // const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); + // Check if user has a timeout + const hasTimeout = await prisma.cooldown.findUnique({ + where: { + guildId_userId_timeoutId: { + guildId: guild.id, + userId: user.id, + timeoutId: customId, + }, + }, + }); - // if (!overDue) { - // const diff = Math.round( - // (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000 - // ); + logger.silly(hasTimeout); - // throw new Error( - // `You must wait ${diff} seconds before using this command.` - // ); - // } + // If user is not on timeout + if (hasTimeout) { + const { guildId, userId, timeoutId, createdAt } = hasTimeout; + const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); - // // Delete timeout - // await timeoutSchema - // .deleteOne({ - // guildId, - // userId, - // timeoutId, - // cooldown, - // }) - // .then(async () => { - // logger.debug( - // `Timeout document ${timeoutId} has been deleted from user ${userId}.` - // ); - // }); - // } - // // Create timeout - // await timeoutSchema.create({ - // guildId: guild?.id || "0", - // userId: user.id, - // cooldown: cooldown, - // timeoutId: customId, - // }); - logger.warn("Button cooldown function is not working with Prisma yet!"); - return false; + if (!overDue) { + const diff = Math.round( + (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000 + ); + + throw new Error( + `You must wait ${diff} seconds before using this command.` + ); + } + + // Delete timeout + const deleteCooldown = await prisma.cooldown.delete({ + where: { + guildId_userId_timeoutId: { + guildId: guild.id, + userId: user.id, + timeoutId: customId, + }, + }, + }); + + logger.silly(deleteCooldown); + + logger.debug( + `Timeout document ${timeoutId} has been deleted from user ${userId}.` + ); + } + + // Create timeout + const createCooldown = await prisma.cooldown.upsert({ + where: { + guildId_userId_timeoutId: { + userId: user.id, + guildId: guild.id, + timeoutId: customId, + }, + }, + update: {}, + create: { + guild: { + connectOrCreate: { + create: { + id: guild.id, + }, + where: { + id: guild.id, + }, + }, + }, + user: { + connectOrCreate: { + create: { + id: user.id, + }, + where: { + id: user.id, + }, + }, + }, + timeoutId: customId, + cooldown, + }, + }); + + logger.silly(createCooldown); }; export const message = async (msg: Message, cooldown: number, id: string) => { - // const { guild, member } = msg; - // if (!guild) throw new Error("Guild is undefined"); - // if (!member) throw new Error("Member is undefined"); + const { guild, member } = msg; - // // Check if user has a timeout - // const hasTimeout = await timeoutSchema.findOne({ - // guildId: guild?.id || "0", - // userId: member.id, - // cooldown: cooldown, - // timeoutId: id, - // }); + if (!guild) throw new Error("Guild not found"); + if (!member) throw new Error("Member is undefined"); - // // If user is not on timeout - // if (hasTimeout) { - // const { guildId, userId, timeoutId, createdAt } = hasTimeout; - // const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); + // Check if user has a timeout + const hasTimeout = await prisma.cooldown.findUnique({ + where: { + guildId_userId_timeoutId: { + guildId: guild.id, + userId: member.id, + timeoutId: id, + }, + }, + }); - // if (!overDue) { - // const diff = Math.round( - // (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000 - // ); + logger.silly(hasTimeout); - // return `User: ${userId} on timeout-id: ${id} with cooldown: ${cooldown} secs with remaining: ${diff} secs.`; - // } + // If user is not on timeout + if (hasTimeout) { + const { guildId, userId, timeoutId, createdAt } = hasTimeout; + const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); - // // Delete timeout - // await timeoutSchema - // .deleteOne({ - // guildId, - // userId: member.id, - // timeoutId: id, - // cooldown, - // }) - // .then(async () => { - // logger.debug( - // `Timeout document ${timeoutId} has been deleted from user ${userId}.` - // ); - // }); - // } - // // Create timeout - // await timeoutSchema.create({ - // guildId: guild?.id || "0", - // userId: member.id, - // cooldown: cooldown, - // timeoutId: id, - // }); + if (!overDue) { + const diff = Math.round( + (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000 + ); - logger.warn("Message cooldown function is not working with Prisma yet!"); - return false; + return `User: ${userId} on timeout-id: ${id} with cooldown: ${cooldown} secs with remaining: ${diff} secs.`; + } + + // Delete timeout + const deleteCooldown = await prisma.cooldown.delete({ + where: { + guildId_userId_timeoutId: { + guildId: guild.id, + userId: member.id, + timeoutId: id, + }, + }, + }); + + logger.silly(deleteCooldown); + + logger.debug( + `Timeout document ${timeoutId} has been deleted from user ${userId}.` + ); + } + + // Create timeout + const createCooldown = await prisma.cooldown.upsert({ + where: { + guildId_userId_timeoutId: { + userId: member.id, + guildId: guild.id, + timeoutId: id, + }, + }, + update: {}, + create: { + guild: { + connectOrCreate: { + create: { + id: guild.id, + }, + where: { + id: guild.id, + }, + }, + }, + user: { + connectOrCreate: { + create: { + id: member.id, + }, + where: { + id: member.id, + }, + }, + }, + timeoutId: id, + cooldown, + }, + }); + + logger.silly(createCooldown); };