Add dpaste for overflow

This commit is contained in:
dlinux-host 2024-10-02 18:50:33 -04:00
parent 35e4c48991
commit 440fd75f13
2 changed files with 121 additions and 82 deletions

View File

@ -10,6 +10,7 @@
"license": "ISC", "license": "ISC",
"description": "", "description": "",
"dependencies": { "dependencies": {
"cmd-promise": "^1.2.0",
"discord.js": "^14.16.3", "discord.js": "^14.16.3",
"jsonfile": "^6.1.0", "jsonfile": "^6.1.0",
"mysql2": "^3.11.3", "mysql2": "^3.11.3",

View File

@ -4,7 +4,8 @@ import unirest from 'unirest';
import { readFileSync } from 'fs'; import { readFileSync } from 'fs';
import mysql from 'mysql2'; import mysql from 'mysql2';
const userWorkingDirectories = new Map(); const userWorkingDirectories = new Map();
import fs from 'fs';
import cmd from 'cmd-promise';
let sshSurfID; // Variable to store the user ID from the database let sshSurfID; // Variable to store the user ID from the database
// Paths to config and tokens files // Paths to config and tokens files
@ -316,7 +317,7 @@ client.on('interactionCreate', async interaction => {
case 'x': { case 'x': {
const command = interaction.options.getString('command'); const command = interaction.options.getString('command');
@ -380,7 +381,42 @@ case 'x': {
pwd: userPWD // Use the current directory or default to '/' pwd: userPWD // Use the current directory or default to '/'
}); });
// Format the command output in a markdown code block // Handle the command output
if (execResponse.stdout.length > 2020) {
// If the output is too large, save it to a temporary file and upload to a paste service
const tempFilePath = '/tmp/paste';
const pasteCommand = `Command: ${command} | Container Owner: ${interaction.user.username}\n${execResponse.stdout}`;
fs.writeFileSync(tempFilePath, pasteCommand, (err) => {
if (err) {
console.error(err);
return;
}
});
const pasteout = await cmd("sleep 2; cat /tmp/paste | dpaste");
// Create an embed with the paste link
const mainEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(`Container Owner: ${interaction.user.username}`)
.setDescription(`The command: ${command} was too large for Discord.`)
.addFields(
{
name: 'Please check the below output log:',
value: pasteout.stdout.replace("Pro tip: you can password protect your paste just by typing a username and password after your paste command.", "")
.replace("Paste Saved: ", "")
.replace("-------------------------------------------------------", "")
}
)
.setFooter({ text: `Requested by ${interaction.user.username}`, iconURL: `${interaction.user.displayAvatarURL()}` });
await interaction.editReply({ embeds: [mainEmbed] });
} else {
// If the output is small enough, format it in a markdown code block
let replyMessage = `\`\`\`\n${execResponse.stdout || 'No output'}\n\`\`\``; let replyMessage = `\`\`\`\n${execResponse.stdout || 'No output'}\n\`\`\``;
// If there is an error, append the error message in another markdown code block // If there is an error, append the error message in another markdown code block
@ -390,15 +426,17 @@ case 'x': {
// Reply with the formatted message // Reply with the formatted message
await interaction.editReply(replyMessage); await interaction.editReply(replyMessage);
}
break; break;
} }
// Helper function to remove directories when using '../' // Helper function to remove directories when using '../'
function RemoveLastDirectoryPartOf(the_url, num) { function RemoveLastDirectoryPartOf(the_url, num) {
var the_arr = the_url.split('/'); var the_arr = the_url.split('/');
the_arr.splice(-num, num); the_arr.splice(-num, num);
return the_arr.join('/') || '/'; return the_arr.join('/') || '/';
} }
case 'notify': case 'notify':