char-slop/city-role-bot

discord auto role applicator bot for charlotte polycule city

git clone https://git.t4t.associates/char-slop/city-role-bot

Charlotte Somadd starboarda4093f0

main
1.5 KiB58 linesraw
1import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
2
3export interface StarboardPost {
4  channelID: string
5  messageID: string
6  signature: string
7  deleted?: boolean
8}
9
10export interface ScanJob {
11  kind: 'messages' | 'publicThreads' | 'privateThreads' | 'joinedThreads'
12  channelID: string
13  before?: string
14}
15
16export interface Candidate {
17  channelID: string
18}
19
20export interface Backfill {
21  phase: 'collecting' | 'posting'
22  candidates: Record<string, Candidate>
23  pending: Record<string, Candidate>
24  processed: number
25  startedAt: string
26  completedAt?: string
27  initialized: boolean
28  jobs: ScanJob[]
29  channels: string[]
30  scanned: number
31  skipped: string[]
32  error?: string
33}
34
35export interface StarboardState {
36  destinationID?: string
37  posts: Record<string, StarboardPost>
38  backfill?: Backfill
39  archivedBoards?: Record<string, { posts: Record<string, StarboardPost>; backfill?: unknown }>
40}
41
42const FILE = new URL('../data/starboard.json', import.meta.url)
43
44export function loadStarboard(): StarboardState {
45  try {
46    return JSON.parse(readFileSync(FILE, 'utf8')) as StarboardState
47  } catch (error) {
48    if ((error as NodeJS.ErrnoException).code === 'ENOENT') return { posts: {} }
49    throw error
50  }
51}
52
53export function saveStarboard(state: StarboardState): void {
54  mkdirSync(new URL('.', FILE), { recursive: true })
55  const temporary = new URL(`${FILE.href}.tmp`)
56  writeFileSync(temporary, JSON.stringify(state, null, 2))
57  renameSync(temporary, FILE)
58}