Skip to content

Commit

Permalink
Add optional user tagging for information commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Delemangi committed Feb 10, 2025
1 parent adc69be commit a9b6995
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 14 deletions.
31 changes: 31 additions & 0 deletions src/commands/course.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
type GuildMember,
roleMention,
SlashCommandBuilder,
userMention,
} from 'discord.js';

import {
Expand Down Expand Up @@ -43,6 +44,9 @@ export const data = new SlashCommandBuilder()
.setDescription('Предмет')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
Expand All @@ -55,6 +59,9 @@ export const data = new SlashCommandBuilder()
.setDescription('Предмет')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
Expand All @@ -79,6 +86,9 @@ export const data = new SlashCommandBuilder()
.setDescription('Предмет')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
Expand All @@ -91,6 +101,9 @@ export const data = new SlashCommandBuilder()
.setDescription('Предмет')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
Expand All @@ -103,6 +116,9 @@ export const data = new SlashCommandBuilder()
.setDescription('Предмет')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
Expand All @@ -122,6 +138,8 @@ const handleCourseParticipants = async (
interaction: ChatInputCommandInteraction,
course: null | string,
) => {
const user = interaction.options.getUser('user');

const information = getParticipants().find(
(participants) =>
participants.course.toLowerCase() === course?.toLowerCase(),
Expand All @@ -135,6 +153,7 @@ const handleCourseParticipants = async (

const embed = getCourseParticipantsEmbed(information);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
Expand All @@ -143,6 +162,8 @@ const handleCourseProfessors = async (
interaction: ChatInputCommandInteraction,
course: null | string,
) => {
const user = interaction.options.getUser('user');

const information = getProfessors().find(
(staff) => staff.course.toLowerCase() === course?.toLowerCase(),
);
Expand All @@ -155,6 +176,7 @@ const handleCourseProfessors = async (

const embed = getCourseProfessorsEmbed(information);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
Expand Down Expand Up @@ -205,6 +227,8 @@ const handleCoursePrerequisite = async (
interaction: ChatInputCommandInteraction,
course: null | string,
) => {
const user = interaction.options.getUser('user');

const information = getPrerequisites().find(
(prerequisites) =>
prerequisites.course.toLowerCase() === course?.toLowerCase(),
Expand All @@ -218,6 +242,7 @@ const handleCoursePrerequisite = async (

const embed = getCoursePrerequisiteEmbed(information);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
Expand All @@ -226,6 +251,8 @@ const handleCourseInfo = async (
interaction: ChatInputCommandInteraction,
course: null | string,
) => {
const user = interaction.options.getUser('user');

const information = getInformation().find(
(info) => info.course.toLowerCase() === course?.toLowerCase(),
);
Expand All @@ -238,6 +265,7 @@ const handleCourseInfo = async (

const embed = getCourseInfoEmbed(information);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
Expand All @@ -246,6 +274,8 @@ const handleCourseSummary = async (
interaction: ChatInputCommandInteraction,
course: null | string,
) => {
const user = interaction.options.getUser('user');

if (course === null || !getCourses().includes(course)) {
await interaction.editReply(commandErrors.courseNotFound);

Expand All @@ -254,6 +284,7 @@ const handleCourseSummary = async (

const embeds = getCourseSummaryEmbed(course);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds,
});
};
Expand Down
6 changes: 6 additions & 0 deletions src/commands/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type ChatInputCommandInteraction,
type GuildMember,
SlashCommandBuilder,
userMention,
} from 'discord.js';

import { getCoursesPrerequisiteEmbed } from '../components/commands.js';
Expand Down Expand Up @@ -29,6 +30,9 @@ export const data = new SlashCommandBuilder()
.setDescription('Курс')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
Expand Down Expand Up @@ -62,9 +66,11 @@ const handleCoursesPrerequisite = async (
interaction: ChatInputCommandInteraction,
) => {
const course = interaction.options.getString('course', true);
const user = interaction.options.getUser('user');

const embed = getCoursesPrerequisiteEmbed(course);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
Expand Down
7 changes: 7 additions & 0 deletions src/commands/link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ChatInputCommandInteraction,
SlashCommandBuilder,
userMention,
} from 'discord.js';

import { getLinkComponents, getLinkEmbed } from '../components/commands.js';
Expand All @@ -21,10 +22,15 @@ export const data = new SlashCommandBuilder()
.setDescription('Линк')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
);

export const execute = async (interaction: ChatInputCommandInteraction) => {
const keyword = interaction.options.getString('link', true);
const user = interaction.options.getUser('user');

const link = Number.isNaN(Number(keyword))
? await getLink(keyword)
: await getNthLink(Number(keyword));
Expand All @@ -39,6 +45,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
const components = getLinkComponents(link);
await interaction.editReply({
components,
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
19 changes: 17 additions & 2 deletions src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ChatInputCommandInteraction,
SlashCommandBuilder,
userMention,
} from 'discord.js';

import {
Expand All @@ -22,15 +23,25 @@ export const data = new SlashCommandBuilder()
.addSubcommand((command) =>
command
.setName('questions')
.setDescription(commandDescriptions['list questions']),
.setDescription(commandDescriptions['list questions'])
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
)
.addSubcommand((command) =>
command.setName('links').setDescription(commandDescriptions['list links']),
command
.setName('links')
.setDescription(commandDescriptions['list links'])
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),
);

const handleListQuestions = async (
interaction: ChatInputCommandInteraction,
) => {
const user = interaction.options.getUser('user');

const questions = await getQuestions();

if (questions === null) {
Expand All @@ -43,11 +54,14 @@ const handleListQuestions = async (

const embed = getListQuestionsEmbed(questions);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};

const handleListLinks = async (interaction: ChatInputCommandInteraction) => {
const user = interaction.options.getUser('user');

const links = await getLinks();

if (links === null) {
Expand All @@ -60,6 +74,7 @@ const handleListLinks = async (interaction: ChatInputCommandInteraction) => {

const embed = getListLinksEmbed(links);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
Expand Down
8 changes: 7 additions & 1 deletion src/commands/session.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ChatInputCommandInteraction,
SlashCommandBuilder,
userMention,
} from 'discord.js';
import { access } from 'node:fs/promises';

Expand All @@ -21,10 +22,15 @@ export const data = new SlashCommandBuilder()
.setDescription('Сесија')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
);

export const execute = async (interaction: ChatInputCommandInteraction) => {
const session = interaction.options.getString('session', true);
const user = interaction.options.getUser('user');

const information = Object.entries(getSessions()).find(
([key]) => key.toLowerCase() === session.toLowerCase(),
);
Expand All @@ -46,7 +52,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {
}

await interaction.editReply({
content: information[0],
content: user ? userMention(user.id) : null,
files: [path],
});
};
7 changes: 7 additions & 0 deletions src/commands/staff.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ChatInputCommandInteraction,
SlashCommandBuilder,
userMention,
} from 'discord.js';

import { getStaffEmbed } from '../components/commands.js';
Expand All @@ -21,10 +22,15 @@ export const data = new SlashCommandBuilder()
.setDescription('Професор')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
);

export const execute = async (interaction: ChatInputCommandInteraction) => {
const professor = interaction.options.getString('professor', true);
const user = interaction.options.getUser('user');

const information = getStaff().find(
(staff) => staff.name.toLowerCase() === professor.toLowerCase(),
);
Expand All @@ -37,6 +43,7 @@ export const execute = async (interaction: ChatInputCommandInteraction) => {

const embed = getStaffEmbed(information);
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds: [embed],
});
};
13 changes: 7 additions & 6 deletions src/shared/commands/classroom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ChatInputCommandInteraction,
SlashCommandBuilder,
userMention,
} from 'discord.js';

import { getClassroomEmbed } from '../../components/commands.js';
Expand All @@ -9,7 +10,6 @@ import { type Command } from '../../lib/types/Command.js';
import {
commandDescriptions,
commandErrors,
commandResponseFunctions,
} from '../../translations/commands.js';

export const getCommonCommand = (
Expand All @@ -24,10 +24,15 @@ export const getCommonCommand = (
.setDescription('Просторија')
.setRequired(true)
.setAutocomplete(true),
)
.addUserOption((option) =>
option.setName('user').setDescription('Корисник').setRequired(false),
),

execute: async (interaction: ChatInputCommandInteraction) => {
const classroom = interaction.options.getString('classroom', true);
const user = interaction.options.getUser('user');

const charPos = classroom.indexOf('(');
const classroomName =
charPos === -1 ? classroom : classroom.slice(0, charPos).trim();
Expand All @@ -46,12 +51,8 @@ export const getCommonCommand = (

const embeds = classrooms.map((cl) => getClassroomEmbed(cl));
await interaction.editReply({
content: user ? userMention(user.id) : null,
embeds,
...(embeds.length > 1
? {
content: commandResponseFunctions.multipleClassrooms(classroomName),
}
: {}),
});
},
});
Loading

0 comments on commit a9b6995

Please sign in to comment.