Added fuzzy search to Discord bot
This commit is contained in:
parent
4837bf7e4f
commit
32fa4aca21
@ -112,4 +112,25 @@ func onMessage(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
s.ChannelMessageSend(m.ChannelID, "https://notify.moe/forum/"+strings.ToLower(strings.Split(m.Content, " ")[1]))
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(m.Content, "!s ") {
|
||||
term := m.Content[len("!s "):]
|
||||
userResults, animeResults := arn.Search(term, 10, 10)
|
||||
message := ""
|
||||
|
||||
for _, user := range userResults {
|
||||
message += "https://notify.moe/" + user.Link() + "\n"
|
||||
}
|
||||
|
||||
for _, anime := range animeResults {
|
||||
message += "https://notify.moe/" + anime.Link() + "\n"
|
||||
}
|
||||
|
||||
if len(userResults) == 0 && len(animeResults) == 0 {
|
||||
message = "Sorry, I couldn't find any anime or users with that term."
|
||||
}
|
||||
|
||||
s.ChannelMessageSend(m.ChannelID, message)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package search
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
"github.com/animenotifier/notify.moe/components"
|
||||
@ -13,63 +11,8 @@ const maxAnime = 9 * 7
|
||||
|
||||
// Get search page.
|
||||
func Get(ctx *aero.Context) string {
|
||||
term := strings.ToLower(ctx.Get("term"))
|
||||
term := ctx.Get("term")
|
||||
|
||||
var users []*arn.User
|
||||
var animeResults []*arn.Anime
|
||||
|
||||
// Search everything in parallel
|
||||
aero.Parallel(func() {
|
||||
// Search users
|
||||
var user *arn.User
|
||||
|
||||
userSearchIndex, err := arn.GetSearchIndex("User")
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for name, id := range userSearchIndex.TextToID {
|
||||
if strings.Index(name, term) != -1 {
|
||||
user, err = arn.GetUser(id)
|
||||
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
users = append(users, user)
|
||||
|
||||
if len(users) >= maxUsers {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}, func() {
|
||||
// Search anime
|
||||
var anime *arn.Anime
|
||||
|
||||
animeSearchIndex, err := arn.GetSearchIndex("Anime")
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for title, id := range animeSearchIndex.TextToID {
|
||||
if strings.Index(title, term) != -1 {
|
||||
anime, err = arn.GetAnime(id)
|
||||
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
animeResults = append(animeResults, anime)
|
||||
|
||||
if len(animeResults) >= maxAnime {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return ctx.HTML(components.Search(users, animeResults))
|
||||
userResults, animeResults := arn.Search(term, maxUsers, maxAnime)
|
||||
return ctx.HTML(components.Search(userResults, animeResults))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user