mirror of
https://github.com/mgerb/go-discord-bot
synced 2026-01-11 01:22:48 +00:00
changed to search end point
This commit is contained in:
@@ -2,8 +2,10 @@ package bothandlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@@ -12,7 +14,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
gifPrefix string = "!gif "
|
gifPrefix string = "!gif "
|
||||||
userAgent string = "go-discord-bot"
|
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
|
// GifHandler - handler for giphy api
|
||||||
@@ -36,6 +38,9 @@ func GifHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|||||||
|
|
||||||
// send http request to reddit
|
// send http request to reddit
|
||||||
func getGiphy(searchTerm string) string {
|
func getGiphy(searchTerm string) string {
|
||||||
|
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", giphyURL+searchTerm, nil)
|
req, err := http.NewRequest("GET", giphyURL+searchTerm, nil)
|
||||||
@@ -52,7 +57,11 @@ func getGiphy(searchTerm string) string {
|
|||||||
return err.Error()
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user