Skip to content

Instantly share code, notes, and snippets.

@Reselim
Last active July 25, 2020 02:08
Show Gist options
  • Save Reselim/986a5c0219a28d030565ca33427e08cb to your computer and use it in GitHub Desktop.
Save Reselim/986a5c0219a28d030565ca33427e08cb to your computer and use it in GitHub Desktop.
const discord = require("discord.js")
const akairo = require("discord-akairo")
const jimp = require("jimp")
const borderSize = 0.04
const prideFlags = {
gay: [ "#FF000E", "#FF5000", "#FAD220", "#138F3E", "#3558A0", "#880082" ],
bisexual: [ "#D60270", "#9B4F96", "#0038A8" ],
trans: [ "#5BCEFA", "#F5A9B8", "#EEEEEE", "#F5A9B8", "#5BCEFA" ],
nonbinary: [ "#FFF433", "#FFFFFF", "#9B59D0", "#2D2D2D" ],
lesbian: [ "#D52D00", "#FF9A56", "#FFFFFF", "#D362A4", "#A30262" ],
asexual: [ "#000000", "#A3A3A3", "#FFFFFF", "#800080" ],
aromantic: [ "#3DA542", "#A8D379", "#FFFFFF", "#A9A9A9", "#000000" ],
agender: [ "#000000", "#BABABA", "#FFFFFF", "#B9F484", "#FFFFFF", "#BABABA", "#000000" ],
pansexual: [ "#FF218C", "#FFD800", "#21B1FF" ],
genderfluid: [ "#FF75A2", "#EFEFEF", "#BE18D6", "#000000", "#333EBD" ],
poly: [ "#0000F7", "#F70000", "#000000" ],
hamburger: [ "#E69138", "#8FCE00", "#F44336", "#FFD966", "#744700", "#E69138" ]
}
function createBaseTexture(colors) {
return new Promise((resolve, reject) => {
// Resize with nearest neighbor
new jimp(1, colors.length, (error, image) => {
if (error) return reject(error)
image.scan(0, 0, image.bitmap.width, image.bitmap.height, (_, y, index) => {
let colorHex = jimp.cssColorToHex(colors[y])
let color = jimp.intToRGBA(colorHex)
image.bitmap.data[index] = color.r
image.bitmap.data[index + 1] = color.g
image.bitmap.data[index + 2] = color.b
image.bitmap.data[index + 3] = color.a
})
resolve(image)
})
})
}
async function createBackgroundTexture(size, flags) {
let textures = await Promise.all(flags.map((flagName) => {
return createBaseTexture(prideFlags[flagName])
}))
textures.forEach((texture, index) => {
let width = size * ((index + 1) / flags.length)
texture.resize(width, size, jimp.RESIZE_NEAREST_NEIGHBOR)
})
return new Promise((resolve, reject) => {
new jimp(size, size, (error, image) => {
if (error) return reject(error)
for (let index = flags.length; index--; index >= 0) {
image.blit(textures[index], 0, 0)
}
image.circle()
resolve(image)
})
})
}
async function createPrideAvatar(avatarImage, flags) {
let avatar = await jimp.read(avatarImage)
let texture = await createBackgroundTexture(avatar.bitmap.width, flags)
let borderOffset = avatar.bitmap.width * borderSize
let targetAvatarSize = avatar.bitmap.width * (1 - borderSize * 2)
avatar.resize(targetAvatarSize, targetAvatarSize)
avatar.circle()
texture.blit(avatar, borderOffset, borderOffset)
return texture
}
class PrideAvatarCommand extends akairo.Command {
constructor() {
super("prideAvatar", {
aliases: [ "pride" ],
args: [
{
id: "flag1",
type: Object.keys(prideFlags),
otherwise: `Please specify a flag! Available flags: ${Object.keys(prideFlags).join(", ")}`
},
{
id: "flag2",
type: Object.keys(prideFlags)
}
]
})
}
async exec(message, { flag1, flag2 }) {
let flags = [ flag1 ]
if (flag2) flags.push(flag2)
let avatar = await createPrideAvatar(message.author.avatarURL({
format: "png",
size: 512
}), flags)
let buffer = await avatar.getBufferAsync(jimp.MIME_PNG)
let attachment = new discord.MessageAttachment(buffer, "pride-avatar.png")
await message.channel.send(attachment)
}
}
module.exports = PrideAvatarCommand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment