add help command

This commit is contained in:
dartz 2024-07-28 20:48:01 -04:00
parent 3d8b63e525
commit b521b034b7
2 changed files with 70 additions and 1 deletions

View file

@ -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",

View file

@ -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}%)` : ""}`,