Dad joke status

Signed-off-by: Etzelia <etzelia@hotmail.com>
pull/8/head
Etzelia 2021-05-13 22:24:39 -05:00
parent cc3ceb6668
commit 9da1eaf3ff
No known key found for this signature in database
GPG Key ID: 708511AE7ABC5314
3 changed files with 56 additions and 27 deletions

View File

@ -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
}

View File

@ -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
}
}

View File

@ -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{