初始化模版工程
This commit is contained in:
45
remote-control/bots/discord/user-store.ts
Normal file
45
remote-control/bots/discord/user-store.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import fs from 'node:fs'
|
||||
import path from 'node:path'
|
||||
|
||||
interface UserMapping {
|
||||
conversationId: string
|
||||
createdAt: string
|
||||
}
|
||||
|
||||
type UserStore = Record<string, UserMapping>
|
||||
|
||||
const STORE_PATH = path.join(process.cwd(), 'remote-control', 'data', 'discord-users.json')
|
||||
|
||||
function readStore(): UserStore {
|
||||
try {
|
||||
if (!fs.existsSync(STORE_PATH)) {
|
||||
return {}
|
||||
}
|
||||
const data = fs.readFileSync(STORE_PATH, 'utf-8')
|
||||
return JSON.parse(data)
|
||||
} catch {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
function writeStore(store: UserStore): void {
|
||||
const dir = path.dirname(STORE_PATH)
|
||||
if (!fs.existsSync(dir)) {
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
}
|
||||
fs.writeFileSync(STORE_PATH, JSON.stringify(store, null, 2), 'utf-8')
|
||||
}
|
||||
|
||||
export function getConversationId(discordUserId: string): string | null {
|
||||
const store = readStore()
|
||||
return store[discordUserId]?.conversationId ?? null
|
||||
}
|
||||
|
||||
export function setConversationId(discordUserId: string, conversationId: string): void {
|
||||
const store = readStore()
|
||||
store[discordUserId] = {
|
||||
conversationId,
|
||||
createdAt: new Date().toISOString(),
|
||||
}
|
||||
writeStore(store)
|
||||
}
|
||||
Reference in New Issue
Block a user