class implementation with fixes and prisma schema changes
This commit is contained in:
12
index.js
12
index.js
@@ -1,11 +1,9 @@
|
|||||||
const discordjs = require("discord.js");
|
const discordjs = require("discord.js");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
const redis = require("./src/functions/redis");
|
const redis = require("./src/functions/redis");
|
||||||
const sql = require("./src/functions/postgres");
|
const { Postgres } = require("./src/functions/postgres");
|
||||||
const { createClient } = require("redis");
|
const { createClient } = require("redis");
|
||||||
const colors = require("colors");
|
const colors = require("colors");
|
||||||
const { PrismaClient } = require("@prisma/client");
|
|
||||||
const { messageCount } = require("./src/functions/postgres");
|
|
||||||
|
|
||||||
colors.setTheme({
|
colors.setTheme({
|
||||||
prompt: "grey",
|
prompt: "grey",
|
||||||
@@ -35,7 +33,7 @@ redisConnection
|
|||||||
.catch((err) => console.error(err.stack.red));
|
.catch((err) => console.error(err.stack.red));
|
||||||
|
|
||||||
//postgres connection
|
//postgres connection
|
||||||
const client = new PrismaClient();
|
const client = new Postgres();
|
||||||
|
|
||||||
cl.cfg = require("./cfg.json");
|
cl.cfg = require("./cfg.json");
|
||||||
cl.cmds = new discordjs.Collection();
|
cl.cmds = new discordjs.Collection();
|
||||||
@@ -56,12 +54,12 @@ cl.on("messageCreate", async (msg) => {
|
|||||||
|
|
||||||
//exp
|
//exp
|
||||||
if (!msg.content.startsWith(cl.cfg.prefix)) {
|
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)) {
|
if (await redis.expCheck(msg.author.id, redisConnection)) {
|
||||||
sql.addExp(client, msg.author.id, 10, 25);
|
client.addExp(msg.author.id, 10, 25);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
sql.messageCount(client, msg.author.id);
|
client.messageCount(msg.author.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const args = msg.content.slice(cl.cfg.prefix.length).trim().split(/ +/);
|
const args = msg.content.slice(cl.cfg.prefix.length).trim().split(/ +/);
|
||||||
|
|||||||
@@ -8,30 +8,29 @@ datasource db {
|
|||||||
}
|
}
|
||||||
|
|
||||||
model badges {
|
model badges {
|
||||||
badge_id Int @id
|
badge_id Int @id
|
||||||
badge_name String
|
badge_name String
|
||||||
badge_description String
|
badge_description String
|
||||||
badge_type Int @default(0)
|
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 givenbadges {
|
||||||
model given_badges {
|
user_id String
|
||||||
user_id String
|
badge_id Int
|
||||||
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")
|
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")
|
users users @relation(fields: [user_id], references: [user_id], onDelete: NoAction, onUpdate: NoAction, map: "user_id")
|
||||||
|
|
||||||
@@index([badge_id], map: "fki_badge_id")
|
@@index([badge_id], map: "fki_badge_id")
|
||||||
@@index([user_id], map: "fki_user_id")
|
@@index([user_id], map: "fki_user_id")
|
||||||
@@ignore
|
|
||||||
}
|
}
|
||||||
|
|
||||||
model users {
|
model users {
|
||||||
user_id String @id(map: "user_pkey")
|
user_id String @id(map: "user_pkey")
|
||||||
exp Int @default(0)
|
exp Int @default(0)
|
||||||
msg_count Int @default(0)
|
msg_count Int @default(0)
|
||||||
voice_time Int @default(0)
|
voice_time Int @default(0)
|
||||||
lvl Int @default(0)
|
lvl Int @default(0)
|
||||||
given_badges given_badges[] @ignore
|
givenbadges givenbadges[]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,51 +6,54 @@ module.exports = {
|
|||||||
const { profile } = require("../functions/postgres");
|
const { profile } = require("../functions/postgres");
|
||||||
const { roundRect, expThreshold } = require("../functions/tools");
|
const { roundRect, expThreshold } = require("../functions/tools");
|
||||||
const canvas = require("canvas");
|
const canvas = require("canvas");
|
||||||
await profile(connect, msg.author.id).then((user) => {
|
await connect
|
||||||
canvas.registerFont("./src/fonts/Lexend-Bold.ttf", {
|
.profile(msg.author.id)
|
||||||
family: "LexendBold",
|
.then((user) => {
|
||||||
});
|
canvas.registerFont("./src/fonts/Lexend-Bold.ttf", {
|
||||||
canvas.registerFont("./src/fonts/Lexend-Thin.ttf", {
|
family: "LexendBold",
|
||||||
family: "LexendThin",
|
|
||||||
});
|
|
||||||
const canva = canvas.createCanvas(1024, 256);
|
|
||||||
let context = canva.getContext("2d");
|
|
||||||
context.fillStyle = "#484848";
|
|
||||||
roundRect(context, 0, 0, 1024, 256, 25);
|
|
||||||
context.fill();
|
|
||||||
context.font = '48px "LexendBold"';
|
|
||||||
context.fillStyle = "#FFFFFF";
|
|
||||||
context.fillText(msg.author.tag, 244, 24 + 48);
|
|
||||||
context.fillText("Lvl " + user.lvl, 850, 24 + 48);
|
|
||||||
context.font = '25px "LexendThin"';
|
|
||||||
context.fillText(user.msg_count + " messages sent", 244, 72 + 25);
|
|
||||||
context.font = '24px "LexendThin"';
|
|
||||||
context.fillText(expThreshold(user.lvl + 1) + " exp", 810, 164 + 24);
|
|
||||||
context.fillText(user.exp + " exp", 250, 164 + 24);
|
|
||||||
context.fillStyle = "#242424";
|
|
||||||
roundRect(context, 244, 200, 700, 30, 50);
|
|
||||||
context.fill();
|
|
||||||
context.fillStyle = "#61F2EA";
|
|
||||||
roundRect(
|
|
||||||
context,
|
|
||||||
244,
|
|
||||||
200,
|
|
||||||
700 *
|
|
||||||
((user.exp - expThreshold(user.lvl)) / expThreshold(user.lvl + 1)),
|
|
||||||
30,
|
|
||||||
50
|
|
||||||
);
|
|
||||||
context.fill();
|
|
||||||
roundRect(context, 34, 34, 188, 188, 100);
|
|
||||||
canvas
|
|
||||||
.loadImage(msg.author.avatarURL({ format: "png" }))
|
|
||||||
.then((image) => {
|
|
||||||
context.clip();
|
|
||||||
context.drawImage(image, 34, 34, 188, 188);
|
|
||||||
msg.reply({
|
|
||||||
files: [canva.toBuffer()],
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
canvas.registerFont("./src/fonts/Lexend-Thin.ttf", {
|
||||||
|
family: "LexendThin",
|
||||||
|
});
|
||||||
|
const canva = canvas.createCanvas(1024, 256);
|
||||||
|
let context = canva.getContext("2d");
|
||||||
|
context.fillStyle = "#484848";
|
||||||
|
roundRect(context, 0, 0, 1024, 256, 25);
|
||||||
|
context.fill();
|
||||||
|
context.font = '48px "LexendBold"';
|
||||||
|
context.fillStyle = "#FFFFFF";
|
||||||
|
context.fillText(msg.author.tag, 244, 24 + 48);
|
||||||
|
context.fillText("Lvl " + user.lvl, 850, 24 + 48);
|
||||||
|
context.font = '25px "LexendThin"';
|
||||||
|
context.fillText(user.msg_count + " messages sent", 244, 72 + 25);
|
||||||
|
context.font = '24px "LexendThin"';
|
||||||
|
context.fillText(expThreshold(user.lvl + 1) + " exp", 810, 164 + 24);
|
||||||
|
context.fillText(user.exp + " exp", 250, 164 + 24);
|
||||||
|
context.fillStyle = "#242424";
|
||||||
|
roundRect(context, 244, 200, 700, 30, 50);
|
||||||
|
context.fill();
|
||||||
|
context.fillStyle = "#61F2EA";
|
||||||
|
roundRect(
|
||||||
|
context,
|
||||||
|
244,
|
||||||
|
200,
|
||||||
|
700 *
|
||||||
|
((user.exp - expThreshold(user.lvl)) / expThreshold(user.lvl + 1)),
|
||||||
|
30,
|
||||||
|
50
|
||||||
|
);
|
||||||
|
context.fill();
|
||||||
|
roundRect(context, 34, 34, 188, 188, 100);
|
||||||
|
canvas
|
||||||
|
.loadImage(msg.author.avatarURL({ format: "png" }))
|
||||||
|
.then((image) => {
|
||||||
|
context.clip();
|
||||||
|
context.drawImage(image, 34, 34, 188, 188);
|
||||||
|
msg.reply({
|
||||||
|
files: [canva.toBuffer()],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.catch((err) => console.error(err.stack));
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
const { lvl, randomExp } = require("./tools");
|
const { lvl, randomExp } = require("./tools");
|
||||||
|
const { PrismaClient } = require("@prisma/client");
|
||||||
|
class Postgres {
|
||||||
|
constructor() {
|
||||||
|
this.client = new PrismaClient();
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = {
|
async messageCount(userID) {
|
||||||
messageCount: async function (connect, userID) {
|
return await this.client.users.update({
|
||||||
const update = await connect.users.update({
|
|
||||||
where: {
|
where: {
|
||||||
user_id: userID,
|
user_id: userID,
|
||||||
},
|
},
|
||||||
@@ -12,49 +16,59 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return update;
|
}
|
||||||
},
|
|
||||||
profile: async function (connect, userID) {
|
async profile(userID) {
|
||||||
return await connect.users.findUnique({
|
return await this.client.users.findUnique({
|
||||||
where: {
|
where: {
|
||||||
user_id: userID,
|
user_id: userID,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
addExp: async function (connect, userID, min, max) {
|
|
||||||
const rExp = randomExp(min, max);
|
async addExp(userID, min, max) {
|
||||||
return await connect.users.upsert({
|
await this.#ifExist(userID).then(async () => {
|
||||||
where: {
|
const rExp = randomExp(min, max);
|
||||||
user_id: userID,
|
return await this.client.users.update({
|
||||||
},
|
where: {
|
||||||
create: {
|
user_id: userID,
|
||||||
user_id: userID,
|
|
||||||
msg_count: 1,
|
|
||||||
exp: randomExp(min, max),
|
|
||||||
lvl: 0,
|
|
||||||
},
|
|
||||||
update: {
|
|
||||||
msg_count: {
|
|
||||||
increment: 1,
|
|
||||||
},
|
},
|
||||||
exp: {
|
data: {
|
||||||
increment: rExp,
|
msg_count: {
|
||||||
|
increment: 1,
|
||||||
|
},
|
||||||
|
exp: {
|
||||||
|
increment: rExp,
|
||||||
|
},
|
||||||
|
lvl: lvl(
|
||||||
|
(
|
||||||
|
await this.client.users.findUnique({
|
||||||
|
where: {
|
||||||
|
user_id: userID,
|
||||||
|
},
|
||||||
|
select: {
|
||||||
|
exp: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
).exp
|
||||||
|
),
|
||||||
},
|
},
|
||||||
lvl: lvl(
|
});
|
||||||
(
|
|
||||||
await connect.users.findUnique({
|
|
||||||
where: {
|
|
||||||
user_id: userID,
|
|
||||||
},
|
|
||||||
select: {
|
|
||||||
exp: true,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
).exp + rExp
|
|
||||||
),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
},
|
}
|
||||||
addBadge: async function (args) {},
|
async #ifExist(userID) {
|
||||||
giveBadge: async function (args) {},
|
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) {},
|
||||||
|
}
|
||||||
|
module.exports.Postgres = Postgres;
|
||||||
|
|||||||
Reference in New Issue
Block a user