Compare commits
13 Commits
67ad8488a5
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 0fc68e9bd8 | |||
| 50b98be6d3 | |||
| 30d4eeea40 | |||
| f0c5e19870 | |||
| c0abf72041 | |||
| 780002b35f | |||
| 1dae50a918 | |||
| f4f563c530 | |||
| ff20bbef2c | |||
| c5c733d14c | |||
| 2517d0cb37 | |||
| f3698f0fc6 | |||
| 950662e0b9 |
@@ -7,4 +7,4 @@ Production instance is named Zenno Rob Roy, because she has read all about Umamu
|
||||
## Running
|
||||
|
||||
The bot always uses the Gateway API.
|
||||
If the `-http` argument is provided, it will also use the HTTP API, and `-token` must also be provided.
|
||||
If the `-http` argument is provided, it will also use the HTTP API, and `-key` must also be provided.
|
||||
|
||||
56
autocomplete/autocomplete.go
Normal file
56
autocomplete/autocomplete.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package autocomplete
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"cmp"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/junegunn/fzf/src/algo"
|
||||
"github.com/junegunn/fzf/src/util"
|
||||
)
|
||||
|
||||
// Set is an autocomplete set.
|
||||
type Set[V any] struct {
|
||||
keys []util.Chars
|
||||
vals []V
|
||||
}
|
||||
|
||||
// Add associates a value with a key in the autocomplete set.
|
||||
// The behavior is undefined if the key already has a value.
|
||||
func (s *Set[V]) Add(key string, val V) {
|
||||
k := util.ToChars([]byte(key))
|
||||
i, _ := slices.BinarySearchFunc(s.keys, k, func(a, b util.Chars) int {
|
||||
return bytes.Compare(a.Bytes(), b.Bytes())
|
||||
})
|
||||
s.keys = slices.Insert(s.keys, i, k)
|
||||
s.vals = slices.Insert(s.vals, i, val)
|
||||
}
|
||||
|
||||
// Find appends to r all values in the set with keys that key matches.
|
||||
func (s *Set[V]) Find(r []V, key string) []V {
|
||||
initFzf()
|
||||
var (
|
||||
p = []rune(key)
|
||||
|
||||
got []V
|
||||
t []algo.Result
|
||||
slab util.Slab
|
||||
)
|
||||
for i := range s.keys {
|
||||
res, _ := algo.FuzzyMatchV2(false, true, true, &s.keys[i], p, false, &slab)
|
||||
if res.Score <= 0 {
|
||||
continue
|
||||
}
|
||||
j, _ := slices.BinarySearchFunc(t, res, func(a, b algo.Result) int { return -cmp.Compare(a.Score, b.Score) })
|
||||
// Insert after all other matches with the same score for stability.
|
||||
for j < len(t) && t[j].Score == res.Score {
|
||||
j++
|
||||
}
|
||||
t = slices.Insert(t, j, res)
|
||||
got = slices.Insert(got, j, s.vals[i])
|
||||
}
|
||||
return append(r, got...)
|
||||
}
|
||||
|
||||
var initFzf = sync.OnceFunc(func() { algo.Init("default") })
|
||||
70
autocomplete/autocomplete_test.go
Normal file
70
autocomplete/autocomplete_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package autocomplete_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"git.sunturtle.xyz/zephyr/horsebot/autocomplete"
|
||||
)
|
||||
|
||||
func these(s ...string) []string { return s }
|
||||
|
||||
func TestAutocomplete(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
add []string
|
||||
search string
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "empty",
|
||||
add: nil,
|
||||
search: "",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "exact",
|
||||
add: these("bocchi"),
|
||||
search: "bocchi",
|
||||
want: these("bocchi"),
|
||||
},
|
||||
{
|
||||
name: "extra",
|
||||
add: these("bocchi", "ryo", "nijika", "kita"),
|
||||
search: "bocchi",
|
||||
want: these("bocchi"),
|
||||
},
|
||||
{
|
||||
name: "short",
|
||||
add: these("bocchi", "ryo", "nijika", "kita"),
|
||||
search: "o",
|
||||
want: these("bocchi", "ryo"),
|
||||
},
|
||||
{
|
||||
name: "unrelated",
|
||||
add: these("bocchi", "ryo", "nijika", "kita"),
|
||||
search: "x",
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "map",
|
||||
add: these("Corazón ☆ Ardiente"),
|
||||
search: "corazo",
|
||||
want: these("Corazón ☆ Ardiente"),
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
var set autocomplete.Set[string]
|
||||
for _, s := range c.add {
|
||||
set.Add(s, s)
|
||||
}
|
||||
got := set.Find(nil, c.search)
|
||||
slices.Sort(c.want)
|
||||
slices.Sort(got)
|
||||
if !slices.Equal(c.want, got) {
|
||||
t.Errorf("wrong results: want %q, got %q", c.want, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
7
go.mod
7
go.mod
@@ -1,10 +1,11 @@
|
||||
module git.sunturtle.xyz/zephyr/horsebot
|
||||
|
||||
go 1.25.6
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260122143238-1ae654c266b7
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260210125822-b55e1bc200a1
|
||||
github.com/disgoorg/disgo v0.19.0-rc.15
|
||||
github.com/junegunn/fzf v0.67.0
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -13,6 +14,8 @@ require (
|
||||
github.com/disgoorg/snowflake/v2 v2.0.3 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/klauspost/compress v1.18.2 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad // indirect
|
||||
golang.org/x/crypto v0.46.0 // indirect
|
||||
golang.org/x/sys v0.39.0 // indirect
|
||||
|
||||
13
go.sum
13
go.sum
@@ -1,7 +1,5 @@
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260118202043-d147d71519e4 h1:3m5C5/9TX4BcW8z8gQAC0+zbsb+jLtJ+VlJk3UibY2s=
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260118202043-d147d71519e4/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260122143238-1ae654c266b7 h1:fneQyGAkeefCemRtuvTO32MIwkcYm3ajeIS6hfXrAno=
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260122143238-1ae654c266b7/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260210125822-b55e1bc200a1 h1:nIk5Mis384wx/ndMa/3zSr1omkWK8rg4I/Z46BCAIe8=
|
||||
git.sunturtle.xyz/zephyr/horse v0.0.0-20260210125822-b55e1bc200a1/go.mod h1:qGXO/93EfCOI1oGSLqrRkPDF/EAdsgLNZJjRKx+i4Lk=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/disgoorg/disgo v0.19.0-rc.15 h1:x0NsV2gcbdjwuztsg2wYXw76p1Cpc8f6ByDrkPcfQtU=
|
||||
@@ -14,16 +12,23 @@ github.com/disgoorg/snowflake/v2 v2.0.3 h1:3B+PpFjr7j4ad7oeJu4RlQ+nYOTadsKapJIzg
|
||||
github.com/disgoorg/snowflake/v2 v2.0.3/go.mod h1:W6r7NUA7DwfZLwr00km6G4UnZ0zcoLBRufhkFWgAc4c=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/junegunn/fzf v0.67.0 h1:naiOdIkV5/ZCfHgKQIV/f5YDWowl95G6yyOQqW8FeSo=
|
||||
github.com/junegunn/fzf v0.67.0/go.mod h1:xlXX2/rmsccKQUnr9QOXPDi5DyV9cM0UjKy/huScBeE=
|
||||
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
|
||||
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad h1:qIQkSlF5vAUHxEmTbaqt1hkJ/t6skqEGYiMag343ucI=
|
||||
github.com/sasha-s/go-csync v0.0.0-20240107134140-fcbab37b09ad/go.mod h1:/pA7k3zsXKdjjAiUhB5CjuKib9KJGCaLvZwtxGC8U0s=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU=
|
||||
golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
||||
15
main.go
15
main.go
@@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.sunturtle.xyz/zephyr/horse/horse"
|
||||
@@ -162,17 +161,9 @@ func skillHandler(data discord.SlashCommandInteractionData, e *handler.CommandEv
|
||||
}
|
||||
|
||||
func skillAutocomplete(e *handler.AutocompleteEvent) error {
|
||||
q := strings.ToLower(e.Data.String("query"))
|
||||
r := make([]discord.AutocompleteChoice, 0, 25)
|
||||
for k, _ := range global.SkillNameToID {
|
||||
if strings.HasPrefix(strings.ToLower(k), q) {
|
||||
r = append(r, discord.AutocompleteChoiceString{Name: k, Value: k})
|
||||
if len(r) == cap(r) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return e.AutocompleteResult(r)
|
||||
q := e.Data.String("query")
|
||||
opts := skillGlobalAuto().Find(nil, q)
|
||||
return e.AutocompleteResult(opts[:min(len(opts), 25)])
|
||||
}
|
||||
|
||||
func skillButton(data discord.ButtonInteractionData, e *handler.ComponentEvent) error {
|
||||
|
||||
35
skill.go
35
skill.go
@@ -3,8 +3,11 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.sunturtle.xyz/zephyr/horse/horse"
|
||||
"git.sunturtle.xyz/zephyr/horse/horse/global"
|
||||
"git.sunturtle.xyz/zephyr/horsebot/autocomplete"
|
||||
"github.com/disgoorg/disgo/discord"
|
||||
)
|
||||
|
||||
@@ -15,13 +18,25 @@ func RenderSkill(id horse.SkillID, all map[horse.SkillID]horse.Skill, groups map
|
||||
}
|
||||
|
||||
thumburl := fmt.Sprintf("https://gametora.com/images/umamusume/skill_icons/utx_ico_skill_%d.png", s.IconID)
|
||||
r := discord.NewContainer(discord.NewSection(discord.NewTextDisplayf("## %s\n%s", s.Name, s.Description)).WithAccessory(discord.NewThumbnail(thumburl)))
|
||||
top := "## " + s.Name
|
||||
if s.UniqueOwner != "" {
|
||||
top += "\n-# " + s.UniqueOwner
|
||||
}
|
||||
r := discord.NewContainer(
|
||||
discord.NewSection(
|
||||
discord.NewTextDisplay(top),
|
||||
discord.NewTextDisplay(s.Description),
|
||||
).WithAccessory(discord.NewThumbnail(thumburl)),
|
||||
)
|
||||
var skilltype string
|
||||
switch {
|
||||
case s.Rarity == 3, s.Rarity == 4, s.Rarity == 5:
|
||||
// unique of various star levels
|
||||
r.AccentColor = 0xaca4d4
|
||||
skilltype = "Unique Skill"
|
||||
case s.UniqueOwner != "":
|
||||
r.AccentColor = 0xcccccc
|
||||
skilltype = "Inherited Unique"
|
||||
case s.Rarity == 2:
|
||||
// rare (gold)
|
||||
r.AccentColor = 0xd7c25b
|
||||
@@ -115,6 +130,18 @@ func isDebuff(s horse.Skill) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// TODO(zeph): autocomplete
|
||||
// if we want to backgroundify construction of an autocomplete map,
|
||||
// use sync.OnceValue and launch a goroutine in main to get the value and discard it
|
||||
var skillGlobalAuto = sync.OnceValue(func() *autocomplete.Set[discord.AutocompleteChoice] {
|
||||
var set autocomplete.Set[discord.AutocompleteChoice]
|
||||
for _, id := range global.OrderedSkills {
|
||||
s := global.AllSkills[id]
|
||||
set.Add(s.Name, discord.AutocompleteChoiceString{Name: s.Name, Value: s.Name})
|
||||
if s.UniqueOwner != "" {
|
||||
if s.Rarity >= 3 {
|
||||
set.Add(s.UniqueOwner, discord.AutocompleteChoiceString{Name: "Unique: " + s.UniqueOwner, Value: s.Name})
|
||||
} else {
|
||||
set.Add(s.UniqueOwner, discord.AutocompleteChoiceString{Name: "Inherited unique: " + s.UniqueOwner, Value: s.Name})
|
||||
}
|
||||
}
|
||||
}
|
||||
return &set
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user