Skip to content

Commit

Permalink
fix examples
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Sep 25, 2023
1 parent 8da6b19 commit e32da28
Show file tree
Hide file tree
Showing 24 changed files with 168 additions and 166 deletions.
2 changes: 1 addition & 1 deletion 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
14 changes: 6 additions & 8 deletions _examples/application_commands/gateway/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import (
"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 @@ -49,21 +47,21 @@ func main() {
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))
}
}
}
20 changes: 11 additions & 9 deletions _examples/auto_moderation/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
Expand All @@ -24,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 @@ -47,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 @@ -84,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 @@ -105,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
18 changes: 9 additions & 9 deletions _examples/components/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package main

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

"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/events"

"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/disgo/gateway"
)

Expand All @@ -19,9 +19,8 @@ var (
)

func main() {
log.SetLevel(log.LevelDebug)
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.IntentGuilds, gateway.IntentGuildMessages, gateway.IntentDirectMessages)),
Expand All @@ -44,16 +43,17 @@ func main() {
}),
)
if err != nil {
log.Fatal("error while building bot: ", err)
slog.Error("error while building bot: ", 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: ", 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
13 changes: 7 additions & 6 deletions _examples/custom_cache/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"sync"
Expand All @@ -21,9 +22,8 @@ var (
)

func main() {
log.SetLevel(log.LevelDebug)
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.IntentGuilds|gateway.IntentGuildMessages|gateway.IntentDirectMessages)),
Expand All @@ -33,7 +33,8 @@ func main() {
),
)
if err != nil {
log.Fatal("error while building bot: ", err)
slog.Error("error while building bot", slog.Any("err", err))
return
}

defer func() {
Expand All @@ -45,10 +46,10 @@ func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err = client.OpenGateway(ctx); 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 Down
24 changes: 12 additions & 12 deletions _examples/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"context"
"errors"
"fmt"
"log/slog"
"net"
"os"
"os/signal"
Expand All @@ -25,9 +25,7 @@ var (
)

func main() {
log.SetLevel(log.LevelTrace)
log.SetFlags(log.LstdFlags | log.Llongfile)
log.Info("starting up")
slog.Info("starting up")

client, err := disgo.New(token,
bot.WithGatewayConfigOpts(gateway.WithIntents(gateway.IntentGuildVoiceStates)),
Expand All @@ -36,16 +34,18 @@ func main() {
}),
)
if err != nil {
log.Fatal("error creating client: ", err)
slog.Error("error creating client", slog.Any("err", err))
return
}

defer client.Close(context.TODO())

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

log.Info("ExampleBot is now running. Press CTRL-C to exit.")
slog.Info("ExampleBot is now running. Press CTRL-C to exit.")
s := make(chan os.Signal, 1)
signal.Notify(s, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-s
Expand All @@ -66,7 +66,7 @@ func play(client bot.Client) {
conn.Close(ctx2)
}()

println("starting playback")
slog.Info("starting playback")

if err := conn.SetSpeaking(ctx, voice.SpeakingFlagMicrophone); err != nil {
panic("error setting speaking flag: " + err.Error())
Expand All @@ -79,18 +79,18 @@ func play(client bot.Client) {
packet, err := conn.UDP().ReadPacket()
if err != nil {
if errors.Is(err, net.ErrClosed) {
println("connection closed")
slog.Info("connection closed")
return
}
fmt.Printf("error while reading from reader: %s", err)
slog.Info("error while reading from reader", slog.Any("err", err))
continue
}
if _, err = conn.UDP().Write(packet.Opus); err != nil {
if errors.Is(err, net.ErrClosed) {
println("connection closed")
slog.Info("connection closed")
return
}
fmt.Printf("error while writing to UDPConn: %s", err)
slog.Info("error while writing to UDPConn", slog.Any("err", err))
continue
}
}
Expand Down
27 changes: 12 additions & 15 deletions _examples/guild_scheduled_events/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"log/slog"
"os"
"os/signal"
"syscall"
Expand All @@ -20,9 +21,7 @@ var (
)

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetLevel(log.LevelDebug)
log.Info("starting example...")
slog.Info("starting example...")

client, err := disgo.New(token,
bot.WithGatewayConfigOpts(
Expand All @@ -34,22 +33,22 @@ func main() {
bot.WithMemberChunkingFilter(bot.MemberChunkingFilterNone),
bot.WithEventListeners(&events.ListenerAdapter{
OnGuildScheduledEventCreate: func(event *events.GuildScheduledEventCreate) {
log.Infof("%T\n", event)
slog.Info("OnGuildScheduledEventCreate")
},
OnGuildScheduledEventUpdate: func(event *events.GuildScheduledEventUpdate) {
log.Infof("%T\n", event)
slog.Info("OnGuildScheduledEventUpdate")
},
OnGuildScheduledEventDelete: func(event *events.GuildScheduledEventDelete) {
log.Infof("%T\n", event)
slog.Info("OnGuildScheduledEventDelete")
},
OnGuildScheduledEventUserAdd: func(event *events.GuildScheduledEventUserAdd) {
log.Infof("%T\n", event)
slog.Info("OnGuildScheduledEventUserAdd")
},
OnGuildScheduledEventUserRemove: func(event *events.GuildScheduledEventUserRemove) {
log.Infof("%T\n", event)
slog.Info("OnGuildScheduledEventUserRemove")
},
OnMessageCreate: func(event *events.MessageCreate) {
log.Infof("%T\n", event)
slog.Info("OnMessageCreate")
if event.Message.Content != "test" {
return
}
Expand All @@ -58,36 +57,34 @@ func main() {
Name: "test",
PrivacyLevel: discord.ScheduledEventPrivacyLevelGuildOnly,
ScheduledStartTime: time.Now().Add(time.Hour),
Description: "",
Description: "test",
EntityType: discord.ScheduledEventEntityTypeVoice,
})

status := discord.ScheduledEventStatusActive
gse, _ = event.Client().Rest().UpdateGuildScheduledEvent(gse.GuildID, gse.ID, discord.GuildScheduledEventUpdate{
Status: &status,
})
//_ = gse.AudioChannel().Connect()

time.Sleep(time.Second * 10)

status = discord.ScheduledEventStatusCompleted
gse, _ = event.Client().Rest().UpdateGuildScheduledEvent(gse.GuildID, gse.ID, discord.GuildScheduledEventUpdate{
Status: &status,
})
//_ = gse.Guilds().Disconnect()
},
}),
)
if err != nil {
log.Fatal("error while building bot instance: ", err)
slog.Error("error while building bot instance", slog.Any("err", err))
return
}

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

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
Loading

0 comments on commit e32da28

Please sign in to comment.