diff --git a/.env.example b/.env.example index 2515465..15c8c93 100644 --- a/.env.example +++ b/.env.example @@ -1,2 +1,3 @@ DISCORD_BOT_TOKEN= -GENERATIVE_AI_TOKEN= \ No newline at end of file +GENERATIVE_AI_TOKEN= +POSTGRES_PASSWORD= \ No newline at end of file diff --git a/README.md b/README.md index 32cca8f..8d3b64c 100644 --- a/README.md +++ b/README.md @@ -30,3 +30,4 @@ Then run the command `npm run start` - `xml2js` - `humanize-duration` - `cavas` +- `pg` diff --git a/config.yml b/config.yml index 3c5f790..4de053a 100644 --- a/config.yml +++ b/config.yml @@ -13,15 +13,20 @@ bot: topP: 1 topK: 40 +database: + enabled: true + user: 'frostbot' + host: '100.103.238.88' + port: 5432 + database: 'data' + blockedUsers: + - '1277149071823339573' + placeholders: image: 'https://discord.com/' link: 'https://discord.com/' logging: - database: - enabled: true - blockedUsers: - - '1277149071823339573' blockedUsers: - '1280698107637792838' - '1277149071823339573' diff --git a/package.json b/package.json index d4697d2..d0501ed 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "humanize-duration": "^3.32.1", "js-yaml": "^4.1.0", "node-fetch": "^2.7.0", + "pg": "^8.16.0", "qrcode": "^1.5.4", "sharp": "^0.33.5", "sqlite3": "^5.1.7", diff --git a/src/plugins/database.js b/src/plugins/database.js index 80767e2..7df7e9a 100644 --- a/src/plugins/database.js +++ b/src/plugins/database.js @@ -1,66 +1,66 @@ -const sqlite3 = require('sqlite3').verbose(); -const path = require('path'); -const dbpath = path.join(__dirname, '..', 'data', 'data.db'); +const { Client } = require('pg'); + function main(ctx) { const { bot, options } = ctx; + const { user, host, port, database } = options.database; + bot.database = { db: null, - loadDataBase() { - this.db = new sqlite3.Database( - dbpath, - sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, - (err) => { - if (err) { - console.error(err.message); - } else { - const createmessages = ` - CREATE TABLE IF NOT EXISTS MESSAGES( - userid TEXT, - id TEXT, - username TEXT, - channelid TEXT, - serverid TEXT, - channelname TEXT, - servername TEXT, - content TEXT, - cleancontent TEXT, - addeddate INT, - date INT, - messagedata TEXT, - attachments TEXT - ); - `; - const createinteractions = ` - CREATE TABLE IF NOT EXISTS INTERACTIONS( - date INTEGER, - serverid TEXT, - channelid TEXT, - userid TEXT, - commandname TEXT, - options TEXT, - raw TEXT, - username TEXT, - servername TEXT, - channelname TEXT - ); - `; - this.db.run(createmessages, (err) => { - if (err) { - console.error(err.message); - } - }); - this.db.run(createinteractions, (err) => { - if (err) { - console.error(err.message); - } - }); - } - } - ); + async loadDataBase() { + this.db = new Client({ + host, + port, + user, + password: process.env.POSTGRES_PASSWORD, + database, + }); + + try { + await this.db.connect(); + console.log('Connected to PostgreSQL.'); + + const createmessages = ` + CREATE TABLE IF NOT EXISTS messages ( + userid TEXT, + id TEXT, + username TEXT, + channelid TEXT, + serverid TEXT, + channelname TEXT, + servername TEXT, + content TEXT, + cleancontent TEXT, + addeddate BIGINT, + date BIGINT, + messagedata TEXT, + attachments TEXT + ); + `; + + const createinteractions = ` + CREATE TABLE IF NOT EXISTS interactions ( + date BIGINT, + serverid TEXT, + channelid TEXT, + userid TEXT, + commandname TEXT, + options TEXT, + raw TEXT, + username TEXT, + servername TEXT, + channelname TEXT + ); + `; + + await this.db.query(createmessages); + await this.db.query(createinteractions); + } catch (err) { + console.error('PostgreSQL Error:', err.message); + } }, }; - if (options.logging.database.enabled) { + if (options.database.enabled) { bot.database.loadDataBase(); } } diff --git a/src/plugins/database_message_logger.js b/src/plugins/database_message_logger.js index ad595e9..e9cbf5e 100644 --- a/src/plugins/database_message_logger.js +++ b/src/plugins/database_message_logger.js @@ -2,53 +2,6 @@ function main(ctx) { const { bot, options } = ctx; const { client } = bot; bot.database.messages = { - add(message) { - if ( - options.logging.database.blockedUsers.includes(message.author.id) || - bot.database.db == null - ) - return; - let data = { - id: message.id, - userid: message.author.id, - username: message.author.username, - channelid: message.channel ? message.channel.id : message.channelId, - serverid: message.guild ? message.guild.id : '', - channelname: message.channel.name ? message.channel.name : '', - servername: message.guild ? message.guild.name : '', - content: message.content.replaceAll(/[^\x20-\x7E\n]/g, ''), - cleancontent: message.cleanContent.replaceAll(/[^\x20-\x7E\n]/g, ''), - date: message.createdTimestamp, - attachments: message.attachments - .map((attachment) => attachment.url) - .join(','), - messagedata: JSON.stringify(message), - }; - bot.database.db.run( - `INSERT INTO MESSAGES(userid, id, username, channelid, serverid, channelname, servername, content, cleancontent, date, addeddate, messagedata, attachments) -VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, - [ - data.userid, - data.id, - data.username, - data.channelid, - data.serverid, - data.channelname, - data.servername, - data.content, - data.cleancontent, - data.date, - new Date().getTime(), - data.messagedata, - data.attachments, - ], - function (err) { - if (err) { - return console.error(err.message); - } - } - ); - }, addinteraction(interaction) { let data = { userid: interaction.user.id, @@ -74,9 +27,11 @@ VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, typeof value === 'bigint' ? value.toString() : value ), }; - bot.database.db.run( - `INSERT INTO INTERACTIONS(userid, username, channelid, serverid, channelname, servername, date, commandname, options, raw) -VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, + bot.database.db.query( + `INSERT INTO interactions ( + userid, username, channelid, serverid, channelname, servername, + date, commandname, options, raw + ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`, [ data.userid, data.username, @@ -88,27 +43,16 @@ VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, data.commandname, data.options, data.raw, - ], - function (err) { - if (err) { - return console.error(err.message); - } - } + ] ); }, }; - if (options.logging.database.enabled) { + if (options.database.enabled) { client.on('interactionCreate', (interaction) => { if (interaction.isCommand()) { bot.database.messages.addinteraction(interaction); } }); - client.on('messageCreate', (message) => { - bot.database.messages.add(message); - }); - client.on('messageUpdate', (_old, newmessage) => { - bot.database.messages.add(newmessage); - }); } } module.exports = main;