-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from datasektionen/feat/nv/verify-slash-command
feat(verify): add new command
- Loading branch information
Showing
10 changed files
with
173 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Message } from "discord.js"; | ||
|
||
export const isKthEmail = (messageText: string) => | ||
new RegExp(/^[a-zA-Z0-9]+@kth[.]se$/).test(messageText); | ||
|
||
export const messageIsToken = (messageText: string) => | ||
messageText.match(/^[a-zA-Z0-9_-]+$/); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ChatInputCommandInteraction, CommandInteraction } from "discord.js"; | ||
import { token_discord, token_email } from "../../../database_config"; | ||
import { generateToken } from "../../../utils/generate_token"; | ||
import { sendMail } from "../../../utils/mail"; | ||
import { isKthEmail } from "./util"; | ||
|
||
export const handleVerifyBegin = async ( | ||
interaction: ChatInputCommandInteraction | ||
) => { | ||
const { user, options } = interaction; | ||
const messageText = options.getString("email", true); | ||
if (!isKthEmail(messageText)) { | ||
await interaction.reply({ | ||
content: "Please, enter a valid KTH email address.", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
const token = generateToken(parseInt(process.env.TOKEN_SIZE as string)); | ||
const timeout = parseInt(process.env.TOKEN_TIMEOUT as string); | ||
await token_discord.set(token, user.id, timeout); | ||
await token_email.set(token, messageText, timeout); | ||
|
||
try { | ||
const result = await sendMail(messageText, token); | ||
console.log(`Email sent, received response: ${JSON.stringify(result)}`); | ||
} catch (error) { | ||
console.error(error); | ||
await interaction.reply({ | ||
content: "Something went wrong, please try again.", | ||
ephemeral: true, | ||
}); | ||
} | ||
await interaction.reply({ | ||
content: `Verification email sent, check ${messageText} for your verification code.`, | ||
ephemeral: true, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum VerifyCommandNames { | ||
BEGIN = "begin", | ||
SUBMIT = "submit", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { ChatInputCommandInteraction } from "discord.js"; | ||
import { | ||
token_discord, | ||
token_email, | ||
verified_users, | ||
} from "../../../database_config"; | ||
import { setN0llanRole, setRoleVerified } from "../../../utils/roles"; | ||
import { messageIsToken } from "./util"; | ||
|
||
export const handleVerifySubmit = async ( | ||
interaction: ChatInputCommandInteraction | ||
) => { | ||
const { user, options } = interaction; | ||
const messageText = options.getString("verification-code", true); | ||
|
||
if (!messageIsToken(messageText)) { | ||
await interaction.reply({ | ||
content: "Not a valid code", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
const [discordId, emailAddress] = await Promise.all([ | ||
token_discord.get(messageText) as Promise<string | undefined>, | ||
token_email.get(messageText) as Promise<string | undefined>, | ||
]); | ||
|
||
if (!emailAddress || !discordId || discordId !== user.id) { | ||
await interaction.reply({ | ||
content: | ||
"Verification unsuccessful, submit the code again or request a new code.", | ||
ephemeral: true, | ||
}); | ||
return; | ||
} | ||
|
||
verified_users.set(discordId, emailAddress); | ||
try { | ||
await setRoleVerified(user); | ||
await interaction.reply({ | ||
content: `Du är nu verifierad. Dubbelkolla att du har blivit tilldelad @**${process.env.DISCORD_VERIFIED_ROLE}** rollen!`, | ||
ephemeral: true, | ||
}); | ||
// setN0llanRole(user, emailAddress.split("@")[0]); | ||
} catch (error) { | ||
console.error(error); | ||
await interaction.reply({ | ||
content: "Something went wrong, please try again.", | ||
ephemeral: true, | ||
}); | ||
} | ||
return; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { SlashCommandBuilder } from "discord.js"; | ||
import { CommandNames } from "../command.names"; | ||
import { VerifyCommandNames } from "./subcommands/verify_command.names"; | ||
|
||
export const verifyCommand = new SlashCommandBuilder() | ||
.setName(CommandNames.VERIFY) | ||
.setDescription( | ||
"Automatically verifies that you are a kth student via @kth.se email" | ||
); | ||
|
||
verifyCommand.addSubcommand((subcommand) => | ||
subcommand | ||
.setName(VerifyCommandNames.BEGIN) | ||
.setDescription("Enter your @kth.se address to receive a verification code") | ||
.addStringOption((option) => | ||
option | ||
.setName("email") | ||
.setDescription("Your @kth address") | ||
.setRequired(true) | ||
) | ||
); | ||
|
||
verifyCommand.addSubcommand((subcommand) => | ||
subcommand | ||
.setName(VerifyCommandNames.SUBMIT) | ||
.setDescription("Verifies you with the code sent to your email") | ||
.addStringOption((option) => | ||
option | ||
.setName("verification-code") | ||
.setDescription("The code sent to your email address") | ||
.setRequired(true) | ||
) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ChatInputCommandInteraction, CommandInteraction } from "discord.js"; | ||
import { handleVerifyBegin } from "./subcommands/verify_begin.handler"; | ||
import { VerifyCommandNames } from "./subcommands/verify_command.names"; | ||
import { handleVerifySubmit } from "./subcommands/verify_submit.handler"; | ||
|
||
export const handleVerify = async ( | ||
interaction: ChatInputCommandInteraction | ||
) => { | ||
const subCommandName = interaction.options.getSubcommand(true); | ||
|
||
switch (subCommandName) { | ||
case VerifyCommandNames.BEGIN: | ||
await handleVerifyBegin(interaction); | ||
break; | ||
case VerifyCommandNames.SUBMIT: | ||
await handleVerifySubmit(interaction); | ||
break; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters