diff --git a/src/commands/commands.names.ts b/src/commands/commands.names.ts index ed2625a..cf73389 100644 --- a/src/commands/commands.names.ts +++ b/src/commands/commands.names.ts @@ -12,4 +12,5 @@ export enum CommandNames { NOLLEGRUPP = "nollegrupp", MOTTAGNINGEN = "mottagningen", TEST = "test", + UNVERIFY = "unverify", } diff --git a/src/commands/commands.ts b/src/commands/commands.ts index 438809c..ff6cd76 100644 --- a/src/commands/commands.ts +++ b/src/commands/commands.ts @@ -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 @@ -31,6 +32,7 @@ export const getOfficialBotCommands = async (): Promise< kthIdCommand, nollegruppCommand, mottagningenCommand, + unverifyCommand, ]; export const getLightBotCommands = async (): Promise< diff --git a/src/commands/handle-commands.ts b/src/commands/handle-commands.ts index 2238b03..c81c3ea 100644 --- a/src/commands/handle-commands.ts +++ b/src/commands/handle-commands.ts @@ -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 @@ -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 diff --git a/src/commands/unverify/unverify.command.ts b/src/commands/unverify/unverify.command.ts new file mode 100644 index 0000000..966b2c3 --- /dev/null +++ b/src/commands/unverify/unverify.command.ts @@ -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) +); diff --git a/src/commands/unverify/unverify.handler.ts b/src/commands/unverify/unverify.handler.ts new file mode 100644 index 0000000..92a2581 --- /dev/null +++ b/src/commands/unverify/unverify.handler.ts @@ -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 { + 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 => { + 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})".` + ); +} diff --git a/src/commands/unverify/unverify.variables.ts b/src/commands/unverify/unverify.variables.ts new file mode 100644 index 0000000..0f114bd --- /dev/null +++ b/src/commands/unverify/unverify.variables.ts @@ -0,0 +1,3 @@ +export enum UnverifyVariables { + USER = "user", +} diff --git a/src/db/db.ts b/src/db/db.ts index cf737be..9f60b84 100644 --- a/src/db/db.ts +++ b/src/db/db.ts @@ -48,6 +48,15 @@ export async function insertUser( return true; } +export async function deleteUser(discordId: string): Promise { + try { + await sql`delete from users where discord_id = ${discordId}`; + } catch (err) { + return false; + } + return true; +} + export async function getDiscordIdByKthid( kthId: string ): Promise { diff --git a/src/shared/utils/auth.ts b/src/shared/utils/auth.ts index 48ab553..6099f4a 100644 --- a/src/shared/utils/auth.ts +++ b/src/shared/utils/auth.ts @@ -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);