Initial Code
This commit is contained in:
104
src/interactions/DailySubmit.ts
Normal file
104
src/interactions/DailySubmit.ts
Normal file
@@ -0,0 +1,104 @@
|
||||
import { MessageFlags, EmbedBuilder } from "discord.js";
|
||||
import BotAction from "../libs/BotAction";
|
||||
import BotClient from "../libs/BotClient";
|
||||
import QuestionScheduler from "../libs/QuestionScheduler";
|
||||
import AnswerGrader from "../libs/AnswerGrader";
|
||||
import PointsManager from "../libs/PointsManager";
|
||||
|
||||
export default class DailySubmit extends BotAction {
|
||||
constructor() {
|
||||
super("daily_submit_");
|
||||
}
|
||||
|
||||
public async execute(Discord: any, client: BotClient, interaction: any): Promise<void> {
|
||||
try {
|
||||
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
|
||||
|
||||
const questionId = interaction.customId.split("_").slice(2).join("_");
|
||||
const userAnswer = interaction.fields.getTextInputValue("answer");
|
||||
|
||||
const scheduler = new QuestionScheduler();
|
||||
await scheduler.initialize();
|
||||
|
||||
const hasAnswered = await scheduler.hasUserAnswered(interaction.user.id, questionId);
|
||||
if (hasAnswered) {
|
||||
await interaction.editReply({ content: "You've already submitted an answer for this question!" });
|
||||
return;
|
||||
}
|
||||
|
||||
const history = await scheduler.getHistory();
|
||||
const question = history.find((q: any) => q.id === questionId);
|
||||
|
||||
if (!question) {
|
||||
await interaction.editReply({ content: "Question not found. Please try again." });
|
||||
return;
|
||||
}
|
||||
|
||||
await interaction.editReply({ content: "✅ Answer submitted! Sending to grader..." });
|
||||
|
||||
const grader = new AnswerGrader();
|
||||
const grade = await grader.gradeAnswer(question, userAnswer);
|
||||
|
||||
await scheduler.saveSubmission({
|
||||
userId: interaction.user.id,
|
||||
username: interaction.user.username,
|
||||
questionId: question.id,
|
||||
answer: userAnswer,
|
||||
gradeResult: grade,
|
||||
timestamp: new Date().toISOString(),
|
||||
});
|
||||
|
||||
const color = grade.is_correct ? 0x22c55e : 0xef4444;
|
||||
const verdict = grade.is_correct ? "✅ CORRECT" : "❌ INCORRECT";
|
||||
|
||||
const score = await scheduler.getUserScore(interaction.user.id, "daily");
|
||||
|
||||
let pointsInfo = "";
|
||||
if (grade.is_correct) {
|
||||
const pointsResult = await PointsManager.awardPoints(interaction.user.id, interaction.user.username, "daily");
|
||||
if (pointsResult) {
|
||||
pointsInfo = `\n**Points Earned:** +2 points (Total: ${pointsResult.userPoints})`;
|
||||
if (pointsResult.teamPoints !== undefined) {
|
||||
pointsInfo += `\n**Team Points:** ${pointsResult.teamPoints}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setTitle(verdict)
|
||||
.setColor(color)
|
||||
.addFields(
|
||||
{ name: "Subject", value: question.subject, inline: true },
|
||||
{ name: "Topic", value: question.topic, inline: true },
|
||||
{ name: "Difficulty", value: question.difficulty_rating, inline: true },
|
||||
{ name: "Your Daily Score", value: `${score.correct}/${score.total} correct today${pointsInfo}`, inline: false }
|
||||
)
|
||||
.setFooter({ text: "Come back tomorrow for a new question!" })
|
||||
.setTimestamp();
|
||||
|
||||
try {
|
||||
const dmChannel = await interaction.user.createDM();
|
||||
await dmChannel.send({ embeds: [embed] });
|
||||
console.log(`[DM] Successfully sent daily grade result to ${interaction.user.username} (${interaction.user.id})`);
|
||||
} catch (dmError) {
|
||||
console.log(`[DM] Failed to DM ${interaction.user.username} (${interaction.user.id}):`, dmError);
|
||||
await interaction.followUp({
|
||||
content: "⚠️ I couldn't DM you the results. Here they are:",
|
||||
embeds: [embed],
|
||||
flags: MessageFlags.Ephemeral
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Error in DailySubmit:", err);
|
||||
try {
|
||||
if (interaction.deferred || interaction.replied) {
|
||||
await interaction.editReply({ content: "An error occurred while grading. Please try again later." });
|
||||
} else {
|
||||
await interaction.reply({ content: "An error occurred. Please try again later.", flags: MessageFlags.Ephemeral });
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Error sending error reply:", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user