-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
144 lines (129 loc) · 3.82 KB
/
db.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
package main
import (
"database/sql"
"errors"
"log"
"net/url"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
)
var (
db *sql.DB
)
func init() {
var err error
db, err = sql.Open("sqlite3", "file:wilhelm.db?_fk=on&mode=rw")
if err != nil {
log.Fatalln(err)
}
}
func dbBotPrefix(gid string) (prefix string) {
row := db.QueryRow("SELECT Prefix FROM Guilds WHERE GuildID = ?;", gid)
if err := row.Scan(&prefix); errors.Is(err, sql.ErrNoRows) {
if _, err := db.Exec("INSERT INTO Guilds(GuildID) VALUES (?);", gid); err != nil {
log.Panicln(err)
}
prefix = dbBotPrefix(gid)
} else if err != nil {
log.Panicln(err)
}
return
}
func dbToggleConsent(uid string) (status bool) {
query := `
INSERT OR IGNORE INTO Users(UserID) VALUES (?);
UPDATE Users SET Consent = Consent != TRUE WHERE UserID = ?;`
if _, err := db.Exec(query, uid, uid); err != nil {
log.Panicln(err)
}
row := db.QueryRow("SELECT Consent FROM Users WHERE UserID = ?;", uid)
if err := row.Scan(&status); err != nil {
log.Panicln(err)
}
return
}
func dbIsConsenting(uid string) (consent bool) {
row := db.QueryRow("SELECT Consent FROM Users WHERE UserID = ?;", uid)
if err := row.Scan(&consent); errors.Is(err, sql.ErrNoRows) {
if _, err := db.Exec("INSERT INTO Users(UserID) VALUES (?);", uid); err != nil {
log.Panicln(err)
}
consent = dbIsConsenting(uid)
} else if err != nil {
log.Panicln(err)
}
return
}
func dbCreateConversation(gid string) (id int64) {
res, err := db.Exec("INSERT INTO Conversations(GuildID) VALUES (?);", gid)
if err != nil {
log.Panicln("failed to insert new conversation:", err)
}
id, err = res.LastInsertId()
if err != nil {
log.Panicln("failed to get conversation id:", err)
}
return
}
func dbCreateAudio(convId int64, uri string) (id int64) {
res, err := db.Exec("INSERT INTO Audio(ConversationID, URI) VALUES (?, ?);",
convId, uri)
if err != nil {
log.Panicln("failed to insert new audio:", err)
}
id, err = res.LastInsertId()
if err != nil {
log.Panicln("failed to get audio id:", err)
}
return
}
func dbEndAudio(audioId int64) {
t := time.Now().UTC().Format(time.RFC3339)
_, err := db.Exec("UPDATE Audio SET EndedAt = ? WHERE AudioID = ?;", t, audioId)
if err != nil {
log.Panicln("failed to set EndedAt for audio '", audioId, "':", err)
}
}
func dbEndConversation(convId int64) {
t := time.Now().UTC().Format(time.RFC3339)
_, err := db.Exec("UPDATE Conversations SET EndedAt = ? WHERE ConversationID = ?;",
t, convId)
if err != nil {
log.Panicln("failed to set EndedAt for conversation '", convId, "':", err)
}
}
func dbAudioSetUserID(audioId int64, uid string) {
_, err := db.Exec("UPDATE Audio SET UserID = ? WHERE AudioID = ?;", uid, audioId)
if err != nil {
log.Panicln("failed to set UserID for audio '", audioId, "':", err)
}
}
func dbPurgeAudioData(audioId int64) {
var fileStr string
row := db.QueryRow("SELECT URI FROM Audio WHERE AudioID = ?;", audioId)
if err := row.Scan(&fileStr); err != nil {
log.Panicln("failed to get URI for audio '", audioId, "':", err)
}
if uri, err := url.Parse(fileStr); err != nil {
log.Panicln("failed to parse URI: ", err)
} else if err = os.Remove(uri.Path); err != nil {
log.Panicln("failed to remove audio '", uri.Path, "':", err)
}
_, err := db.Exec("DELETE FROM Audio WHERE AudioID = ?;", audioId)
if err != nil {
log.Panicln("failed to delete record from Audio '", audioId, "':", err)
}
}
func dbGetConsent(uid string) (consent bool) {
row := db.QueryRow("SELECT Consent FROM Users WHERE UserID = ?;", uid)
if err := row.Scan(&consent); errors.Is(err, sql.ErrNoRows) {
if _, err := db.Exec("INSERT INTO Users(UserID) VALUES (?);", uid); err != nil {
log.Panicln("failed to insert new user:", err)
}
consent = dbGetConsent(uid)
} else if err != nil {
log.Panicf("failed to get consent status for '%s': %v", uid, err)
}
return
}