This commit is contained in:
dartz 2024-07-28 21:15:45 -04:00
parent 33bb09e08c
commit a0a5c37faf

View file

@ -1,4 +1,4 @@
import {REST, Routes, Client, GatewayIntentBits, CommandInteraction, EmbedBuilder, CommandInteractionOption, CommandInteractionOptionResolver } from "discord.js";
import {REST, Routes, Client, Guild, GatewayIntentBits, CommandInteraction, EmbedBuilder, CommandInteractionOption, CommandInteractionOptionResolver } from "discord.js";
import * as fs from "node:fs";
import commands from "./commands.js";
import {MakePrediction} from "./predictor.js";
@ -19,10 +19,14 @@ if (!config.token) {
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', async () => {
console.log(`Logged in as ${client.user!.tag}!`);
console.log('\x1b[32m', `Logged in as ${client.user!.tag}! Bot is currently in ${client.guilds.cache.size} servers`, '\x1b[0m');
await publishSlashCommands(client.application!.id);
});
client.on('guildCreate', async (guild: Guild) => {
console.log('\x1b[32m', `Bot joined ${guild.name}`, '\x1b[0m');
});
client.on('interactionCreate', async i => {
if (i instanceof CommandInteraction) {
await i.deferReply();
@ -57,11 +61,12 @@ if (!config.token) {
}
await i.editReply(`${polls}`);
break;
case "help":
var command = i.options.get('command')?.value as string
let help_command_json = JSON.parse(fs.readFileSync('data/help.json', "utf-8"));
let help_command = help_command_json[command];
console.log(`Help command - executed by ${i.user.username} (${i.user.id})`);
console.log('\x1b[33m', `Help command executed by ${i.user.username} (${i.user.id})`, '\x1b[0m');
function minTwoDigits(n: number) {
return (n < 10 ? '0' : '') + n;
@ -119,6 +124,7 @@ if (!config.token) {
var source = i.options.get('source')?.value as string
let prediction_query = await fetch(`http://127.0.0.1:3000/api/v1/predictions/presidential/${source}`); // temp test url
let prediction_results = await prediction_query.json();
console.log('\x1b[33m', `Predictions ${source} command executed by ${i.user.username} (${i.user.id})`, '\x1b[0m');
if (prediction_query.ok) {
if(prediction_results[source] != undefined) {
@ -164,6 +170,7 @@ if (!config.token) {
: `http://127.0.0.1:3000/api/v1/election/${country}/${election_type}/${year}`;
const query = await fetch(queryURL);
let results = await query.json();
console.log('\x1b[33m', `Election command executed by ${i.user.username} (${i.user.id})`, '\x1b[0m');
if (query.ok) {
if(results[year] != undefined) {
@ -211,6 +218,7 @@ if (!config.token) {
var electionname = (i.options as CommandInteractionOptionResolver).getSubcommand();
var overlayimage = (i.options as CommandInteractionOptionResolver).getAttachment('image_overlay');
var overlayopacity = Number(i.options.get('image_opacity')?.value);
console.log('\x1b[33m', `Simulate ${electionname} command executed by ${i.user.username} (${i.user.id})`, '\x1b[0m');
var election = structuredClone(Elections[electionname]);
for (const candidate of election.candidates) {
@ -287,5 +295,5 @@ if (!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");
console.log('\x1b[32m', 'Successfully registered slash commands!', '\x1b[0m');
}