Skip to content

Commit

Permalink
Added a console, Fixed mutiple messages bug
Browse files Browse the repository at this point in the history
  • Loading branch information
CptShad committed May 1, 2021
1 parent 1788db5 commit 88ba441
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 21 deletions.
13 changes: 10 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const path = require("path");
require("./modules/Flash");
const { ipcMain } = require("electron");
const Discord = require("./modules/Discord");
const AQMessage = require("./modules/AQMessage")
const AQMessage = require("./modules/AQMessage");
const { fstat } = require("fs");

let mainWindow;
function createWindow() {
Expand All @@ -25,8 +26,12 @@ function createWindow() {
require("./modules/app");
}

app.disableHardwareAcceleration();
app.on("ready", createWindow);
app.on("window-all-closed", () => app.quit());
app.on("window-all-closed", () => {
require('fs').unlinkSync(path.join(__dirname, "Console.log"));
app.quit();
});



Expand All @@ -45,7 +50,9 @@ ipcMain.on("packet", (event, packet) => {
}
});
ipcMain.on("discord-start", (event, args) => {
Discord.Initiate(args[0], args[1]);
if (args[1] == true)
Discord.Initiate(args[0], args[1]);
else Discord.DestroyClient();
});
ipcMain.on("discord-isEnabled", (event) => {
event.returnValue = Discord.IsLogging;
Expand Down
1 change: 0 additions & 1 deletion modules/ConfigHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const DefaultFile = {
}

const configLoc = path.join(__dirname, "..", "Config.json")

class ConfigHandler
{
static InitializeConfig()
Expand Down
43 changes: 34 additions & 9 deletions modules/Discord.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ const { dialog } = require('electron')
//Deleted the Config Json cache and updates the require whenever the config file is edited.
//This enables Dynamic JSON File Loading
function nocache(module) {
require("fs").watchFile(path.resolve(module), () => {
require("fs").watchFile(path.resolve(module), () => {
console.log("Config File Updated");
Log.Write("Config File Updated", true);
delete require.cache[require.resolve(module)]; Config = require(module)
})
}
Expand All @@ -18,6 +19,7 @@ var Config = require(path.join(__dirname, "..", "Config.json"))
function ReadyListener(client)
{
console.log(`Logged in as ${client.user.tag}!`);
Log.Write(`Logged in as ${client.user.tag}!`, true);
dialog.showMessageBox({
message: `Logged in as ${client.user.tag}!`
})
Expand Down Expand Up @@ -89,7 +91,7 @@ function MessageListener(Message)

var Content = String(Message.content);
var defaultMatch = /(\w{2,3}) (.+)/;
var match = Content.match(/(\w{2,3}) (.+):\s*(.+)/);
var match = Content.match(/(\w{2,3}) (.+?):\s*(.+)/);
var defaultMatched = false;
if (match == null)
{
Expand Down Expand Up @@ -118,6 +120,10 @@ function MessageListener(Message)
}
else if (Config['discord']['SeamlessMode'])
{
if (match[1].toLocaleLowerCase() == "dm") {
DMHandler(Message.author.username, match[2], match[3]);
return;
}
var zone = findZone(Message.channel.name);
TextHandler(Message.author.username, Content, zone);
}
Expand All @@ -130,18 +136,29 @@ class Discord
static IsReady;
static IsLogging;
static Prefix;
static Initiate(token, enableLogging)
static Initiate(token)
{
this.Token;
this.Client = new DiscordModule.Client();
if (this.Client) this.DestroyClient();
this.Client = new DiscordModule.Client();
this.IsReady = true;
this.IsLogging = enableLogging;
this.IsLogging = true;
this.Client.login(token).catch(err => {
Log.Write("Invalid Token", true);
console.log("Invalid Token")
this.IsReady = false;
});
if (this.IsReady)
this.RegisterEvents();
this.RegisterEvents();
}
static DestroyClient()
{
if (this.Client)
{
this.Client.removeAllListeners();
this.Client.destroy();
}
this.Client = null;
this.IsReady = false;
this.IsLogging = false;
}
static RegisterEvents()
{
Expand All @@ -164,11 +181,18 @@ class Discord
var CategoryName = Config['discord']['CategoryName'];
var ChannelName = Config['discord'][Channel]['name'];
var Category = Discord.Client.channels.cache.find(ch => ch.type == "category" && ch.name.toLowerCase() == CategoryName.toLowerCase())
if (Category == null)
{
console.log(`Could not find category "${CategoryName}"`);
Log.Write(`Could not find category "${CategoryName}"`, true);
return;
}
var ChannelbyName = Category.children.find(ch => ch.name.toLowerCase() == ChannelName.toLowerCase());
if (ChannelbyName != null)
{
console.log(Message);
Log.Write(Message);
Log.Write(Message, true);
if (Config['discord']['embedMessages'])
ChannelbyName.send({
embed: {
Expand All @@ -186,7 +210,8 @@ class Discord
}
}
catch(error) {
console.log("Error finding discord channel.")
console.log("Error sending discord message.");
Log.Write("Error sending discord message.", true);
}
}

Expand Down
23 changes: 18 additions & 5 deletions modules/Log.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require("path");
const dateFormat = require("dateformat");
const dedent = require("dedent");

const consoleLog = path.join(__dirname, "..", "Console.log");
class Log {
static logPath;
static Initialize() {
Expand All @@ -28,10 +29,22 @@ class Log {
if (err) console.log(err);
});
}
static Write(message) {
fs.appendFile(this.logPath, `[${dateFormat(new Date(), "HH:mm:ss")}] ${message}\n`, (err) => {
if (err) console.log(err);
});
}
static Write(message, console = false) {
if (console == false) {
fs.appendFile(this.logPath, `[${dateFormat(new Date(), "HH:mm:ss")}] ${message}\n`, (err) => {
if (err) console.log(err);
});
}
else {
fs.appendFile(this.logPath, `[${dateFormat(new Date(), "HH:mm:ss")}] ${message}\n`, (err) => {
if (err) console.log(err);
});

//Console
fs.appendFile(consoleLog, `[${dateFormat(new Date(), "HH:mm:ss")}] ${message}\n`, (err) => {
if (err) console.log(err);
});
}
}
}
module.exports = Log;
49 changes: 49 additions & 0 deletions public/console.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<!DOCTYPE html>
<html>
<head>
<title>Console</title>
<link rel="stylesheet" href="./bootstrap/bootstrap.min.css">
<style>
.margin {
margin: 10px;
}
.txt-area {
height: 84vh !important;
width: 100%;
position: static;
margin-top: 15px;
resize: none;
background-color: white !important;
}

</style>
<script>
const { ipcRenderer } = require("electron");
const path = require("path");
const fs = require("fs");
const consoleLog = path.join(__dirname, "..", "Console.log");

function updateConsole() {
var consoleWindow = document.getElementById('consoleWindow');
fs.readFile(consoleLog, 'utf8', function(err, data) {
if (err) console.log(err);
if (data) {
consoleWindow.innerHTML = data;
consoleWindow.scrollTop = consoleWindow.scrollHeight
}
});
}

onload = function () {
updateConsole();
fs.watchFile(consoleLog, updateConsole);
}
</script>
</head>
<body>
<div class="margin">
<h5>Console</h5>
<textarea class="txt-area form-control" readonly id="consoleWindow"></textarea>
</div>
</body>
</html>
37 changes: 34 additions & 3 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
const { BrowserWindow } = require('electron').remote
const path = require('path')
var SettingsOpen = false;
var ConsoleOpen = false;
onload = function() {
var btn = document.getElementById("btn");
btn.addEventListener("click", function(event) {
var settingsBtn = document.getElementById("settingsBtn");
var consoleBtn = document.getElementById('consoleBtn');
settingsBtn.addEventListener("click", function(event) {
if (SettingsOpen) { Settings.show(); return; }
let Settings = new BrowserWindow({
width: 450,
Expand All @@ -56,13 +58,42 @@
Settings.on('ready-to-show', () => { Settings.show() });

});

consoleBtn.addEventListener("click", function(event) {
if (ConsoleOpen) { ConsoleOpen.show(); return; }
let Console = new BrowserWindow({
width: 450,
height: 430,
minWidth: 450,
minHeight: 430,
webPreferences: {
nodeIntegration: true
},
show: false
});
Console.setMenu(null);
Console.setIcon(path.join(__dirname, "Logo256.png"));
Console.loadFile(path.join(__dirname, "console.html"));
ConsoleOpen = true;

Console.on('close', function(event) {
ConsoleOpen = false;
});
Console.on('ready-to-show', () => { Console.show() });

});


}
</script>
</head>
<body>
<div class = "nav">
<button id = "btn">
<button id = "settingsBtn">
Settings
</button>
<button id = "consoleBtn" style="float: right;">
Console
</button>
</div>
<div class="flashContainer">
Expand Down

0 comments on commit 88ba441

Please sign in to comment.