diff --git a/discord/dad.go b/discord/dad.go index a9fb940..c91c39f 100644 --- a/discord/dad.go +++ b/discord/dad.go @@ -2,8 +2,11 @@ package discord import ( "encoding/json" + "errors" "io/ioutil" "net/http" + + "go.jolheiser.com/beaver" ) const ( @@ -28,33 +31,9 @@ func init() { return "", nil } - req, err := http.NewRequest(http.MethodGet, dadJokeAPI, nil) + dj, err := newDadJoke() if err != nil { - return "", err - } - req.Header.Add("Accept", "application/json") - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return "", err - } - - if resp.StatusCode != http.StatusOK { - return dadJokeErr, nil - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return "", err - } - - var dj dadJoke - if err := json.Unmarshal(body, &dj); err != nil { - return "", nil - } - - // Check status again, in case API returned an error - if dj.Status != http.StatusOK { + beaver.Warnf("error getting new dad joke: %v", err) return dadJokeErr, nil } @@ -63,3 +42,37 @@ func init() { help: "Get a random Dad joke", }) } + +func newDadJoke() (*dadJoke, error) { + req, err := http.NewRequest(http.MethodGet, dadJokeAPI, nil) + if err != nil { + return nil, err + } + req.Header.Add("Accept", "application/json") + + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil, err + } + + if resp.StatusCode != http.StatusOK { + return nil, errors.New("non-ok status") + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + + var dj *dadJoke + if err := json.Unmarshal(body, &dj); err != nil { + return nil, errors.New("could not unmarshal") + } + + // Check status again, in case API returned an error + if dj.Status != http.StatusOK { + return nil, errors.New("API error") + } + + return dj, nil +} diff --git a/discord/discord.go b/discord/discord.go index 93c5f89..c3e9571 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -165,6 +165,9 @@ func isStaff(authorRoleIDs, staffRoleIDs []string) bool { func readyHandler() func(s *discordgo.Session, m *discordgo.Ready) { return func(s *discordgo.Session, r *discordgo.Ready) { beaver.Infof("https://discord.com/api/oauth2/authorize?client_id=%s&permissions=0&redirect_uri=https://birbmc.com&scope=bot", r.User.ID) + + // Init status changer + go updateStatus(s) } } @@ -276,3 +279,16 @@ func leaveHandler(cfg *config.Config) func(s *discordgo.Session, m *discordgo.Gu sendMessage(s, cfg.LeaveChannel, fmt.Sprintf("%s (%s) left the server. :sob:", m.Mention(), m.User.String()), true) } } + +func updateStatus(s *discordgo.Session) { + ticker := time.NewTicker(time.Minute * 30) + for { + dj, err := newDadJoke() + if err != nil { + beaver.Warnf("could not get new dad joke: %v", err) + } else if err := s.UpdateStatus(1, dj.Joke); err != nil { + beaver.Warnf("could not update status: %v", err) + } + <-ticker.C + } +} diff --git a/main.go b/main.go index acef729..5c3ac85 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,7 @@ import ( var configFlag string func main() { - flag.StringVar(&configFlag, "config", "sedbot.toml", "Set config path") + flag.StringVar(&configFlag, "config", "canopeas.toml", "Set config path") flag.Parse() beaver.Console.Format = beaver.FormatOptions{