Ensure your slash command has 3 options! Option 0 is the text input where the user inputs the encoded or normal text to be encoded into. Option 1 is the cipher key (this type of cipher requires a key to be encoded or decoded) format this the same as option 0. Then option 2 will be a boolean value.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
function vigenereCipher(text, key, isDecode) {
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
let result = '';
let keyIndex = 0;
for (let i = 0; i < text.length; i++) {
const char = text[i];
const isUpperCase = char === char.toUpperCase();
const currentAlphabet = isUpperCase ? alphabet : alphabet.toLowerCase();
const index = currentAlphabet.indexOf(char);
if (index === -1) {
result += char;
} else {
const keyChar = key[keyIndex % key.length].toUpperCase();
const keyShift = alphabet.indexOf(keyChar);
const shift = isDecode ? -keyShift : keyShift;
const newIndex =
(index + shift + currentAlphabet.length) % currentAlphabet.length;
result += currentAlphabet[newIndex];
keyIndex++;
}
}
return result;
}
const text = context.params.event.data.options[0].value; // Option 0
const key = context.params.event.data.options[1].value; // Option 1
const isDecode = context.params.event.data.options[2].value; // Option 3 (false for encode, true for decode)
const result = vigenereCipher(text, key, isDecode);
const interactionToken = context.params.event.token;
await lib.discord.interactions['@1.0.1'].responses.create({
token: interactionToken,
content: ``,
embeds: [
{
type: 'rich',
title: `Encode/Decode`,
description: `Original text: ${text}\n\nDecoded/Encoded Text: ${result}\n\nKey: ${key}`,
color: 0x000000,
},
],
});