Browse Source

feat: add settings for subject and grade levels

development
Abheek Dhawan 2 years ago
parent
commit
245b1c818c
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 128
      src/commands/settings.ts
  2. 5
      src/commands/train.ts
  3. 9
      src/models/userConfig.ts

128
src/commands/settings.ts

@ -1,7 +1,8 @@
import { SlashCommandBuilder } from '@discordjs/builders'; import { SlashCommandBuilder } from '@discordjs/builders';
import { Message, MessageActionRow, MessageSelectMenu } from 'discord.js'; import { Message, MessageActionRow, MessageSelectMenu } from 'discord.js';
import { CommandInteraction, MessageEmbed } from 'discord.js'; import { CommandInteraction, MessageEmbed } from 'discord.js';
import log from '../helpers/log.js'; import log from '../helpers/log';
import userConfig from '../models/userConfig';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('settings') .setName('settings')
@ -23,8 +24,7 @@ export const data = new SlashCommandBuilder()
.setName('gradelevels') .setName('gradelevels')
.setDescription('Changes grade level of problems'); .setDescription('Changes grade level of problems');
return subcommand; return subcommand;
}) });
;
export async function execute(interaction : CommandInteraction) { export async function execute(interaction : CommandInteraction) {
const action = interaction.options.getSubcommand(); const action = interaction.options.getSubcommand();
@ -59,24 +59,37 @@ export async function execute(interaction : CommandInteraction) {
); );
interaction.followUp({ interaction.followUp({
embeds: [settingsEmbed], embeds: [/* settingsEmbed*/],
components: [menu], components: [menu],
}) })
.then((dispMsg => { .then((dispMsg => {
const w = dispMsg as Message; const w = dispMsg as Message;
let h;
const dispFilter = i => ['selectdisp'].includes(i.customId) && i.user.id == interaction.user.id; // <== ATTENTION! First argument... const dispFilter = i => ['selectdisp'].includes(i.customId) && i.user.id == interaction.user.id; // <== ATTENTION! First argument...
w.awaitMessageComponent({ filter: dispFilter, componentType: 'SELECT_MENU' }) w.awaitMessageComponent({ filter: dispFilter, componentType: 'SELECT_MENU' })
.then(dispChoice => { .then(async dispChoice => {
h = dispChoice.values; const vals = dispChoice.values;
if (h == 'subjects') { const config = await userConfig.findById(interaction.user.id);
interaction.editReply({ content: 'Current subjects setting:', components: [] }); if (!config) {
await interaction.editReply({ content: 'You don\'t have a configuration!', embeds: [], components: [] });
}
else if (vals.length === 1 && vals.at(0) === 'subjects') {
await interaction.editReply({
content: `Current subjects setting: ${config.subjects.toString().split(',').join(', ')}`,
components: [],
});
} }
else if (h == 'gradelevels') { else if (vals.length === 1 && vals.at(0) === 'gradelevels') {
interaction.editReply({ content: 'Current grade level setting: ', components: [] }); await interaction.editReply({
content: `Current grade level setting: ${config.gradeLevels.toString().split(',').join(', ')}`,
components: [],
});
} }
else { else {
err => log({ logger: '\'Error occurred: /settings:display did not equal subjects or gradelevels.\'', content: `${err}`, level: 'error' }); err => log({
logger: '\'Error occurred: /settings:display did not equal subjects or gradelevels.\'',
content: `${err}`,
level: 'error',
});
} }
}); });
})); }));
@ -104,37 +117,39 @@ export async function execute(interaction : CommandInteraction) {
{ {
label: 'Middle School', label: 'Middle School',
description: 'Middle school level problems', description: 'Middle school level problems',
value: 'ms', value: 'MS',
}, },
{ {
label: 'High School', label: 'High School',
description: 'High school level problems', description: 'High school level problems',
value: 'hs', value: 'HS',
}, },
]), ]),
); );
interaction.followUp({ interaction.followUp({
embeds: [settingsEmbed], embeds: [/* settingsEmbed*/],
components: [menu], components: [menu],
}) })
.then((lvlMsg => { .then((lvlMsg => {
const w = lvlMsg as Message; const w = lvlMsg as Message;
let h;
const lvlFilter = i => ['selectlvl'].includes(i.customId) && i.user.id == interaction.user.id; // <== ATTENTION! First argument... const lvlFilter = i => ['selectlvl'].includes(i.customId) && i.user.id == interaction.user.id; // <== ATTENTION! First argument...
w.awaitMessageComponent({ filter: lvlFilter, componentType: 'SELECT_MENU' }) w.awaitMessageComponent({ filter: lvlFilter, componentType: 'SELECT_MENU' })
.then(lvlChoice => { .then(async lvlChoice => {
h = lvlChoice.values; const vals = lvlChoice.values;
if (h == 'ms') { const levels = new Array<string>();
interaction.editReply({ content: 'Level set to: Middle School', components: [] }); await userConfig.findOneAndUpdate({ _id: interaction.user.id }, { gradeLevels: vals }, { upsert: true, new: true });
} await vals.forEach(v => {
else if (h == 'hs') { switch (v) {
interaction.editReply({ content: 'Level set to: High School', components: [] }); case 'MS':
} levels.push('Middle School');
else { break;
interaction.editReply({ content: 'Level set to: All', components: [] }); case 'HS':
levels.push('High School');
} }
}); });
await interaction.editReply({ content: `Level set to: ${levels.toString().split(',').join(', ')}`, embeds: [], components: [] });
});
})); }));
break; break;
} }
@ -160,83 +175,56 @@ export async function execute(interaction : CommandInteraction) {
{ {
label: 'Astronomy', label: 'Astronomy',
description: 'Astronomy', description: 'Astronomy',
value: 'astro', value: 'ASTRONOMY',
}, },
{ {
label: 'Biology', label: 'Biology',
description: 'Biology', description: 'Biology',
value: 'bio', value: 'BIOLOGY',
}, },
{ {
label: 'Earth Science', label: 'Earth Science',
description: 'Earth Science', description: 'Earth Science',
value: 'es', value: 'EARTH SCIENCE',
}, },
{ {
label: 'Chemistry', label: 'Chemistry',
description: 'Chemistry', description: 'Chemistry',
value: 'chem', value: 'CHEMISTRY',
}, },
{ {
label: 'Physics', label: 'Physics',
description: 'Physics', description: 'Physics',
value: 'phy', value: 'PHYSICS',
}, },
{ {
label: 'Mathematics', label: 'Mathematics',
description: 'Mathematics', description: 'Mathematics',
value: 'math', value: 'MATH',
}, },
{ {
label: 'Energy', label: 'Energy',
description: 'Energy', description: 'Energy',
value: 'energy', value: 'ENERGY',
}, },
]), ]),
); );
interaction.followUp({ interaction.followUp({
embeds: [settingsEmbed], embeds: [/* settingsEmbed*/],
components: [menu], components: [menu],
}) })
.then((subjectMsg => { .then((subjectMsg => {
const w = subjectMsg as Message;
let h;
const subjectFilter = i => ['selectsubject'].includes(i.customId) && i.user.id == interaction.user.id; // <== ATTENTION! First argument... const subjectFilter = i => ['selectsubject'].includes(i.customId) && i.user.id == interaction.user.id; // <== ATTENTION! First argument...
w.awaitMessageComponent({ filter: subjectFilter, componentType: 'SELECT_MENU' }) (subjectMsg as Message).awaitMessageComponent({ filter: subjectFilter, componentType: 'SELECT_MENU' })
.then(subjectChoice => { .then(async subjectChoice => {
let sendstring = 'Subjects set to: '; const vals = subjectChoice.values;
h = subjectChoice.values; await userConfig.findOneAndUpdate({ _id: interaction.user.id }, { subjects: vals }, { upsert: true, new: true });
if (h.includes('astro')) { const subjects = new Array<string>();
// astro processing code here await vals.forEach(v => {
sendstring = sendstring + 'Astronomy, '; subjects.push(v.toLowerCase().split(' ').map(w => w[0].toUpperCase() + w.substring(1)).join(' '));
} });
if (h.includes('bio')) { await interaction.editReply({ content: `Subjects set to: ${subjects.toString().split(',').join(', ')}`, components: [], embeds: [] });
// bio processing code here
sendstring = sendstring + 'Biology, ';
}
if (h.includes('es')) {
// earth science processing code here
sendstring = sendstring + 'Earth Science, ';
}
if (h.includes('chem')) {
// chemistry processing code here
sendstring = sendstring + 'Chemistry, ';
}
if (h.includes('phy')) {
// physics processing code here
sendstring = sendstring + 'Physics, ';
}
if (h.includes('math')) {
// math processing code here
sendstring = sendstring + 'Math, ';
}
if (h.includes('energy')) {
// energy processing code here
sendstring = sendstring + 'Energy, ';
}
sendstring = sendstring.slice(0, -2);
interaction.editReply({ content: sendstring, components: [] });
}); });
})); }));
break; break;

