From e428ce388c3774c6b91dacb5793d67e23de495cc Mon Sep 17 00:00:00 2001 From: steev Date: Mon, 24 Apr 2023 00:40:34 +0200 Subject: [PATCH] can now store users in database + database file --- Commons/Data/Database.js | 5 +- Commons/Data/Reactions.js | 50 +++++++++++++++ Commons/Data/User.js | 61 +++++++++++++++++++ Commons/Services/Reaction.js | 14 +++++ Commons/Services/Roles.js | 11 ---- README.md | 2 +- devpengu.sql | 115 +++++++++++++++++++++++++++++++++++ index.js | 24 ++++++-- package.json | 4 +- 9 files changed, 264 insertions(+), 22 deletions(-) create mode 100644 Commons/Data/Reactions.js create mode 100644 Commons/Data/User.js create mode 100644 devpengu.sql diff --git a/Commons/Data/Database.js b/Commons/Data/Database.js index 6a405c3..89c5d2a 100644 --- a/Commons/Data/Database.js +++ b/Commons/Data/Database.js @@ -5,11 +5,10 @@ const Database = async (db) => { const server = sql.createConnection({ host : db.DB_HOST, user : db.DB_USERNAME, - password : db.DB_PASSWORD - , + password : db.DB_PASSWORD, database : db.DB_NAME }); - + server.connect(function(err) { if (err) { console.error('error connecting Database in Data.Database: ' + err.stack); diff --git a/Commons/Data/Reactions.js b/Commons/Data/Reactions.js new file mode 100644 index 0000000..f29eac2 --- /dev/null +++ b/Commons/Data/Reactions.js @@ -0,0 +1,50 @@ +const mysql = require("mysql"); +/** + * creates a Reaction and stores it in the databse + * @param sql {Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a Reaction on + * @param Reaction {string} the id of which Reaction you want to store + * @param Reactionname {string} the Reactionname of the Reaction you want to store + * @param nickname {string} if the nickname of the Reaction if he got any + * @param owner {boolean} whether or not the Reaction is the server owner + */ +let CreateReaction = async (sql, server, Reaction, Reactionname, owner) => { + throw "not implemented"; +} + +/** + * gets a Reaction from databse + * @param sql {mysql.Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a Reaction on + * @param Reaction {string} the id of which Reaction you want to store + */ +let GetReaction = async (sql, server, Reaction) => { + throw "not implemented"; +} + +/** + * updates a Reaction and stores it in the databse + * @param sql {mysql.Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a Reaction on + * @param Reaction {string} the id of which Reaction you want to store + * @param Reactionname {string} the Reactionname of the Reaction you want to store + * @param nickname {string} if the nickname of the Reaction if he got any + * @param owner {boolean} whether or not the Reaction is the server owner + * @param xp {number} the xp the specified Reaction has + * @param level {number} the level the Reaction has + */ +let UpdateReaction = async (sql, server, Reaction, Reactionname, nickname, owner, xp, level) => { + throw "not implemented"; +} + +/** + * gets a Reaction from databse + * @param sql {mysql.Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a Reaction on + * @param Reaction {string} the id of which Reaction you want to store + */ +let RemoveReaction = async (sql, server, Reaction) => { + throw "not implemented"; +} + +module.exports = {CreateReaction, GetReaction, UpdateReaction, RemoveReaction} \ No newline at end of file diff --git a/Commons/Data/User.js b/Commons/Data/User.js new file mode 100644 index 0000000..5245d66 --- /dev/null +++ b/Commons/Data/User.js @@ -0,0 +1,61 @@ +const mysql = require("mysql"); +/** + * creates a user and stores it in the databse + * @param sql {Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a user on + * @param user {string} the id of which user you want to store + * @param username {string} the username of the user you want to store + * @param nickname {string} if the nickname of the user if he got any + * @param owner {boolean} whether or not the user is the server owner + */ +let CreateUser = async (sql, server, user, username, owner) => { + sql.query({ + sql: 'INSERT INTO serverusers SET ?', + timeout: 40000, // 40s + values: {serverid: server, userid: user, username: username, owner: owner} + }, function (error, results, fields) { + // error will be an Error if one occurred during the query + if (error != undefined | null) { + throw `could not create user on server ${server} with userid ${user} due to error: ` + error + } + // results will contain the results of the query + // fields will contain information about the returned results fields (if any) + }); +} + +/** + * gets a user from databse + * @param sql {mysql.Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a user on + * @param user {string} the id of which user you want to store + */ +let GetUser = async (sql, server, user) => { + throw "not implemented"; +} + +/** + * updates a user and stores it in the databse + * @param sql {mysql.Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a user on + * @param user {string} the id of which user you want to store + * @param username {string} the username of the user you want to store + * @param nickname {string} if the nickname of the user if he got any + * @param owner {boolean} whether or not the user is the server owner + * @param xp {number} the xp the specified user has + * @param level {number} the level the user has + */ +let UpdateUser = async (sql, server, user, username, nickname, owner, xp, level) => { + throw "not implemented"; +} + +/** + * gets a user from databse + * @param sql {mysql.Connection} the mysql connection object + * @param server {string} the id of which server you want ot create a user on + * @param user {string} the id of which user you want to store + */ +let RemoveUser = async (sql, server, user) => { + throw "not implemented"; +} + +module.exports = {CreateUser, GetUser, UpdateUser, RemoveUser} \ No newline at end of file diff --git a/Commons/Services/Reaction.js b/Commons/Services/Reaction.js index f17f778..3807f46 100644 --- a/Commons/Services/Reaction.js +++ b/Commons/Services/Reaction.js @@ -52,6 +52,20 @@ const DevData = { MessageType: "public", } } + }, + "1099317452170596523": { + type: "reactionRole", + reactions: { + "🪙":{ + Action: "role", + Role: { + Name: "Gold Role", + ID: "1051891644007469096" + }, + Restriction: "AddOnly", + MessageType: "public", + }, + } } } diff --git a/Commons/Services/Roles.js b/Commons/Services/Roles.js index 89d4ad3..9d968ba 100644 --- a/Commons/Services/Roles.js +++ b/Commons/Services/Roles.js @@ -94,17 +94,6 @@ revokes a given roles to a given user */ const RemoveRoles = async (userVal, roles, bot) => { - [ - { - name: "role", - reason: "given as a test" - }, - { - name: "role", - reason: undefined - }, - ] - // error handling if (userVal == undefined) { throw "error user was undefined" } if (roleName == undefined) { throw "error roleNames was undefined" } diff --git a/README.md b/README.md index 6815d2c..7b6c384 100644 --- a/README.md +++ b/README.md @@ -19,4 +19,4 @@ Initial Release - Level - XP - Reward V1 - - using level to give rewards + - using level to give rewards \ No newline at end of file diff --git a/devpengu.sql b/devpengu.sql new file mode 100644 index 0000000..9f456a6 --- /dev/null +++ b/devpengu.sql @@ -0,0 +1,115 @@ +-- phpMyAdmin SQL Dump +-- version 5.2.0 +-- https://www.phpmyadmin.net/ +-- +-- Host: 127.0.0.1 +-- Erstellungszeit: 24. Apr 2023 um 00:39 +-- Server-Version: 10.4.27-MariaDB +-- PHP-Version: 8.2.0 + +SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; +START TRANSACTION; +SET time_zone = "+00:00"; + + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; + +-- +-- Datenbank: `devpengu` +-- + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `reactions` +-- + +CREATE TABLE `reactions` ( + `id` bigint(20) NOT NULL, + `message` varchar(20) NOT NULL, + `type` varchar(30) NOT NULL, + `reaction` varchar(30) NOT NULL, + `action` varchar(20) NOT NULL, + `rolename` varchar(30) DEFAULT NULL, + `roleid` varchar(30) DEFAULT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `servers` +-- + +CREATE TABLE `servers` ( + `id` bigint(25) NOT NULL, + `name` varchar(300) NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- -------------------------------------------------------- + +-- +-- Tabellenstruktur für Tabelle `serverusers` +-- + +CREATE TABLE `serverusers` ( + `id` bigint(20) NOT NULL, + `serverid` bigint(25) NOT NULL, + `userid` bigint(25) NOT NULL, + `owner` tinyint(1) NOT NULL DEFAULT 0, + `username` varchar(300) NOT NULL, + `nickname` varchar(300) DEFAULT NULL, + `xp` bigint(20) NOT NULL DEFAULT 0, + `level` bigint(20) NOT NULL DEFAULT 1 +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; + +-- +-- Indizes der exportierten Tabellen +-- + +-- +-- Indizes für die Tabelle `reactions` +-- +ALTER TABLE `reactions` + ADD PRIMARY KEY (`id`); + +-- +-- Indizes für die Tabelle `servers` +-- +ALTER TABLE `servers` + ADD PRIMARY KEY (`id`); + +-- +-- Indizes für die Tabelle `serverusers` +-- +ALTER TABLE `serverusers` + ADD PRIMARY KEY (`id`); + +-- +-- AUTO_INCREMENT für exportierte Tabellen +-- + +-- +-- AUTO_INCREMENT für Tabelle `reactions` +-- +ALTER TABLE `reactions` + MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT für Tabelle `servers` +-- +ALTER TABLE `servers` + MODIFY `id` bigint(25) NOT NULL AUTO_INCREMENT; + +-- +-- AUTO_INCREMENT für Tabelle `serverusers` +-- +ALTER TABLE `serverusers` + MODIFY `id` bigint(20) NOT NULL AUTO_INCREMENT; +COMMIT; + +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; diff --git a/index.js b/index.js index e83efec..991be90 100644 --- a/index.js +++ b/index.js @@ -2,18 +2,28 @@ require("dotenv").config(); const { Client, GatewayIntentBits, Collection, REST, Routes, Events, Partials } = require('discord.js'); const fs = require('fs'); const sql = require('mysql'); +const {CreateUser} = require('./Commons/Data/User') const bot = new Client({ - intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions ], + intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions, GatewayIntentBits.GuildMembers ], partials: [Partials.Message, Partials.Channel, Partials.Reaction], }); bot.commands = new Collection(); -const { HandleReaction } = require("./Commons/Services/Reaction"); +const server = sql.createConnection({ + host : process.env.DB_HOST, + user : process.env.DB_USERNAME, + password : process.env.DB_PASSWORD, + database : process.env.DB_NAME +}); -const Database = require("./Commons/Data/Database"); -const server = Database(process.env) +server.connect(function(err) { + if (err) { + console.error('error connecting Database in Data.Database: ' + err.stack); + process.exit(1) + } +}); /* =============== * File Import * @@ -24,6 +34,11 @@ for (const file of commandFiles) { bot.commands.set(command.name, command); } +bot.on(Events.GuildMemberAdd, async user => { + console.log(`new user found`, user.guild.id.toString(), user.user.id.toString(), user.user.username, false) + await CreateUser(server, user.guild.id.toString(), user.user.id.toString(), user.user.username, false); +}) + bot.on(Events.InteractionCreate, async interaction => { // TODO: Adde Rolebased Permission Tracking rather then relying on discord permission system if (!interaction.isChatInputCommand()) return; @@ -37,7 +52,6 @@ bot.on(Events.MessageReactionAdd, async (reaction, user) => { HandleReaction(reaction, user, undefined, "add", bot) }) - bot.on(Events.MessageReactionRemove, async (reaction, user) => { if (user.bot) return; HandleReaction(reaction, user, server, "remove", bot) diff --git a/package.json b/package.json index 2dc1cc3..9aa9a80 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "dev-penguins", - "version": "1.0.0", + "version": "0.6.0", "description": "", - "main": "index.js", + "main": "sharding.js", "scripts": { "start": "node sharding", "test": "node ."