2020-06-12 17:01:17 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
dadJokeAPI = "https://icanhazdadjoke.com/"
|
|
|
|
dadJokeErr = "Could not get a Dad joke. :slight_frown:"
|
|
|
|
)
|
|
|
|
|
|
|
|
type dadJoke struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Joke string `json:"joke"`
|
|
|
|
Status int `json:"status"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
commands["dad"] = command{
|
|
|
|
validate: func(cmd commandInit) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
run: func(cmd commandInit) (string, error) {
|
2020-06-16 02:19:20 +00:00
|
|
|
if !memeRateLimit.Try() {
|
|
|
|
return "Woah, slow down!", nil
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:01:17 +00:00
|
|
|
req, err := http.NewRequest(http.MethodGet, dadJokeAPI, nil)
|
|
|
|
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 {
|
|
|
|
return dadJokeErr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dj.Joke, nil
|
|
|
|
},
|
|
|
|
help: "Get a random Dad joke",
|
|
|
|
}
|
|
|
|
}
|