diff --git a/server.js b/server.js index 6e2b50d..7a29ae1 100644 --- a/server.js +++ b/server.js @@ -2,6 +2,7 @@ const fs = require('fs'); const createInitialConfig = require("./util/createInitialConfig") const convertTimerJSONToSQL = require('./util/timer/convertTimerJSONToSQL.js'); const createTimersTable = require('./server/createDatabaseTables/createTimersTable'); +const convertCustomCommandsJSONToSQL = require('./server/convertCustomCommandsJSONToSQL.js'); if(!fs.existsSync("./data/config.json")) { createInitialConfig(); } @@ -10,11 +11,22 @@ async function checkAndConvertJSONToSQL(){ if(fs.existsSync("./data/timers.json")){ process.stdout.write(true + "\n") await convertTimerJSONToSQL(); + fs.renameSync('data/timers.json', 'data/timers.json.old'); console.log("Renamed timers.json to timers.json.old"); }else{ process.stdout.write(false + "\n") } + + process.stdout.write("Checking if customCommands.json exists... ") + if(fs.existsSync('./data/customCommands.json')){ + process.stdout.write(true + "\n") + await convertCustomCommandsJSONToSQL(); + + fs.renameSync('data/customCommands.json', 'data/customCommands.json.old'); + }else{ + process.stdout.write(false + "\n") + } } diff --git a/server/convertCustomCommandsJSONToSQL.js b/server/convertCustomCommandsJSONToSQL.js new file mode 100644 index 0000000..101d7e4 --- /dev/null +++ b/server/convertCustomCommandsJSONToSQL.js @@ -0,0 +1,28 @@ +const sqlite3 = require('sqlite3').verbose(); +module.exports = async function () { + const customCommands = require('../data/customCommands.json') + const db = new sqlite3.Database('data/database.db'); + return new Promise((resolve, reject) => { + customCommands.forEach(command => { + const isDeleted = false; + + db.run(`INSERT INTO customCommands ( + customName, + customMessage, + author, + isDeleted + ) VALUES (?, ?, ?, ?)`, [command.customName, command.customMessage, command.author, isDeleted], function (error) { + if (error) { + console.error(`Error while converting customCommands.json to SQL: ${error}`) + reject(error); + } + }) + + + }); + + db.close(); + console.log("Converted customCommands.json to SQL successfully."); + resolve(); + }) +}