Add conversion of custom commands to SQL
This commit is contained in:
parent
904fa25a6d
commit
11ad0af0b9
3 changed files with 110 additions and 11 deletions
83
server.js
83
server.js
|
@ -2,31 +2,62 @@ const fs = require('fs');
|
||||||
const createInitialConfig = require("./util/createInitialConfig")
|
const createInitialConfig = require("./util/createInitialConfig")
|
||||||
const convertTimerJSONToSQL = require('./util/timer/convertTimerJSONToSQL.js');
|
const convertTimerJSONToSQL = require('./util/timer/convertTimerJSONToSQL.js');
|
||||||
const createTimersTable = require('./server/createDatabaseTables/createTimersTable');
|
const createTimersTable = require('./server/createDatabaseTables/createTimersTable');
|
||||||
|
const createCustomCommandsTable = require('./server/createDatabaseTables/createCustomCommandsTable.js');
|
||||||
|
const convertCustomCommandsJSONToSQL = require('./server/convertCustomCommandsJSONToSQL.js');
|
||||||
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
if(!fs.existsSync("./data/config.json")) {
|
if(!fs.existsSync("./data/config.json")) {
|
||||||
createInitialConfig();
|
createInitialConfig();
|
||||||
}
|
}
|
||||||
async function checkAndConvertJSONToSQL(){
|
|
||||||
process.stdout.write("Checking if timers.json exists... ")
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
const { Collection, Client, GatewayIntentBits, Partials } = require('discord.js');
|
||||||
function startBot(){
|
const client = new Client({ intents: [GatewayIntentBits.Guilds,
|
||||||
const { Collection, Client, GatewayIntentBits, Partials } = require('discord.js');
|
|
||||||
const client = new Client({ intents: [GatewayIntentBits.Guilds,
|
|
||||||
GatewayIntentBits.GuildMessages,
|
GatewayIntentBits.GuildMessages,
|
||||||
GatewayIntentBits.MessageContent,
|
GatewayIntentBits.MessageContent,
|
||||||
GatewayIntentBits.GuildMembers,
|
GatewayIntentBits.GuildMembers,
|
||||||
GatewayIntentBits.GuildPresences
|
GatewayIntentBits.GuildPresences
|
||||||
], partials: [Partials.Channel] });
|
], partials: [Partials.Channel] });
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async function checkAndConvertJSONToSQL(){
|
||||||
|
process.stdout.write("Checking if timers.json exists... ")
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function loadcustomCommands() {
|
||||||
|
const db = new sqlite3.Database('data/database.db');
|
||||||
|
client.customCommands = new Collection();
|
||||||
|
db.all("SELECT * FROM customCommands", (error, command) => {
|
||||||
|
if(error){
|
||||||
|
console.error("Error while loading custom commands:\n",error.message)
|
||||||
|
}else {
|
||||||
|
client.customCommands.set(command.customName, command.customMessage);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function startBot(){
|
||||||
const {
|
const {
|
||||||
globalPrefix,
|
globalPrefix,
|
||||||
token,
|
token,
|
||||||
|
@ -75,9 +106,25 @@ function startBot(){
|
||||||
|
|
||||||
async function prepareBot(){
|
async function prepareBot(){
|
||||||
const createLastfmTable = require('./server/createDatabaseTables/createLastfmTable');
|
const createLastfmTable = require('./server/createDatabaseTables/createLastfmTable');
|
||||||
await createTimersTable();
|
|
||||||
await createLastfmTable();
|
|
||||||
await checkAndConvertJSONToSQL();
|
const taskGroups = [
|
||||||
|
[
|
||||||
|
createTimersTable(),
|
||||||
|
createLastfmTable(),
|
||||||
|
createCustomCommandsTable()
|
||||||
|
],
|
||||||
|
[
|
||||||
|
loadcustomCommands(),
|
||||||
|
checkAndConvertJSONToSQL()
|
||||||
|
]
|
||||||
|
]
|
||||||
|
|
||||||
|
taskGroups.forEach(async (taskGroup) => {
|
||||||
|
await Promise.all(taskGroup)
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareBot().then( () => {
|
prepareBot().then( () => {
|
||||||
|
|
28
server/convertCustomCommandsJSONToSQL.js
Normal file
28
server/convertCustomCommandsJSONToSQL.js
Normal file
|
@ -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();
|
||||||
|
})
|
||||||
|
}
|
24
server/createDatabaseTables/createCustomCommandsTable.js
Normal file
24
server/createDatabaseTables/createCustomCommandsTable.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
const sqlite3 = require('sqlite3').verbose();
|
||||||
|
|
||||||
|
module.exports = async function () {
|
||||||
|
const db = new sqlite3.Database('data/database.db');
|
||||||
|
return new Promise ((resolve, reject)=>{
|
||||||
|
db.run(
|
||||||
|
`CREATE TABLE IF NOT EXISTS customCommands (
|
||||||
|
customName TEXT PRIMARY KEY,
|
||||||
|
customMessage TEXT,
|
||||||
|
author TEXT,
|
||||||
|
isDeleted INTEGER)`,
|
||||||
|
(err) => {
|
||||||
|
if (err) {
|
||||||
|
console.error(`Error while creating table 'customCommands': ${err}`);
|
||||||
|
reject(err);
|
||||||
|
} else {
|
||||||
|
console.log("Table 'customCommands' created successfully.");
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
db.close();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
})
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue