Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some improvements #14

Open
wants to merge 4 commits into
base: v1.0.0-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"devDependencies": {
"aoi.js": "^6.8.5",
"node": "^17.9.1",
"tailwindcss": "^3.4.3"
},
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion src/api/api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const util = require("../utilFuncs.js");
const util = require("../utils.js");
function loadAPI(data,params){
function checkAuth(req, res, next, perms) {
const auth = params.auth;
Expand Down
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { Panel } = require("./panel.js");
const { AccountsPermissions } = require('./utils.js');

module.exports = {
Panel: Panel
Panel,
AccountsPermissions
}
172 changes: 111 additions & 61 deletions src/panel.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,119 @@
const util = require("./utilFuncs.js");
const bodyParser = require('body-parser');
const { AoiError } = require("aoi.js");

const c_version = require('../package.json').version;
const { PanelLogs } = require("./panelLogs.js");
const {
checkAccounts,
checkVersion,
checkPackage,
checkAuth,
checkPanel,
getAllDirs
} = require("./utils.js");
const api = require('./api/api.js');
const gui = require("./gui/gui.js");
const { PanelLogs } = require("./panelLogs.js");

const { AoiError } = require("aoi.js");


class Panel {
constructor(params) {
this.params = params;
util.checkVersion();
util.checkPackage();
util.checkParams(params);
const accounts = require(process.cwd()+params.accounts);
this.accounts = accounts;
var logs = new PanelLogs(params);
this.logs = logs;
params.client.awaitedCommand({
name: "panelawaitedcommand",
code: `$cacheMembers[$guildID;false]$suppressErrors`
});
params.client.readyCommand({
channel: "$randomChannelID",
code: `$forEachGuild[1s;{};panelawaitedcommand;]$suppressErrors`
})
setTimeout(()=>{util.checkPanel(this)},10000)



}
loadPanel(){
const params = this.params;
const app = require("express")();
this.app = app;

const thirtyDays = 1000 * 60 * 60 * 24 * 30;

app.use(require("express").json()) // <==== parse request body as JSON
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(require("express-session")({
secret: require('crypto').randomBytes(16).toString("hex"),
cookie: { maxAge: thirtyDays },
resave: true,
saveUninitialized: true
}))

app.listen(params.port,function(){

AoiError.createConsoleMessage(
[],"green",{
text: "panel running on port " + params.port,
textColor: "white"
}
);

});
api.loadAPI(this,params,util.checkAuth);
gui.loadGUI(this,params);
}
#options;

constructor(options) {
this.#options = this.#resolveOptions(options);

checkVersion();
checkPackage();

this.logs = new PanelLogs(this.#options);

this.#initCommands();

setTimeout(() => {
checkPanel(this);
}, 10000);
}

// TODO: Rename it to "options" when replace all panel "params" calls
get params() {
return this.#options;
}


module.exports = {
Panel
}

#resolveOptions(options) {
if (!options.client) throw new Error("Please provide a valid aoi.js client");
if (!options.port) throw new Error("Please provide a valid port");
if (!options.accounts) throw new Error("Please provide a valid accounts file path");

this.accounts = require(process.cwd() + options.accounts);
checkAccounts(this.accounts);

AoiError.createConsoleMessage(
[
{
text: `Installed version: ${c_version}`,
textColor: "green",
},
{
text: `Accounts Made for aoi.panel: ${this.accounts.length}`,
textColor: "blue",
},
],
"white",
{
text: "@aoijs/aoi.panel ",
textColor: "cyan",
}
);

return options;
}

#initCommands() {
const client = this.#options.client;
client.awaitedCommand({
name: "panelawaitedcommand",
code: `$cacheMembers[$guildID;false]$suppressErrors`
});

client.readyCommand({
channel: "$randomChannelID",
code: `$forEachGuild[1s;{};panelawaitedcommand;]$suppressErrors`
});
}

loadPanel() {
this.app = this.#initApp();
this.app.listen(this.#options.port, () =>
AoiError.createConsoleMessage(
[], "green", {
text: "panel running on port " + this.#options.port,
textColor: "white"
}
)
);

api.loadAPI(this, this.#options, checkAuth);
gui.loadGUI(this, this.#options);
}

#initApp() {
const app = require("express")();
const thirtyDays = 1000 * 60 * 60 * 24 * 30;

app.use(require("express").json())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json())
app.use(require("express-session")({
secret: require('crypto').randomBytes(16).toString("hex"),
cookie: { maxAge: thirtyDays },
resave: true,
saveUninitialized: true
}))

return app;
}
};

module.exports = {
Panel
};
107 changes: 50 additions & 57 deletions src/panelLogs.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,55 @@
const fs = require('fs');
const { format } = require('date-fns');
const path = require('path');

class PanelLogs {

constructor(data){
this.data = data;
this.folder = __dirname+"/logs/panelLogs";

if(!fs.existsSync(this.folder)){
fs.mkdirSync(this.folder, {recursive: true});
}

}
newLog(logdata,type,ogcode,code){

const date = Date.now();
var json = JSON.parse(fs.readFileSync(this.folder+"/logs.json").toString());
if(type!="edit"){
let log = {
"date":date,
"data":logdata
}
json.push(log);
}
else{
let log = {
"date":date,
"data":logdata,
"type":"edit",
"ogcode":ogcode,
"code":code
}
json.push(log);
}
//console.log(log);
fs.writeFileSync(this.folder+"/logs.json",JSON.stringify(json));

}
getLogs(){
let jjs = JSON.parse(fs.readFileSync(this.folder+"/logs.json").toString());
return jjs;
}
resetLogs(usr){
const date = Date.now();
let json = []
let log = {
"date":date,
"data":`${usr} cleared the logs.`
}
json.push(log);

fs.writeFileSync(this.folder+"/logs.json",JSON.stringify(json));
}



}
constructor(data) {
this.data = data;
this.folderPath = path.join(__dirname, '/logs/panel');
this.filePath = path.join(this.folderPath, 'logs.json');

const oldFolderPath = path.join(__dirname, '/logs/panelLogs');
if (fs.existsSync(oldFolderPath)) {
fs.renameSync(oldFolderPath, this.folderPath);
} else if (!fs.existsSync(this.folderPath)) {
fs.mkdirSync(this.folderPath, { recursive: true });
};
}

newLog(data, type, ogcode, code) {
const logs = JSON.parse(fs.readFileSync(this.filePath));
const newLog = {
date: Date.now(),
data,
...(type === 'edit' ? {
type,
ogcode,
code
} : {})
};

logs.push(newLog);
fs.writeFileSync(this.filePath, JSON.stringify(logs));
}

getLogs() {
if (!fs.existsSync(this.filePath)) {
fs.writeFileSync(this.filePath, JSON.stringify([]));
return [];
};

return JSON.parse(fs.readFileSync(this.filePath));
}

resetLogs(username) {
const newLogs = [{
date: Date.now(),
data: `${username} cleared the logs.`
}];

fs.writeFileSync(this.filePath, JSON.stringify(newLogs));
}
};

module.exports = {
PanelLogs
}
PanelLogs
};
Loading