-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move functions to files, clean and refactor code, update readme
- Loading branch information
1 parent
2d2f8e0
commit 011083f
Showing
12 changed files
with
346 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Copy this file to .env and fill in the values. | ||
|
||
CHANNEL_ID="Discord channel ID" | ||
OWNERS_IDS="ID 1,ID 2,ID 3" | ||
TOKEN="Discord bot token" | ||
CUSTOM_CWD="Default path to the bot's working directory (optional - remove this line if you don't need it)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
export const defaultConfig = { | ||
channel: process.env.CHANNEL_ID, | ||
token: process.env.TOKEN, | ||
embedColor: "#5865f2", | ||
channel: process.env.CHANNEL_ID, // Channel ID | ||
token: process.env.TOKEN, // Discord bot token | ||
owners: [...process.env.OWNERS_IDS.split(",")], // Array of owners IDs (separated by commas) | ||
embedColor: "#5865f2", // Discord's blurple | ||
emojis: { | ||
loading: "<a:loading:895227261752582154>", | ||
loading: "<a:loading:895227261752582154>", // https://cdn.discordapp.com/emojis/895227261752582154.gif?v=1 | ||
output: "📤", | ||
error: "❌", | ||
change: "↪️", | ||
}, | ||
owners: [...process.env.OWNERS_IDS.split(",")], | ||
debugger: { | ||
changeDir: true, // Displays the directory change in the terminal | ||
showCommand: true, // Displays the command run in the terminal | ||
moritoringUpdates: false, // Displays the monitoring updates in the terminal (every 5 seconds) | ||
displayEventList: false, // Displays the event list in the terminal | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ActivityType } from "discord.js"; | ||
import { defaultConfig } from "../../config.js"; | ||
import { logger } from "../../utils/logger.js"; | ||
|
||
/** | ||
* Handles the ready event. | ||
* | ||
* @param {object} client The Discord client. | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function ready(client) { | ||
try { | ||
defaultConfig.channel = client.channels.cache.get(defaultConfig.channel); | ||
if (!defaultConfig.channel) return logger("error", "Channel not found! Please check your CHANNEL_ID .env variable."); | ||
|
||
if (!(await defaultConfig.channel.fetchWebhooks()).size) { | ||
await defaultConfig.channel.createWebhook({ | ||
name: "SSH", | ||
avatar: client.user.displayAvatarURL(), | ||
reason: "SSH Webhook", | ||
}); | ||
} | ||
|
||
logger("ready", `Logged in as ${client.user.tag}! (ID: ${client.user.id})`); | ||
client.user.setActivity("all ports!", { type: ActivityType.Watching }); | ||
} catch (error) { | ||
logger("error", error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import { EmbedBuilder } from "discord.js"; | ||
import { defaultConfig } from "../../config.js"; | ||
import { execCommand } from "../../utils/execCommand.js"; | ||
import { logger } from "../../utils/logger.js"; | ||
|
||
/** | ||
* Creates an embed. | ||
* | ||
* @param {string} description The embed description. | ||
* @param {string} color The embed color. | ||
* @returns {EmbedBuilder} The embed. | ||
*/ | ||
function createEmbed(description, color) { | ||
return new EmbedBuilder() // prettier | ||
.setDescription(description) | ||
.setColor(color); | ||
} | ||
|
||
/** | ||
* Handles the messageCreate event. | ||
* | ||
* @param {object} client The Discord client. | ||
* @param {object} message The message object. | ||
* @returns {Promise<void>} | ||
*/ | ||
export async function messageCreate(client, message) { | ||
try { | ||
if (message.author.bot) return; | ||
if (message.channel !== defaultConfig.channel && !defaultConfig.owners.includes(message.author.id)) return; | ||
if (!message.content) return; | ||
|
||
const [command, ...args] = message.content.split(" "); | ||
|
||
if (command === "cd") { | ||
const newCWD = args.join(" "); | ||
if (!newCWD) return; | ||
|
||
const resolvedPath = path.resolve(client.customCWD, newCWD); | ||
if (!fs.existsSync(resolvedPath)) { | ||
const error = createEmbed(`${defaultConfig.emojis.error} **Directory does not exist**`, defaultConfig.embedColor); | ||
return message.reply({ embeds: [error] }); | ||
} | ||
|
||
try { | ||
process.chdir(resolvedPath); | ||
defaultConfig.debugger.changeDir && logger("event", `Changed directory from ${client.customCWD} to ${resolvedPath}`); | ||
|
||
const changedDirectory = createEmbed(`${defaultConfig.emojis.change} **Changed directory from \`${client.customCWD}\` to \`${resolvedPath}\`**`, defaultConfig.embedColor); | ||
|
||
client.customCWD = resolvedPath; | ||
return message.reply({ embeds: [changedDirectory] }); | ||
} catch (err) { | ||
defaultConfig.debugger.changeDir && logger("error", err); | ||
const error = createEmbed(`${defaultConfig.emojis.error} **${err.message}**`, defaultConfig.embedColor); | ||
|
||
return message.reply({ embeds: [error] }); | ||
} | ||
} | ||
|
||
const wait = createEmbed(`${defaultConfig.emojis.loading} **Waiting for server response...**`, defaultConfig.embedColor); | ||
|
||
await message.reply({ embeds: [wait] }); | ||
|
||
await execCommand(client, message.content); | ||
} catch (error) { | ||
logger("error", error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,150 +1,38 @@ | ||
import "dotenv/config"; | ||
import { spawn } from "node:child_process"; | ||
import { EventEmitter } from "node:events"; | ||
import path from "node:path"; | ||
import chalk from "chalk"; | ||
import { EmbedBuilder, Client, GatewayIntentBits, Events, ActivityType, codeBlock } from "discord.js"; | ||
import stripAnsi from "strip-ansi"; | ||
import { cpuTemperature as checkCpuTemperature, currentLoad, mem } from "systeminformation"; | ||
import { defaultConfig } from "./config.js"; | ||
console.log(chalk.cyan(chalk.bold("[DISCORD] > Starting SSH..."))); | ||
|
||
const client = new Client({ | ||
allowedMentions: { | ||
parse: ["users", "roles"], | ||
repliedUser: false, | ||
}, | ||
intents: GatewayIntentBits.Guilds | GatewayIntentBits.GuildMembers | GatewayIntentBits.GuildPresences | GatewayIntentBits.GuildMessages | GatewayIntentBits.MessageContent, | ||
}); | ||
|
||
let customCWD = process.env.CUSTOM_CWD || process.cwd(); | ||
|
||
let monitoringData = { | ||
cpuTemperature: 0, | ||
cpuUsage: 0, | ||
memoryPercentage: 0, | ||
}; | ||
|
||
EventEmitter.prototype._maxListeners = 100; | ||
|
||
const chunkString = (str, n, acc) => { | ||
if (str.length === 0) return acc; | ||
|
||
acc.push(str.substring(0, n)); | ||
return chunkString(str.substring(n), n, acc); | ||
}; | ||
|
||
setInterval(async () => { | ||
const cpuTemp = await checkCpuTemperature(); | ||
const cpuUsageTest = await currentLoad(); | ||
const memory = await mem(); | ||
|
||
monitoringData = { | ||
cpuTemperature: cpuTemp.main, | ||
cpuUsage: cpuUsageTest.currentLoad.toFixed(2), | ||
memoryPercentage: ((memory.used / 1048576 / (memory.total / 1048576)) * 100).toFixed(2), | ||
}; | ||
}, 5000); | ||
|
||
async function exec(input) { | ||
let output = ""; | ||
|
||
const args = input.split(" "); | ||
const command = args.shift(); | ||
|
||
const cmd = spawn(`${command}`, args, { | ||
shell: true, | ||
env: { COLUMNS: 128 }, | ||
cwd: customCWD, | ||
import { Client, GatewayIntentBits } from "discord.js"; | ||
import loadEvents from "./utils/loadEvents.js"; | ||
import { logger } from "./utils/logger.js"; | ||
|
||
logger("event", "Starting SSH Bot session..."); | ||
logger("info", `Running version v${process.env.npm_package_version} on Node.js ${process.version} on ${process.platform} ${process.arch}`); | ||
logger("info", "Check out the source code at /~https://github.com/igorkowalczyk/discord-ssh!"); | ||
logger("info", "Don't forget to star the repository, it helps a lot!"); | ||
|
||
try { | ||
const client = new Client({ | ||
allowedMentions: { | ||
parse: ["users", "roles"], | ||
repliedUser: false, | ||
}, | ||
intents: GatewayIntentBits.Guilds | GatewayIntentBits.GuildMembers | GatewayIntentBits.GuildPresences | GatewayIntentBits.GuildMessages | GatewayIntentBits.MessageContent, | ||
}); | ||
|
||
cmd.stdout.on("data", (data) => (output += data)); | ||
cmd.stderr.on("data", (data) => (output += data)); | ||
|
||
cmd.on("exit", async () => { | ||
const outputDiscord = chunkString(output || "", 3000, []); | ||
client.customCWD = process.env.CUSTOM_CWD || process.cwd(); | ||
|
||
const embed = new EmbedBuilder() // prettier | ||
.setColor("#4f545c") | ||
.setTitle(`${defaultConfig.emojis.output} Output`) | ||
.setTimestamp(); | ||
logger("info", "Loading events..."); | ||
await loadEvents(client); | ||
|
||
outputDiscord.map((item, index) => { | ||
const index2 = index + 1; | ||
embed.setFooter({ text: `Page ${index2}/${outputDiscord.length}`, icon: client.user.displayAvatarURL() }); | ||
embed.setDescription(codeBlock(stripAnsi(item, true) || "No output!")); | ||
|
||
if (index2 == outputDiscord.length) embed.setDescription(embed.data.description + `\n${codeBlock(`CWD: ${customCWD}\nCPU: ${monitoringData.cpuUsage}% | RAM: ${monitoringData.memoryPercentage}% | Temp: ${monitoringData.cpuTemperature}°C`)}`); | ||
|
||
const finalMessage = defaultConfig.channel.messages.cache.first(); | ||
if (index2 !== 1) defaultConfig.channel.send({ embeds: [embed] }); | ||
|
||
finalMessage.reply({ embeds: [embed] }); | ||
}); | ||
}); | ||
logger("info", "Logging in..."); | ||
await client.login(process.env.TOKEN); | ||
} catch (error) { | ||
logger("error", error); | ||
process.exit(1); | ||
} | ||
|
||
client.on(Events.MessageCreate, async (message) => { | ||
if (message.author.bot) return; | ||
if (message.channel !== defaultConfig.channel && !defaultConfig.owners.includes(message.author.id)) return; | ||
if (!message.content) return; | ||
|
||
if (message.content.startsWith("cd")) { | ||
const newCWD = message.content.split(" ")[1]; | ||
if (!newCWD) return; | ||
|
||
try { | ||
process.chdir(path.resolve(customCWD, newCWD)); | ||
|
||
const changedDirectory = new EmbedBuilder() // prettier | ||
.setDescription(`${defaultConfig.emojis.change} **Changed directory from \`${customCWD}\` to \`${path.resolve(customCWD, newCWD)}\`**`) | ||
.setColor(defaultConfig.embedColor); | ||
|
||
customCWD = path.resolve(customCWD, newCWD); | ||
return message.reply({ embeds: [changedDirectory] }); | ||
} catch (err) { | ||
const error = new EmbedBuilder() // prettier | ||
.setDescription(`${defaultConfig.emojis.error} **${err.message}**`) | ||
.setColor(defaultConfig.embedColor); | ||
|
||
return message.reply({ embeds: [error] }); | ||
} | ||
} | ||
|
||
const wait = new EmbedBuilder() // prettier | ||
.setDescription(`${defaultConfig.emojis.loading} **Waiting for server response...**`) | ||
.setColor(defaultConfig.embedColor); | ||
|
||
await message.reply({ embeds: [wait] }); | ||
|
||
await exec(message.content); | ||
}); | ||
|
||
client.once(Events.ClientReady, async () => { | ||
defaultConfig.channel = client.channels.cache.get(defaultConfig.channel); | ||
if (!defaultConfig.channel) throw new Error("Invalid CHANNEL_ID in .env!"); | ||
|
||
if (!(await defaultConfig.channel.fetchWebhooks()).size) { | ||
await defaultConfig.channel.createWebhook({ | ||
name: "SSH", | ||
avatar: client.user.displayAvatarURL(), | ||
reason: "SSH Webhook", | ||
}); | ||
} | ||
console.log(chalk.cyan(chalk.bold(`[DISCORD] > Logged in as ${client.user.tag}`))); | ||
client.user.setActivity("all ports!", { type: ActivityType.Watching }); | ||
}); | ||
|
||
process.stdin.on("data", (data) => exec(data.toString(), { terminal: true })); | ||
|
||
client.login(process.env.TOKEN).catch((error) => { | ||
throw new Error(error); | ||
}); | ||
|
||
process.on("unhandledRejection", async (reason) => { | ||
return console.log(chalk.red(chalk.bold(`[ERROR] > Unhandled Rejection: ${reason}`))); | ||
return logger("error", reason); | ||
}); | ||
|
||
process.on("uncaughtException", async (err) => { | ||
return console.log(chalk.red(chalk.bold(`[ERROR] > Uncaught Exception: ${err}`))); | ||
return logger("error", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.