changed redditbooru function to async function

This commit is contained in:
2020-06-06 20:20:16 -05:00
parent 545591700a
commit 110c1c472f
2 changed files with 39 additions and 25 deletions

View File

@@ -63,7 +63,11 @@ func message_create (s *discordgo.Session, m *discordgo.MessageCreate) {
// awwnime (test)
if message == "awwnime" {
s.ChannelMessageSend(m.ChannelID, get_redditbooru_image("awwnime"))
// get url
url := <-get_redditbooru_image("awwnime")
// print message with url
s.ChannelMessageSend(m.ChannelID, url)
}
//get_redditbooru("awwnime")

View File

@@ -1,5 +1,5 @@
/*
This contains our functions used for image searches"
This contains our functions used for image searches
*/
package main
@@ -29,7 +29,13 @@ func getArrayLen(value []byte) (int, error) {
}
// redditbooru request
func get_redditbooru_image(sub string) string{
func get_redditbooru_image(sub string) <-chan string{
// make the channel
ret := make(chan string)
go func() {
defer close(ret)
// create the proper url with the subreddit
url := "https://" + sub + ".redditbooru.com/images/?limit=1000"
@@ -58,6 +64,10 @@ func get_redditbooru_image(sub string) string{
// select a random url from our list
img_url,err := jsonparser.GetString(out, "[" + strconv.Itoa(random_img) + "]", "cdnUrl")
return img_url
// set the return value
ret <- img_url
}()
return ret
}