Skip to content

Commit

Permalink
Add clickable links to the title of embeds
Browse files Browse the repository at this point in the history
  • Loading branch information
post04 committed Apr 8, 2021
1 parent f79162d commit 66ec31d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
15 changes: 14 additions & 1 deletion bot/discordPages.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,16 @@ func ReactionListen(session *discordgo.Session, reaction *discordgo.MessageReact
}
// decrease current page
pageListeners[reaction.MessageID].CurrentPage -= 1
URL := pageListeners[reaction.MessageID].Data.URL
if pageListeners[reaction.MessageID].Type == "functions" {
URL += "#pkg-functions"
} else {
URL += "#pkg-types"
}
session.ChannelMessageEditEmbed(reaction.ChannelID, reaction.MessageID, &discordgo.MessageEmbed{
Title: pageListeners[reaction.MessageID].Type,
Description: formatForMessage(pageListeners[reaction.MessageID]),
URL: URL,
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("Page %v/%v", pageListeners[reaction.MessageID].CurrentPage, pageListeners[reaction.MessageID].PageLimit),
},
Expand All @@ -107,9 +114,15 @@ func ReactionListen(session *discordgo.Session, reaction *discordgo.MessageReact
}
// update current page by 1
pageListeners[reaction.MessageID].CurrentPage++
URL := pageListeners[reaction.MessageID].Data.URL
if pageListeners[reaction.MessageID].Type == "functions" {
URL += "#pkg-functions"
} else {
URL += "#pkg-types"
}
session.ChannelMessageEditEmbed(reaction.ChannelID, reaction.MessageID, &discordgo.MessageEmbed{
Title: pageListeners[reaction.MessageID].Type,
URL: pageListeners[reaction.MessageID].Data.URL,
URL: URL,
Description: formatForMessage(pageListeners[reaction.MessageID]),
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("Page %v/%v", pageListeners[reaction.MessageID].CurrentPage, pageListeners[reaction.MessageID].PageLimit),
Expand Down
20 changes: 19 additions & 1 deletion bot/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,22 @@ func queryResponse(pkg, name string) *discordgo.MessageEmbed {
}
return &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s: %s", pkg, name),
URL: fmt.Sprintf("%v#%v", doc.URL, correctName(name)),
Description: msg,
Footer: &discordgo.MessageEmbedFooter{
Text: fmt.Sprintf("%v#%v", doc.URL, name),
Text: fmt.Sprintf("%v#%v", doc.URL, correctName(name)),
},
}
}

// this function is used to capitalize the first letter of a word
func correctName(word string) string {
first := word[:1]
word = word[1:]
word = strings.ToUpper(first) + word
return word
}

// errResponse is like fmt.Sprintf, formats a message and returns an embed.
func errResponse(format string, args ...interface{}) *discordgo.MessageEmbed {
return &discordgo.MessageEmbed{
Expand All @@ -124,7 +133,11 @@ func pkgResponse(pkg string) *discordgo.MessageEmbed {

embed := &discordgo.MessageEmbed{
Title: fmt.Sprintf("Info for %s", pkg),
URL: fmt.Sprintf("%v", doc.URL),
Description: fmt.Sprintf("Types: %v\nFunctions: %v", len(doc.Types), len(doc.Functions)),
Footer: &discordgo.MessageEmbedFooter{
Text: doc.URL,
},
}
if doc.Overview != "" {
embed.Description += fmt.Sprintf("\nOverview: %v", doc.Overview)
Expand Down Expand Up @@ -178,6 +191,7 @@ func methodResponse(pkg, t, name string) *discordgo.MessageEmbed {
}
return &discordgo.MessageEmbed{
Title: fmt.Sprintf("%s: func(%s) %s", pkg, t, name),
URL: hyper,
Description: msg,
Footer: &discordgo.MessageEmbedFooter{
Text: hyper,
Expand Down Expand Up @@ -221,6 +235,7 @@ func HandleFuncsPages(s *discordgo.Session, m *discordgo.MessageCreate, prefix s
}
m, err := s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
Title: "functions",
URL: doc.URL + "#pkg-functions",
Description: formatForMessage(page),
Footer: &discordgo.MessageEmbedFooter{
Text: "Page 1/" + fmt.Sprint(page.PageLimit),
Expand Down Expand Up @@ -270,6 +285,7 @@ func HandleTypesPages(s *discordgo.Session, m *discordgo.MessageCreate, prefix s
}
m, err := s.ChannelMessageSendEmbed(m.ChannelID, &discordgo.MessageEmbed{
Title: "types",
URL: doc.URL + "#pkg-types",
Description: formatForMessage(page),
Footer: &discordgo.MessageEmbedFooter{
Text: "Page 1/" + fmt.Sprint(page.PageLimit),
Expand Down Expand Up @@ -329,6 +345,7 @@ func methodGlobResponse(pkg, t, name string) *discordgo.MessageEmbed {
}
return &discordgo.MessageEmbed{
Title: "Matches",
URL: doc.URL,
Description: msg,
Footer: &discordgo.MessageEmbedFooter{
Text: doc.URL,
Expand Down Expand Up @@ -380,6 +397,7 @@ func queryGlobResponse(pkg, name string) *discordgo.MessageEmbed {
}
return &discordgo.MessageEmbed{
Title: fmt.Sprintf("Matches for `%s` in package %s", name, pkg),
URL: doc.URL,
Description: msg,
Footer: &discordgo.MessageEmbedFooter{
Text: doc.URL,
Expand Down
1 change: 1 addition & 0 deletions commandHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (handler *CommandHandler) GenHelp() {
embedDesc.WriteString(handler.Prefix)
embedDesc.WriteString(name)
embedDesc.WriteString(strings.Repeat(" ", (longestCommand-len(name))+1))
embedDesc.WriteString("#")
embedDesc.WriteString(handler.Commands[name].Description)
embedDesc.WriteRune('\n')
}
Expand Down

0 comments on commit 66ec31d

Please sign in to comment.