discordgo
discordgo copied to clipboard
How to properly deal with ws connection errors? (broken pipe)
Hi!
I am getting these kind of errors at random times:
2021/12/14 17:11:20 [DG0] wsapi.go:82:Open() error connecting to gateway wss://gateway.discord.gg/?v=8&encoding=json, write tcp XXXXXX:59644->XXXXXX:443: write: broken pipe
2021/12/14 17:11:21 [DG0] wsapi.go:835:reconnect() error reconnecting to gateway, write tcp XXXXXX:59644->XXXXXX:443: write: broken pipe
Those usually take the entire program down as well. Is there a proper way to deal with / catch those errors and manually do a re-connection?
I saw that there is some re-connection code with backoff already here: https://github.com/bwmarrin/discordgo/blob/master/wsapi.go#L848-L852
But in my case there is only one attempt to reconnect before it stops to do anything.
Initializer code:
dg, err := discordgo.New("Bot " + DISCORD_BOT_TOKEN)
dg.ShouldReconnectOnError = true
if err != nil {
fmt.Println("error creating Discord session,", err)
panic(err)
}
err = dg.Open()
if err != nil {
fmt.Println("error connecting to Discord socket,", err)
panic(err)
}
err = dg.UpdateGameStatus(1, "Foo")
if err != nil {
fmt.Println("error updating discord game status", err)
panic(err)
}
You can override the default log function using discordgo.Logger
to intercept these errors.
It should work fine, not sure where the problem is coming from. Can you share more context? Additionally, try to enable s.Debug = true
, and see what it prints