Added URL shortener

This commit is contained in:
Raven Scott 2022-09-24 14:19:45 -04:00
parent 0862fab3b7
commit fe217b04f5
1 changed files with 55 additions and 0 deletions

View File

@ -0,0 +1,55 @@
const { EmbedBuilder } = require('discord.js');
const fs = require('fs');
const { exec } = require("child_process");
module.exports = {
name: "short-url",
description: "Shortens a URL using small.ssh.surf",
options: [{
"name": "url",
"description": "The URL that you want shorter",
"required": true,
"type": 3 // 6 is type USER
}],
run: async (client, interaction) => {
let rand = Math.floor(Math.random() * 99999).toString();
let URL = interaction.options._hoistedOptions[0].value
var dir = '/var/www/html/short/' + rand;
const content = "<?php\nheader(\"Location: " + URL + "\")\n?>";
console.log(content)
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
console.log("Created: " + '/var/www/html/short/' + rand)
try {
fs.writeFileSync('/var/www/html/short/' + rand + "/index.php", content);
// file written successfully
exec("chown -R www-data:www-data /var/www/html/short/", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
const embed = new EmbedBuilder()
.setColor("#FF0000")
.setTitle("The URL has been shrunk!")
.setDescription("https://small.ssh.surf/" + rand)
.setTimestamp()
.setFooter({ text: `Requested by ${interaction.user.tag}`, iconURL: `${interaction.user.displayAvatarURL()}` });
interaction.followUp({ embeds: [embed] });
rand = Math.floor(Math.random() * 99999).toString();
});
} catch (err) {
console.error(err);
}
}
},
};