-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from TibbyRocks/feature/random-quotes
Added the /wisdom command to give out a random quote
- Loading branch information
Showing
2 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package wisdom | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"github.com/tibbyrocks/tibby/internal/utils" | ||
) | ||
|
||
type quote struct { | ||
Id string `json:"_id"` | ||
Content string `json:"content"` | ||
Author string `json:"author"` | ||
} | ||
|
||
var ( | ||
quotehost string = "https://api.quotable.io" | ||
log = utils.Log | ||
) | ||
|
||
func getRandomQuotes(amount int) []quote { | ||
var quoteArray []quote | ||
|
||
reqUrl, _ := url.Parse(quotehost + "/quotes/random") | ||
q := reqUrl.Query() | ||
q.Add("limit", fmt.Sprint(amount)) | ||
reqUrl.RawQuery = q.Encode() | ||
|
||
req, err := http.NewRequest("GET", reqUrl.String(), nil) | ||
if err != nil { | ||
log.Error(err.Error()) | ||
} | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Error(err.Error()) | ||
} | ||
|
||
if err := json.NewDecoder(res.Body).Decode("eArray); err != nil { | ||
log.Error(err.Error()) | ||
} | ||
|
||
return quoteArray | ||
} | ||
|
||
func getQuoteByID(id string) quote { | ||
var resolvedQuote quote | ||
|
||
reqUrl, _ := url.Parse(quotehost + "/quotes/" + id) | ||
req, err := http.NewRequest("GET", reqUrl.String(), nil) | ||
if err != nil { | ||
log.Error(err.Error()) | ||
} | ||
|
||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
log.Error(err.Error()) | ||
} | ||
|
||
if res.StatusCode == 404 { | ||
resolvedQuote.Content = "Four-Oh-Four" | ||
resolvedQuote.Author = "The Server" | ||
resolvedQuote.Id = "N0T-F0UND" | ||
} else { | ||
if err := json.NewDecoder(res.Body).Decode(&resolvedQuote); err != nil { | ||
log.Error(err.Error()) | ||
} | ||
} | ||
|
||
return resolvedQuote | ||
} | ||
|
||
func GetQuote(i *discordgo.InteractionCreate) string { | ||
optionMap := utils.GetOptionsFromInteraction(i) | ||
var quoteID string | ||
var result quote | ||
|
||
if val, ok := optionMap["quoteid"]; ok { | ||
quoteID = val.StringValue() | ||
} | ||
|
||
if len(quoteID) == 0 { | ||
result = getRandomQuotes(1)[0] | ||
} else { | ||
result = getQuoteByID(quoteID) | ||
} | ||
|
||
return buildMessageSingle(result) | ||
} | ||
|
||
func buildMessageSingle(quote quote) string { | ||
var formatString string = ` | ||
*%v* | ||
-%v | ||
ID: %v | ||
` | ||
return fmt.Sprintf(formatString, quote.Content, quote.Author, quote.Id) | ||
} |