diff --git a/bot.go b/bot.go index 3a4e059..cbed5d3 100644 --- a/bot.go +++ b/bot.go @@ -14,6 +14,7 @@ import ( "github.com/tibbyrocks/tibby/internal/commands/radlibs" "github.com/tibbyrocks/tibby/internal/commands/textmanipulation" "github.com/tibbyrocks/tibby/internal/commands/translations" + "github.com/tibbyrocks/tibby/internal/commands/wisdom" "github.com/tibbyrocks/tibby/internal/types" "github.com/tibbyrocks/tibby/internal/utils" ) @@ -72,6 +73,18 @@ var ( Name: "docs", Description: fmt.Sprintf("Get the %s docs", customs.BotName), }, + { + Name: "wisdom", + Description: "Get a (random) quote", + Options: []*discordgo.ApplicationCommandOption{ + { + Type: discordgo.ApplicationCommandOptionString, + Name: "quoteid", + Description: "quotable.io quote ID", + Required: false, + }, + }, + }, } commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){ @@ -179,6 +192,25 @@ var ( }, }) }, + "wisdom": func(s *discordgo.Session, i *discordgo.InteractionCreate) { + + quoteMsg := wisdom.GetQuote(i) + + s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{ + Type: discordgo.InteractionResponseChannelMessageWithSource, + Data: &discordgo.InteractionResponseData{ + Embeds: []*discordgo.MessageEmbed{ + { + Description: quoteMsg, + Author: &discordgo.MessageEmbedAuthor{ + Name: fmt.Sprintf("%s Wisdom", customs.BotName), + IconURL: s.State.User.AvatarURL("1024"), + }, + }, + }, + }, + }) + }, } ) diff --git a/internal/commands/wisdom/wisdom.go b/internal/commands/wisdom/wisdom.go new file mode 100644 index 0000000..dfbfad9 --- /dev/null +++ b/internal/commands/wisdom/wisdom.go @@ -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) +}