diff --git a/Makefile b/Makefile index 154ff14..970187d 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,10 @@ GO ?= go fmt: $(GO) fmt ./... +.PHONY: imp +imp: + imp -w + .PHONY: generate generate: $(GO) generate ./... @@ -21,4 +25,7 @@ build: $(GO) build .PHONY: build-all -build-all: generate build \ No newline at end of file +build-all: generate build + +.PHONY: check +check: generate imp fmt test vet build \ No newline at end of file diff --git a/discord/dad.go b/discord/dad.go new file mode 100644 index 0000000..84a6fb3 --- /dev/null +++ b/discord/dad.go @@ -0,0 +1,60 @@ +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) { + 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", + } +} diff --git a/discord/discord.go b/discord/discord.go index 903c440..8e4fed6 100644 --- a/discord/discord.go +++ b/discord/discord.go @@ -44,12 +44,12 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error) } Echo(cfg) + bot.AddHandler(readyHandler()) bot.AddHandler(commandHandler(cfg, db)) bot.AddHandler(messageHandler(cfg, db)) bot.AddHandler(reactionAddHandler()) bot.AddHandler(reactionRemoveHandler()) - beaver.Infof("https://discord.com/api/oauth2/authorize?client_id=%s&permissions=0&redirect_uri=https://birbmc.com&scope=bot", bot.State.User.ID) return bot, nil } @@ -79,6 +79,12 @@ func isStaff(authorRoleIDs, staffRoleIDs []string) bool { return false } +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) + } +} + func commandHandler(cfg *config.Config, db *database.Database) func(s *discordgo.Session, m *discordgo.MessageCreate) { return func(s *discordgo.Session, m *discordgo.MessageCreate) { // Ignore bots