Skip to content

Commit

Permalink
fix: add returns after interaction replies and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasvatn committed Aug 27, 2022
1 parent c4e0093 commit 0faf87a
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 28 deletions.
3 changes: 2 additions & 1 deletion src/commands/add/add.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChatInputCommandInteraction } from "discord.js";
import { ChatInputCommandInteraction, InteractionResponse } from "discord.js";

export const handleAdd = async (interaction: ChatInputCommandInteraction) => {
const { options } = interaction;
Expand All @@ -9,4 +9,5 @@ export const handleAdd = async (interaction: ChatInputCommandInteraction) => {
content: `The sum of the two numbers is: ${num1 + num2}`,
ephemeral: true,
});
return;
};
5 changes: 3 additions & 2 deletions src/commands/ping/ping.handler.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CommandInteraction } from "discord.js";
import { ChatInputCommandInteraction, InteractionResponse } from "discord.js";

export const handlePing = async (interaction: CommandInteraction) => {
export const handlePing = async (interaction: ChatInputCommandInteraction) => {
await interaction.reply({
content: "pong",
ephemeral: true,
});
return;
};
25 changes: 17 additions & 8 deletions src/commands/register-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import { handleVerify } from "./verify/verify.handler";

export const registerCommands = async () => {
const guild = await getGuild();
commands.forEach((command) => guild.commands.create(command));
Promise.all(commands.map((command) => guild.commands.create(command)));

discordClient.on("interactionCreate", async (interaction) => {
if (!interaction.isChatInputCommand()) {
return;
}

switch (interaction.commandName) {
case CommandNames.PING:
return await handlePing(interaction);
case CommandNames.ADD:
return await handleAdd(interaction);
case CommandNames.VERIFY:
return await handleVerify(interaction);
try {
switch (interaction.commandName) {
case CommandNames.PING:
await handlePing(interaction);
return;
case CommandNames.ADD:
await handleAdd(interaction);
return;
case CommandNames.VERIFY:
await handleVerify(interaction);
return;
default:
throw new Error(`Command name not found ${interaction.commandName}`);
}
} catch (error) {
console.error(error);
}
});
};
12 changes: 7 additions & 5 deletions src/commands/verify/subcommands/verify-begin.handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChatInputCommandInteraction, CommandInteraction } from "discord.js";
import { ChatInputCommandInteraction, InteractionResponse } from "discord.js";
import { tokenDiscord, tokenEmail } from "../../../database-config";
import { generateToken } from "../../../utils/generate-token";
import { sendMail } from "../../../utils/mail";
Expand All @@ -25,15 +25,17 @@ export const handleVerifyBegin = async (
try {
const result = await sendMail(messageText, token);
console.log(`Email sent, received response: ${JSON.stringify(result)}`);
await interaction.reply({
content: `Verification email sent, check ${messageText} for your verification code.`,
ephemeral: true,
});
return;
} catch (error) {
console.error(error);
await interaction.reply({
content: "Something went wrong, please try again.",
ephemeral: true,
});
return;
}
await interaction.reply({
content: `Verification email sent, check ${messageText} for your verification code.`,
ephemeral: true,
});
};
7 changes: 4 additions & 3 deletions src/commands/verify/subcommands/verify-submit.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
tokenEmail,
verifiedUsers,
} from "../../../database-config";
import { setN0llanRole, setRoleVerified } from "../../../utils/roles";
import { setRoleVerified } from "../../../utils/roles";
import { messageIsToken } from "./util";

export enum VariableNames {
Expand All @@ -14,7 +14,7 @@ export enum VariableNames {
export const handleVerifySubmit = async (
interaction: ChatInputCommandInteraction
) => {
const { member, user, options } = interaction;
const { user, options } = interaction;
const messageText = options.getString(VariableNames.VERIFICATION_CODE, true);

if (!messageIsToken(messageText)) {
Expand Down Expand Up @@ -46,13 +46,14 @@ export const handleVerifySubmit = async (
content: `Du är nu verifierad. Dubbelkolla att du har blivit tilldelad @**${process.env.DISCORD_VERIFIED_ROLE}** rollen!`,
ephemeral: true,
});
return;
// setN0llanRole(user, emailAddress.split("@")[0]);
} catch (error) {
console.error(error);
await interaction.reply({
content: "Something went wrong, please try again.",
ephemeral: true,
});
return;
}
return;
};
12 changes: 6 additions & 6 deletions src/commands/verify/verify.handler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChatInputCommandInteraction, CommandInteraction } from "discord.js";
import { ChatInputCommandInteraction, InteractionResponse } from "discord.js";
import { handleVerifyBegin } from "./subcommands/verify-begin.handler";
import { VerifySubcommandNames } from "./verify-subcommands-names.enum";
import { handleVerifySubmit } from "./subcommands/verify-submit.handler";
import { VerifySubcommandNames } from "./verify-subcommands-names.enum";

export const handleVerify = async (
interaction: ChatInputCommandInteraction
Expand All @@ -10,10 +10,10 @@ export const handleVerify = async (

switch (subCommandName) {
case VerifySubcommandNames.BEGIN:
await handleVerifyBegin(interaction);
break;
return await handleVerifyBegin(interaction);
case VerifySubcommandNames.SUBMIT:
await handleVerifySubmit(interaction);
break;
return await handleVerifySubmit(interaction);
default:
throw new Error(`Command name not found ${interaction.commandName}`);
}
};
4 changes: 2 additions & 2 deletions src/utils/guild.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Guild, User } from "discord.js";
import { Guild, GuildMember, User } from "discord.js";
import { discordClient } from "..";

export async function getGuild(): Promise<Guild> {
Expand All @@ -7,7 +7,7 @@ export async function getGuild(): Promise<Guild> {
);
}

export async function getGuildMember(user: User) {
export async function getGuildMember(user: User): Promise<GuildMember> {
const guild = await getGuild();
return await guild.members.fetch(user);
}
2 changes: 1 addition & 1 deletion src/utils/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function setRoleVerified(user: User): Promise<void> {
await setRole(user, process.env.DISCORD_VERIFIED_ROLE as string);
}

export async function setN0llanRole(user: User, kthId: string) {
export async function setN0llanRole(user: User, kthId: string): Promise<void> {
const hodisUser = await getHodisUser(kthId);
if (hodisUser.tag.split(",").includes("D22")) {
await setRole(user, "n0llan");
Expand Down

0 comments on commit 0faf87a

Please sign in to comment.