Skip to content

Instantly share code, notes, and snippets.

@Corex24
Forked from weskerty/sysinfo.js
Last active February 20, 2025 10:05
Show Gist options
  • Select an option

  • Save Corex24/08af4299048a7eee9f8962c8b15e34b1 to your computer and use it in GitHub Desktop.

Select an option

Save Corex24/08af4299048a7eee9f8962c8b15e34b1 to your computer and use it in GitHub Desktop.
View System Info ant Tools Version
const { bot } = require('../lib');
const os = require('os');
const { exec } = require('child_process');
const fs = require('fs').promises;
const util = require('util');
const path = require('path');
const https = require('https');
const execAsync = util.promisify(exec);
class FastFetchDownloader {
constructor() {
// paths
this.config = {
binPath: path.join(process.cwd(), 'media', 'bin'),
};
// Ssitema y Descargas // No Probado.
this.fastfetchBinaries = new Map([
['linux-x64', {
url: 'https://github.com/fastfetch-cli/fastfetch/releases/download/2.35.0/fastfetch-linux-amd64.tar.gz',
relativePath: 'fastfetch-linux-amd64/usr/bin/fastfetch',
}],
['linux-arm64', {
url: 'https://github.com/fastfetch-cli/fastfetch/releases/download/2.35.0/fastfetch-linux-aarch64.tar.gz',
relativePath: 'fastfetch-linux-aarch64/usr/bin/fastfetch',
}],
['win32-x64', {
url: 'https://github.com/fastfetch-cli/fastfetch/releases/download/2.35.0/fastfetch-windows-amd64.zip',
relativePath: 'fastfetch-windows-amd64/fastfetch.exe',
}],
]);
}
async ensureDirectories() {
await fs.mkdir(this.config.binPath, { recursive: true });
}
getPlatformInfo() {
let platform = os.platform();
let arch = os.arch();
if (platform === 'android') {
platform = 'android';
arch = arch === 'arm64' ? 'arm64' : 'x64';
} else if (platform === 'linux') {
arch = (arch === 'arm64' || arch === 'aarch64') ? 'arm64' : 'x64';
} else if (platform === 'win32') {
arch = 'x64';
}
return { platform, arch };
}
// Instalar Fastfetch con apt, ya que el precompilado me falla.
async tryInstallFromPackageManager() {
const { platform } = this.getPlatformInfo();
try {
if (platform === 'android') {
console.log('Trying to install fastfetch using pkg...');
await execAsync('pkg update -y');
await execAsync('pkg install fastfetch -y');
return true;
} else if (platform === 'linux') {
console.log('Trying to install fastfetch using apt...');
await execAsync('sudo apt update');
await execAsync('sudo apt install fastfetch -y');
return true;
}
} catch (error) {
console.log('Package manager installation failed:', error.message);
return false;
}
return false;
}
// verificar fastfetch
async isFastFetchAvailable() {
try {
const { stdout } = await execAsync('which fastfetch');
if (stdout.trim()) {
return true;
}
} catch {}
return false;
}
// descarga y extraccion del precompilado
async downloadAndExtractFastFetch() {
const { platform, arch } = this.getPlatformInfo();
const key = `${platform === 'android' ? 'linux' : platform}-${arch}`;
const binary = this.fastfetchBinaries.get(key);
if (!binary) {
throw new Error(`Unsupported System: ${key}`);
}
await this.ensureDirectories();
const downloadPath = path.join(this.config.binPath, path.basename(binary.url));
const extractPath = this.config.binPath;
try {
// descarga
console.log('Downloading fastfetch...');
await execAsync(`curl -fsSL -o "${downloadPath}" "${binary.url}"`);
// comando de extraccion
console.log('Extracting fastfetch...');
if (platform === 'win32') {
await execAsync(`powershell -Command "Expand-Archive -Path '${downloadPath}' -DestinationPath '${extractPath}' -Force"`);
} else {
await execAsync(`tar xf "${downloadPath}" -C "${extractPath}"`);
}
// permisos +x para permitir ejecucion
const binaryPath = path.join(this.config.binPath, binary.relativePath);
if (platform !== 'win32') {
await fs.chmod(binaryPath, '755');
}
// borrar comprimido
await fs.unlink(downloadPath);
return binaryPath;
} catch (error) {
console.error('Error during download/extract:', error);
throw error;
}
}
async getFastFetchPath() {
if (await this.isFastFetchAvailable()) {
console.log('Found system-wide fastfetch installation');
return 'fastfetch';
}
if (await this.tryInstallFromPackageManager()) {
console.log('Successfully installed fastfetch from package manager');
return 'fastfetch';
}
const { platform, arch } = this.getPlatformInfo();
const key = `${platform === 'android' ? 'linux' : platform}-${arch}`;
const binary = this.fastfetchBinaries.get(key);
const localBinaryPath = path.join(this.config.binPath, binary.relativePath);
try {
await fs.access(localBinaryPath);
console.log('Using existing local fastfetch binary');
return localBinaryPath;
} catch {
console.log('Downloading and extracting fastfetch...');
return await this.downloadAndExtractFastFetch();
}
}
}
async function runSpeedtest(message) {
try {
const tempDir = process.env.TEMP_DOWNLOAD_DIR || path.join(process.cwd(), 'media');
const speedtestPath = path.join(tempDir, 'bin', 'ookla-speedtest.py');
await fs.mkdir(path.dirname(speedtestPath), { recursive: true });
try {
await fs.access(speedtestPath);
} catch {
const speedtestUrl = 'https://raw.githubusercontent.com/weskerty/MysticTools/refs/heads/main/Utilidades/ookla-speedtest.py';
await execAsync(`curl -fsSL -o "${speedtestPath}" "${speedtestUrl}"`);
await execAsync(`chmod +x ${speedtestPath}`);
}
let stdout;
try {
const result = await execAsync(`python3 ${speedtestPath} --secure --share`);
stdout = result.stdout.trim();
} catch (error) {
const result = await execAsync(`python ${speedtestPath} --secure --share`);
stdout = result.stdout.trim();
}
const imageUrlMatch = stdout.match(/http[^"]+\.png/);
if (imageUrlMatch) {
const imageUrl = imageUrlMatch[0];
const fetch = (await import('node-fetch')).default;
const response = await fetch(imageUrl);
const imageBuffer = Buffer.from(await response.arrayBuffer());
await message.send(
imageBuffer,
{
fileName: 'speedtest_result.png',
mimetype: 'image/png',
caption: stdout
},
'image'
);
} else {
// por si falle la img
await message.send(stdout);
}
return stdout;
} catch (error) {
console.error('Speedtest error:', error);
return '❌ Error in speedtest';
}
}
bot(
{
pattern: 'sysinfo ?(.*)',
fromMe: true,
desc: 'All System Server Info',
type: 'machine',
},
async (message) => {
try {
const fastFetchDownloader = new FastFetchDownloader();
const fastFetchPath = await fastFetchDownloader.getFastFetchPath();
console.log('Using fastfetch from:', fastFetchPath);
// -l none sin formato, -c all todos los plugins incluido ip local, remover -c all si asi lo prefieres.
const { stdout: sysInfo } = await execAsync(`"${fastFetchPath}" -l none -c all`);
await message.send(sysInfo);
await runSpeedtest(message);
} catch (error) {
console.error('SYSINFO error:', error);
await message.send(`❌ Error getting system information: ${error.message}`);
}
}
);
module.exports = { FastFetchDownloader };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment