Skip to content

Commit e703403

Browse files
committed
Rework all database interactions into prisma
1 parent 65fb67a commit e703403

29 files changed

+643
-586
lines changed

.golangci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ linters-settings:
2323
# The maximal average package complexity.
2424
# If it's higher than 0.0 (float) the check is enabled
2525
# Default: 0.0
26-
package-average: 10.0
26+
package-average: 15.0
2727

2828
errcheck:
2929
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
@@ -71,11 +71,11 @@ linters-settings:
7171
# Checks the number of lines in a function.
7272
# If lower than 0, disable the check.
7373
# Default: 60
74-
lines: 100
74+
lines: 150
7575
# Checks the number of statements in a function.
7676
# If lower than 0, disable the check.
7777
# Default: 40
78-
statements: 50
78+
statements: 60
7979
# Ignore comments when counting lines.
8080
# Default false
8181
ignore-comments: true
@@ -338,3 +338,6 @@ issues:
338338
- gosec
339339
- noctx
340340
- wrapcheck
341+
342+
exclude-files:
343+
- ".*db_gen.go"

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,28 @@ Wisp can be configured either through parameters passed to the bot or through en
6767
You can find all env variables in the [.env.example](.env.example) file.
6868

6969
You can find all command line arguments [flags.go](internal/shared/flags.go)
70+
71+
## Development
72+
73+
### Prisma
74+
75+
```bash
76+
go run github.com/steebchen/prisma-client-go generate
77+
go run github.com/steebchen/prisma-client-go db push
78+
```
79+
80+
### Running the bot
81+
82+
Use [air](https://github.com/air-verse/air) to run the bot with hot reloading.
83+
84+
### Windows
85+
86+
```bash
87+
air --build.cmd "go build -o /tmp/main.exe cmd\main.go"
88+
```
89+
90+
### Linux
91+
92+
```bash
93+
air --build.cmd "go build -o /tmp/main cmd/main.go"
94+
```

cmd/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main //nolint:cyclop // Complexity is high due to number of commands, this can maybe be refactored in the future
1+
package main
22

33
import (
44
"os"
@@ -8,7 +8,6 @@ import (
88
"github.com/rs/zerolog/log"
99

1010
"math280h/wisp/internal/core"
11-
"math280h/wisp/internal/db"
1211
"math280h/wisp/internal/history"
1312
"math280h/wisp/internal/moderation"
1413
"math280h/wisp/internal/reports"
@@ -60,7 +59,7 @@ func main() {
6059
discordgo.IntentsGuildMembers |
6160
discordgo.IntentMessageContent
6261

63-
db.InitDB()
62+
shared.InitDB()
6463

6564
if *shared.PrettyLogs {
6665
log.Logger = log.Output( //nolint:reassign // This only changes if the user prefers JSON over PrettyLogs

db/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# gitignore generated by Prisma Client Go. DO NOT EDIT.
2+
*_gen.go

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ require (
77
github.com/joho/godotenv v1.5.1
88
github.com/mattn/go-sqlite3 v1.14.24
99
github.com/rs/zerolog v1.33.0
10+
github.com/shopspring/decimal v1.4.0
11+
github.com/steebchen/prisma-client-go v0.45.0
1012
)
1113

1214
require (
1315
github.com/gorilla/websocket v1.4.2 // indirect
1416
github.com/mattn/go-colorable v0.1.13 // indirect
1517
github.com/mattn/go-isatty v0.0.20 // indirect
16-
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
18+
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2 // indirect
19+
golang.org/x/crypto v0.27.0 // indirect
1720
golang.org/x/sys v0.28.0 // indirect
1821
)

go.sum

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
github.com/bwmarrin/discordgo v0.28.1 h1:gXsuo2GBO7NbR6uqmrrBDplPUx2T3nzu775q/Rd1aG4=
22
github.com/bwmarrin/discordgo v0.28.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
33
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
4+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
46
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
7+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
8+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
59
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
610
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
711
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
@@ -15,11 +19,22 @@ github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
1519
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
1620
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
1721
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
22+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
23+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1824
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
1925
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
2026
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
21-
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
27+
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
28+
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
29+
github.com/steebchen/prisma-client-go v0.45.0 h1:sXqCi96/1HIh3CScfd/27OlcR/YDXDtgbrpkDSk+3TY=
30+
github.com/steebchen/prisma-client-go v0.45.0/go.mod h1:4sNeijtPnYSX04/CIXRptUrczEsmPDpWSYR7ahY+H+c=
31+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
32+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
33+
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2 h1:PRtbRKwblE8ZfI8qOhofcjn9y8CmKZI7trS5vDMeJX0=
34+
go.mongodb.org/mongo-driver/v2 v2.0.0-beta2/go.mod h1:UGLb3ZgEzaY0cCbJpH9UFt9B6gEXiTPzsnJS38nBeoU=
2235
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
36+
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
37+
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
2338
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
2439
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
2540
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -30,3 +45,5 @@ golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
3045
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
3146
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
3247
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
48+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
49+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/core/messages.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package core
22

33
import (
4-
"math280h/wisp/internal/db"
4+
"context"
5+
"math280h/wisp/db"
56
"math280h/wisp/internal/reports"
67
"math280h/wisp/internal/shared"
78

@@ -29,9 +30,20 @@ func HandleIncomingMessages(s *discordgo.Session, m *discordgo.MessageCreate) {
2930
}
3031

3132
if channel.ParentID == *shared.ReportCategory {
32-
userID, _ := db.GetReportByChannelID(m.ChannelID)
33-
log.Debug().Msg("Message is to user: " + userID)
34-
userChannel, usrChnlErr := s.UserChannelCreate(userID)
33+
// userID, _ := db.GetReportByChannelID(m.ChannelID)
34+
reportObj, reportErr := shared.DBClient.Report.FindFirst(
35+
db.Report.ChannelID.Equals(m.ChannelID),
36+
).With(
37+
db.Report.User.Fetch(),
38+
).Exec(context.Background())
39+
if reportErr != nil {
40+
log.Error().Err(reportErr).Msg("Failed to get report")
41+
return
42+
}
43+
user := reportObj.User()
44+
45+
log.Debug().Msg("Message is to user: " + user.UserID)
46+
userChannel, usrChnlErr := s.UserChannelCreate(user.UserID)
3547
if usrChnlErr != nil {
3648
log.Error().Err(usrChnlErr).Msg("Error creating user channel")
3749
return
@@ -44,7 +56,18 @@ func HandleIncomingMessages(s *discordgo.Session, m *discordgo.MessageCreate) {
4456
return
4557
}
4658
} else {
47-
db.CreateMessage(m.ID, m.Content, m.Author.ID, m.Author.Mention(), m.Timestamp.String(), m.ChannelID)
59+
_, msgErr := shared.DBClient.Message.CreateOne(
60+
db.Message.ID.Set(m.ID),
61+
db.Message.Content.Set(m.Content),
62+
db.Message.Author.Link(
63+
db.User.UserID.Equals(m.Author.ID),
64+
),
65+
db.Message.ChannelID.Set(m.ChannelID),
66+
).Exec(context.Background())
67+
if msgErr != nil {
68+
log.Error().Err(err).Msg("Failed to create message")
69+
return
70+
}
4871
}
4972
return
5073
}

internal/db/infractions.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

internal/db/init.go

Lines changed: 0 additions & 86 deletions
This file was deleted.

internal/db/messages.go

Lines changed: 0 additions & 53 deletions
This file was deleted.

0 commit comments

Comments
 (0)