All files / utils createGuildSnapshot.ts

100% Statements 61/61
100% Branches 6/6
100% Functions 3/3
100% Lines 61/61

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 621x 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 6x 6x 6x 6x 6x 6x 6x 6x 6x 1x 1x 6x 6x 1x 1x 6x 6x 1x 1x 6x 6x 6x 6x 6x 6x 6x 1x 1x 3x 3x 3x 3x 3x 3x  
import type { Guild } from "discord.js"
import { z } from "zod"
 
const discordSnowflakeSchema = z.string().regex(/^\d{17,20}$/)
 
export const guildSnapshotSchema = z.object({
  discordGuildId: discordSnowflakeSchema,
  name: z.string().trim().min(1).max(100),
  description: z.string().max(1_024).optional(),
  iconUrl: z.url().optional(),
  iconHash: z.string().max(256).optional(),
  ownerDiscordId: discordSnowflakeSchema.optional(),
  memberCount: z.int().nonnegative(),
  presenceCount: z.int().nonnegative().optional(),
  botJoinedAt: z.int().nonnegative().optional(),
})
 
export const guildLeftSnapshotSchema = z.object({
  discordGuildId: discordSnowflakeSchema,
  name: z.string().trim().min(1).max(100).optional(),
  leftAt: z.int().nonnegative().optional(),
})
 
export type GuildSnapshot = z.infer<typeof guildSnapshotSchema>
export type GuildLeftSnapshot = z.infer<typeof guildLeftSnapshotSchema>
 
export function createGuildSnapshot(guild: Guild): GuildSnapshot {
  const iconUrl = guild.iconURL()
  const snapshot: GuildSnapshot = {
    discordGuildId: guild.id,
    name: guild.name,
    ownerDiscordId: guild.ownerId,
    memberCount: guild.memberCount,
  }
 
  if (guild.description !== null) {
    snapshot.description = guild.description
  }
 
  if (iconUrl !== null) {
    snapshot.iconUrl = iconUrl
  }
 
  if (guild.icon !== null) {
    snapshot.iconHash = guild.icon
  }
 
  if (guild.joinedTimestamp !== null) {
    snapshot.botJoinedAt = guild.joinedTimestamp
  }
 
  return guildSnapshotSchema.parse(snapshot)
}
 
export function createGuildLeftSnapshot(guild: Guild): GuildLeftSnapshot {
  return guildLeftSnapshotSchema.parse({
    discordGuildId: guild.id,
    name: guild.name,
    leftAt: Date.now(),
  })
}