Skip to content

Commit

Permalink
Created and implemented an admin-only command to get a users KTH ID f…
Browse files Browse the repository at this point in the history
…rom their user ID/mentioning them
  • Loading branch information
alfredfl committed Dec 8, 2024
1 parent 9f13226 commit 9b5a39b
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/commands/commands.names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export enum CommandNames {
COMMUNITY = "community",
TRANSLATE_MSG = "Translate to English",
CLUB = "club",
KTHID = "kthid",
}
2 changes: 2 additions & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { translateMsgCommand } from "./translate/translateMsg.command";
import { clubCommand } from "./club/club.command";
import { messageCommand } from "./message/message.command";
import { ContextMenuCommandBuilder, SlashCommandBuilder } from "discord.js";
import { kthIdCommand } from "./kthid/kthid.command";

type ApplicationCommandBuilder =
| SlashCommandBuilder
Expand All @@ -27,6 +28,7 @@ export const getOfficialBotCommands = async (): Promise<
translateMsgCommand,
clubCommand,
messageCommand,
kthIdCommand,
];

export const getLightBotCommands = async (): Promise<
Expand Down
4 changes: 4 additions & 0 deletions src/commands/handle-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { handleTranslateMsg } from "./translate/translateMsg.handler";
import { handleClub } from "./club/club.handler";
import { handleMessage } from "./message/message.handler";
import { BaseInteraction } from "discord.js";
import { handleKthId } from "./kthid/kthid.handler";

export const handleCommands = (): void => {
harmonyClient.on("interactionCreate", async (interaction) => {
Expand Down Expand Up @@ -139,6 +140,9 @@ const handleChatInputCommand = async (
case CommandNames.MESSAGE:
await handleMessage(guildInteraction);
return;
case CommandNames.KTHID:
await handleKthId(guildInteraction);
return;
default:
throw new CommandNotFoundError(
guildInteraction.commandName
Expand Down
17 changes: 17 additions & 0 deletions src/commands/kthid/kthid.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { CommandNames } from "../commands.names";
import { KthIdVariables } from "./kthid.variables";

export const kthIdCommand = new SlashCommandBuilder()
.setName(CommandNames.KTHID)
.setDescription("Get the KTH ID of a user")
.setDefaultMemberPermissions(PermissionFlagsBits.ManageGuild);

kthIdCommand.addStringOption((option) =>
option
.setName(KthIdVariables.USER)
.setDescription(
"A valid user or user ID."
)
.setRequired(true)
);
23 changes: 23 additions & 0 deletions src/commands/kthid/kthid.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { getKthIdByUserId} from "../../db/db";
import { GuildChatInputCommandInteraction } from "../../shared/types/GuildChatInputCommandType";
import { KthIdVariables } from "./kthid.variables";

export const handleKthId = async (
interaction: GuildChatInputCommandInteraction
): Promise<void> => {
const { options } = interaction;
const user = options
.getString(KthIdVariables.USER, true)
.replace(/[\D]/g, '');

Check failure on line 11 in src/commands/kthid/kthid.handler.ts

View workflow job for this annotation

GitHub Actions / Lint and Build

Strings must use doublequote
const kthId = await getKthIdByUserId(user);
await interaction.deferReply({ ephemeral: true });
if (!kthId) {
await interaction.editReply({
content: `Found no KTH ID mathing the provided Discord account`,

Check failure on line 16 in src/commands/kthid/kthid.handler.ts

View workflow job for this annotation

GitHub Actions / Lint and Build

Strings must use doublequote
});
} else {
await interaction.editReply({
content: `The KTH ID of the provided user is: **${kthId}**`,
});
}
}
3 changes: 3 additions & 0 deletions src/commands/kthid/kthid.variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum KthIdVariables {
USER = "user"
}

0 comments on commit 9b5a39b

Please sign in to comment.