Implement needed commands

This commit is contained in:
Oliver-Akins 2021-12-02 15:37:30 -06:00
parent f125fc1a03
commit 7412fa7862
2 changed files with 102 additions and 0 deletions

72
src/commands/next.ts Normal file
View file

@ -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: {}
});

30
src/commands/start.ts Normal file
View file

@ -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: {}
});