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