A slash command that generates five random colors making up a palette.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const base64 = require(`base64-url`);
// the API we got our colors from is the Colormind API
const colormind = require(`colormind-magic-palette`);
// Replies to the slash command invocation
await lib.discord.interactions['@1.0.1'].responses.create({
token: `${context.params.event.token}`,
response_type: 'DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE',
});
// Randomizes the first color by the bot choosing random Hexcode through Array
// I found the results nicer than just pulling 5 entirely random colors
let multi = 0
let hexarray = [`A`,`B`,`C`,`D`,`E`,`F`,`0`,`1`,`2`,`3`,`4`,`5`,`6`,`7`,`8`,`9`];
let randomhex = new Array();
do {this ["decideHex" + multi] = hexarray[Math.floor(Math.random() * hexarray.length)];
randomhex.push(this ["decideHex"+ multi]);
multi++}
while (multi < 6)
// Joining the Hexcode array into a string
let finalfirsthex = randomhex.join("")
// Finally retrieve the rest of the colors through the API
let palette = await colormind.generatePalette('default', [
"#" + finalfirsthex
]);
let i = 0;
let a = 1;
let b = 1;
do {
this['clean' + a] = palette[i].replace('#', '');
this['color' + a] = `https://color-hex.org/colors/${this['clean' + a]}`;
this['requestColor' + a] = await axios.request({
method: 'GET',
url: `https://www.thecolorapi.com/id?&hex=${this['clean' + a]}`,
});
this['nameColor' + a] = this['requestColor' + a].data.name.value;
this['htmlColor' + a] = `https://www.thecolorapi.com/id?format=html&hex=${
this['clean' + a]
}`;
this['picImg' + a] = `https://color-hex.org/colors/${
this['clean' + a]
}.png`;
// Encoding the second to last color URLs to Base64 so they can be embedded together
if (a > 1 && a < 6) {
this['colorEncode' + b] = await base64.encode(`${this['picImg' + a]}`);
console.log(
`The encoded color ${a} is ${
this['colorEncode' + b]
}. Its b value is ${b}`
);
b++;
}
i++;
a++;
} while (a < 6);
// Stitch together all five color PNGs into a link that can be embedded
let fullpalette = `https://res.cloudinary.com/demo/image/fetch/w_200,h_200/l_fetch:${colorEncode1},w_200,h_200,x_200/l_fetch:${colorEncode2},w_200,h_200,x_300/l_fetch:${colorEncode3},w_200,h_200,x_400/l_fetch:${colorEncode4},w_200,h_200,x_500/${picImg1}`;
// The embed message presentation
await lib.discord.interactions['@1.0.1'].followups.create({
token: `${context.params.event.token}`,
channel_id: context.params.event.channel_id,
content: ``,
embeds: [
{
type: 'rich',
title: `Random Palette`,
description: `_From your left to right:_
\`I.\` [${nameColor1}](${htmlColor1}) [${palette[0]}]
\`II.\` [${nameColor2}](${htmlColor2}) [${palette[1]}]
\`III.\` [${nameColor3}](${htmlColor3}) [${palette[2]}]
\`IV.\` [${nameColor4}](${htmlColor4}) [${palette[3]}]
\`V.\` [${nameColor5}](${htmlColor5}) [${palette[4]}]`,
color: 0xab5d1a, // embed edge color, change it into whatever you want
image: {
url: `${fullpalette}`,
height: 0,
width: 0,
},
},
],
});
// enjoy! Made by Vibrantium