Skip to content

Commit

Permalink
show url embeds less
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhapz committed Jun 27, 2024
1 parent 2a9f228 commit eff0171
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
24 changes: 17 additions & 7 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,33 @@ func (b *botState) handleDocs(e *gateway.InteractionCreateEvent, d *discord.Comm
}
}

func (b *botState) handleDocsText(m *gateway.MessageCreateEvent, queries []string) {
log.Printf("%s used docs(%v) text version", m.Author.Tag(), queries)
type textQuery struct {
query string
source string
}

func (b *botState) handleDocsText(m *gateway.MessageCreateEvent, queries []textQuery) {
if len(queries) > 0 {
log.Printf("%s used docs(%v) text version", m.Author.Tag(), queries)
}

var internal []discord.Embed
var embeds []discord.Embed
var more []bool
for _, query := range queries {
switch query {
for _, q := range queries {
switch q.query {
case "?", "help", "usage":
internal = append(internal, helpEmbed())
case "alias", "aliases":
internal = append(internal, aliasList(b.cfg.Aliases))
default:
embed, m := b.docs(m.Author, query, false)
embed, m := b.docs(m.Author, q.query, false)
if strings.HasPrefix(embed.Title, "Error") {
continue
}
if strings.HasPrefix(embed.Title, "Package") && q.source == "urlre" {
continue
}
embeds = append(embeds, embed)
more = append(more, m)
}
Expand All @@ -185,7 +195,7 @@ func (b *botState) handleDocsText(m *gateway.MessageCreateEvent, queries []strin
data, ok := interactionMap[m.ID.String()]
if ok {
mu.Lock()
interactionMap[m.ID.String()].query = queries[0]
interactionMap[m.ID.String()].query = queries[0].query
mu.Unlock()

b.state.EditMessageComplex(m.ChannelID, data.messageID, api.EditMessageData{
Expand All @@ -202,7 +212,7 @@ func (b *botState) handleDocsText(m *gateway.MessageCreateEvent, queries []strin
id: m.ID.String(),
created: time.Now(),
userID: m.Author.ID,
query: queries[0],
query: queries[0].query,
}
mu.Unlock()

Expand Down
14 changes: 8 additions & 6 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ func (b *botState) OnCommand(e *gateway.InteractionCreateEvent) {
}

var (
cmdre = regexp.MustCompile(`\$\[([\w\d/.]+)\]`)
urlre = regexp.MustCompile(`pkg.go.dev/([\w\d/.#]+)`)
cmdre = regexp.MustCompile(`\$\[([\w\d/.]+)\]`)
urlre = regexp.MustCompile(`^(https?://)?pkg.go.dev/([\w\d/.#]+)$`)
escURLre = regexp.MustCompile(`<(https?://)?pkg.go.dev/([\w\d/.#]+)>`)
)

func (b *botState) OnMessage(m *gateway.MessageCreateEvent) {
Expand All @@ -89,14 +90,15 @@ func (b *botState) OnMessage(m *gateway.MessageCreateEvent) {
return
}

var queries []string
var queries []textQuery
for _, v := range cmdre.FindAllStringSubmatch(m.Content, 3) {
queries = append(queries, v[1])
queries = append(queries, textQuery{v[1], "cmdre"})
}

m.Content = escURLre.ReplaceAllString(m.Content, "")
for _, v := range urlre.FindAllStringSubmatch(m.Content, 3) {
s := strings.ReplaceAll(v[1], "#", ".")
queries = append(queries, s)
s := strings.ReplaceAll(v[2], "#", ".")
queries = append(queries, textQuery{s, "urlre"})
}

b.handleDocsText(m, queries)
Expand Down

0 comments on commit eff0171

Please sign in to comment.