From 60d41c2ef865fbbf06d63f416aa81e00b3c39ef7 Mon Sep 17 00:00:00 2001 From: Elijah R Date: Sat, 1 Jun 2024 15:13:00 -0400 Subject: [PATCH] add customizable candidates --- src/commands.ts | 20 +++++++++++++++----- src/index.ts | 8 +++++--- src/predictor.ts | 10 ++++------ 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/commands.ts b/src/commands.ts index 3ca0c35..2b6d119 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -4,11 +4,21 @@ const commands = [ name: 'simulate', description: "Simulate the 2024 United States Presidential Election", options: [ - { - name: 'bias', - description: 'Adds a bias to a certain candidate. Use a positive number for Biden bias, negative for Trump bias', - type: ApplicationCommandOptionType.Number, - } + { + name: 'bias', + description: 'Adds a bias to a certain candidate. Use a positive number for Biden bias, negative for Trump bias', + 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, + } ], } ]; diff --git a/src/index.ts b/src/index.ts index e5efba0..080c745 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,13 +23,15 @@ if (!config.token) { if (i instanceof CommandInteraction) { switch (i.commandName) { 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() .setTitle("2024 United States Presidential Election Simulator") .setDescription(`The CalubViem Press has called the 2024 United States Presidential Election for ${result.winner}!`) .addFields( - {name: `${result.winner === result.gopCandidate ? ":white_check_mark:" : ""} ${result.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 === gopcandidate ? ":white_check_mark:" : ""} ${gopcandidate}`, value: `${result.gopVotes} Electoral Votes`, inline: true}, + {name: `${result.winner === demcandidate ? ":white_check_mark:" : ""} ${demcandidate}`, value: `${result.demVotes} Electoral Votes`, inline: true}, ) .setImage("attachment://election.png") .setTimestamp(); diff --git a/src/predictor.ts b/src/predictor.ts index 7998469..ab0227b 100644 --- a/src/predictor.ts +++ b/src/predictor.ts @@ -5,11 +5,9 @@ import { SVG, registerWindow } from '@svgdotjs/svg.js' import * as fs from "node:fs/promises"; 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"); -export function MakePrediction(bias: number) : Promise { +export function MakePrediction(bias: number, gopcandidate : string, demcandidate : string) : Promise { return new Promise(async res => { if (Number.isNaN(bias)) bias = 0; const window = createSVGWindow(); @@ -38,9 +36,9 @@ export function MakePrediction(bias: number) : Promise { res({ gopVotes, demVotes, - gopCandidate: GOP_CANDIDATE, - demCandidate: DEM_CANDIDATE, - winner: gopVotes > demVotes ? GOP_CANDIDATE : DEM_CANDIDATE, + gopCandidate: gopcandidate, + demCandidate: demcandidate, + winner: gopVotes > demVotes ? gopcandidate : demcandidate, svg: draw.svg(), png, });