can now store users in database + database file
This commit is contained in:
@ -5,11 +5,10 @@ const Database = async (db) => {
|
|||||||
const server = sql.createConnection({
|
const server = sql.createConnection({
|
||||||
host : db.DB_HOST,
|
host : db.DB_HOST,
|
||||||
user : db.DB_USERNAME,
|
user : db.DB_USERNAME,
|
||||||
password : db.DB_PASSWORD
|
password : db.DB_PASSWORD,
|
||||||
,
|
|
||||||
database : db.DB_NAME
|
database : db.DB_NAME
|
||||||
});
|
});
|
||||||
|
|
||||||
server.connect(function(err) {
|
server.connect(function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error('error connecting Database in Data.Database: ' + err.stack);
|
console.error('error connecting Database in Data.Database: ' + err.stack);
|
||||||
|
50
Commons/Data/Reactions.js
Normal file
50
Commons/Data/Reactions.js
Normal 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
61
Commons/Data/User.js
Normal 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}
|
@ -52,6 +52,20 @@ const DevData = {
|
|||||||
MessageType: "public",
|
MessageType: "public",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"1099317452170596523": {
|
||||||
|
type: "reactionRole",
|
||||||
|
reactions: {
|
||||||
|
"🪙":{
|
||||||
|
Action: "role",
|
||||||
|
Role: {
|
||||||
|
Name: "Gold Role",
|
||||||
|
ID: "1051891644007469096"
|
||||||
|
},
|
||||||
|
Restriction: "AddOnly",
|
||||||
|
MessageType: "public",
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,17 +94,6 @@ revokes a given roles to a given user
|
|||||||
*/
|
*/
|
||||||
const RemoveRoles = async (userVal, roles, bot) => {
|
const RemoveRoles = async (userVal, roles, bot) => {
|
||||||
|
|
||||||
[
|
|
||||||
{
|
|
||||||
name: "role",
|
|
||||||
reason: "given as a test"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: "role",
|
|
||||||
reason: undefined
|
|
||||||
},
|
|
||||||
]
|
|
||||||
|
|
||||||
// error handling
|
// error handling
|
||||||
if (userVal == undefined) { throw "error user was undefined" }
|
if (userVal == undefined) { throw "error user was undefined" }
|
||||||
if (roleName == undefined) { throw "error roleNames was undefined" }
|
if (roleName == undefined) { throw "error roleNames was undefined" }
|
||||||
|
@ -19,4 +19,4 @@ Initial Release
|
|||||||
- Level
|
- Level
|
||||||
- XP
|
- XP
|
||||||
- Reward V1
|
- Reward V1
|
||||||
- using level to give rewards
|
- using level to give rewards
|
115
devpengu.sql
Normal file
115
devpengu.sql
Normal 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 */;
|
24
index.js
24
index.js
@ -2,18 +2,28 @@ require("dotenv").config();
|
|||||||
const { Client, GatewayIntentBits, Collection, REST, Routes, Events, Partials } = require('discord.js');
|
const { Client, GatewayIntentBits, Collection, REST, Routes, Events, Partials } = require('discord.js');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const sql = require('mysql');
|
const sql = require('mysql');
|
||||||
|
const {CreateUser} = require('./Commons/Data/User')
|
||||||
|
|
||||||
const bot = new Client({
|
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],
|
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
|
||||||
});
|
});
|
||||||
|
|
||||||
bot.commands = new Collection();
|
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");
|
server.connect(function(err) {
|
||||||
const server = Database(process.env)
|
if (err) {
|
||||||
|
console.error('error connecting Database in Data.Database: ' + err.stack);
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
/* ===============
|
/* ===============
|
||||||
* File Import *
|
* File Import *
|
||||||
@ -24,6 +34,11 @@ for (const file of commandFiles) {
|
|||||||
bot.commands.set(command.name, command);
|
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 => {
|
bot.on(Events.InteractionCreate, async interaction => {
|
||||||
// TODO: Adde Rolebased Permission Tracking rather then relying on discord permission system
|
// TODO: Adde Rolebased Permission Tracking rather then relying on discord permission system
|
||||||
if (!interaction.isChatInputCommand()) return;
|
if (!interaction.isChatInputCommand()) return;
|
||||||
@ -37,7 +52,6 @@ bot.on(Events.MessageReactionAdd, async (reaction, user) => {
|
|||||||
HandleReaction(reaction, user, undefined, "add", bot)
|
HandleReaction(reaction, user, undefined, "add", bot)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
bot.on(Events.MessageReactionRemove, async (reaction, user) => {
|
bot.on(Events.MessageReactionRemove, async (reaction, user) => {
|
||||||
if (user.bot) return;
|
if (user.bot) return;
|
||||||
HandleReaction(reaction, user, server, "remove", bot)
|
HandleReaction(reaction, user, server, "remove", bot)
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{
|
{
|
||||||
"name": "dev-penguins",
|
"name": "dev-penguins",
|
||||||
"version": "1.0.0",
|
"version": "0.6.0",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "sharding.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node sharding",
|
"start": "node sharding",
|
||||||
"test": "node ."
|
"test": "node ."
|
||||||
|
Reference in New Issue
Block a user