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

View file

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