Refreactor to add a command handler

This commit is contained in:
raven
2023-01-11 15:48:57 -05:00
parent da9f17117a
commit aceead9064
12 changed files with 343 additions and 292 deletions

15
commands/AI.js Normal file
View File

@ -0,0 +1,15 @@
// AI.js
var unirest = require('unirest');
async function AIRequest(prompt) {
console.log("Please wait while the AI thinks about this....\n")
let AIRequest = await unirest
.post('https://codex-ai-v9q6.onrender.com/')
.headers({ 'Accept': 'application/json', 'Content-Type': 'application/json' })
.send({ "prompt": prompt })
return AIRequest.body.bot
}
module.exports = { AIRequest }

14
commands/cd.js Normal file
View File

@ -0,0 +1,14 @@
async function changeDir(commandArgs, USERPWD) {
let newDir;
if (commandArgs.startsWith("/")) {
newDir = commandArgs;
console.log("Directory Changed to: " + newDir)
} else {
newDir = USERPWD + "/" + commandArgs;
console.log("Directory Changed to: " + newDir)
}
return newDir;
}
module.exports = { changeDir }

20
commands/exec.js Normal file
View File

@ -0,0 +1,20 @@
// exec.js
const unirest = require("unirest");
async function runCMD(key, cmd, pwd) {
let requestData = await unirest
.post('https://api.discord-linux.com/exec')
.headers({
'Accept': 'application/json', 'Content-Type': 'application/json',
'x-discord-linux-auth': key
})
.send({ "cmd": cmd, "pwd": pwd })
return requestData.body.stdout
}
async function execute(key, cmd, pwd) {
const stdout = await runCMD(key, cmd, pwd)
console.log(stdout)
}
module.exports = { execute, runCMD }

33
commands/login.js Normal file
View File

@ -0,0 +1,33 @@
var unirest = require('unirest');
async function login(key, conns, MYKEY) {
getUSERNAME(key)
.then((data) => {
process.stdout.write("\033[2J")
process.stdout.write("\033[0f")
console.log("Hello, " + data + "\nYou are now logged in.\n\n\n")
USERNAME = data
MYKEY.push(key)
LOGGEDIN = true
for (const conn of conns) {
conn.write(`${USERNAME} is now logged in.`)
}
})
.catch(err => {
console.log("Invalid Key")
for (const conn of conns) {
conn.write("Invalid Key, please try again.")
}
})
}
// API Functions
async function getUSERNAME(key) {
let requestUSERNAME = await unirest
.get('https://api.discord-linux.com/hello')
.headers({ 'Accept': 'application/json', 'Content-Type': 'application/json', 'x-discord-linux-auth': key })
return requestUSERNAME.body.message.replace("Hello, ", "").replace("!", "")
}
module.exports = { login }

20
commands/restart.js Normal file
View File

@ -0,0 +1,20 @@
// restart.js
const unirest = require("unirest");
async function restart(key) {
async function restartContainer(key) {
let restartContainer = await unirest
.get('https://api.discord-linux.com/restart')
.headers({
'Accept': 'application/json', 'Content-Type': 'application/json',
'x-discord-linux-auth': key
})
return restartContainer.body
}
const response = await restartContainer(key)
console.log(response)
}
module.exports = { restart }

19
commands/start.js Normal file
View File

@ -0,0 +1,19 @@
// start.js
const unirest = require("unirest");
async function startContainer(key) {
let startContainer = await unirest
.get('https://api.discord-linux.com/start')
.headers({
'Accept': 'application/json', 'Content-Type': 'application/json',
'x-discord-linux-auth': key
})
return startContainer.body
}
async function start(key) {
const response = await startContainer(key)
console.log(response)
}
module.exports = { start, startContainer }

19
commands/stats.js Normal file
View File

@ -0,0 +1,19 @@
// stats.js
const unirest = require("unirest");
async function getStats(key) {
let requestData = await unirest
.get('https://api.discord-linux.com/stats')
.headers({
'Accept': 'application/json', 'Content-Type': 'application/json',
'x-discord-linux-auth': key
})
return requestData.body
}
async function stats(key) {
const response = await getStats(key)
console.log(response)
}
module.exports = { stats, getStats }

19
commands/stop.js Normal file
View File

@ -0,0 +1,19 @@
// start.js
const unirest = require("unirest");
async function stopContainer(key) {
let stopContainer = await unirest
.get('https://api.discord-linux.com/stop')
.headers({
'Accept': 'application/json', 'Content-Type': 'application/json',
'x-discord-linux-auth': key
})
return stopContainer.body
}
async function stop(key) {
const response = await stopContainer(key)
console.log(response)
}
module.exports = { stop, stopContainer }