This repository was archived by the owner on Sep 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Extreme hacky way of adding another command for past questions
Come back to this later and make it make sense
- Loading branch information
1 parent
cd0044e
commit 002f167
Showing
6 changed files
with
346 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,3 +134,4 @@ logs*.* | |
course_resources/ | ||
RemoteAuth.zip | ||
dist/ | ||
past_questions/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
import { IArgs, IClient } from "../../types"; | ||
import { List, Message } from "whatsapp-web.js"; | ||
import { getMutedStatus } from "../../models/misc"; | ||
import { | ||
PAST_QUESTIONS_REPLIES, | ||
WAIT_REPLIES, | ||
REACT_EMOJIS, | ||
} from "../../utils/data"; | ||
import { | ||
currentEnv, | ||
pickRandomReply, | ||
currentPrefix, | ||
sendPastQuestions, | ||
} from "../../utils/helpers"; | ||
|
||
// Past Questions are referred to as "pasco" | ||
|
||
const execute = async (client: IClient, msg: Message, args: IArgs) => { | ||
if ((await getMutedStatus()) === true) return; | ||
|
||
const { isListResponse, lastPrefixUsed } = args; | ||
// if (currentEnv === 'production') { | ||
const contact = await msg.getContact(); | ||
const curChat = await msg.getChat(); | ||
const chatFromContact = await contact.getChat(); | ||
|
||
if (curChat.isGroup) await msg.react(pickRandomReply(REACT_EMOJIS)); | ||
|
||
const list = new List( | ||
"\nThis is a list of courses with available past questions", | ||
"See courses", | ||
[ | ||
{ | ||
title: "", | ||
rows: [ | ||
// { | ||
// id: | ||
// lastPrefixUsed === process.env.DEV_PREFIX | ||
// ? "pasco-416_dev" | ||
// : "pasco-416_prod", | ||
// title: "System Programming", | ||
// description: "CSCD 416", | ||
// }, | ||
// { | ||
// id: | ||
// lastPrefixUsed === process.env.DEV_PREFIX | ||
// ? "pasco-418_dev" | ||
// : "pasco-418_prod", | ||
// title: "Computer Systems Security", | ||
// description: "CSCD 418", | ||
// }, | ||
{ | ||
id: | ||
lastPrefixUsed === process.env.DEV_PREFIX | ||
? "pasco-422_dev" | ||
: "pasco-422_prod", | ||
title: "Human Computer Interaction", | ||
description: "CSCD 422", | ||
}, | ||
// { | ||
// id: | ||
// lastPrefixUsed === process.env.DEV_PREFIX | ||
// ? "pasco-424_dev" | ||
// : "pasco-424_prod", | ||
// title: "Management Principles", | ||
// description: "CSCD 424", | ||
// }, | ||
// { | ||
// id: | ||
// lastPrefixUsed === process.env.DEV_PREFIX | ||
// ? "pasco-400_dev" | ||
// : "pasco-400_prod", | ||
// title: "Project", | ||
// description: "CSCD 400", | ||
// }, | ||
// { id: lastPrefixUsed === process.env.DEV_PREFIX ? 'pasco-426_dev' : 'pasco-426_prod', title: 'Multimedia Applications', description: 'CSCD 426' }, | ||
// { | ||
// id: | ||
// lastPrefixUsed === process.env.DEV_PREFIX | ||
// ? "pasco-428_dev" | ||
// : "pasco-428_prod", | ||
// title: "Expert Systems", | ||
// description: "CSCD 428", | ||
// }, | ||
// { id: lastPrefixUsed === process.env.DEV_PREFIX ? 'pasco-432_dev' : 'pasco-432_prod', title: 'Concurrent & Distributed Systems', description: 'CSCD 432' }, | ||
// { id: lastPrefixUsed === process.env.DEV_PREFIX ? 'pasco-434_dev' : 'pasco-434_prod', title: 'Mobile Computing', description: 'CSCD 434' }, | ||
], | ||
}, | ||
], | ||
pickRandomReply(PAST_QUESTIONS_REPLIES), | ||
"Powered by Ethereal bot" | ||
); | ||
|
||
!isListResponse && (await chatFromContact.sendMessage(list)); | ||
// } else { | ||
// await msg.reply("The bot is currently hosted locally, so this operation cannot be performed.\n\nThe Grandmaster's data is at stake🐦") | ||
// } | ||
|
||
if (isListResponse) { | ||
if (msg.selectedRowId) { | ||
const selectedRowId = msg.selectedRowId.split("-")[1]; | ||
|
||
console.log(`[SLIDES CMD] Slides from ${currentEnv} env`); | ||
|
||
switch (selectedRowId) { | ||
case "416_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); // have to repeat this to avoid it leaking when bot environments are running simultaneously | ||
sendPastQuestions(msg, "CSCD 416"); | ||
break; | ||
|
||
case "416_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 416"); | ||
break; | ||
|
||
case "418_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 418"); | ||
break; | ||
|
||
case "418_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 418"); | ||
break; | ||
|
||
case "422_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 422"); | ||
break; | ||
|
||
case "422_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 422"); | ||
break; | ||
|
||
case "424_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 424"); | ||
break; | ||
|
||
case "424_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 424"); | ||
break; | ||
|
||
case "400_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 400"); | ||
break; | ||
|
||
case "400_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 400"); | ||
break; | ||
|
||
case "426_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 426"); | ||
break; | ||
|
||
case "426_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 426"); | ||
break; | ||
|
||
case "428_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 428"); | ||
break; | ||
|
||
case "428_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 428"); | ||
break; | ||
|
||
case "432_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 432"); | ||
break; | ||
|
||
case "432_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 432"); | ||
break; | ||
|
||
case "434_dev": | ||
if (currentEnv !== "development") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 434"); | ||
break; | ||
|
||
case "434_prod": | ||
if (currentEnv !== "production") break; | ||
await msg.reply(pickRandomReply(WAIT_REPLIES)); | ||
sendPastQuestions(msg, "CSCD 434"); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
args.isListResponse = false; | ||
} | ||
}; | ||
|
||
module.exports = { | ||
name: "pasco", | ||
description: "Get course materials for all courses 📚", | ||
alias: ["pascos", "pq"], | ||
category: "everyone", // admin | everyone | ||
help: `To use this command, type:\n*${currentPrefix}pasco*, then choose a course from the list provided`, | ||
execute, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// -------------------------------------------------- | ||
// pastQuestions.ts contains the schema for course past questions | ||
// -------------------------------------------------- | ||
|
||
const { Schema, model } = require('mongoose'); | ||
const fs = require('fs'); | ||
|
||
|
||
/** | ||
* Schema for course past questions | ||
*/ | ||
const PastQuestionsSchema = new Schema({ | ||
title: String, | ||
courseCode: String, | ||
binData: String, // when Buffer is used, puppeteer complains that it's not properly encoded | ||
}); | ||
|
||
const prodModelName = "pq-files"; | ||
const PastQuestionsModel = model(prodModelName, PastQuestionsSchema); | ||
//todo: Since we can't push `past_questions`, we should only allow uploading courses to development collection | ||
|
||
/** | ||
* Helper function to encode the local courses materials and save them on the cloud database. | ||
* @async | ||
*/ | ||
const initCollection = async () => { | ||
const count = await PastQuestionsModel.countDocuments({}); | ||
console.log('[PAST_QUESTIONS MODEL] Doc count:', count); | ||
if (!count) { | ||
const dir = './past_questions'; | ||
for (const folder of fs.readdirSync(dir)) { | ||
console.log('[PAST_QUESTIONS MODEL]', folder) | ||
const courseCode = folder.split('-')[0].trim(); | ||
for (const file of fs.readdirSync(dir + '/' + folder)) { | ||
const binData = fs.readFileSync(dir + '/' + folder + '/' + file, { encoding: 'base64' }); | ||
const doc = new PastQuestionsModel({ title: file, courseCode, binData }); | ||
try { | ||
await doc.save(); | ||
} catch (err) { | ||
console.error('[PAST_QUESTIONS MODEL ERROR]', err); | ||
} | ||
} | ||
} | ||
console.log("[PAST_QUESTIONS MODEL] Done encoding all past questions!"); | ||
} else console.log('[PAST_QUESTIONS MODEL]', prodModelName + " collection is not empty"); | ||
} | ||
initCollection(); | ||
|
||
|
||
/** | ||
* Gets specific past questions from the database. | ||
* @param {string} courseCode String representing the course code for a specific course | ||
* @returns | ||
*/ | ||
export const getPastQuestions = async (courseCode: string) => { | ||
const res = await PastQuestionsModel.find({ courseCode }); | ||
// console.log('[PAST_QUESTIONS MODEL]', res); | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters