Use sqlite database for creating new custom commands
All checks were successful
CI / CI (push) Successful in 21s
Lint Codebase / eslint (push) Successful in 16s

This commit is contained in:
SileNce5k 2024-12-19 19:22:42 +01:00
parent 7702f10587
commit 345a03a3b0
Signed by: SileNce
GPG key ID: B0A142BB4291B204
2 changed files with 30 additions and 26 deletions

View file

@ -1,25 +1,33 @@
const fs = require('fs');
module.exports = function(customName, customMessage, author){
let sendText;
const customPath = './data/customCommands.json';
let json = fs.readFileSync(customPath, 'utf8');
let customCommands = JSON.parse(json)
const sqlite3 = require('sqlite3').verbose();
module.exports = async function(client, customName, customMessage, author){
let sendText = "";
let isExists = client.customCommands.get(customName);
let isExists = false;
customCommands.forEach(function (customCommand) {
if (customCommand.customName === customName) {
sendText = "This custom command already exists";
isExists = true;
}
});
if (!isExists) {
let newCustomCommand = {
"customName": customName, "customMessage": customMessage, "author": author
}
customCommands.push(newCustomCommand)
sendText = `New custom command with the name "${customName}" added`
const db = new sqlite3.Database("data/database.db");
sendText = await new Promise((resolve, reject)=>{
db.run(`INSERT INTO customCommands (
customName,
customMessage,
author,
isDeleted
) VALUES (?, ?, ?, ?)`, [customName, customMessage, author, false],
function(error){
if(error){
console.error(error)
let sendText = "Error while inserting new custom command.";
reject(sendText);
}else{
client.customCommands.set(customName, customMessage)
let sendText = `New custom command with the name "${customName}" added`
resolve(sendText)
}
})
})
db.close();
}
fs.writeFileSync(customPath, JSON.stringify(customCommands, null, 4))
return sendText;
}