♻️ Cooldowns now in Prisma
This commit is contained in:
parent
8e16c7fb64
commit
5aa373bff3
1 changed files with 246 additions and 129 deletions
|
@ -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);
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue