a fun bot for the hc slack
1import { environment, slackApp } from "../../../index";
2import handleHelp from "../handlers/help";
3import { handleHistory } from "../handlers/history";
4import type { MessageResponse } from "../types";
5import * as Sentry from "@sentry/bun";
6import { blog } from "../../../libs/Logger";
7import handleHome from "../handlers/home";
8import { db } from "../../../libs/db";
9import { users as usersTable } from "../../../libs/schema";
10import { eq } from "drizzle-orm";
11import { handleSettings } from "../handlers/settings";
12
13export default function setupCommands() {
14 // Main command handler
15 slackApp.command(
16 environment === "dev" ? "/takes-dev" : "/takes",
17 async () => Promise.resolve(),
18 async ({ payload, context }): Promise<void> => {
19 try {
20 const userId = payload.user_id;
21 const channelId = payload.channel_id;
22 const text = payload.text || "";
23 const args = text.trim().split(/\s+/);
24 const subcommand = args[0]?.toLowerCase() || "";
25
26 let response: MessageResponse | undefined;
27
28 const userFromDB = await db
29 .select()
30 .from(usersTable)
31 .where(eq(usersTable.id, userId));
32
33 if (userFromDB.length === 0) {
34 await handleSettings(context.triggerId as string, userId);
35 return;
36 }
37
38 // Route to the appropriate handler function
39 switch (subcommand) {
40 case "history":
41 response = await handleHistory(userId);
42 break;
43 case "help":
44 response = await handleHelp();
45 break;
46 case "settings":
47 await handleSettings(
48 context.triggerId as string,
49 userId,
50 true,
51 );
52 return;
53 default:
54 response = await handleHome(userId);
55 break;
56 }
57
58 if (!response) {
59 throw new Error("No response received from handler");
60 }
61
62 if (context.respond) {
63 await context.respond(response);
64 }
65 } catch (error) {
66 if (error instanceof Error)
67 blog(
68 `Error in \`${payload.command}\` command: ${error.message}`,
69 "error",
70 );
71
72 // Capture the error in Sentry
73 Sentry.captureException(error, {
74 extra: {
75 command: payload.command,
76 userId: payload.user_id,
77 channelId: payload.channel_id,
78 text: payload.text,
79 },
80 });
81
82 // Respond with error message to user
83 if (context.respond) {
84 await context.respond({
85 text: "An error occurred while processing your request. Please be patent while we try to put out the fire.",
86 response_type: "ephemeral",
87 });
88 }
89 }
90 },
91 );
92}