changed redditbooru function to async function
This commit is contained in:
@@ -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")
|
||||
|
@@ -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
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user