Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/commands/commands.names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export enum CommandNames {
NOLLEGRUPP = "nollegrupp",
MOTTAGNINGEN = "mottagningen",
TEST = "test",
UNVERIFY = "unverify",
}
2 changes: 2 additions & 0 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { messageCommand } from "./message/message.command";
import { ContextMenuCommandBuilder, SlashCommandBuilder } from "discord.js";
import { kthIdCommand } from "./kthid/kthid.command";
import { mottagningenCommand } from "./mottagningen/mottagningen.command";
import { unverifyCommand } from "./unverify/unverify.command";

type ApplicationCommandBuilder =
| SlashCommandBuilder
Expand All @@ -31,6 +32,7 @@ export const getOfficialBotCommands = async (): Promise<
kthIdCommand,
nollegruppCommand,
mottagningenCommand,
unverifyCommand,
];

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 @@ -33,6 +33,7 @@ import { handleNollegrupp } from "./nollegrupp/nollegrupp.handler";
import * as log from "../shared/utils/log";
import { handleMottagningen } from "./mottagningen/mottagningen.handler";
import { handleTest } from "../tests/test";
import { handleUnverify } from "./unverify/unverify.handler";

export async function handleInteractions(
interaction: Interaction
Expand Down Expand Up @@ -175,6 +176,9 @@ const handleChatInputCommand = async (
case CommandNames.TEST:
await handleTest(guildInteraction);
return;
case CommandNames.UNVERIFY:
await handleUnverify(guildInteraction);
return;
default:
throw new CommandNotFoundError(
guildInteraction.commandName
Expand Down
14 changes: 14 additions & 0 deletions src/commands/unverify/unverify.command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PermissionFlagsBits, SlashCommandBuilder } from "discord.js";
import { UnverifyVariables } from "./unverify.variables";

export const unverifyCommand = new SlashCommandBuilder()
.setName("unverify")
.setDescription("Remove a user's verification from HarmonyDB.")
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator);

unverifyCommand.addUserOption((option) =>
option
.setName(UnverifyVariables.USER)
.setDescription("A valid user or user ID.")
.setRequired(true)
);
39 changes: 39 additions & 0 deletions src/commands/unverify/unverify.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { MessageFlags } from "discord.js";
import { GuildChatInputCommandInteraction } from "../../shared/types/GuildChatInputCommandType";
import { UnverifyVariables } from "./unverify.variables";
import { deleteUser } from "../../db/db";
import { removeRole } from "../../shared/utils/roles";
import * as log from "../../shared/utils/log";

export async function handleUnverify(
interaction: GuildChatInputCommandInteraction
): Promise<void> {
const { options } = interaction;
const user = options.getUser(UnverifyVariables.USER, true);
await interaction.deferReply({ flags: MessageFlags.Ephemeral });

const success = await deleteUser(user.id);

if (!success) {
interaction.editReply({
content:
"Unverification failed under mysterious circumstances. Contact D-Sys to unverify the user.",
});
log.warning("Unverification failed under mysterious circumstances.");
}

// And now, remove the verified role on all servers.
const guilds = await interaction.client.guilds.fetch();

Promise.all(
guilds.map(async (e): Promise<void> => {
const guild = await e.fetch();
removeRole(user, "verified", guild);
})
);

interaction.editReply({ content: "You have been unverified!" });
log.info(
`Successfully unverified user with user.id = "${user.id}", user.username = "${user.username})".`
);
}
3 changes: 3 additions & 0 deletions src/commands/unverify/unverify.variables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum UnverifyVariables {
USER = "user",
}
9 changes: 9 additions & 0 deletions src/db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ export async function insertUser(
return true;
}

export async function deleteUser(discordId: string): Promise<boolean> {
try {
await sql`delete from users where discord_id = ${discordId}`;
} catch (err) {
return false;
}
return true;
}

export async function getDiscordIdByKthid(
kthId: string
): Promise<string | null> {
Expand Down
2 changes: 0 additions & 2 deletions src/shared/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,6 @@ export async function setAliasRole(

year += 2000;

log.info(`${year}`);

const date = new Date();
const difference =
date.getFullYear() - year - (date.getMonth() < 8 ? 1 : 0);
Expand Down
Loading