Postgres is alive

This commit is contained in:
Stanislaw
2022-03-11 21:16:45 +01:00
parent 5d5efa884d
commit f2371974cd
7 changed files with 158 additions and 97 deletions

View File

@@ -1,5 +1,5 @@
{
"prefix": ".",
"prefix": "&",
"hexBlue": "61f2ea",
"hexRed": "fb636b",
"iconurl": "https://i.imgur.com/4AmRDyX.png",

View File

@@ -1,8 +1,19 @@
const discordjs = require("discord.js");
const fs = require("fs");
const redis = require("./src/functions/redis");
const sql = require("./src/functions/postgres");
const { createClient } = require("redis");
const { connect } = require("http2");
const { Client } = require("pg");
const colors = require("colors");
colors.setTheme({
prompt: "grey",
info: "green",
warn: "yellow",
debug: "blue",
error: "red",
});
require("dotenv").config();
const cl = new discordjs.Client({
@@ -13,10 +24,22 @@ const cl = new discordjs.Client({
],
});
//redis connect
const redisConnection = createClient({
url: process.env.REDIS_TOKEN,
});
redisConnection.connect();
redisConnection
.connect()
.then(() => console.log("✔️ redis connected".info))
.catch((err) => console.error(err.stack.red));
//postgresql connect
const client = new Client();
client
.connect()
.then(() => console.log("✔️ postgres connected".info))
.catch((err) => console.error(err.stack.red));
cl.cfg = require("./cfg.json");
cl.cmds = new discordjs.Collection();
@@ -33,6 +56,9 @@ cl.on("messageCreate", async (msg) => {
if (msg.author.bot) return;
if (!msg.content.startsWith(cl.cfg.prefix)) {
if (await redis.expCheck(msg.author.id, redisConnection)) {
sql.addExp(client, msg.author.id);
} else {
sql.messageCount(client, msg.author.id);
}
}
const args = msg.content.slice(cl.cfg.prefix.length).trim().split(/ +/);
@@ -57,7 +83,7 @@ cl.on("messageCreate", async (msg) => {
});
cl.once("ready", () => {
console.log(`bot ready; logged in as ${cl.user.tag}\n--`);
console.log(`bot ready; logged in as ${cl.user.tag}\n--`.info);
cl.user.setActivity(".pomoc", { type: "LISTENING" });
});
cl.login(process.env.TOKEN); // here comes the boooy

14
package-lock.json generated
View File

@@ -11,6 +11,7 @@
"dependencies": {
"@discordjs/opus": "^0.5.3",
"@discordjs/voice": "^0.7.5",
"colors": "^1.4.0",
"daemon": "^1.1.0",
"discord.js": "^13.1.0",
"dotenv": "^10.0.0",
@@ -637,6 +638,14 @@
"color-support": "bin.js"
}
},
"node_modules/colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==",
"engines": {
"node": ">=0.1.90"
}
},
"node_modules/combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
@@ -2819,6 +2828,11 @@
"resolved": "https://registry.npmjs.org/color-support/-/color-support-1.1.3.tgz",
"integrity": "sha512-qiBjkpbMLO/HL68y+lh4q0/O1MZFj2RX6X/KmMa3+gJD3z+WwI1ZzDHysvqHGS3mP6mznPckpXmw1nI9cJjyRg=="
},
"colors": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz",
"integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA=="
},
"combined-stream": {
"version": "1.0.8",
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",

View File

@@ -23,6 +23,7 @@
"dependencies": {
"@discordjs/opus": "^0.5.3",
"@discordjs/voice": "^0.7.5",
"colors": "^1.4.0",
"daemon": "^1.1.0",
"discord.js": "^13.1.0",
"dotenv": "^10.0.0",

View File

@@ -1 +1,21 @@
module.exports = {};
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}'`;
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}'`;
await connect.query(query);
}
},
};