can now store users in database + database file

This commit is contained in:
2023-04-24 00:40:34 +02:00
parent 16dac26a2c
commit e428ce388c
9 changed files with 264 additions and 22 deletions

View File

@ -5,8 +5,7 @@ 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
});

50
Commons/Data/Reactions.js Normal file
View File

@ -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}

61
Commons/Data/User.js Normal file
View File

@ -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}

View File

@ -52,6 +52,20 @@ const DevData = {
MessageType: "public",
}
}
},
"1099317452170596523": {
type: "reactionRole",
reactions: {
"🪙":{
Action: "role",
Role: {
Name: "Gold Role",
ID: "1051891644007469096"
},
Restriction: "AddOnly",
MessageType: "public",
},
}
}
}

View File

@ -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" }

115
devpengu.sql Normal file
View File

@ -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 */;

View File

@ -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)

View File

@ -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 ."