-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbOperations.go
151 lines (136 loc) · 4.67 KB
/
dbOperations.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"github.com/bwmarrin/discordgo"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
)
func getOrCreateGuildRecord(discordGuild *discordgo.Guild) (*core.Record, error) {
guildRecord, err := app.FindFirstRecordByFilter("guilds", "guild_id={:gId}", dbx.Params{"gId": discordGuild.ID})
if err != nil {
collection, _ := app.FindCollectionByNameOrId("guilds")
guildRecord = core.NewRecord(collection)
}
guildRecord.Load(map[string]any{
"guild_id": discordGuild.ID,
"name": discordGuild.Name,
})
if err := app.Save(guildRecord); err != nil {
app.Logger().Error("Could not create/update guild record", "error", err)
return nil, err
}
return guildRecord, err
}
func getOrCreateGuildRecordById(txApp core.App, discordGuildId string) (*core.Record, error) {
guildRecord, err := txApp.FindFirstRecordByFilter("guilds", "guild_id={:gId}", dbx.Params{"gId": discordGuildId})
if err != nil {
collection, _ := txApp.FindCollectionByNameOrId("guilds")
guildRecord = core.NewRecord(collection)
}
guildRecord.Load(map[string]any{
"guild_id": discordGuildId,
})
if err := txApp.Save(guildRecord); err != nil {
app.Logger().Error("Could not create/update guild record", "error", err)
return nil, err
}
return guildRecord, err
}
func getTargetEventChannel(sourceChannel, guildId string) string {
if sourceChannel == "" {
return ""
}
targetChannels := []struct {
Id string `db:"annoucementChannelId"`
}{}
err := app.DB().Select("annoucementChannelId").From("eventAnnouncementsConfig").Where(dbx.NewExp("guild = {:gId}", dbx.Params{"gId": guildId})).AndWhere(dbx.NewExp("channelId = {:channel}", dbx.Params{"channel": sourceChannel})).Limit(1).All(&targetChannels) //app.FindFirstRecordByFilter("eventAnnouncementsConfig", "guild = {:gId} && channelId = {:channel}", dbx.Params{"gId": guildId, "channel": sourceChannel})
if err != nil {
app.Logger().Debug("Could not find event target channel", "error", err)
return ""
}
if len(targetChannels) > 0 {
return targetChannels[0].Id
} else {
return ""
}
}
func getOrCreatePlayer(txApp core.App, guildId string, discordUser *discordgo.User, data map[string]any) (*core.Record, error) {
guildRecord, err := getOrCreateGuildRecordById(txApp, guildId)
if err != nil {
app.Logger().Error("Could not create/update player record missing guild record", "error", err)
return nil, err
}
nickRecord, err := txApp.FindFirstRecordByFilter("players", "guild = {:guildId} && userId = {:userId}", dbx.Params{"guildId": guildRecord.Id, "userId": discordUser.ID})
if err != nil {
collection, err := txApp.FindCollectionByNameOrId("players")
if err != nil {
return nil, err
}
nickRecord = core.NewRecord(collection)
data["guild"] = guildRecord.Id
data["userId"] = discordUser.ID
}
data["name"] = discordUser.Username
nickRecord.Load(data)
if err := txApp.Save(nickRecord); err != nil {
app.Logger().Error("Could not create/update player record", "error", err)
return nil, err
}
return nickRecord, nil
}
func registerUserOnEvent(eventId, guildId, playerId, regType string) {
el, err := app.FindFirstRecordByData("eventLogs", "eventId", eventId)
if err != nil {
return
}
member, err := discord.GuildMember(guildId, playerId)
if err != nil {
return
}
pl, err := getOrCreatePlayer(app, guildId, member.User, map[string]any{})
if err != nil {
return
}
playerLogRecord, err := app.FindFirstRecordByFilter("eventPlayerLogs", "eventLog={:el} && player={:pl}", dbx.Params{"el": el.Id, "pl": pl.Id})
if err != nil {
collection, _ := app.FindCollectionByNameOrId("eventPlayerLogs")
playerLogRecord = core.NewRecord(collection)
}
playerLogRecord.Load(map[string]any{
"eventLog": el.Id,
"player": pl.Id,
"status": regType,
})
app.Save(playerLogRecord)
}
func updateGuildPlayer(guildRecord *core.Record) {
members, err := discord.GuildMembers(guildRecord.GetString("guild_id"), "", 1000)
if err != nil {
app.Logger().Error("Cannot get info about guild members", "error", err)
}
app.RunInTransaction(func(txApp core.App) error {
_, err := txApp.DB().Update("players", dbx.Params{"active": false}, dbx.NewExp("guild={:guild}", dbx.Params{"guild": guildRecord.Id})).Execute()
if err != nil {
app.Logger().Error("Could not se active false on players", "error", err)
return err
}
for _, v := range members {
if v.User.Bot {
continue
}
nick := ""
if v.Nick == "" {
nick = v.User.GlobalName
} else {
nick = v.Nick
}
if nick == "" {
nick = v.User.Username
}
_, err := getOrCreatePlayer(txApp, guildRecord.GetString("guild_id"), v.User, map[string]any{"serverNick": nick, "active": "true"})
if err != nil {
return err
}
}
return nil
})
}