class implementation with fixes and prisma schema changes

This commit is contained in:
Stanislaw
2022-04-29 00:43:24 +02:00
parent f308503816
commit a2c8e1cfee
4 changed files with 124 additions and 110 deletions

View File

@@ -1,11 +1,9 @@
const discordjs = require("discord.js");
const fs = require("fs");
const redis = require("./src/functions/redis");
const sql = require("./src/functions/postgres");
const { Postgres } = require("./src/functions/postgres");
const { createClient } = require("redis");
const colors = require("colors");
const { PrismaClient } = require("@prisma/client");
const { messageCount } = require("./src/functions/postgres");
colors.setTheme({
prompt: "grey",
@@ -35,7 +33,7 @@ redisConnection
.catch((err) => console.error(err.stack.red));
//postgres connection
const client = new PrismaClient();
const client = new Postgres();
cl.cfg = require("./cfg.json");
cl.cmds = new discordjs.Collection();
@@ -56,12 +54,12 @@ cl.on("messageCreate", async (msg) => {
//exp
if (!msg.content.startsWith(cl.cfg.prefix)) {
messageCount(client, msg.author.id);
client.messageCount(msg.author.id);
if (await redis.expCheck(msg.author.id, redisConnection)) {
sql.addExp(client, msg.author.id, 10, 25);
client.addExp(msg.author.id, 10, 25);
}
} else {
sql.messageCount(client, msg.author.id);
client.messageCount(msg.author.id);
}
const args = msg.content.slice(cl.cfg.prefix.length).trim().split(/ +/);

View File

@@ -12,19 +12,18 @@ model badges {
badge_name String
badge_description String
badge_type Int @default(0)
given_badges given_badges[] @ignore
givenbadges givenbadges[]
}
/// The underlying table does not contain a valid unique identifier and can therefore currently not be handled by the Prisma Client.
model given_badges {
model givenbadges {
user_id String
badge_id Int
date_of_confer DateTime @id @db.Date
badges badges @relation(fields: [badge_id], references: [badge_id], onDelete: NoAction, onUpdate: NoAction, map: "badge_id")
users users @relation(fields: [user_id], references: [user_id], onDelete: NoAction, onUpdate: NoAction, map: "user_id")
@@index([badge_id], map: "fki_badge_id")
@@index([user_id], map: "fki_user_id")
@@ignore
}
model users {
@@ -33,5 +32,5 @@ model users {
msg_count Int @default(0)
voice_time Int @default(0)
lvl Int @default(0)
given_badges given_badges[] @ignore
givenbadges givenbadges[]
}

View File

@@ -6,7 +6,9 @@ module.exports = {
const { profile } = require("../functions/postgres");
const { roundRect, expThreshold } = require("../functions/tools");
const canvas = require("canvas");
await profile(connect, msg.author.id).then((user) => {
await connect
.profile(msg.author.id)
.then((user) => {
canvas.registerFont("./src/fonts/Lexend-Bold.ttf", {
family: "LexendBold",
});
@@ -51,6 +53,7 @@ module.exports = {
files: [canva.toBuffer()],
});
});
});
})
.catch((err) => console.error(err.stack));
},
};

View File

@@ -1,8 +1,12 @@
const { lvl, randomExp } = require("./tools");
const { PrismaClient } = require("@prisma/client");
class Postgres {
constructor() {
this.client = new PrismaClient();
}
module.exports = {
messageCount: async function (connect, userID) {
const update = await connect.users.update({
async messageCount(userID) {
return await this.client.users.update({
where: {
user_id: userID,
},
@@ -12,28 +16,24 @@ module.exports = {
},
},
});
return update;
},
profile: async function (connect, userID) {
return await connect.users.findUnique({
}
async profile(userID) {
return await this.client.users.findUnique({
where: {
user_id: userID,
},
});
},
addExp: async function (connect, userID, min, max) {
}
async addExp(userID, min, max) {
await this.#ifExist(userID).then(async () => {
const rExp = randomExp(min, max);
return await connect.users.upsert({
return await this.client.users.update({
where: {
user_id: userID,
},
create: {
user_id: userID,
msg_count: 1,
exp: randomExp(min, max),
lvl: 0,
},
update: {
data: {
msg_count: {
increment: 1,
},
@@ -42,7 +42,7 @@ module.exports = {
},
lvl: lvl(
(
await connect.users.findUnique({
await this.client.users.findUnique({
where: {
user_id: userID,
},
@@ -50,11 +50,25 @@ module.exports = {
exp: true,
},
})
).exp + rExp
).exp
),
},
});
});
}
async #ifExist(userID) {
if ((await !this.profile(userID)) == null) {
await this.client.users.create({
data: {
user_id: userID,
msg_count: 0,
lvl: 0,
},
addBadge: async function (args) {},
giveBadge: async function (args) {},
};
});
}
}
//addBadge: async function (args) {},
//giveBadge: async function (args) {},
}
module.exports.Postgres = Postgres;