ElectionsBot/src/index.ts

49 lines
2.1 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":
var result = await MakePrediction();
var embed = new EmbedBuilder()
.setTitle("2024 United States Presidential Election")
.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},
)
.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");
}