Skip to content
This repository has been archived by the owner on Sep 11, 2020. It is now read-only.

Commit

Permalink
Added notes to JS files
Browse files Browse the repository at this point in the history
  • Loading branch information
MrG0ld committed Mar 28, 2019
1 parent 7de8c0b commit b16b038
Show file tree
Hide file tree
Showing 7 changed files with 304 additions and 184 deletions.
151 changes: 103 additions & 48 deletions menu/bot/bot.js
Original file line number Diff line number Diff line change
@@ -1,80 +1,134 @@
//--------------------------------------------Modules--------------------------------------------//
//---------------------------------------------------------------------//
// Modules
//
// This is where you put the NPM Modules to use in this file.
//---------------------------------------------------------------------//

var path = require('path');
var fse = require('fs-extra');
const { exec } = require('child_process');

var {exec} = require('child_process');
var $ = jQuery = require('jquery');
//-----------------------------------------------------------------------------------------------//


//---------------------------------------------------------------------//
// JSON OBJ
//
// Use these JSON objects that contain the App Data.
// obj -> data.json
// obj_bot -> bot_data.json
//---------------------------------------------------------------------//

var obj = JSON.parse(fse.readFileSync(dataDataPath, 'utf8'));
var obj_bot = JSON.parse(fse.readFileSync(BotDataPath, 'utf8'));

// Load values
//---------------------------------------------------------------------//
// Load Values
//
// Loads values from JSON files.
//---------------------------------------------------------------------//

$('#BotBackupSelect').val(obj_bot.Backup_Timer_Type);
$('#BotBackupAmount').val(obj_bot.Backup_Timer_Value);

if($('#BotBackupSelect').val() == 1) {
$('#BotBackupAmount').prop('disabled', true);
}

function BotStartClick() {
// Check if Bot Path exists and is valid
//---------------------------------------------------------------------//
// function BotConsoleLog(text)
//
// This function sends text to Bot Console Log.
//---------------------------------------------------------------------//

function BotConsoleLog(text) {
$('#BotConsoleLog').append(text);
if(!$('#BotConsoleLog').is(':visible')) {
$('#BotConsoleLogNumber').html(parseInt($('#BotConsoleLogNumber').text(),10) + 1 || 1)
}
}

//---------------------------------------------------------------------//
// function CheckIfDBMPathExists()
//
// This function checks if DBM Path exists and is valid.
//---------------------------------------------------------------------//

function CheckIfDBMPathExists() {
if(obj.DBM_Path == "") {
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - ERROR: DBM Path isn't set in Settings\n`);
return;
}

if(!fse.existsSync(obj.DBM_Path)) {
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - ERROR: DBM Path isn't valid\n`);
return;
}
}

//---------------------------------------------------------------------//
// function CheckIfBotPathExists()
//
// This function checks if Bot Path exists and is valid.
//---------------------------------------------------------------------//

function CheckIfBotPathExists() {
if(obj.Bot_Path == "") {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't set in Settings\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't set in Settings\n`);
return;
}

if(!fse.existsSync(obj.Bot_Path)) {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't valid\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't valid\n`);
return;
}
}

//---------------------------------------------------------------------//
// function BotStartClick()
//
// This is the function that starts your bot when you
// press the Start button on Bot tab.
//---------------------------------------------------------------------//

function BotStartClick() {
//-----------------------------Check if Bot Path exists and is valid-----------------------------//
CheckIfBotPathExists();
//-----------------------------------------------------------------------------------------------//


const grep = process.platform === "win32" ? exec(`start cmd.exe /k "cd "${obj.Bot_Path}" && node bot.js"`) : exec(`gnome-terminal --command="bash -c 'cd ${(obj.Bot_Path).replace(/\ /g, "\\ ")}; node bot.js; ls; $SHELL'"`);
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - Bot Started\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - Bot Started\n`);

if(process.platform === "win32") { // Linux doesn't detect this...
grep.on('close', (code, signal) => {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - Bot Stopped\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - Bot Stopped\n`);
});
}

