Skip to content

Commit

Permalink
Retry Discord connection
Browse files Browse the repository at this point in the history
  • Loading branch information
rkfg committed Nov 19, 2020
1 parent 2f1a12f commit a793b3d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,14 +178,27 @@ func sendMsg(c chan string, s *discordgo.Session) {
}
}

func bot() error {
dg, err := discordgo.New("Bot " + config.Token)
if err != nil {
return err
func tryConnect(dg *discordgo.Session, retries int) (err error) {
for i := 0; i < retries; i++ {
err = dg.Open()
if err == nil {
break
} else {
log.Printf("Error connecting: %s, retrying...", err)
}
time.Sleep(1000)
}
err = dg.Open()
return
}

func bot() (err error) {
var dg *discordgo.Session
dg, err = discordgo.New("Bot " + config.Token)
if err != nil {
return err
return
}
if err = tryConnect(dg, 5); err != nil {
return
}
defer dg.Close()
if tr, ok := http.DefaultTransport.(*http.Transport); ok {
Expand Down

0 comments on commit a793b3d

Please sign in to comment.