From 7412fa786252ceea73aa698b017c363d25247896 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Thu, 2 Dec 2021 15:37:30 -0600 Subject: [PATCH] Implement needed commands --- src/commands/next.ts | 72 +++++++++++++++++++++++++++++++++++++++++++ src/commands/start.ts | 30 ++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 src/commands/next.ts create mode 100644 src/commands/start.ts diff --git a/src/commands/next.ts b/src/commands/next.ts new file mode 100644 index 0000000..e416de6 --- /dev/null +++ b/src/commands/next.ts @@ -0,0 +1,72 @@ +// +// next.ts +// +// Written by: Oliver Akins (2021/12/02) +// + + +import axios from "axios"; +import { PERM } from "../constants"; +import { REGISTER_COMMAND } from "../cmd_handler"; +import { question_index, change_index, questions, users, reset_users, user_right_count} from "../main"; + + +const NEXT_COMMAND = (ctx: msg_data, args: string[]): string => { + + // Ensure the quiz has been started. + if (question_index < 0) { + return `Cannot go the next question before "!start"ing the quiz.`; + }; + + // count users who got it right + let users_correct = Object.values(users) + .filter(x => x == questions[question_index]) + .length; + + for (var user in users) { + let delta = 0; + if (users[user] == questions[question_index]) { + delta = 1; + }; + + if (user_right_count[user] != null) { + user_right_count[user] = delta; + } else { + user_right_count[user] += delta; + }; + }; + + reset_users(); + + // Check if questions has run dry + if (question_index == questions.length) { + + // Send message in Discord for record-keeping + axios.post( + `https://ptb.discord.com/api/webhooks/916068083863539782/9k2b66SUIeY8hl5id7gdnZF6Abc8Fb1f5-RVlgbUJqirNmALkrrgrCWdsufcZ5Bn2a4i`, + { + content: `\`\`\`json\n${JSON.stringify(users, null, "\t")}\n\`\`\`\n\n\n\`\`\`${user_right_count}` + } + ); + + change_index(-1); + return `${users_correct} got the last question right. Quiz has ended. Kthxbai.`; + }; + + change_index(question_index + 1); + return `${users_correct} got that question right. Changed to the next question!`; +}; + + +REGISTER_COMMAND({ + description: "", + executable: NEXT_COMMAND, + requires_confirm: false, + case_sensitive: false, + name: "start", + opt_args: 0, + args: [], + level: PERM.MOD, + arg_info: [], + flags: {} +}); \ No newline at end of file diff --git a/src/commands/start.ts b/src/commands/start.ts new file mode 100644 index 0000000..1c939d4 --- /dev/null +++ b/src/commands/start.ts @@ -0,0 +1,30 @@ +// +// start.ts +// +// Written by: Oliver Akins (2021/12/02) +// + + +import { PERM } from "../constants"; +import { REGISTER_COMMAND } from "../cmd_handler"; +import { change_index } from "../main"; + + +const START_COMMAND = (ctx: msg_data, args: string[]): string => { + change_index(0); + return `Quiz has been started!`; +}; + + +REGISTER_COMMAND({ + description: "", + executable: START_COMMAND, + requires_confirm: false, + case_sensitive: false, + name: "start", + opt_args: 0, + args: [], + level: PERM.MOD, + arg_info: [], + flags: {} +}); \ No newline at end of file