1
0
mirror of https://github.com/mgerb/go-discord-bot synced 2026-01-09 16:42:48 +00:00

changed to search end point

This commit is contained in:
2017-03-29 22:37:09 -05:00
parent f463209191
commit 75bcbf907d

View File

@@ -2,8 +2,10 @@ package bothandlers
import (
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/tidwall/gjson"
@@ -12,7 +14,7 @@ import (
const (
gifPrefix string = "!gif "
userAgent string = "go-discord-bot"
giphyURL string = "http://api.giphy.com/v1/stickers/random?api_key=dc6zaTOxFJmzC&rating=r&tag="
giphyURL string = "http://api.giphy.com/v1/stickers/search?&api_key=dc6zaTOxFJmzC&limit=10&q="
)
// GifHandler - handler for giphy api
@@ -36,6 +38,9 @@ func GifHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// send http request to reddit
func getGiphy(searchTerm string) string {
rand.Seed(time.Now().UnixNano())
client := &http.Client{}
req, err := http.NewRequest("GET", giphyURL+searchTerm, nil)
@@ -52,7 +57,11 @@ func getGiphy(searchTerm string) string {
return err.Error()
}
data := gjson.Get(string(body), "data.url")
data := gjson.Get(string(body), "data").Array()
return data.String()
if len(data) < 1 {
return "null"
}
return data[rand.Intn(len(data))].Get("images.fixed_height.url").String()
}