feat(database): add update user function

This commit is contained in:
Anthony 2022-08-06 16:46:34 +01:00
parent 7a2543cea1
commit c0a0e10547
2 changed files with 61 additions and 22 deletions

View File

@ -27,10 +27,6 @@ datasource db {
model User { model User {
id String @id @db.VarChar(255) id String @id @db.VarChar(255)
level Int @default(0)
xp Int @default(0)
balance Int @default(0)
lastDaily DateTime?
vegan Boolean @default(false) vegan Boolean @default(false)
trusted Boolean @default(false) trusted Boolean @default(false)
activist Boolean @default(false) activist Boolean @default(false)

View File

@ -17,7 +17,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
import type { GuildMember } from 'discord.js'; import type { GuildMember, GuildMemberRoleManager } from 'discord.js';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
import { IDs } from './ids'; import { IDs } from './ids';
@ -43,6 +43,22 @@ export async function userExists(user: GuildMember) {
return false; return false;
} }
function getRoles(roles: GuildMemberRoleManager) {
// Checks what roles the user has
const rolesDict = {
vegan: roles.cache.has(IDs.roles.vegan.vegan),
activist: roles.cache.has(IDs.roles.vegan.activist),
plus: roles.cache.has(IDs.roles.vegan.plus),
notVegan: roles.cache.has(IDs.roles.nonvegan.nonvegan),
vegCurious: roles.cache.has(IDs.roles.nonvegan.vegCurious),
convinced: roles.cache.has(IDs.roles.nonvegan.convinced),
trusted: roles.cache.has(IDs.roles.trusted),
muted: roles.cache.has(IDs.roles.restrictions.muted),
};
return rolesDict;
}
// Adds the user to the database if they were already on the server before the bot/database // Adds the user to the database if they were already on the server before the bot/database
export async function addExistingUser(user: GuildMember) { export async function addExistingUser(user: GuildMember) {
// Initialises Prisma Client // Initialises Prisma Client
@ -60,28 +76,55 @@ export async function addExistingUser(user: GuildMember) {
return; return;
} }
// Checks what roles the user has // Parse all the roles into a dictionary
const hasVegan = user.roles.cache.has(IDs.roles.vegan.vegan); const roles = getRoles(user.roles);
const hasActivist = user.roles.cache.has(IDs.roles.vegan.activist);
const hasPlus = user.roles.cache.has(IDs.roles.vegan.plus);
const hasNotVegan = user.roles.cache.has(IDs.roles.nonvegan.nonvegan);
const hasVegCurious = user.roles.cache.has(IDs.roles.nonvegan.vegCurious);
const hasConvinced = user.roles.cache.has(IDs.roles.nonvegan.convinced);
const hasTrusted = user.roles.cache.has(IDs.roles.trusted);
const hasMuted = user.roles.cache.has(IDs.roles.restrictions.muted);
// Create the user in the database // Create the user in the database
await prisma.user.create({ await prisma.user.create({
data: { data: {
id: user.id, id: user.id,
vegan: hasVegan, vegan: roles.vegan,
trusted: hasTrusted, trusted: roles.trusted,
activist: hasActivist, activist: roles.activist,
plus: hasPlus, plus: roles.plus,
notVegan: hasNotVegan, notVegan: roles.notVegan,
vegCurious: hasVegCurious, vegCurious: roles.vegCurious,
convinced: hasConvinced, convinced: roles.convinced,
muted: hasMuted, muted: roles.muted,
},
});
// Close the database connection
await prisma.$disconnect();
}
export async function updateUser(user: GuildMember) {
// Check if the user is already on the database
if (!(await userExists(user))) {
await addExistingUser(user);
return;
}
// Parse all the roles into a dictionary
const roles = getRoles(user.roles);
// Initialises Prisma Client
const prisma = new PrismaClient();
await prisma.user.update({
where: {
id: user.id,
},
data: {
id: user.id,
vegan: roles.vegan,
trusted: roles.trusted,
activist: roles.activist,
plus: roles.plus,
notVegan: roles.notVegan,
vegCurious: roles.vegCurious,
convinced: roles.convinced,
muted: roles.muted,
}, },
}); });