Add image opacity support

This commit is contained in:
dartz 2024-07-25 18:46:41 -04:00
parent 4d5dc235d1
commit ef2ccf95f4
2 changed files with 5 additions and 4 deletions

View file

@ -106,7 +106,8 @@ if (!config.token) {
case "simulate":
var electionname = (i.options as CommandInteractionOptionResolver).getSubcommand();
var overlayimage = (i.options as CommandInteractionOptionResolver).getAttachment('image_overlay');
var overlayopacity = Number(i.options.get('image_opacity')?.value);
var election = structuredClone(Elections[electionname]);
for (const candidate of election.candidates) {
var option = (i.options as CommandInteractionOptionResolver).getString(candidate.party.toLowerCase().replace(/ /g, "_") + "_candidate");
@ -152,7 +153,7 @@ if (!config.token) {
return;
}
}
var result = await MakePrediction(election, overlayimage?.url);
var result = await MakePrediction(election, overlayimage?.url, overlayopacity ? Math.round((overlayopacity / 100) * 255) : 255);
var embed = new EmbedBuilder()
.setTitle(election.title)
.setDescription(election.description.replace("$WINNER", result.winner))

View file

@ -5,7 +5,7 @@ import crypto from "crypto";
import Election from "./election.js";
import VoteType from './VoteType.js';
export function MakePrediction(election : Election, overlay_image?: string) : Promise<Prediction> {
export function MakePrediction(election : Election, overlay_image?: string, overlay_opacity?: number) : Promise<Prediction> {
return new Promise(async res => {
const window = createSVGWindow();
registerWindow(window, window.document);
@ -50,7 +50,7 @@ export function MakePrediction(election : Election, overlay_image?: string) : Pr
const overlay = await sharp(overlayBuffer)
.composite([{
input: Buffer.from([0, 0, 0, 255]),
input: Buffer.from([0, 0, 0, overlay_opacity !== undefined ? overlay_opacity : 255]),
raw: {
width: 1,
height: 1,