From 4af3c0adfad64062623ab0ed5d10dafb94497ac3 Mon Sep 17 00:00:00 2001 From: Stanislaw <62724833+BoberITman@users.noreply.github.com> Date: Tue, 15 Mar 2022 12:10:15 +0100 Subject: [PATCH] add leveling --- index.js | 3 ++- src/cmds/profil.js | 20 +++++++++++++++++++- src/functions/postgres.js | 24 +++++++++++++++++++----- src/functions/tools.js | 5 +++++ 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/functions/tools.js diff --git a/index.js b/index.js index 1ec20e8..1b5c02c 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ const sql = require("./src/functions/postgres"); const { createClient } = require("redis"); const { Client } = require("pg"); const colors = require("colors"); +const { connect } = require("http2"); colors.setTheme({ prompt: "grey", @@ -68,7 +69,7 @@ cl.on("messageCreate", async (msg) => { const cmd = cl.cmds.get(cmdName); try { - cmd.execute(cl, msg, args); + cmd.execute(cl, msg, args, client); } catch (error) { console.error( `msgCommand error: ${cmdName} with args ${args} by ${msg.author.tag}\n--\n${error}\n--` diff --git a/src/cmds/profil.js b/src/cmds/profil.js index 2721535..665757d 100644 --- a/src/cmds/profil.js +++ b/src/cmds/profil.js @@ -1,4 +1,22 @@ module.exports = { name: "profil", - execute(cl, msg) {}, + 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" } + ); + }*/ + }, }; diff --git a/src/functions/postgres.js b/src/functions/postgres.js index 44f606d..adf2885 100644 --- a/src/functions/postgres.js +++ b/src/functions/postgres.js @@ -1,21 +1,35 @@ +const { lvl } = 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); }, addExp: async function (connect, userID) { - let query = `SELECT users.user_id FROM users WHERE users.user_id = '${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 { - query = `UPDATE users SET exp = users.exp + ${Math.floor( - Math.random() * 25 - )},msg_count = msg_count + 1 WHERE users.user_id = '${userID}'`; + 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); } }, + 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; + }, }; diff --git a/src/functions/tools.js b/src/functions/tools.js new file mode 100644 index 0000000..0250c27 --- /dev/null +++ b/src/functions/tools.js @@ -0,0 +1,5 @@ +module.exports = { + lvl: function (exp) { + return Math.floor(Math.sqrt(exp) / 5); + }, +};