53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/Xe/ln"
|
|
"github.com/bwmarrin/discordgo"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
)
|
|
|
|
func hipster(ctx context.Context, s *discordgo.Session, m *discordgo.Message, parv []string) error {
|
|
sp, ctx := opentracing.StartSpanFromContext(ctx, "hipster")
|
|
defer sp.Finish()
|
|
|
|
msg, err := getHipsterText(ctx)
|
|
if err != nil {
|
|
ln.Error(ctx, err, ln.F{"action": "get hipster text"})
|
|
return err
|
|
}
|
|
|
|
_, err = s.ChannelMessageSend(m.ChannelID, msg)
|
|
return err
|
|
}
|
|
|
|
func getHipsterText(ctx context.Context) (string, error) {
|
|
req, err := http.NewRequest(http.MethodGet, "http://hipsterjesus.com/api/?type=hipster-centric&html=false¶s=1", nil)
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
textStruct := &struct {
|
|
Text string `json:"text"`
|
|
}{}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
json.Unmarshal(body, textStruct)
|
|
|
|
text := strings.Split(textStruct.Text, ". ")[0]
|
|
|
|
return text, nil
|
|
}
|