Skip to content

Commit ed1d938

Browse files
committed
Merge branch 'v13' of github.com:Ohtuproju2021syksy/Discord-Bot-better into v13
2 parents 1ae4e80 + 4bd9733 commit ed1d938

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

__tests__/jest/slashCommands/slash_instructors.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ const {
1111
const roleString = "test";
1212
const initialResponse = "Fetching instructors...";
1313

14+
const Course = {
15+
create: jest.fn(),
16+
findOne: jest
17+
.fn(() => true)
18+
.mockImplementationOnce(() => false),
19+
destroy: jest.fn(),
20+
};
21+
22+
const models = {
23+
Course,
24+
};
25+
1426
afterEach(() => {
1527
jest.clearAllMocks();
1628
});
@@ -19,7 +31,7 @@ describe("slash instructors command", () => {
1931
test("instructors command used outside course channels", async () => {
2032
const client = studentInteractionWithoutOptions.client;
2133
const response = "Use the command in a course channel.";
22-
await execute(studentInteractionWithoutOptions, client);
34+
await execute(studentInteractionWithoutOptions, client, models);
2335
expect(sendEphemeral).toHaveBeenCalledTimes(1);
2436
expect(sendEphemeral).toHaveBeenCalledWith(studentInteractionWithoutOptions, initialResponse);
2537
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
@@ -32,7 +44,7 @@ describe("slash instructors command", () => {
3244
client.guild.roles.create({ id: 5, name: `${roleString} ${courseAdminRole}`, members: [{ displayName: "teacher", user: { id: 1 } }] });
3345
client.guild.roles.create({ id: 1, name: "faculty", members: [{ displayName: "teacher", user: { id: 1 } }] });
3446
const response = `No instructors for ${roleString}`;
35-
await execute(studentInteractionWithoutOptions, client);
47+
await execute(studentInteractionWithoutOptions, client, models);
3648
expect(sendEphemeral).toHaveBeenCalledTimes(1);
3749
expect(sendEphemeral).toHaveBeenCalledWith(studentInteractionWithoutOptions, initialResponse);
3850
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);
@@ -44,7 +56,7 @@ describe("slash instructors command", () => {
4456
client.guild.roles.create({ id: 5, name: `${roleString} ${courseAdminRole}`, members: [{ displayName: "teacher", user: { id: 1 } }] });
4557
client.guild.roles.create({ id: 1, name: "faculty", members: [{ displayName: "teacher", user: { id: 1 } }] });
4658
const response = `No instructors for ${roleString}`;
47-
await execute(defaultStudentInteraction, client);
59+
await execute(defaultStudentInteraction, client, models);
4860
expect(sendEphemeral).toHaveBeenCalledTimes(1);
4961
expect(sendEphemeral).toHaveBeenCalledWith(defaultStudentInteraction, initialResponse);
5062
expect(editErrorEphemeral).toHaveBeenCalledTimes(1);

src/discordBot/commands/student/instructors.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@ const { SlashCommandBuilder } = require("@discordjs/builders");
22
const { getCourseNameFromCategory, listCourseInstructors } = require("../../services/service");
33
const { editEphemeral, editErrorEphemeral, sendEphemeral } = require("../../services/message");
44
const { courseAdminRole, facultyRole } = require("../../../../config.json");
5+
const { findCourseFromDb } = require("../../../db/services/courseService");
56

6-
const execute = async (interaction, client) => {
7+
const execute = async (interaction, client, models) => {
78
await sendEphemeral(interaction, "Fetching instructors...");
89
const guild = client.guild;
910

1011
const category = guild.channels.cache.get(interaction.channelId).parent;
11-
if (!category) {
12+
const courseName = category?.name ? getCourseNameFromCategory(category.name) : null;
13+
const course = await findCourseFromDb(courseName, models.Course);
14+
if (!category || !course) {
1215
return await editErrorEphemeral(interaction, "Use the command in a course channel.");
1316
}
14-
const roleString = getCourseNameFromCategory(category.name);
1517

16-
const adminsString = await listCourseInstructors(guild, roleString, courseAdminRole, facultyRole);
18+
const adminsString = await listCourseInstructors(guild, courseName, courseAdminRole, facultyRole);
1719

18-
if (adminsString === "") return await editErrorEphemeral(interaction, `No instructors for ${roleString}`);
20+
if (adminsString === "") return await editErrorEphemeral(interaction, `No instructors for ${courseName}`);
1921

20-
await editEphemeral(interaction, `Here are the instructors for ${roleString}: ${adminsString}`);
22+
await editEphemeral(interaction, `Here are the instructors for ${courseName}: ${adminsString}`);
2123
};
2224

2325
module.exports = {

0 commit comments

Comments
 (0)