Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate to slog & go 1.21 #294

Merged
merged 21 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
name: Go

on: [ push ]
on:
push:
pull_request:

jobs:
gobuild:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.21
- uses: actions/checkout@v3
- name: go build
run: go build -v ./...
Expand All @@ -18,7 +20,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.21
- uses: actions/checkout@v3
- name: go build
run: go test -v ./...
Expand All @@ -28,7 +30,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.21
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ A full Ping Pong example can also be found [here](https://github.com/disgoorg/di

### Logging

DisGo uses our own small [logging interface](https://github.com/disgoorg/log) which you can use with most other logging libraries. This lib also comes with a default logger which is based on the standard log package.
DisGo uses [slog](https://pkg.go.dev/log/slog) for logging.

## Documentation

Expand Down Expand Up @@ -156,10 +156,6 @@ Being used in production by FredBoat, Dyno, LewdBot, and more.

Is a [Lavalink-Client](https://github.com/freyacodes/Lavalink) which can be used to communicate with Lavalink to play/search tracks

### [DisLog](https://github.com/disgoorg/dislog)

Is a Discord webhook logger hook for [logrus](https://github.com/sirupsen/logrus)

## Other Golang Discord Libraries

* [discordgo](https://github.com/bwmarrin/discordgo)
Expand Down
20 changes: 9 additions & 11 deletions _examples/application_commands/gateway/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2"
)

var (
Expand Down Expand Up @@ -40,30 +39,29 @@ var (
)

func main() {
log.SetLevel(log.LevelInfo)
log.Info("starting example...")
log.Info("disgo version: ", disgo.Version)
slog.Info("starting example...")
slog.Info("disgo version", slog.String("version", disgo.Version))

client, err := disgo.New(token,
bot.WithDefaultGateway(),
bot.WithEventListenerFunc(commandListener),
)
if err != nil {
log.Fatal("error while building disgo instance: ", err)
slog.Error("error while building disgo instance", slog.Any("err", err))
return
}

defer client.Close(context.TODO())

if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), guildID, commands); err != nil {
log.Fatal("error while registering commands: ", err)
slog.Error("error while registering commands", slog.Any("err", err))
}

if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("error while connecting to gateway: ", err)
slog.Error("error while connecting to gateway", slog.Any("err", err))
}

log.Infof("example is now running. Press CTRL-C to exit.")
slog.Info("example is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
Expand All @@ -78,7 +76,7 @@ func commandListener(event *events.ApplicationCommandInteractionCreate) {
Build(),
)
if err != nil {
event.Client().Logger().Error("error on sending response: ", err)
slog.Error("error on sending response", slog.Any("err", err))
}
}
}
21 changes: 9 additions & 12 deletions _examples/application_commands/http/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/httpserver"
"github.com/disgoorg/snowflake/v2"
"github.com/oasisprotocol/curve25519-voi/primitives/ed25519"
)

var (
Expand Down Expand Up @@ -43,9 +42,8 @@ var (
)

func main() {
log.SetLevel(log.LevelDebug)
log.Info("starting example...")
log.Info("disgo version: ", disgo.Version)
slog.Info("starting example...")
slog.Info("disgo version", slog.String("version", disgo.Version))

// use custom ed25519 verify implementation
httpserver.Verify = func(publicKey httpserver.PublicKey, message, sig []byte) bool {
Expand All @@ -60,21 +58,20 @@ func main() {
bot.WithEventListenerFunc(commandListener),
)
if err != nil {
log.Fatal("error while building disgo instance: ", err)
return
panic("error while building disgo instance: " + err.Error())
}

defer client.Close(context.TODO())

if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), guildID, commands); err != nil {
log.Fatal("error while registering commands: ", err)
panic("error while registering commands: " + err.Error())
}

if err = client.OpenHTTPServer(); err != nil {
log.Fatal("error while starting http server: ", err)
panic("error while starting http server: " + err.Error())
}

log.Info("example is now running. Press CTRL-C to exit.")
slog.Info("example is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
Expand Down
6 changes: 3 additions & 3 deletions _examples/application_commands/http/go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/disgoorg/disgo/_examples/application_commands/http

go 1.18
go 1.21

replace github.com/disgoorg/disgo => ../../../

require (
github.com/disgoorg/disgo v0.16.8
github.com/disgoorg/log v1.2.1
github.com/disgoorg/snowflake/v2 v2.0.1
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce
)
Expand All @@ -14,6 +15,5 @@ require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b // indirect
golang.org/x/crypto v0.12.0 // indirect
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b // indirect
golang.org/x/sys v0.11.0 // indirect
)
12 changes: 5 additions & 7 deletions _examples/application_commands/http/go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/disgoorg/disgo v0.16.8 h1:tvUeX+3Iu8U6koDc8RAgcQadRciWJwsI95Y7edHqq2g=
github.com/disgoorg/disgo v0.16.8/go.mod h1:5fsaUpfu6Yv0p+PfmsAeQkV395KQskVu/d1bdq8vsNI=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/disgoorg/json v1.1.0 h1:7xigHvomlVA9PQw9bMGO02PHGJJPqvX5AnwlYg/Tnys=
github.com/disgoorg/json v1.1.0/go.mod h1:BHDwdde0rpQFDVsRLKhma6Y7fTbQKub/zdGO5O9NqqA=
github.com/disgoorg/log v1.2.1 h1:kZYAWkUBcGy4LbZcgYtgYu49xNVLy+xG5Uq3yz5VVQs=
github.com/disgoorg/log v1.2.1/go.mod h1:hhQWYTFTnIGzAuFPZyXJEi11IBm9wq+/TVZt/FEwX0o=
github.com/disgoorg/snowflake/v2 v2.0.1 h1:CuUxGLwggUxEswZOmZ+mZ5i0xSumQdXW9tXW7uGqe+0=
github.com/disgoorg/snowflake/v2 v2.0.1/go.mod h1:SPU9c2CNn5DSyb86QcKtdZgix9osEtKrHLW4rMhfLCs=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce h1:/pEpMk55wH0X+E5zedGEMOdLuWmV8P4+4W3+LZaM6kg=
github.com/oasisprotocol/curve25519-voi v0.0.0-20230110094441-db37f07504ce/go.mod h1:hVoHR2EVESiICEMbg137etN/Lx+lSrHPTD39Z/uE+2s=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b h1:qYTY2tN72LhgDj2rtWG+LI6TXFl2ygFQQ4YezfVaGQE=
github.com/sasha-s/go-csync v0.0.0-20210812194225-61421b77c44b/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b h1:r+vk0EmXNmekl0S0BascoeeoHk/L7wmaW2QF90K+kYI=
golang.org/x/exp v0.0.0-20230801115018-d63ba01acd4b/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
19 changes: 8 additions & 11 deletions _examples/application_commands/localization/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"

"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/snowflake/v2"
)

var (
Expand Down Expand Up @@ -64,30 +63,28 @@ var (
)

func main() {
log.SetLevel(log.LevelTrace)
log.Info("starting example...")
log.Infof("disgo version: %s", disgo.Version)
slog.Info("starting example...")
slog.Info("disgo version", slog.String("version", disgo.Version))

client, err := disgo.New(token,
bot.WithDefaultGateway(),
bot.WithEventListenerFunc(commandListener),
)
if err != nil {
log.Fatal("error while building disgo instance: ", err)
return
panic("error while building disgo instance: " + err.Error())
}

defer client.Close(context.TODO())

if _, err = client.Rest().SetGuildCommands(client.ApplicationID(), guildID, commands); err != nil {
log.Fatal("error while registering commands: ", err)
panic("error while registering commands: " + err.Error())
}

if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("error while connecting to gateway: ", err)
panic("error while connecting to gateway: " + err.Error())
}

log.Infof("example is now running. Press CTRL-C to exit.")
slog.Info("example is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
Expand Down
26 changes: 13 additions & 13 deletions _examples/auto_moderation/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"

"github.com/disgoorg/json"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/json"
"github.com/disgoorg/snowflake/v2"
)

var (
Expand All @@ -26,9 +25,8 @@ var (
)

func main() {
log.SetLevel(log.LevelInfo)
log.Info("starting example...")
log.Infof("disgo version: %s", disgo.Version)
slog.Info("starting example...")
slog.Info("disgo version", slog.String("version", disgo.Version))

client, err := disgo.New(token,
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentAutoModerationConfiguration, gateway.IntentAutoModerationExecution)),
Expand All @@ -49,16 +47,18 @@ func main() {
}),
)
if err != nil {
log.Fatal("error while building bot: ", err)
slog.Error("error while building bot", slog.Any("err", err))
return
}

defer client.Close(context.TODO())

if err = client.OpenGateway(context.TODO()); err != nil {
log.Fatal("error while connecting to gateway: ", err)
slog.Error("error while connecting to gateway", slog.Any("err", err))
return
}

log.Infof("example is now running. Press CTRL-C to exit.")
slog.Info("example is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-s
Expand Down Expand Up @@ -86,7 +86,7 @@ func showCaseAutoMod(client bot.Client) {
Enabled: json.Ptr(true),
})
if err != nil {
log.Error("error while creating rule: ", err)
slog.Error("error while creating rule", slog.Any("err", err))
return
}

Expand All @@ -107,15 +107,15 @@ func showCaseAutoMod(client bot.Client) {
},
})
if err != nil {
log.Error("error while updating rule: ", err)
slog.Error("error while updating rule", slog.Any("err", err))
return
}

time.Sleep(time.Second * 10)

err = client.Rest().DeleteAutoModerationRule(guildID, rule.ID)
if err != nil {
log.Error("error while deleting rule: ", err)
slog.Error("error while deleting rule", slog.Any("err", err))
return
}

Expand Down