forked from Minecraft/canopeas
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>discord-sync
parent
cc3ceb6668
commit
8e83885118
|
@ -2,8 +2,11 @@ package discord
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"go.jolheiser.com/beaver"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -28,33 +31,9 @@ func init() {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, dadJokeAPI, nil)
|
dj, err := newDadJoke()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
beaver.Warnf("error getting new dad joke: %v", 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 {
|
|
||||||
return dadJokeErr, nil
|
return dadJokeErr, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,3 +42,37 @@ func init() {
|
||||||
help: "Get a random Dad joke",
|
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) {
|
func readyHandler() func(s *discordgo.Session, m *discordgo.Ready) {
|
||||||
return func(s *discordgo.Session, r *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)
|
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)
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
2
main.go
2
main.go
|
@ -16,7 +16,7 @@ import (
|
||||||
var configFlag string
|
var configFlag string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.StringVar(&configFlag, "config", "sedbot.toml", "Set config path")
|
flag.StringVar(&configFlag, "config", "canopeas.toml", "Set config path")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
beaver.Console.Format = beaver.FormatOptions{
|
beaver.Console.Format = beaver.FormatOptions{
|
||||||
|
|
Loading…
Reference in New Issue