From 968d670b2922a57b4ee242994114fd1a2f18a84c Mon Sep 17 00:00:00 2001 From: Daniel Tam Date: Tue, 9 Jun 2020 14:38:26 -0500 Subject: [PATCH] added func to search based on best criteria --- bunnybot.go | 14 +++++++++++++- images.go | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/bunnybot.go b/bunnybot.go index e5d39ed..471ac6b 100644 --- a/bunnybot.go +++ b/bunnybot.go @@ -68,12 +68,24 @@ func message_create (s *discordgo.Session, m *discordgo.MessageCreate) { // awwnime (test) if message == "awwnime" { // get url - url := <-get_subreddit_image("awwnime") + url := <-get_image("awwnime") // print message with url s.ChannelMessageSend(m.ChannelID, url) } + // other image test + if message == "self" { + // get url + url := <-get_image("self") + + if(len(url) > 0) { + s.ChannelMessageSend(m.ChannelID, url) + } else { + s.ChannelMessageSend(m.ChannelID, "No images found, please try again") + } + } + //get_redditbooru("awwnime") } diff --git a/images.go b/images.go index 1529a91..62e924d 100644 --- a/images.go +++ b/images.go @@ -85,6 +85,41 @@ func build_redditbooru_sources() { } } +// search based on the most appropriate location +func get_image(sub string) <-chan string { + // make the channell + ret := make(chan string) + + go func() { + defer close(ret) + + // setup variable + var image string + var found bool = false + + // check to see if it is in the redditbooru sources + for _,title := range redditbooru_sources { + if title == sub { + image = <-get_redditbooru_image(sub) + found = true + ret <- image + return + } + } + + + // if not in redditbooru, check reddit + if found == false { + image = <-get_subreddit_image(sub) + ret <- image + return + } + + }() + + return ret +} + // redditbooru request func get_redditbooru_image(sub string) <-chan string{ // make the channel @@ -124,7 +159,7 @@ func get_redditbooru_image(sub string) <-chan string{ // set the return value ret <- img_url - }() + }() return ret }