add customizable candidates

This commit is contained in:
Elijah R 2024-06-01 15:13:00 -04:00
parent ed4ab43caf
commit 60d41c2ef8
3 changed files with 24 additions and 14 deletions

View file

@ -4,11 +4,21 @@ const commands = [
name: 'simulate', name: 'simulate',
description: "Simulate the 2024 United States Presidential Election", description: "Simulate the 2024 United States Presidential Election",
options: [ options: [
{ {
name: 'bias', name: 'bias',
description: 'Adds a bias to a certain candidate. Use a positive number for Biden bias, negative for Trump bias', description: 'Adds a bias to a certain candidate. Use a positive number for Biden bias, negative for Trump bias',
type: ApplicationCommandOptionType.Number, type: ApplicationCommandOptionType.Number,
} },
{
name: 'gopcandidate',
description: 'The GOP candidate to simulate (default: Donald Trump)',
type: ApplicationCommandOptionType.String,
},
{
name: 'demcandidate',
description: 'The Democratic candidate to simulate (default: Joe Biden)',
type: ApplicationCommandOptionType.String,
}
], ],
} }
]; ];

View file

@ -23,13 +23,15 @@ if (!config.token) {
if (i instanceof CommandInteraction) { if (i instanceof CommandInteraction) {
switch (i.commandName) { switch (i.commandName) {
case "simulate": case "simulate":
var result = await MakePrediction(Number(i.options.get('bias')?.value)); var gopcandidate = i.options.get('gopcandidate')?.value as string || "Donald J. Trump";
var demcandidate = i.options.get('demcandidate')?.value as string || "Joseph R. Biden Jr.";
var result = await MakePrediction(Number(i.options.get('bias')?.value), gopcandidate, demcandidate);
var embed = new EmbedBuilder() var embed = new EmbedBuilder()
.setTitle("2024 United States Presidential Election Simulator") .setTitle("2024 United States Presidential Election Simulator")
.setDescription(`The CalubViem Press has called the 2024 United States Presidential Election for ${result.winner}!`) .setDescription(`The CalubViem Press has called the 2024 United States Presidential Election for ${result.winner}!`)
.addFields( .addFields(
{name: `${result.winner === result.gopCandidate ? ":white_check_mark:" : ""} ${result.gopCandidate}`, value: `${result.gopVotes} Electoral Votes`, inline: true}, {name: `${result.winner === gopcandidate ? ":white_check_mark:" : ""} ${gopcandidate}`, value: `${result.gopVotes} Electoral Votes`, inline: true},
{name: `${result.winner === result.demCandidate ? ":white_check_mark:" : ""} ${result.demCandidate}`, value: `${result.demVotes} Electoral Votes`, inline: true}, {name: `${result.winner === demcandidate ? ":white_check_mark:" : ""} ${demcandidate}`, value: `${result.demVotes} Electoral Votes`, inline: true},
) )
.setImage("attachment://election.png") .setImage("attachment://election.png")
.setTimestamp(); .setTimestamp();

View file

@ -5,11 +5,9 @@ import { SVG, registerWindow } from '@svgdotjs/svg.js'
import * as fs from "node:fs/promises"; import * as fs from "node:fs/promises";
import sharp from "sharp"; import sharp from "sharp";
const GOP_CANDIDATE = "Donald J. Trump";
const DEM_CANDIDATE = "Joseph R. Biden Jr.";
const BASE_SVG = await fs.readFile("assets/ElectoralCollege2024.svg", "utf-8"); const BASE_SVG = await fs.readFile("assets/ElectoralCollege2024.svg", "utf-8");
export function MakePrediction(bias: number) : Promise<Prediction> { export function MakePrediction(bias: number, gopcandidate : string, demcandidate : string) : Promise<Prediction> {
return new Promise(async res => { return new Promise(async res => {
if (Number.isNaN(bias)) bias = 0; if (Number.isNaN(bias)) bias = 0;
const window = createSVGWindow(); const window = createSVGWindow();
@ -38,9 +36,9 @@ export function MakePrediction(bias: number) : Promise<Prediction> {
res({ res({
gopVotes, gopVotes,
demVotes, demVotes,
gopCandidate: GOP_CANDIDATE, gopCandidate: gopcandidate,
demCandidate: DEM_CANDIDATE, demCandidate: demcandidate,
winner: gopVotes > demVotes ? GOP_CANDIDATE : DEM_CANDIDATE, winner: gopVotes > demVotes ? gopcandidate : demcandidate,
svg: draw.svg(), svg: draw.svg(),
png, png,
}); });