Skip to content

Commit

Permalink
Added the /wisdom command to give out a random quote
Browse files Browse the repository at this point in the history
  • Loading branch information
mozoarella committed Feb 18, 2024
1 parent d5060c7 commit fe6d5c6
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
32 changes: 32 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -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"),
},
},
},
},
})
},
}
)

Expand Down
102 changes: 102 additions & 0 deletions internal/commands/wisdom/wisdom.go
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(&quoteArray); 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)
}

0 comments on commit fe6d5c6

Please sign in to comment.