Files
gestion/commands/utils/encode.js
T
2024-03-01 15:55:17 +01:00

67 lines
2.0 KiB
JavaScript

module.exports = {
name: 'encode',
description: 'Encoder ou décoder un language chiffré',
utilisation: '<base64|binaire> <encode|decode> <texte>',
emote: "🔐",
category: "utils",
async execute(message, args) {
if (args.length < 3) {
return message.reply('Usage: `encode <base64|binaire> <encode|decode> <texte>`');
}
const subcommand = args[0];
const action = args[1];
const text = args.slice(2).join(' ');
if (subcommand !== 'base64' && subcommand !== 'binaire') {
return message.reply('Invalid subcommand. Use `base64` or `binaire`.');
}
if (action !== 'encode' && action !== 'decode') {
return message.reply('Invalid action. Use `encode` or `decode`.');
}
if (subcommand === 'base64') {
let result;
if (action === 'encode') {
result = btoa(text);
} else {
result = atob(text);
}
return message.channel.send(`**Resultat:** \`${result}\``);
}
if (subcommand === 'binaire') {
let result;
if (action === 'encode') {
result = texteVersBinaire(text);
} else {
result = binaireVersTexte(text);
}
return message.channel.send(`**Resultat:** \`${result}\``);
}
},
};
function texteVersBinaire(texte) {
let binaire = '';
for (let i = 0; i < texte.length; i++) {
let charCode = texte.charCodeAt(i).toString(2);
charCode = '0'.repeat(8 - charCode.length) + charCode;
binaire += charCode + ' ';
}
return binaire.trim();
}
function binaireVersTexte(binaire) {
let texte = '';
const octets = binaire.split(' ');
for (let i = 0; i < octets.length; i++) {
const decimal = parseInt(octets[i], 2);
const caractere = String.fromCharCode(decimal);
texte += caractere;
}
return texte;
}