Use sqlite database for creating new custom commands
This commit is contained in:
parent
7702f10587
commit
345a03a3b0
2 changed files with 30 additions and 26 deletions
|
@ -20,11 +20,7 @@ module.exports = {
|
||||||
"<prefix>custom variables - list all variables you can use",
|
"<prefix>custom variables - list all variables you can use",
|
||||||
"<prefix>custom count - display the amount of custom commands"
|
"<prefix>custom count - display the amount of custom commands"
|
||||||
],
|
],
|
||||||
execute({message, args, client, prefix, owners}) {
|
async execute({message, args, client, prefix, owners}) {
|
||||||
const customPath = 'data/customCommands.json';
|
|
||||||
if(!fs.existsSync(customPath)){
|
|
||||||
fs.writeFileSync(customPath,"[]")
|
|
||||||
}
|
|
||||||
let sendText;
|
let sendText;
|
||||||
let isEmbed = false;
|
let isEmbed = false;
|
||||||
if (args){
|
if (args){
|
||||||
|
@ -37,7 +33,7 @@ module.exports = {
|
||||||
message.channel.send("Message can't be empty");
|
message.channel.send("Message can't be empty");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
sendText = addCustomCommand(customName, customMessage, message.author.id);
|
sendText = await addCustomCommand(client, customName, customMessage, message.author.id);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case "remove":
|
case "remove":
|
||||||
|
|
|
@ -1,25 +1,33 @@
|
||||||
const fs = require('fs');
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
module.exports = function(customName, customMessage, author){
|
module.exports = async function(client, customName, customMessage, author){
|
||||||
let sendText;
|
|
||||||
const customPath = './data/customCommands.json';
|
let sendText = "";
|
||||||
|
let isExists = client.customCommands.get(customName);
|
||||||
let json = fs.readFileSync(customPath, 'utf8');
|
|
||||||
let customCommands = JSON.parse(json)
|
|
||||||
|
|
||||||
let isExists = false;
|
|
||||||
customCommands.forEach(function (customCommand) {
|
|
||||||
if (customCommand.customName === customName) {
|
|
||||||
sendText = "This custom command already exists";
|
|
||||||
isExists = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
if (!isExists) {
|
if (!isExists) {
|
||||||
let newCustomCommand = {
|
const db = new sqlite3.Database("data/database.db");
|
||||||
"customName": customName, "customMessage": customMessage, "author": author
|
sendText = await new Promise((resolve, reject)=>{
|
||||||
}
|
|
||||||
customCommands.push(newCustomCommand)
|
db.run(`INSERT INTO customCommands (
|
||||||
sendText = `New custom command with the name "${customName}" added`
|
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;
|
return sendText;
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue