File size: 2,153 Bytes
2b587a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { readJSON, writeJSON } from './hfClient.js'

const guildCache = new Map()

export function getGuildConfig(guildId) {
  return guildCache.get(guildId) ?? null
}

export async function setGuildConfig(guildId, config) {
  guildCache.set(guildId, config)
  await writeJSON(`guilds/${guildId}/config.json`, config)
  return config
}

export async function loadGuildConfig(guildId) {
  const cached = guildCache.get(guildId)
  if (cached) return cached

  const stored = await readJSON(`guilds/${guildId}/config.json`)
  if (stored !== null) {
    guildCache.set(guildId, stored)
    return stored
  }

  return initGuildConfig(guildId)
}

export async function initGuildConfig(guildId) {
  const config = {
    guildId,
    welcomeChannelId: null,
    leaveChannelId: null,
    autoRoles: [],
    welcomeMessage: 'Welcome {user} to {server}! You are member #{count}.',
    leaveMessage: '{user} has left the server. We now have {count} members.',
    dmMessage:
      'Hey {username}, really glad you made it to {server}.\n\nTake a look around and feel free to jump into any conversation.\n\nIf you need anything, the team is around.',
    dmEnabled: true,
    welcomeBackground: 'default1',
    leaveBackground: 'default1',
    cardTextColor: '#ffffff',
    cardAccentColor: '#5865F2',
    createdAt: new Date().toISOString(),
  }

  return setGuildConfig(guildId, config)
}

export function deleteGuildConfig(guildId) {
  guildCache.delete(guildId)
}

export async function getMemberData(guildId, userId) {
  return readJSON(`guilds/${guildId}/members/${userId}.json`)
}

export async function setMemberData(guildId, userId, data) {
  await writeJSON(`guilds/${guildId}/members/${userId}.json`, data)
}

export async function initMemberData(guildId, userId, username) {
  const record = {
    userId,
    username,
    guildId,
    joinedAt: new Date().toISOString(),
    leftAt: null,
    joinCount: 1,
    xp: 0,
    level: 0,
    messageCount: 0,
    roles: [],
    warnings: 0,
    dmSent: false,
  }

  await setMemberData(guildId, userId, record)
  return record
}