From b521b034b7139392489f8495098417b4c67d1063 Mon Sep 17 00:00:00 2001 From: dartz Date: Sun, 28 Jul 2024 20:48:01 -0400 Subject: [PATCH] add help command --- src/commands.ts | 12 ++++++++++ src/index.ts | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/src/commands.ts b/src/commands.ts index 10d03e3..e17a5c9 100644 --- a/src/commands.ts +++ b/src/commands.ts @@ -26,6 +26,18 @@ const commands = [ } ], }, + { + name: 'help', + description: "Get help with the bot", + options: [ + { + name: 'command', + description: 'The command to get help with', + type: ApplicationCommandOptionType.String, + required: false + }, + ], + }, { name: 'test', description: "election api test", diff --git a/src/index.ts b/src/index.ts index 1a754e4..8869fe6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -57,7 +57,64 @@ 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})`); + + function minTwoDigits(n: number) { + return (n < 10 ? '0' : '') + n; + } + + if (command === undefined) { + // uptime (is there a better way to do this?) + let totalSeconds = (client.uptime! / 1000); + let days = Math.floor(totalSeconds / 86400); + totalSeconds %= 86400; + let hours = Math.floor(totalSeconds / 3600); + totalSeconds %= 3600; + let minutes = Math.floor(totalSeconds / 60); + let seconds = Math.floor(totalSeconds % 60); + + // add list of commands to help + let description = "Civics is a non-partisan bot that lets you view live election results, polls, predictions, and more.\nType /help (command) for help with a command.\nList of commands:\n"; + const categories: { [key: string]: string[] } = {}; + Object.entries(help_command_json).forEach(([cmd, details]: [string, any]) => { + const category = details[0].category; + if (!categories[category]) { + categories[category] = []; + } + categories[category].push(`\`${cmd}\``); + }); + + Object.entries(categories).forEach(([category, cmds]) => { + description += `:blue_circle: **${category}**: ${cmds.join(', ')}\n`; + }); + + var embed = new EmbedBuilder() + .setTitle(`Civics`) + .setDescription(description) + .addFields( + { name: 'Bot Version', value: 'v1.04', inline: true }, + { name: 'Total Servers', value: `${client.guilds.cache.size}`, inline: true }, + // @ts-ignore + { name: 'Uptime', value: `${days} days, ${minTwoDigits(hours)}:${minTwoDigits(minutes)}:${minTwoDigits(seconds)}`, inline: true } + ); + await i.editReply({embeds: [embed]}); + break; + } + + if (!Object.keys(help_command_json).includes(command)) { + await i.editReply(`Invalid command!`); + break; + } else { + await i.editReply('```c\n' + `# usage\n${help_command[0].usage}\n# description\n${help_command[0].description}\n# examples\n${help_command[0].examples}` + '```\n```md\n' + `${help_command[0].notes}` + '```'); + break; + } + break; + case "predictions": 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 @@ -112,7 +169,7 @@ if (!config.token) { if(results[year] != undefined) { var embed = new EmbedBuilder() .setTitle(` ${results[year][0].election_name}`) - .setDescription("API test") + .setDescription(`${results[year][0].is_ongoing ? ":red_circle: **LIVE**: This election is currently ongoing, results may change!\n" : ""}API test`) .addFields(Object.values(results[year][0].candidates).map((candidate: any) => ({ name: `${candidate.winner ? ":white_check_mark:" : ""} ${CandidateEmojis[candidate.party] ?? ""} ${candidate.name} ${candidate.incumbent ? "(I)" : ""} (${candidate.party})`, value: `${results[year][0].election_type === "electoral" ? `${candidate.electoral_votes} electoral votes\n` : ""}${results[year][0].election_type === "parliament" ? `${candidate.seats_won} seats\n` : ""}${candidate.states_carried !== null && candidate.states_carried !== undefined ? `${candidate.states_carried} states carried\n` : ""}${candidate.delegates !== null && candidate.delegates !== undefined ? `${candidate.delegates.toLocaleString()} delegates\n` : ""}${candidate.delegates !== null && candidate.contests_won !== undefined ? `${candidate.contests_won} contests won\n` : ""}${candidate.votes !== null && candidate.votes !== undefined ? `${candidate.votes.toLocaleString()} votes (${candidate.percent}%)` : ""}`,