orm and profile first implementation

This commit is contained in:
Stanislaw
2022-04-20 15:20:22 +02:00
parent 4f11342db8
commit f308503816
14 changed files with 597 additions and 265 deletions

View File

@@ -2,20 +2,20 @@ module.exports = {
name: "dodajodznake",
async execute(cl, msg, args) {
const { get } = require("https");
const { createWriteStream, exists } = require("fs");
const { createWriteStream, access } = require("fs");
if (msg.member.permissions.has(0x20)) {
if (msg.attachments.at(0) != null) {
console.log("attachmentadded");
msg.attachments.each((x) => {
if (x.contentType.startsWith("application/json")) {
exists(`src/badges/json/${x.name}`, (e) => {
access(`src/badges/json/${x.name}`, (e) => {
if (!e) {
const file = createWriteStream(`src/badges/json/${x.name}`);
get(x.url, (res) => {
res.pipe(file);
});
} else {
msg.channel.send("nuda");
msg.channel.send("Taka odznaka już istnieje");
}
});
} else if (x.contentType.startsWith("image/")) {
@@ -26,7 +26,7 @@ module.exports = {
res.pipe(file);
});
} else {
msg.channel.send("nuda");
msg.channel.send("Taka odznaka już istnieje");
}
});
}

View File

@@ -1,22 +0,0 @@
module.exports = {
name: "profil",
execute(cl, msg, args, connect) {
const { profil } = require("../functions/postgres");
const { MessageEmbed } = require("discord.js");
async () => {
const user = profil(connect, msg.author.id);
};
console.debug(user);
/*if (user) {
const embed = new MessageEmbed()
.setColor("#F5F5F5")
.setTitle(msg.author.name)
.addFields(
{ name: "Regular field title", value: "Some value here" },
{ name: "Regular field title", value: "Some value here" },
{ name: "Regular field title", value: "Some value here" },
{ name: "Regular field title", value: "Some value here" }
);
}*/
},
};

56
src/cmds/profile.js Normal file
View File

@@ -0,0 +1,56 @@
const { ReactionUserManager } = require("discord.js");
module.exports = {
name: "profile",
async execute(cl, msg, args, connect) {
const { profile } = require("../functions/postgres");
const { roundRect, expThreshold } = require("../functions/tools");
const canvas = require("canvas");
await profile(connect, msg.author.id).then((user) => {
canvas.registerFont("./src/fonts/Lexend-Bold.ttf", {
family: "LexendBold",
});
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()],
});
});
});
},
};

BIN
src/fonts/Lexend-Bold.ttf Normal file

Binary file not shown.

BIN
src/fonts/Lexend-Thin.ttf Normal file

Binary file not shown.

View File

@@ -1,36 +1,59 @@
const { lvl } = require("./tools");
const { lvl, randomExp } = require("./tools");
module.exports = {
messageCount: async function (connect, userID) {
query = `UPDATE users SET msg_count = msg_count + 1 WHERE users.user_id = '${userID}'`;
await connect.query(query);
const update = await connect.users.update({
where: {
user_id: userID,
},
data: {
msg_count: {
increment: 1,
},
},
});
return update;
},
addExp: async function (connect, userID) {
let query = `SELECT users.user_id, users.exp FROM users WHERE users.user_id = '${userID}'`;
const check = await connect.query(query);
if (check.rowCount == 0) {
query = `INSERT INTO users(user_id,msg_count,exp) VALUES ('${userID}',1,${Math.floor(
Math.random() * 25
)})`;
await connect.query(query);
} else {
let exp = check.rows[0].exp + Math.floor(Math.random() * 25);
console.debug(lvl(exp));
query =
query = `UPDATE users SET exp = ${exp},msg_count = msg_count + 1,lvl = ${lvl(
exp
)} WHERE users.user_id = '${userID}'`;
await connect.query(query);
}
profile: async function (connect, userID) {
return await connect.users.findUnique({
where: {
user_id: userID,
},
});
},
profil: async function (connect, userID) {
let query = `SELECT * FROM users WHERE users.user_id = '${userID}'`;
const check = await connect.query(query);
if (check.rowCount == 0) {
query = `INSERT INTO users(user_id) VALUES ('${userID}')`;
await connect.query(query);
}
return check.rows;
addExp: async function (connect, userID, min, max) {
const rExp = randomExp(min, max);
return await connect.users.upsert({
where: {
user_id: userID,
},
create: {
user_id: userID,
msg_count: 1,
exp: randomExp(min, max),
lvl: 0,
},
update: {
msg_count: {
increment: 1,
},
exp: {
increment: rExp,
},
lvl: lvl(
(
await connect.users.findUnique({
where: {
user_id: userID,
},
select: {
exp: true,
},
})
).exp + rExp
),
},
});
},
addBadge: async function (args) {},
giveBadge: async function (args) {},

View File

@@ -2,4 +2,22 @@ module.exports = {
lvl: function (exp) {
return Math.floor(Math.sqrt(exp) / 5);
},
expThreshold: function (lvl) {
return Math.floor(lvl * lvl * 25);
},
randomExp: function (min, max) {
return Math.floor(Math.random() * (max - min)) + min;
},
roundRect: function (ctx, x, y, width, height, radius) {
if (width < 2 * radius) radius = width / 2;
if (height < 2 * radius) radius = height / 2;
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.arcTo(x + width, y, x + width, y + height, radius);
ctx.arcTo(x + width, y + height, x, y + height, radius);
ctx.arcTo(x, y + height, x, y, radius);
ctx.arcTo(x, y, x + width, y, radius);
ctx.closePath();
return ctx;
},
};