♻️ Cooldowns now in Prisma

This commit is contained in:
Axel Olausson Holtenäs 2022-10-19 20:58:12 +02:00
parent 8e16c7fb64
commit 5aa373bff3

View file

@ -1,157 +1,274 @@
// Dependencies // Dependencies
import { ButtonInteraction, CommandInteraction, Message } from "discord.js"; import { ButtonInteraction, CommandInteraction, Message } from "discord.js";
import addSeconds from "../../helpers/addSeconds";
import logger from "../../middlewares/logger"; import logger from "../../middlewares/logger";
import prisma from "../../prisma";
export const command = async (i: CommandInteraction, cooldown: number) => { export const command = async (i: CommandInteraction, cooldown: number) => {
// const { guild, user, commandId } = i; const { guild, user, commandId } = i;
// // Check if user has a timeout if (!guild) throw new Error("Guild not found");
// const hasTimeout = await timeoutSchema.findOne({
// guildId: guild?.id || "0",
// userId: user.id,
// cooldown: cooldown,
// timeoutId: commandId,
// });
// // If user is not on timeout // Check if user has a timeout
// if (hasTimeout) { const hasTimeout = await prisma.cooldown.findUnique({
// const { guildId, userId, timeoutId, createdAt } = hasTimeout; where: {
// const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); guildId_userId_timeoutId: {
guildId: guild.id,
userId: user.id,
timeoutId: commandId,
},
},
});
// if (!overDue) { logger.silly(hasTimeout);
// const diff = Math.round(
// (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
// );
// throw new Error( // If user is not on timeout
// `You must wait ${diff} seconds before using this command.` if (hasTimeout) {
// ); const { guildId, userId, timeoutId, createdAt } = hasTimeout;
// } const overDue = (await addSeconds(cooldown, createdAt)) < new Date();
// // Delete timeout if (!overDue) {
// await timeoutSchema const diff = Math.round(
// .deleteOne({ (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
// guildId, );
// userId,
// timeoutId, throw new Error(
// cooldown, `You must wait ${diff} seconds before using this command.`
// }) );
// .then(async () => { }
// logger.debug(
// `Timeout document ${timeoutId} has been deleted from user ${userId}.` // Delete timeout
// ); const deleteCooldown = await prisma.cooldown.delete({
// }); where: {
// } guildId_userId_timeoutId: {
// // Create timeout guildId: guild.id,
// await timeoutSchema.create({ userId: user.id,
// guildId: guild?.id || "0", timeoutId: commandId,
// userId: user.id, },
// cooldown: cooldown, },
// timeoutId: commandId, });
// });
logger.warn("Command cooldown function is not working with Prisma yet!"); logger.silly(deleteCooldown);
return false;
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) => { export const button = async (i: ButtonInteraction, cooldown: number) => {
// const { guild, user, customId } = i; const { guild, user, customId } = i;
// // Check if user has a timeout if (!guild) throw new Error("Guild not found");
// const hasTimeout = await timeoutSchema.findOne({
// guildId: guild?.id || "0",
// userId: user.id,
// cooldown: cooldown,
// timeoutId: customId,
// });
// // If user is not on timeout // Check if user has a timeout
// if (hasTimeout) { const hasTimeout = await prisma.cooldown.findUnique({
// const { guildId, userId, timeoutId, createdAt } = hasTimeout; where: {
// const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); guildId_userId_timeoutId: {
guildId: guild.id,
userId: user.id,
timeoutId: customId,
},
},
});
// if (!overDue) { logger.silly(hasTimeout);
// const diff = Math.round(
// (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
// );
// throw new Error( // If user is not on timeout
// `You must wait ${diff} seconds before using this command.` if (hasTimeout) {
// ); const { guildId, userId, timeoutId, createdAt } = hasTimeout;
// } const overDue = (await addSeconds(cooldown, createdAt)) < new Date();
// // Delete timeout if (!overDue) {
// await timeoutSchema const diff = Math.round(
// .deleteOne({ (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
// guildId, );
// userId,
// timeoutId, throw new Error(
// cooldown, `You must wait ${diff} seconds before using this command.`
// }) );
// .then(async () => { }
// logger.debug(
// `Timeout document ${timeoutId} has been deleted from user ${userId}.` // Delete timeout
// ); const deleteCooldown = await prisma.cooldown.delete({
// }); where: {
// } guildId_userId_timeoutId: {
// // Create timeout guildId: guild.id,
// await timeoutSchema.create({ userId: user.id,
// guildId: guild?.id || "0", timeoutId: customId,
// userId: user.id, },
// cooldown: cooldown, },
// timeoutId: customId, });
// });
logger.warn("Button cooldown function is not working with Prisma yet!"); logger.silly(deleteCooldown);
return false;
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) => { export const message = async (msg: Message, cooldown: number, id: string) => {
// const { guild, member } = msg; const { guild, member } = msg;
// if (!guild) throw new Error("Guild is undefined");
// if (!member) throw new Error("Member is undefined");
// // Check if user has a timeout if (!guild) throw new Error("Guild not found");
// const hasTimeout = await timeoutSchema.findOne({ if (!member) throw new Error("Member is undefined");
// guildId: guild?.id || "0",
// userId: member.id,
// cooldown: cooldown,
// timeoutId: id,
// });
// // If user is not on timeout // Check if user has a timeout
// if (hasTimeout) { const hasTimeout = await prisma.cooldown.findUnique({
// const { guildId, userId, timeoutId, createdAt } = hasTimeout; where: {
// const overDue = (await addSeconds(cooldown, createdAt)) < new Date(); guildId_userId_timeoutId: {
guildId: guild.id,
userId: member.id,
timeoutId: id,
},
},
});
// if (!overDue) { logger.silly(hasTimeout);
// const diff = Math.round(
// (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
// );
// 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 if (!overDue) {
// await timeoutSchema const diff = Math.round(
// .deleteOne({ (new Date(hasTimeout.createdAt).getTime() - new Date().getTime()) / 1000
// 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,
// });
logger.warn("Message cooldown function is not working with Prisma yet!"); return `User: ${userId} on timeout-id: ${id} with cooldown: ${cooldown} secs with remaining: ${diff} secs.`;
return false; }
// 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);
}; };