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 14:58:40 -04:00
|
|
|
var result = await MakePrediction(Number(i.options.get('bias')?.value));
|
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(
|
|
|
|
{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");
|
|
|
|
}
|