5
src/commands/train.ts

@ -5,6 +5,7 @@ import { decode } from 'html-entities';
import axios from 'axios'; import axios from 'axios';
import userScore from '../models/userScore'; import userScore from '../models/userScore';
import userConfig from '../models/userConfig';
import log from '../helpers/log.js'; import log from '../helpers/log.js';
import { updateScore } from '../helpers/db.js'; import { updateScore } from '../helpers/db.js';
@ -60,10 +61,12 @@ export async function execute(interaction : CommandInteraction) {
}); });
let categoryArray : string[] = []; let categoryArray : string[] = [];
const configCategories = await userConfig.findById(interaction.user.id);
switch (subject) { switch (subject) {
case null: case null:
categoryArray = ['BIOLOGY', 'PHYSICS', 'CHEMISTRY', 'EARTH AND SPACE', 'ASTRONOMY', 'MATH']; // categoryArray = ['BIOLOGY', 'PHYSICS', 'CHEMISTRY', 'EARTH AND SPACE', 'ASTRONOMY', 'MATH'];
categoryArray = configCategories.subjects;
break; break;
case 'astro': case 'astro':
case 'astronomy': case 'astronomy':

9
src/models/userConfig.ts

@ -1,18 +1,15 @@
import mongoose from 'mongoose'; import mongoose from 'mongoose';
const userConfigSchema = new mongoose.Schema({ const userConfigSchema = new mongoose.Schema({
authorID: { _id: String,
type: String,
required: true,
},
subjects: { subjects: {
type: [String], type: [String],
required: false, required: true,
default: null, default: null,
}, },
gradeLevels: { gradeLevels: {
type: [String], type: [String],
required: false, required: true,
default: null, default: null,
}, },
}); });

Loading…
Cancel
Save