grep.on('error', (err) => {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: ${err}\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - ERROR: ${err}\n`);
});
}

//---------------------------------------------------------------------//
// function BotCopyActionsClick()
//
// This is the function that copies and pastes your actions folder
// on your bot folder when you press the Copy DBM Actions Folder
// button on Bot tab.
//---------------------------------------------------------------------//

function BotCopyActionsClick() {
const start = new Date();
var processB1 = 0;

$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - Copying and pasting DBM Actions Folder...\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - Copying and pasting DBM Actions Folder...\n`);

// Check if DBM Path exists and is valid
if(obj.DBM_Path == "") {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: DBM Path isn't set in Settings\n`);
return;
}

if(!fse.existsSync(obj.DBM_Path)) {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: DBM Path isn't valid\n`);
return;
}
//------------------------Check if DBM Path and Bot Path exists and is valid---------------------//
CheckIfDBMPathExists();
CheckIfBotPathExists();
//-----------------------------------------------------------------------------------------------//

// Check if Bot Path exists and is valid
if(obj.Bot_Path == "") {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't set in Settings\n`);
return;
}

if(!fse.existsSync(obj.Bot_Path)) {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't valid\n`);
return;
}
//-----------------------------------------------------------------------------------------------//

fse.readdirSync(path.join(obj.DBM_Path, "actions")).forEach(function(file, index, arr) {
if(file.match(/^.+\.js$/i)) {
Expand All @@ -83,12 +137,20 @@ function BotCopyActionsClick() {
}
processB1++;
if(processB1 === arr.length) {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - DBM Actions Folder copied and pasted in your Bot successfully! (${((new Date() - start)*10**-3).toFixed(3)}s)\n`);
BotConsoleLog(`${new Date().toTimeString().slice(0,8)} - DBM Actions Folder copied and pasted in your Bot successfully! (${((new Date() - start)*10**-3).toFixed(3)}s)\n`);
}
});
}

BotBackupSelectOnChange();
//---------------------------------------------------------------------//
// function BotBackupSelectOnChange()
//
// This is the function that makes the backup data folder system
// work.
//---------------------------------------------------------------------//

BotBackupSelectOnChange(); //Executes the function BotBackupSelectOnChange() on start of the app

function BotBackupSelectOnChange() {
const BackupSelectValue = $("#BotBackupSelect").val();
let timesetBackup = null;
Expand All @@ -112,29 +174,22 @@ function BotBackupSelectOnChange() {

if (!isNaN(BackupSelectValue) && timesetBackup != null) {
this.currentInterval = setInterval(function() {
// Check if Bot Path exists and is valid
if(obj.Bot_Path == "") {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't set in Settings\n`);
return;
}

if(!fse.existsSync(obj.Bot_Path)) {
$('#BotConsoleLog').append(`${new Date().toTimeString().slice(0,8)} - ERROR: Bot Path isn't valid\n`);
return;
}
//-----------------------------Check if Bot Path exists and is valid-----------------------------//
CheckIfBotPathExists();
//-----------------------------------------------------------------------------------------------//

const d = new Date();
const fname = ("0" + d.getDate()).slice(-2) + "-" + ("0" + (d.getMonth() + 1)).slice(-2) + "-" + d.getFullYear() + "-" + d.toLocaleTimeString().replace(/:/g, "-");
fse.ensureDirSync(path.join(BackupsPath, fname));
fse.copySync(path.join(obj.Bot_Path, "data"), path.join(BackupsPath, fname));

$('#BotConsoleLog').append(`${d.toTimeString().slice(0,8)} - New backup folder created: ${fname}\n`);
BotConsoleLog(`${d.toTimeString().slice(0,8)} - New backup folder created: ${fname}\n`);
}, timesetBackup);
};

