File size: 4,563 Bytes
046b271 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
const fs = require('fs');
const axios = require('axios');
const path = './config.env';
const FormData = require("form-data");
async function empiretourl(path) {
if (!fs.existsSync(path)) {
throw new Error(`File not found: ${path}`);
}
const form = new FormData();
const fileStream = fs.createReadStream(path);
form.append("file", fileStream);
const originalFileName = path.split("/").pop();
form.append("originalFileName", originalFileName);
try {
const response = await axios.post("https://cdn.empiretech.biz.id/api/upload.php", form, {
headers: {
...form.getHeaders(),
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
return response.data;
} catch (error) {
if (error.response) {
throw new Error(`API Error: ${error.response.status} - ${JSON.stringify(error.response.data)}`);
} else if (error.request) {
throw new Error("No response received from the server.");
} else {
throw new Error(`Request Error: ${error.message}`);
}
}
}
// Fetch a buffer from a URL
const getBuffer = async (url, options) => {
try {
options = options || {};
const res = await axios({
method: 'get',
url,
headers: {
'DNT': 1,
'Upgrade-Insecure-Request': 1
},
...options,
responseType: 'arraybuffer'
});
return res.data;
} catch (e) {
console.error(e);
return null;
}
};
// Get admin participants from a group
const getGroupAdmins = (participants) => {
const admins = [];
for (let participant of participants) {
if (participant.admin !== null) admins.push(participant.id);
}
return admins;
};
// Generate a random string with an extension
const getRandom = (ext) => {
return `${Math.floor(Math.random() * 10000)}${ext}`;
};
// Format large numbers with suffixes (e.g., K, M, B)
const h2k = (eco) => {
const lyrik = ['', 'K', 'M', 'B', 'T', 'P', 'E'];
const ma = Math.floor(Math.log10(Math.abs(eco)) / 3);
if (ma === 0) return eco.toString();
const scale = Math.pow(10, ma * 3);
const scaled = eco / scale;
const formatted = scaled.toFixed(1).replace(/\.0$/, '');
return formatted + lyrik[ma];
};
// Check if a string is a URL
const isUrl = (url) => {
return url.match(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%.+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%+.~#?&/=]*)/
);
};
// Convert a JavaScript object or array to a JSON string
const Json = (string) => {
return JSON.stringify(string, null, 2);
};
// Function to calculate and format uptime
const runtime = (seconds) => {
seconds = Math.floor(seconds);
const d = Math.floor(seconds / (24 * 60 * 60));
seconds %= 24 * 60 * 60;
const h = Math.floor(seconds / (60 * 60));
seconds %= 60 * 60;
const m = Math.floor(seconds / 60);
const s = Math.floor(seconds % 60);
if (d > 0) return `${d}d ${h}h ${m}m ${s}s`;
if (h > 0) return `${h}h ${m}m ${s}s`;
if (m > 0) return `${m}m ${s}s`;
return `${s}s`;
};
// Delay execution for a specified time
const sleep = async (ms) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};
// Fetch JSON from a URL
const fetchJson = async (url, options) => {
try {
options = options || {};
const res = await axios({
method: 'GET',
url: url,
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'
},
...options
});
return res.data;
} catch (err) {
console.error(err);
return null;
}
};
// Save config settings
const saveConfig = (key, value) => {
let configData = fs.existsSync(path) ? fs.readFileSync(path, 'utf8').split('\n') : [];
let found = false;
configData = configData.map(line => {
if (line.startsWith(`${key}=`)) {
found = true;
return `${key}=${value}`;
}
return line;
});
if (!found) configData.push(`${key}=${value}`);
fs.writeFileSync(path, configData.join('\n'), 'utf8');
// Reload updated environment variables
require('dotenv').config({ path });
};
module.exports = {
getBuffer,
getGroupAdmins,
getRandom,
h2k,
isUrl,
Json,
runtime,
sleep,
fetchJson,
saveConfig,
empiretourl
};
|