Merge remote-tracking branch 'origin/verification' into verification

This commit is contained in:
Anthony 2022-09-23 19:14:51 +01:00
commit 79c6614f8d
2 changed files with 106 additions and 100 deletions

View File

@ -21,112 +21,19 @@ import { Command, RegisterBehavior } from '@sapphire/framework';
import {
MessageEmbed, MessageActionRow, MessageButton, Constants, ButtonInteraction,
} from 'discord.js';
import { PrismaClient } from '@prisma/client';
import { isMessageInstance } from '@sapphire/discord.js-utilities';
import { addExistingUser, userExists } from '../../utils/database/dbExistingUser';
import {
addToDatabase,
findNotes,
getNote,
deactivateNote,
deactivateAllNotes,
} from '../../utils/database/sus';
import IDs from '../../utils/ids';
// TODO add a check when they join the server to give the user the sus role again
async function addToDatabase(userId: string, modId: string, message: string) {
// Initialise the database connection
const prisma = new PrismaClient();
// Add the user to the database
await prisma.sus.create({
data: {
user: {
connect: {
id: userId,
},
},
mod: {
connect: {
id: modId,
},
},
note: message,
},
});
// Close the database connection
await prisma.$disconnect();
}
// Get a list of sus notes from the user
async function findNotes(userId: string, active: boolean) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to get the specific user's sus notes
const note = await prisma.sus.findMany({
where: {
userId,
active,
},
});
// Close the database connection
await prisma.$disconnect();
return note;
}
// Get one note from the id
async function getNote(noteId: number) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to get the specific user's sus notes
const note = await prisma.sus.findUnique({
where: {
id: noteId,
},
});
// Close the database connection
await prisma.$disconnect();
return note;
}
async function deactivateNote(noteId: number) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to deactivate the specific sus note
await prisma.sus.update({
where: {
id: noteId,
},
data: {
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}
async function deactivateAllNotes(userId: string) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to deactivate the specific user's sus notes
await prisma.sus.updateMany({
where: {
userId: {
contains: userId,
},
},
data: {
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}
// Main command
class SusCommand extends Command {
public constructor(context: Command.Context) {
super(context, {

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

@ -0,0 +1,99 @@
import { PrismaClient } from '@prisma/client';
export async function addToDatabase(userId: string, modId: string, message: string) {
// Initialise the database connection
const prisma = new PrismaClient();
// Add the user to the database
await prisma.sus.create({
data: {
user: {
connect: {
id: userId,
},
},
mod: {
connect: {
id: modId,
},
},
note: message,
},
});
// Close the database connection
await prisma.$disconnect();
}
// Get a list of sus notes from the user
export async function findNotes(userId: string, active: boolean) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to get the specific user's sus notes
const note = await prisma.sus.findMany({
where: {
userId,
active,
},
});
// Close the database connection
await prisma.$disconnect();
return note;
}
// Get one note from the id
export async function getNote(noteId: number) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to get the specific user's sus notes
const note = await prisma.sus.findUnique({
where: {
id: noteId,
},
});
// Close the database connection
await prisma.$disconnect();
return note;
}
export async function deactivateNote(noteId: number) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to deactivate the specific sus note
await prisma.sus.update({
where: {
id: noteId,
},
data: {
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}
export async function deactivateAllNotes(userId: string) {
// Initialise the database connection
const prisma = new PrismaClient();
// Query to deactivate the specific user's sus notes
await prisma.sus.updateMany({
where: {
userId: {
contains: userId,
},
},
data: {
active: false,
},
});
// Close the database connection
await prisma.$disconnect();
}