ElectionsBot/src/index.ts

51 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-06-01 02:14:08 -04:00
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 "simulate":
2024-06-01 15:13:00 -04:00
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);
2024-06-01 02:14:08 -04:00
var embed = new EmbedBuilder()
2024-06-01 14:58:40 -04:00
.setTitle("2024 United States Presidential Election Simulator")
2024-06-01 02:14:08 -04:00
.setDescription(`The CalubViem Press has called the 2024 United States Presidential Election for ${result.winner}!`)
.addFields(
2024-06-01 15:13:00 -04:00
{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},
2024-06-01 02:14:08 -04:00
)
.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");
}