// Store value in Data
//------------------------------------Store value in Data----------------------------------------//
obj_bot.Backup_Timer_Value = $("#BotBackupAmount").val();
obj_bot.Backup_Timer_Type = BackupSelectValue;
fse.writeFileSync(BotDataPath, JSON.stringify(obj_bot));
//-----------------------------------------------------------------------------------------------//
}
2 changes: 1 addition & 1 deletion menu/menu/menu.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<% include ../../menu/sidebar/sidebar %>
</div>

<div style="margin-left: 250px;">
<div id="MenuAllWindows" style="margin-left: 250px;">
<div id="ModsWindow"> <!-- Mods Stuff -->
<% include ../../menu/mods/mods %>
</div>
Expand Down
60 changes: 37 additions & 23 deletions menu/menu/menu.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
//--------------------------------------------Modules--------------------------------------------//
//var electron = require('electron');
//---------------------------------------------------------------------//
// Modules
//
// This is where you put the NPM Modules to use in this file.
//---------------------------------------------------------------------//

var path = require('path');
//var ejs = require('ejs-electron');
var fse = require('fs-extra');
//const { exec } = require('child_process');
//const {ipcRenderer} = electron;

var $ = jQuery = require('jquery');
//-----------------------------------------------------------------------------------------------//

//---------------------------------------------------------------------//
// Check If Backups Folder Exists
//
// This checks if the Backups folder exists.
// If not, creates one.
//---------------------------------------------------------------------//


// Check if backups folder exists----------------------------------------------------------------//
var BackupsPath = path.join(require('electron').remote.app.getPath('userData'), "backups");
if(fse.existsSync(BackupsPath) == false) {
fse.mkdirSync(BackupsPath);
}
//-----------------------------------------------------------------------------------------------//


//---------------------------------------------------------------------//
// Check If Data Folder Exists
//
// This checks if the data folder exists.
// If not, creates one.
//---------------------------------------------------------------------//

// Check if data folder exists-------------------------------------------------------------------//
var DataPath = path.join(require('electron').remote.app.getPath('userData'), "data");
if(fse.existsSync(DataPath) == false) {
fse.mkdirSync(DataPath);
}
//-----------------------------------------------------------------------------------------------//

//---------------------------------------------------------------------//
// Check If bot_data.json Exists
//
// This checks if the bot_data.json file exists.
// If not, creates one.
//---------------------------------------------------------------------//


// Check if bot_data.json exists-----------------------------------------------------------------//
var BotDataPath = path.join(DataPath, "bot_data.json");
if(fse.existsSync(BotDataPath) == false) {
const obj_bot = JSON.stringify({
Expand All @@ -38,23 +47,29 @@ if(fse.existsSync(BotDataPath) == false) {
});
fse.writeFileSync(BotDataPath, obj_bot);
}
//-----------------------------------------------------------------------------------------------//


//---------------------------------------------------------------------//
// Check If mods_data.json Exists
//
// This checks if the mods_data.json file exists.
// If not, creates one.
//---------------------------------------------------------------------//

// Check if mods_data.json exists----------------------------------------------------------------//
var ModsDataPath = path.join(DataPath, "mods_data.json");
if(fse.existsSync(ModsDataPath) == false) {
const obj_mods = JSON.stringify({
"Alternative_Mods": false
});
fse.writeFileSync(ModsDataPath, obj_mods);
}
//-----------------------------------------------------------------------------------------------//

//---------------------------------------------------------------------//
// Check If data.json Exists
//
// This checks if the data.json file exists.
// If not, creates one.
//---------------------------------------------------------------------//


// Check if data.json exists---------------------------------------------------------------------//
var dataDataPath = path.join(DataPath, "data.json");
if(fse.existsSync(dataDataPath) == false) {
const obj = JSON.stringify({
Expand All @@ -63,5 +78,4 @@ if(fse.existsSync(dataDataPath) == false) {
"Discord_RPC": true
});
fse.writeFileSync(dataDataPath, obj);
}
//-----------------------------------------------------------------------------------------------//
}
Loading

0 comments on commit b16b038

Please sign in to comment.