Dad joke status (#8)
Dad joke status Signed-off-by: Etzelia <etzelia@hotmail.com> Reviewed-on: https://git.birbmc.com/Etzelia/canopeas/pulls/8 Co-Authored-By: Etzelia <etzelia@hotmail.com> Co-Committed-By: Etzelia <etzelia@hotmail.com>pull/9/head
parent
cc3ceb6668
commit
8e83885118
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue