summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoruakci <git@uakci.space>2022-11-07 02:30:26 +0100
committeruakci <git@uakci.space>2022-11-07 02:35:47 +0100
commit77b95f3a07f1c18b345f8e312f3321c3e38658bc (patch)
treea3b2ef282f4653ce50421f89f2e5adb3a47a3906
parentreplace spe and nui with zugai’s lil serial tool (diff)
downloadnuogai-77b95f3a07f1c18b345f8e312f3321c3e38658bc.tar.gz
nuogai-77b95f3a07f1c18b345f8e312f3321c3e38658bc.zip
simplify and refactor toadua search + add bias
-rw-r--r--bot.go106
-rw-r--r--go.mod20
-rw-r--r--go.sum35
3 files changed, 92 insertions, 69 deletions
diff --git a/bot.go b/bot.go
index 170789a..6d8b5fb 100644
--- a/bot.go
+++ b/bot.go
@@ -174,34 +174,17 @@ func respond(message string, callback func(Response)) {
parts := strings.Fields(message)
cmd, args, rest := parts[0], parts[1:], strings.Join(parts[1:], " ")
restQuery := url.QueryEscape(rest)
- if strings.HasPrefix(cmd, "%") {
- cmd_ := cmd[1:]
- showNotes := false
- if strings.HasPrefix(cmd_, "!") {
- cmd_ = cmd_[1:]
- showNotes = true
- }
- var (
- n int
- err error
- )
- if len(cmd_) > 0 {
- n, err = strconv.Atoi(cmd_)
- } else {
- if showNotes {
- n = 1
- } else {
- n = 3
- }
- }
- if err == nil {
- if n >= 0 {
- Toadua(args, returnText, n, showNotes)
- } else {
- returnText("less than zero, that's quite many")
+ if matches := toaduaCmdRe.FindStringSubmatch(cmd); matches != nil {
+ n := 1
+ if matches[1] != "" {
+ var err error
+ n, err = strconv.Atoi(matches[1])
+ if err != nil {
+ returnText("wrong kinda number")
}
- return
}
+ Toadua(args, n, returnText)
+ return
} else if strings.HasPrefix(cmd, "?") && len(cmd) > 1 && cmd[1] != '?' {
fragments := strings.Split(strings.TrimSpace(cmd[1:]+" "+rest), "/")
for i, fragment := range fragments {
@@ -379,7 +362,30 @@ func respond(message string, callback func(Response)) {
}
}
-func Toadua(args []string, returnText func(string), howMany int, showNotes bool) {
+type ToaduaRequest struct {
+ Action string `json:"action"`
+ Query interface{} `json:"query"`
+ PreferredScope string `json:"preferred_scope"`
+ PreferredScopeBias float64 `json:"preferred_scope_bias"`
+}
+
+type ToaduaResponse struct {
+ Success bool `json:"success"`
+ Error string `json:"error"`
+ Entries []struct {
+ Id string `json:"id"`
+ User string `json:"user"`
+ Head string `json:"head"`
+ Body string `json:"body"`
+ Score int `json:"score"`
+ Notes []struct {
+ User string `json:"user"`
+ Content string `json:"content"`
+ } `json:"notes"`
+ } `json:"results"`
+}
+
+func Toadua(args []string, howMany int, returnText func(string)) {
if len(args) == 0 {
returnText("please supply a query")
return
@@ -391,12 +397,11 @@ func Toadua(args []string, returnText func(string), howMany int, showNotes bool)
args = args[1:]
}
query := strings.Join(args, " ")
- mars, err := json.Marshal(struct {
- S string `json:"action"`
- I interface{} `json:"query"`
- }{
- "search",
- ToaduaQuery(query),
+ mars, err := json.Marshal(ToaduaRequest{
+ Action: "search",
+ Query: ToaduaQuery(query),
+ PreferredScope: "en",
+ PreferredScopeBias: 16,
})
if err != nil {
log.Print(err)
@@ -410,21 +415,7 @@ func Toadua(args []string, returnText func(string), howMany int, showNotes bool)
returnText(err.Error())
return
}
- var resp struct {
- Success bool `json:"success"`
- Error string `json:"error"`
- Entries []struct {
- Id string `json:"id"`
- User string `json:"user"`
- Head string `json:"head"`
- Body string `json:"body"`
- Score int `json:"score"`
- Notes []struct {
- User string `json:"user"`
- Content string `json:"content"`
- } `json:"notes"`
- } `json:"results"`
- }
+ var resp ToaduaResponse
body, err := ioutil.ReadAll(raw.Body)
if err != nil {
log.Print(err)
@@ -454,7 +445,11 @@ func Toadua(args []string, returnText func(string), howMany int, showNotes bool)
last := min(first+howMany, len(resp.Entries))
var b strings.Builder
b.Grow(2000)
- fmt.Fprintf(&b, "\u2003(%d–%d/%d)", first+1, last, len(resp.Entries))
+ searchProngs := strconv.Itoa(first + 1)
+ if last > first+1 {
+ searchProngs += "–" + strconv.Itoa(last)
+ }
+ fmt.Fprintf(&b, "\u2003(%s/%d)", searchProngs, len(resp.Entries))
soFar := b.String()
for _, e := range resp.Entries[first:last] {
// if i != 0 {
@@ -462,11 +457,7 @@ func Toadua(args []string, returnText func(string), howMany int, showNotes bool)
// }
// b.WriteString(" — ")
b.WriteString("**" + e.Head + "**")
- if showNotes {
- fmt.Fprintf(&b, " (%s)", e.User)
- } else if e.User == "official" {
- b.WriteString(" ❦")
- }
+ fmt.Fprintf(&b, " (%s)", e.User)
if e.Score != 0 {
b.WriteString(" ")
if e.Score > 0 {
@@ -478,11 +469,8 @@ func Toadua(args []string, returnText func(string), howMany int, showNotes bool)
// b.WriteString(" — ")
b.WriteString("\n\u2003")
b.WriteString(strings.Join(strings.Split(e.Body, "\n"), "\n\u2003"))
- if showNotes {
- b.WriteString("")
- for _, note := range e.Notes {
- fmt.Fprintf(&b, "\n\u2003\u2003• (%s) %s", note.User, note.Content)
- }
+ for _, note := range e.Notes {
+ fmt.Fprintf(&b, "\n\u2003\u2003• (%s) %s", note.User, note.Content)
}
old := soFar
soFar = b.String()
diff --git a/go.mod b/go.mod
index 5f76146..50015a9 100644
--- a/go.mod
+++ b/go.mod
@@ -3,18 +3,18 @@ module git.uakci.pl/toaq/nuogai
go 1.17
require (
- github.com/JohannesKaufmann/html-to-markdown v1.3.5
- github.com/bwmarrin/discordgo v0.25.0
+ github.com/JohannesKaufmann/html-to-markdown v1.3.6
+ github.com/bwmarrin/discordgo v0.26.1
github.com/eaburns/toaq v0.0.0-20210614121731-80ccb209650b
- golang.org/x/text v0.3.7
+ golang.org/x/text v0.4.0
)
require (
- github.com/PuerkitoBio/goquery v1.5.1 // indirect
- github.com/andybalholm/cascadia v1.1.0 // indirect
- github.com/eaburns/peggy v1.0.0 // indirect
- github.com/gorilla/websocket v1.4.2 // indirect
- golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
- golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
- golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
+ github.com/PuerkitoBio/goquery v1.8.0 // indirect
+ github.com/andybalholm/cascadia v1.3.1 // indirect
+ github.com/eaburns/peggy v1.0.2 // indirect
+ github.com/gorilla/websocket v1.5.0 // indirect
+ golang.org/x/crypto v0.1.0 // indirect
+ golang.org/x/net v0.1.0 // indirect
+ golang.org/x/sys v0.1.0 // indirect
)
diff --git a/go.sum b/go.sum
index 7d6df2c..837e68d 100644
--- a/go.sum
+++ b/go.sum
@@ -1,15 +1,25 @@
github.com/JohannesKaufmann/html-to-markdown v1.3.5 h1:FrP3D5IqpxkNOk97TvbFduSo0JQKs/ZpgjuxpmAEFRA=
github.com/JohannesKaufmann/html-to-markdown v1.3.5/go.mod h1:JNSClIRYICFDiFhw6RBhBeWGnMSSKVZ6sPQA+TK4tyM=
+github.com/JohannesKaufmann/html-to-markdown v1.3.6 h1:i3Ma4RmIU97gqArbxZXbFqbWKm7XtImlMwVNUouQ7Is=
+github.com/JohannesKaufmann/html-to-markdown v1.3.6/go.mod h1:Ol3Jv/xw8jt8qsaLeSh/6DBBw4ZBJrTqrOu3wbbUUg8=
github.com/PuerkitoBio/goquery v1.5.1 h1:PSPBGne8NIUWw+/7vFBV+kG2J/5MOjbzc7154OaKCSE=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
+github.com/PuerkitoBio/goquery v1.8.0 h1:PJTF7AmFCFKk1N6V6jmKfrNH9tV5pNE6lZMkG0gta/U=
+github.com/PuerkitoBio/goquery v1.8.0/go.mod h1:ypIiRMtY7COPGk+I/YbZLbxsxn9g5ejnI2HSMtkjZvI=
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
+github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
+github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/bwmarrin/discordgo v0.25.0 h1:NXhdfHRNxtwso6FPdzW2i3uBvvU7UIQTghmV2T4nqAs=
github.com/bwmarrin/discordgo v0.25.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
+github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE=
+github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/eaburns/peggy v1.0.0 h1:cvZIO5GGq2Qy1l1Va0Mte6LFVTY6GtAGbzSvouFcjUk=
github.com/eaburns/peggy v1.0.0/go.mod h1:X2pbl0EV5erfnK8kSGwo0lBCgMGokvR1E6KerAoDKXg=
+github.com/eaburns/peggy v1.0.2 h1:RJwNVF4cvzLGiKInHGBT8sVwP5HDuVP/PaBU9tHO9Rk=
+github.com/eaburns/peggy v1.0.2/go.mod h1:X2pbl0EV5erfnK8kSGwo0lBCgMGokvR1E6KerAoDKXg=
github.com/eaburns/pretty v1.0.0 h1:00W1wrrtMXUSqLPN0txS8j7g9qFXy6nA5vZVqVQOo6w=
github.com/eaburns/pretty v1.0.0/go.mod h1:retcK8A0KEgdmb0nuxhvyxixwCmEPO7SKlK0IJhjg8A=
github.com/eaburns/toaq v0.0.0-20210614121731-80ccb209650b h1:2mu0jEGoRhKMpkHMInz584EWFa0n3oEzpxoAo6PN1IE=
@@ -19,6 +29,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
+github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
@@ -27,31 +39,52 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sebdah/goldie/v2 v2.5.1 h1:hh70HvG4n3T3MNRJN2z/baxPR8xutxo7JVxyi2svl+s=
github.com/sebdah/goldie/v2 v2.5.1/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
+github.com/sebdah/goldie/v2 v2.5.3 h1:9ES/mNN+HNUbNWpVAlrzuZ7jE+Nrczbj8uFRjM7624Y=
+github.com/sebdah/goldie/v2 v2.5.3/go.mod h1:oZ9fp0+se1eapSRjfYbsV/0Hqhbuu3bJVvKI/NNtssI=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
+github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
+github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/velour/chat v0.0.0-20180713122344-fd1d1606cb89/go.mod h1:ejwOYCjnDMyO5LXFXRARQJGBZ6xQJZ3rgAHE5drSuMM=
github.com/yuin/goldmark v1.2.0 h1:WOOcyaJPlzb8fZ8TloxFe8QZkhOOJx87leDa9MIT9dc=
github.com/yuin/goldmark v1.2.0/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
+github.com/yuin/goldmark v1.4.14 h1:jwww1XQfhJN7Zm+/a1ZA/3WUiEBEroYFNTiV3dKwM8U=
+github.com/yuin/goldmark v1.4.14/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
+golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
+golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200320220750-118fecf932d8/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
+golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
+golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
+golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
+golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
+golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
@@ -59,3 +92,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
+gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=