Add Dad joke API and fix OAuth URL

Signed-off-by: Etzelia <etzelia@hotmail.com>
rate-insult
Etzelia 2020-06-12 12:01:17 -05:00
parent ea5237fd16
commit 2ce8fff91e
No known key found for this signature in database
GPG Key ID: 3CAEB74806C4ADE5
3 changed files with 75 additions and 2 deletions

View File

@ -4,6 +4,10 @@ GO ?= go
fmt: fmt:
$(GO) fmt ./... $(GO) fmt ./...
.PHONY: imp
imp:
imp -w
.PHONY: generate .PHONY: generate
generate: generate:
$(GO) generate ./... $(GO) generate ./...
@ -21,4 +25,7 @@ build:
$(GO) build $(GO) build
.PHONY: build-all .PHONY: build-all
build-all: generate build build-all: generate build
.PHONY: check
check: generate imp fmt test vet build

60
discord/dad.go 100644
View File

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

View File

@ -44,12 +44,12 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error)
} }
Echo(cfg) Echo(cfg)
bot.AddHandler(readyHandler())
bot.AddHandler(commandHandler(cfg, db)) bot.AddHandler(commandHandler(cfg, db))
bot.AddHandler(messageHandler(cfg, db)) bot.AddHandler(messageHandler(cfg, db))
bot.AddHandler(reactionAddHandler()) bot.AddHandler(reactionAddHandler())
bot.AddHandler(reactionRemoveHandler()) 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 return bot, nil
} }
@ -79,6 +79,12 @@ func isStaff(authorRoleIDs, staffRoleIDs []string) bool {
return false 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) { func commandHandler(cfg *config.Config, db *database.Database) func(s *discordgo.Session, m *discordgo.MessageCreate) {
return func(s *discordgo.Session, m *discordgo.MessageCreate) { return func(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore bots // Ignore bots