feat(arabot): add database for bans

This commit is contained in:
smyalygames 2022-10-28 02:24:44 +01:00
parent 16fb194cd3
commit a40925e9e9
4 changed files with 131 additions and 8 deletions

View File

@ -0,0 +1,12 @@
/*
Warnings:
- Added the required column `endModId` to the `Ban` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE "Ban" ADD COLUMN "endModId" TEXT NOT NULL,
ADD COLUMN "endTime" TIMESTAMP(3);
-- AddForeignKey
ALTER TABLE "Ban" ADD CONSTRAINT "Ban_endModId_fkey" FOREIGN KEY ("endModId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

View File

@ -0,0 +1,8 @@
-- DropForeignKey
ALTER TABLE "Ban" DROP CONSTRAINT "Ban_endModId_fkey";
-- AlterTable
ALTER TABLE "Ban" ALTER COLUMN "endModId" DROP NOT NULL;
-- AddForeignKey
ALTER TABLE "Ban" ADD CONSTRAINT "Ban_endModId_fkey" FOREIGN KEY ("endModId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE;

View File

@ -43,6 +43,7 @@ model User {
RestrictMod Restrict[] @relation("restMod")
BanUser Ban[] @relation("banUser")
BanMod Ban[] @relation("banMod")
BanEndMod Ban[] @relation("endBanMod")
TempBanUser TempBan[] @relation("tbanUser")
TempBanMod TempBan[] @relation("tbanMod")
}
@ -99,14 +100,17 @@ model Restrict {
}
model Ban {
id Int @id @default(autoincrement())
user User @relation("banUser", fields: [userId], references: [id])
userId String
mod User @relation("banMod", fields: [modId], references: [id])
modId String
time DateTime @default(now())
active Boolean @default(true)
reason String
id Int @id @default(autoincrement())
user User @relation("banUser", fields: [userId], references: [id])
userId String
mod User @relation("banMod", fields: [modId], references: [id])
modId String
time DateTime @default(now())
endMod User? @relation("endBanMod", fields: [endModId], references: [id])
endModId String?
endTime DateTime?
active Boolean @default(true)
reason String
}
model TempBan {

99
src/utils/database/ban.ts Normal file
View File

@ -0,0 +1,99 @@
import { PrismaClient } from '@prisma/client';
export async function addBan(userId: string, modId: string, reason: string) {
// Initialise the database connection
const prisma = new PrismaClient();
// Add the user to the database
await prisma.ban.create({
data: {
user: {
connect: {
id: userId,
},
},
mod: {
connect: {
id: modId,
},
},
reason,
},
});
// Close the database connection
await prisma.$disconnect();
}
export async function removeBan(userId: string, modId: string) {
// Initialise the database connection
const prisma = new PrismaClient();
const ban = await prisma.ban.findFirst({
where: {
userId,
},
orderBy: {
id: 'desc',
},
});
if (ban === null) {
return;
}
// Query to deactivate the specific sus note
await prisma.ban.update({
where: {
id: ban.id,
},
data: {
endModId: modId,
endTime: new Date(),
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}
export async function checkActive(userId: string) {
// Initialise the database connection
const prisma = new PrismaClient();
const ban = await prisma.ban.findFirst({
where: {
userId,
},
orderBy: {
id: 'desc',
},
});
if (ban === null) {
return false;
}
return ban.active;
}
export async function getReason(userId: string) {
// Initialise the database connection
const prisma = new PrismaClient();
const ban = await prisma.ban.findFirst({
where: {
userId,
},
orderBy: {
id: 'desc',
},
});
if (ban === null) {
return '';
}
return ban.reason;
}