husteno-o feat: code review system, type safety, error handling, AI provider fallback · 9/9/2026
5ccaeae
46 lines · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { ChatInputCommandInteraction } from "discord.js";

const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴"];

/**
 * Executes an async task while progressively rendering smooth animated braille status messages.
 * Uses Discord rate-limit safe timing (200ms intervals, max 2 frames).
 */
export async function withProgressAnimation<T>(
  interaction: ChatInputCommandInteraction,
  frames: string[],
  task: () => Promise<T>,
): Promise<T> {
  if (!interaction.deferred && !interaction.replied) {
    await interaction.deferReply();
  }

  if (frames.length > 0) {
    await interaction
      .editReply({ content: `${SPINNER_FRAMES[0]} [ GITBOT ] ${frames[0]}` })
      .catch(() => {});
  }

  let currentFrame = 1;
  const interval = setInterval(async () => {
    if (currentFrame < frames.length) {
      const spinner = SPINNER_FRAMES[currentFrame % SPINNER_FRAMES.length];
      const frameText = frames[currentFrame];
      currentFrame++;
      await interaction
        .editReply({ content: `${spinner} [ GITBOT ] ${frameText}` })
        .catch(() => {});
    }
  }, 220);
  interval.unref?.();

  try {
    const result = await task();
    clearInterval(interval);
    return result;
  } catch (err) {
    clearInterval(interval);
    throw err;
  }
}