Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/bot/src/app/commands/(User)/hevy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const chatInput: ChatInputCommand = async ({ interaction }) => {
const pendingUserVerification = await findOrCreateUserVerification(
discordUserId,
hevyUsername,
interaction as unknown as ChatInputCommandInteraction,
);

if (!pendingUserVerification) {
Expand Down
53 changes: 47 additions & 6 deletions apps/bot/src/features/core/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { cacheLife, cacheTag, revalidateTag } from "@commandkit/cache";
import { HevyVerification, prisma } from "@repo/db";

import client from "@/app";
import { MessageFlags } from "discord.js";
import { MessageFlags, TextChannel, User } from "discord.js";
import { successfulyLinkedToHevy } from "../hevy/hevy.embeds";
import {
getUserByDiscordId,
UserWithHevyVerification,
} from "../hevy/hevy.service";
import { generatePrivateFollowInstructionsComponents } from "../hevy/verification.embeds";
import { Logger } from "commandkit";
export const setAutoShareEnabledStatus = async (
guildId: string,
user: UserWithHevyVerification,
Expand Down Expand Up @@ -77,18 +78,58 @@ export const getUserAutoShareConfig = async (
});
};

export const sendSuccessfullVerificationDM = async (
export const sendSuccessfullVerificationMessage = async (
verification: HevyVerification,
) => {
const user = client.users.cache.get(verification.userDiscordId);
if (user) {
await user.send({
flags: MessageFlags.IsComponentsV2,
components: successfulyLinkedToHevy(verification, false),
});
Logger.info(`Sending verification DM`);
try {
await sendSuccessfullVerificationDM(user, verification);
} catch (error) {
Logger.warn(error);
Logger.warn(
`Failed to send verification DM. Replying to original interaction.`,
);
await sendSuccessfullVerificationViaInteraction(verification);
}
}
};

export const sendSuccessfullVerificationDM = async (
user: User,
verification: HevyVerification,
) => {
await user.send({
flags: MessageFlags.IsComponentsV2,
components: successfulyLinkedToHevy(verification, false),
});
};

export const sendSuccessfullVerificationViaInteraction = async (
verification: HevyVerification,
) => {
if (
verification.originalInteractionChannelId == null ||
verification.originalInteractionId == null
)
return;
const channel = (await client.channels.fetch(
verification.originalInteractionChannelId,
)) as TextChannel;
if (!channel) return;

const interaction = await channel.messages.fetch(
verification.originalInteractionId,
);

await interaction.reply({
flags: MessageFlags.IsComponentsV2 | MessageFlags.Ephemeral,

components: successfulyLinkedToHevy(verification, false),
});
};

export const sendPrivateAccountInstructionsDM = async (
userDiscordId: string,
) => {
Expand Down
12 changes: 9 additions & 3 deletions apps/bot/src/features/hevy/verification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Logger } from "commandkit";
import { track } from "commandkit/analytics";
import {
sendPrivateAccountInstructionsDM,
sendSuccessfullVerificationDM,
sendSuccessfullVerificationMessage,
} from "../core/user.service";
import { sendActivity } from "../liveActivity/liveActivity.service";
import {
Expand All @@ -20,6 +20,7 @@ import {
setIsHevyProfilePrivate,
setUserHevyUsername,
} from "./hevy.service";
import { ChatInputCommandInteraction } from "discord.js";

const MAX_CODE_GENERATION_ATTEMPTS = 10;

Expand All @@ -38,6 +39,7 @@ export const generateVerificationCode = (length: number) => {
export const insertVerificationCode = async (
discordId: string,
hevyUsername: string,
interaction?: ChatInputCommandInteraction,
) => {
Logger.info(`Inserting verification code: ${discordId} - ${hevyUsername}`);
let verificationCode = "";
Expand All @@ -61,6 +63,8 @@ export const insertVerificationCode = async (
workoutId: verificationConfig.workoutShortId,
userDiscordId: discordId,
username: hevyUsername,
originalInteractionId: interaction?.id,
originalInteractionChannelId: interaction?.channelId,
},
});

Expand Down Expand Up @@ -141,6 +145,7 @@ export const getRemainingPendingVerifications = async (): Promise<
export const findOrCreateUserVerification = async (
discordId: string,
hevyUsername: string,
interaction?: ChatInputCommandInteraction,
) => {
let hevyVerification = await getUserLatestPendingVerification(
discordId,
Expand All @@ -150,7 +155,8 @@ export const findOrCreateUserVerification = async (
if (hevyVerification == null) {
Logger.info(`No verification for this user. Creating one.`);
hevyVerification =
(await insertVerificationCode(discordId, hevyUsername)) ?? null;
(await insertVerificationCode(discordId, hevyUsername, interaction)) ??
null;
} else {
Logger.info(`User verification already exists. Returning it.`);
}
Expand Down Expand Up @@ -215,7 +221,7 @@ export const executeVerificationTask = async () => {
verification.userDiscordId,
hevyProfile.private_profile,
);
await sendSuccessfullVerificationDM(verification);
await sendSuccessfullVerificationMessage(verification);
await deleteComment(correspondingComment.id);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "public"."HevyVerification" ADD COLUMN "orignalInteractionId" TEXT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Warnings:

- You are about to drop the column `orignalInteractionId` on the `HevyVerification` table. All the data in the column will be lost.

*/
-- AlterTable
ALTER TABLE "public"."HevyVerification" DROP COLUMN "orignalInteractionId",
ADD COLUMN "originalInteractionChannelId" TEXT,
ADD COLUMN "originalInteractionId" TEXT;
24 changes: 13 additions & 11 deletions packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ enum UserVerificationStatus {
}

model HevyVerification {
id String @id @unique @default(cuid())
lastCheck DateTime?
workoutId String
verificationCode String @unique
status UserVerificationStatus @default(pending)
User User @relation(fields: [userDiscordId], references: [discordId])
userDiscordId String @unique
username String
privateProfile Boolean @default(false)
followedByBot Boolean @default(false)
privateInstructionsSent Boolean @default(false)
id String @id @unique @default(cuid())
lastCheck DateTime?
workoutId String
verificationCode String @unique
status UserVerificationStatus @default(pending)
User User @relation(fields: [userDiscordId], references: [discordId])
userDiscordId String @unique
username String
privateProfile Boolean @default(false)
followedByBot Boolean @default(false)
privateInstructionsSent Boolean @default(false)
originalInteractionId String?
originalInteractionChannelId String?

createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand Down
Loading