import {REST, Routes, Client, GatewayIntentBits, CommandInteraction, EmbedBuilder } from "discord.js"; import * as fs from "node:fs"; import commands from "./commands.js"; import {MakePrediction} from "./predictor.js"; const configraw = fs.readFileSync("config.json", "utf-8"); const config = JSON.parse(configraw); if (!config.token) { console.error("Please provide a Token and Client ID in config.json"); process.exit(1); } (async () => { const client = new Client({ intents: [GatewayIntentBits.Guilds] }); client.on('ready', async () => { console.log(`Logged in as ${client.user!.tag}!`); await publishSlashCommands(client.application!.id); }); client.on('interactionCreate', async i => { if (i instanceof CommandInteraction) { switch (i.commandName) { case "poll": const rcp_polls = JSON.parse(fs.readFileSync('data/rcp_polls.json', "utf-8")); let polls = ""; Object.keys(rcp_polls.approval.polls).forEach(poll => { polls += `:red_circle: **${rcp_polls.approval.polls[poll].pollster}** - (**${rcp_polls.approval.polls[poll].date}**), ${rcp_polls.approval.polls[poll].sample}, **${rcp_polls.approval.polls[poll].approve}**% approve, **${rcp_polls.approval.polls[poll].disapprove}**% disapprove (spread: **${rcp_polls.approval.polls[poll].spread}**)\n`; }); await i.reply(`Biden Approval Rating\n\n${polls}`); break; case "simulate": var simulationtype = i.options.get('type')?.value as string || "presidential"; var year = Number(i.options.get('year')?.value); switch(simulationtype) { case "presidential": if (year == 1864) { var gopcandidate = i.options.get('gopcandidate')?.value as string || "Abraham Lincoln"; var demcandidate = i.options.get('demcandidate')?.value as string || "George B. McClellan"; var result = await MakePrediction(Number(i.options.get('bias')?.value), gopcandidate, demcandidate, simulationtype, Number(i.options.get('year')?.value)); var embed = new EmbedBuilder() .setTitle("1864 United States Presidential Election Simulator") .setDescription(`The CalubViem Press has rewritten history, and has called the 1864 United States Presidential Election for ${result.winner}!`) .addFields( {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(); await i.reply({embeds: [embed], files: [{attachment: result.png, name: "election.png"}]}); break; } else { 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, simulationtype, Number(i.options.get('year')?.value)); 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 === 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(); await i.reply({embeds: [embed], files: [{attachment: result.png, name: "election.png"}]}); break; } case "senate": var gopcandidate = i.options.get('gopcandidate')?.value as string || "Republicans"; var demcandidate = i.options.get('demcandidate')?.value as string || "Democrats"; var result = await MakePrediction(Number(i.options.get('bias')?.value), gopcandidate, demcandidate, simulationtype, 0); 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 === gopcandidate ? ":white_check_mark:" : ""} ${gopcandidate}`, value: `${result.gopVotes} seats`, inline: true}, {name: `${result.winner === demcandidate ? ":white_check_mark:" : ""} ${demcandidate}`, value: `${result.demVotes} seats`, inline: true}, ) .setImage("attachment://election.png") .setTimestamp(); await i.reply({embeds: [embed], files: [{attachment: result.png, name: "election.png"}]}); break; } } } }); client.login(config.token); })(); async function publishSlashCommands(clientid : string) { const rest = new REST({ version: '10' }).setToken(config.token); await rest.put(Routes.applicationCommands(clientid), {body: commands}); console.log("Successfully registered slash commands"); }