From 48125ba8967c93688eee6b89adbecec2424cea35 Mon Sep 17 00:00:00 2001 From: smyalygames Date: Fri, 17 Feb 2023 01:03:42 +0000 Subject: [PATCH] feat(arabot): add temp ban database utilities --- src/utils/database/tempBan.ts | 98 +++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 src/utils/database/tempBan.ts diff --git a/src/utils/database/tempBan.ts b/src/utils/database/tempBan.ts new file mode 100644 index 0000000..00d73e8 --- /dev/null +++ b/src/utils/database/tempBan.ts @@ -0,0 +1,98 @@ +import { container } from '@sapphire/framework'; +import type { Snowflake } from 'discord.js'; + +export async function addTempBan( + userId: Snowflake, + modId: Snowflake, + endTime: Date, + reason: string, +) { + // Add the user to the database + await container.database.tempBan.create({ + data: { + user: { + connect: { + id: userId, + }, + }, + mod: { + connect: { + id: modId, + }, + }, + endTime, + reason, + }, + }); +} + +export async function removeTempBan(userId: Snowflake, modId?: Snowflake) { + const ban = await container.database.tempBan.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + }); + + if (ban === null) { + return; + } + + if (modId !== undefined) { + await container.database.tempBan.update({ + where: { + id: ban.id, + }, + data: { + endModId: modId, + active: false, + }, + }); + return; + } + + await container.database.tempBan.update({ + where: { + id: ban.id, + }, + data: { + active: false, + }, + }); +} + +export async function checkTempBan(userId: Snowflake) { + const ban = await container.database.tempBan.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + }); + + if (ban === null) { + return false; + } + + return ban.active; +} + +export async function getTempBanReason(userId: Snowflake) { + const ban = await container.database.tempBan.findFirst({ + where: { + userId, + }, + orderBy: { + id: 'desc', + }, + }); + + if (ban === null) { + return ''; + } + + return ban.reason; +}