Skip to content

Commit

Permalink
refactor: use kebab-case for file names
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvatn committed Aug 27, 2022
1 parent 4dc7245 commit c4e0093
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/commands/add/add.command.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/commands/ping/ping.command.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@ 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));

discordClient.on("interactionCreate", async (interaction) => {
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);
}
});
};
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -22,8 +26,8 @@ export const handleVerifySubmit = async (
}

const [discordId, emailAddress] = await Promise.all([
token_discord.get(messageText) as Promise<string | undefined>,
token_email.get(messageText) as Promise<string | undefined>,
tokenDiscord.get(messageText) as Promise<string | undefined>,
tokenEmail.get(messageText) as Promise<string | undefined>,
]);

if (!emailAddress || !discordId || discordId !== user.id) {
Expand All @@ -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({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum VerifyCommandNames {
export enum VerifySubcommandNames {
BEGIN = "begin",
SUBMIT = "submit",
}
8 changes: 4 additions & 4 deletions src/commands/verify/verify.command.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions src/commands/verify/verify.handler.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
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
) => {
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;
}
Expand Down
11 changes: 11 additions & 0 deletions src/database-config.ts
Original file line number Diff line number Diff line change
@@ -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 };
11 changes: 0 additions & 11 deletions src/database_config.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -32,6 +32,6 @@ async function main() {

discordClient.on("message", onMessage);
discordClient.on("guildMemberAdd", onWelcome);
register_commands();
registerCommands();
}
main();
14 changes: 7 additions & 7 deletions src/messages/on_dm.ts → src/messages/on-dm.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 {
Expand All @@ -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<string>,
token_email.get(messageText) as Promise<string>,
tokenDiscord.get(messageText) as Promise<string>,
tokenEmail.get(messageText) as Promise<string>,
]);

if (emailAddress && discordId && discordId !== message.author.id) {
verified_users.set(discordId, emailAddress);
verifiedUsers.set(discordId, emailAddress);
try {
await setRoleVerified(message.author);
message.channel.send(
Expand Down
2 changes: 1 addition & 1 deletion src/messages/on_message.ts → src/messages/on-message.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit c4e0093

Please sign in to comment.