All files discord.ts

100% Statements 91/91
100% Branches 17/17
100% Functions 5/5
100% Lines 91/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 921x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 1x 1x 7x 7x 7x 7x 7x 3x 3x 7x 7x 2x 2x 2x 2x 2x 2x 7x 1x 1x 14x 14x 14x 14x 14x 5x 5x 14x 14x 5x 5x 5x 5x 14x 14x 4x 14x 1x 1x 1x 1x 14x 14x 14x 1x 4x 4x 4x 4x 4x  
import { createEnv } from "@t3-oss/env-core"
 
import { nodeEnv, optionalString, optionalUrl } from "./shared"
 
const runtimeModes = ["single", "sharded"] as const
 
export type DiscordBotRuntimeMode = (typeof runtimeModes)[number]
export type DiscordBotShardCount = "auto" | number
 
type RuntimeConfigEnv = {
  NODE_ENV?: string
  DISCORD_BOT_RUNTIME_MODE?: string
  DISCORD_BOT_SHARD_COUNT?: string
}
 
export const discordEnv = createEnv({
  server: {
    NODE_ENV: nodeEnv,
    CONVEX_URL: optionalUrl,
    DISCORD_BOT_CONVEX_SECRET: optionalString,
    DISCORD_BOT_TOKEN: optionalString,
    DISCORD_BOT_RUNTIME_MODE: optionalString,
    DISCORD_BOT_SHARD_COUNT: optionalString,
    DISCORD_BOT_PERMISSIONS: optionalString,
    // DISCORD_PUBLIC_KEY: optionalString,
    DISCORD_CLIENT_ID: optionalString,
    DISCORD_APPLICATION_ID: optionalString,
    DISCORD_TEST_GUILD_ID: optionalString,
    // OPENAI_API_KEY: optionalString,
  },
  runtimeEnv: process.env,
})
 
export function resolveDiscordBotRuntimeConfig(
  env: RuntimeConfigEnv = process.env
) {
  return {
    mode: resolveDiscordBotRuntimeMode(env.DISCORD_BOT_RUNTIME_MODE),
    shardCount: resolveDiscordBotShardCount(env.DISCORD_BOT_SHARD_COUNT),
  }
}
 
export function resolveDiscordBotRuntimeMode(
  value: string | undefined
): DiscordBotRuntimeMode {
  const normalizedValue = value?.trim().toLowerCase()
 
  if (!normalizedValue) {
    return "single"
  }
 
  if (isDiscordBotRuntimeMode(normalizedValue)) {
    return normalizedValue
  }
 
  throw new Error(
    `Invalid DISCORD_BOT_RUNTIME_MODE "${value}". Expected "single" or "sharded".`
  )
}
 
export function resolveDiscordBotShardCount(
  value: string | undefined
): DiscordBotShardCount {
  const normalizedValue = value?.trim().toLowerCase()
 
  if (!normalizedValue || normalizedValue === "auto") {
    return "auto"
  }
 
  if (!/^[1-9]\d*$/.test(normalizedValue)) {
    throw new Error(
      `Invalid DISCORD_BOT_SHARD_COUNT "${value}". Expected "auto" or a positive safe integer.`
    )
  }
 
  const shardCount = Number(normalizedValue)
 
  if (!Number.isSafeInteger(shardCount)) {
    throw new Error(
      `Invalid DISCORD_BOT_SHARD_COUNT "${value}". Expected "auto" or a positive safe integer.`
    )
  }
 
  return shardCount
}
 
function isDiscordBotRuntimeMode(
  value: string
): value is DiscordBotRuntimeMode {
  return runtimeModes.includes(value as DiscordBotRuntimeMode)
}