diff --git a/src/commands/add/add.command.ts b/src/commands/add/add.command.ts index 6f9dbab..6170025 100644 --- a/src/commands/add/add.command.ts +++ b/src/commands/add/add.command.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from "discord.js"; -import { CommandNames } from "../command.names"; +import { CommandNames } from "../command-names.enum"; export const addCommand = new SlashCommandBuilder() .setName(CommandNames.ADD) diff --git a/src/commands/command.names.ts b/src/commands/command-names.enum.ts similarity index 100% rename from src/commands/command.names.ts rename to src/commands/command-names.enum.ts diff --git a/src/commands/ping/ping.command.ts b/src/commands/ping/ping.command.ts index e875e4a..6767280 100644 --- a/src/commands/ping/ping.command.ts +++ b/src/commands/ping/ping.command.ts @@ -1,5 +1,5 @@ import { SlashCommandBuilder } from "discord.js"; -import { CommandNames } from "../command.names"; +import { CommandNames } from "../command-names.enum"; export const pingCommand = new SlashCommandBuilder() .setName(CommandNames.PING) diff --git a/src/commands/register_commands.ts b/src/commands/register-commands.ts similarity index 72% rename from src/commands/register_commands.ts rename to src/commands/register-commands.ts index 67a2c47..b26aab9 100644 --- a/src/commands/register_commands.ts +++ b/src/commands/register-commands.ts @@ -2,11 +2,11 @@ import { discordClient } from ".."; import { getGuild } from "../utils/guild"; import { handleAdd } from "./add/add.handler"; import { commands } from "./commands"; -import { CommandNames } from "./command.names"; +import { CommandNames } from "./command-names.enum"; import { handlePing } from "./ping/ping.handler"; import { handleVerify } from "./verify/verify.handler"; -export const register_commands = async () => { +export const registerCommands = async () => { const guild = await getGuild(); commands.forEach((command) => guild.commands.create(command)); @@ -14,16 +14,14 @@ export const register_commands = async () => { if (!interaction.isChatInputCommand()) { return; } + switch (interaction.commandName) { case CommandNames.PING: - await handlePing(interaction); - break; + return await handlePing(interaction); case CommandNames.ADD: - await handleAdd(interaction); - break; + return await handleAdd(interaction); case CommandNames.VERIFY: - await handleVerify(interaction); - break; + return await handleVerify(interaction); } }); }; diff --git a/src/commands/verify/subcommands/verify_begin.handler.ts b/src/commands/verify/subcommands/verify-begin.handler.ts similarity index 81% rename from src/commands/verify/subcommands/verify_begin.handler.ts rename to src/commands/verify/subcommands/verify-begin.handler.ts index c08891e..ae5960b 100644 --- a/src/commands/verify/subcommands/verify_begin.handler.ts +++ b/src/commands/verify/subcommands/verify-begin.handler.ts @@ -1,6 +1,6 @@ import { ChatInputCommandInteraction, CommandInteraction } from "discord.js"; -import { token_discord, token_email } from "../../../database_config"; -import { generateToken } from "../../../utils/generate_token"; +import { tokenDiscord, tokenEmail } from "../../../database-config"; +import { generateToken } from "../../../utils/generate-token"; import { sendMail } from "../../../utils/mail"; import { isKthEmail } from "./util"; @@ -19,8 +19,8 @@ export const handleVerifyBegin = async ( 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); + await tokenDiscord.set(token, user.id, timeout); + await tokenEmail.set(token, messageText, timeout); try { const result = await sendMail(messageText, token); diff --git a/src/commands/verify/subcommands/verify_submit.handler.ts b/src/commands/verify/subcommands/verify-submit.handler.ts similarity index 71% rename from src/commands/verify/subcommands/verify_submit.handler.ts rename to src/commands/verify/subcommands/verify-submit.handler.ts index acc6cb2..8d533b7 100644 --- a/src/commands/verify/subcommands/verify_submit.handler.ts +++ b/src/commands/verify/subcommands/verify-submit.handler.ts @@ -1,17 +1,21 @@ import { ChatInputCommandInteraction } from "discord.js"; import { - token_discord, - token_email, - verified_users, -} from "../../../database_config"; + tokenDiscord, + tokenEmail, + verifiedUsers, +} from "../../../database-config"; import { setN0llanRole, setRoleVerified } from "../../../utils/roles"; import { messageIsToken } from "./util"; +export enum VariableNames { + VERIFICATION_CODE = "verification-code", +} + export const handleVerifySubmit = async ( interaction: ChatInputCommandInteraction ) => { - const { user, options } = interaction; - const messageText = options.getString("verification-code", true); + const { member, user, options } = interaction; + const messageText = options.getString(VariableNames.VERIFICATION_CODE, true); if (!messageIsToken(messageText)) { await interaction.reply({ @@ -22,8 +26,8 @@ export const handleVerifySubmit = async ( } const [discordId, emailAddress] = await Promise.all([ - token_discord.get(messageText) as Promise, - token_email.get(messageText) as Promise, + tokenDiscord.get(messageText) as Promise, + tokenEmail.get(messageText) as Promise, ]); if (!emailAddress || !discordId || discordId !== user.id) { @@ -35,7 +39,7 @@ export const handleVerifySubmit = async ( return; } - verified_users.set(discordId, emailAddress); + verifiedUsers.set(discordId, emailAddress); try { await setRoleVerified(user); await interaction.reply({ diff --git a/src/commands/verify/subcommands/verify_command.names.ts b/src/commands/verify/verify-subcommands-names.enum.ts similarity index 52% rename from src/commands/verify/subcommands/verify_command.names.ts rename to src/commands/verify/verify-subcommands-names.enum.ts index 2f82961..639693a 100644 --- a/src/commands/verify/subcommands/verify_command.names.ts +++ b/src/commands/verify/verify-subcommands-names.enum.ts @@ -1,4 +1,4 @@ -export enum VerifyCommandNames { +export enum VerifySubcommandNames { BEGIN = "begin", SUBMIT = "submit", } diff --git a/src/commands/verify/verify.command.ts b/src/commands/verify/verify.command.ts index 05afe8d..39d5391 100644 --- a/src/commands/verify/verify.command.ts +++ b/src/commands/verify/verify.command.ts @@ -1,6 +1,6 @@ import { SlashCommandBuilder } from "discord.js"; -import { CommandNames } from "../command.names"; -import { VerifyCommandNames } from "./subcommands/verify_command.names"; +import { CommandNames } from "../command-names.enum"; +import { VerifySubcommandNames } from "./verify-subcommands-names.enum"; export const verifyCommand = new SlashCommandBuilder() .setName(CommandNames.VERIFY) @@ -10,7 +10,7 @@ export const verifyCommand = new SlashCommandBuilder() verifyCommand.addSubcommand((subcommand) => subcommand - .setName(VerifyCommandNames.BEGIN) + .setName(VerifySubcommandNames.BEGIN) .setDescription("Enter your @kth.se address to receive a verification code") .addStringOption((option) => option @@ -22,7 +22,7 @@ verifyCommand.addSubcommand((subcommand) => verifyCommand.addSubcommand((subcommand) => subcommand - .setName(VerifyCommandNames.SUBMIT) + .setName(VerifySubcommandNames.SUBMIT) .setDescription("Verifies you with the code sent to your email") .addStringOption((option) => option diff --git a/src/commands/verify/verify.handler.ts b/src/commands/verify/verify.handler.ts index 0041f8a..c44cdca 100644 --- a/src/commands/verify/verify.handler.ts +++ b/src/commands/verify/verify.handler.ts @@ -1,7 +1,7 @@ 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"; +import { handleVerifyBegin } from "./subcommands/verify-begin.handler"; +import { VerifySubcommandNames } from "./verify-subcommands-names.enum"; +import { handleVerifySubmit } from "./subcommands/verify-submit.handler"; export const handleVerify = async ( interaction: ChatInputCommandInteraction @@ -9,10 +9,10 @@ export const handleVerify = async ( const subCommandName = interaction.options.getSubcommand(true); switch (subCommandName) { - case VerifyCommandNames.BEGIN: + case VerifySubcommandNames.BEGIN: await handleVerifyBegin(interaction); break; - case VerifyCommandNames.SUBMIT: + case VerifySubcommandNames.SUBMIT: await handleVerifySubmit(interaction); break; } diff --git a/src/database-config.ts b/src/database-config.ts new file mode 100644 index 0000000..f0ab94a --- /dev/null +++ b/src/database-config.ts @@ -0,0 +1,11 @@ +import Keyv from "keyv"; + +// TODO: We probably do not need 3 key-value stores for this if we used some class for this. +const tokenEmail = new Keyv(); +const tokenDiscord = new Keyv(); +const verifiedUsers = new Keyv(); + +tokenDiscord.clear(); +tokenEmail.clear(); + +export { tokenEmail, tokenDiscord, verifiedUsers }; diff --git a/src/database_config.ts b/src/database_config.ts deleted file mode 100644 index b3bd4c2..0000000 --- a/src/database_config.ts +++ /dev/null @@ -1,11 +0,0 @@ -import Keyv from "keyv"; - -// TODO: We probably do not need 3 key-value stores for this if we used some class for this. -const token_email = new Keyv(); -const token_discord = new Keyv(); -const verified_users = new Keyv(); - -token_discord.clear(); -token_email.clear(); - -export { token_email, token_discord, verified_users }; diff --git a/src/index.ts b/src/index.ts index 5d68500..9c9e98f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,7 @@ import { Client as DiscordClient, GatewayIntentBits } from "discord.js"; -import { register_commands } from "./commands/register_commands"; -import { onMessage } from "./messages/on_message"; -import { onWelcome } from "./messages/on_welcome"; +import { registerCommands } from "./commands/register-commands"; +import { onMessage } from "./messages/on-message"; +import { onWelcome } from "./messages/on-welcome"; /**p * Goes through all dotenv vars and checks if they are defined. @@ -32,6 +32,6 @@ async function main() { discordClient.on("message", onMessage); discordClient.on("guildMemberAdd", onWelcome); - register_commands(); + registerCommands(); } main(); diff --git a/src/messages/on_dm.ts b/src/messages/on-dm.ts similarity index 80% rename from src/messages/on_dm.ts rename to src/messages/on-dm.ts index f336b0d..34ffcbc 100644 --- a/src/messages/on_dm.ts +++ b/src/messages/on-dm.ts @@ -1,8 +1,8 @@ import { setN0llanRole, setRoleVerified } from "../utils/roles"; -import { token_discord, token_email, verified_users } from "../database_config"; +import { tokenDiscord, tokenEmail, verifiedUsers } from "../database-config"; import { sendMail } from "../utils/mail"; import { Message } from "discord.js"; -import { generateToken } from "../utils/generate_token"; +import { generateToken } from "../utils/generate-token"; import { isKthEmail, messageIsToken, @@ -12,8 +12,8 @@ export async function onDM(message: Message, messageText: string) { if (isKthEmail(messageText)) { const token = generateToken(parseInt(process.env.TOKEN_SIZE as string)); const timeout = parseInt(process.env.TOKEN_TIMEOUT as string); - await token_discord.set(token, message.author.id, timeout); - await token_email.set(token, messageText, timeout); + await tokenDiscord.set(token, message.author.id, timeout); + await tokenEmail.set(token, messageText, timeout); let result; try { @@ -29,12 +29,12 @@ export async function onDM(message: Message, messageText: string) { if (messageIsToken(messageText)) { const [discordId, emailAddress] = await Promise.all([ - token_discord.get(messageText) as Promise, - token_email.get(messageText) as Promise, + tokenDiscord.get(messageText) as Promise, + tokenEmail.get(messageText) as Promise, ]); if (emailAddress && discordId && discordId !== message.author.id) { - verified_users.set(discordId, emailAddress); + verifiedUsers.set(discordId, emailAddress); try { await setRoleVerified(message.author); message.channel.send( diff --git a/src/messages/on_message.ts b/src/messages/on-message.ts similarity index 98% rename from src/messages/on_message.ts rename to src/messages/on-message.ts index 162cf1d..9e3ddee 100644 --- a/src/messages/on_message.ts +++ b/src/messages/on-message.ts @@ -1,6 +1,6 @@ import { ChannelType, Message } from "discord.js"; import { hasRoleVerified } from "../utils/roles"; -import { onDM } from "./on_dm"; +import { onDM } from "./on-dm"; /** * Listens to message events emitted to the Client, and responds according to diff --git a/src/messages/on_welcome.ts b/src/messages/on-welcome.ts similarity index 100% rename from src/messages/on_welcome.ts rename to src/messages/on-welcome.ts diff --git a/src/utils/generate_token.ts b/src/utils/generate-token.ts similarity index 100% rename from src/utils/generate_token.ts rename to src/utils/generate-token.ts