Skip to content

Commit

Permalink
Merge branch 'wip'
Browse files Browse the repository at this point in the history
  • Loading branch information
mohemohe committed Jul 15, 2024
2 parents 3632992 + cdcfd57 commit 778213f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,5 @@ modules.xml
.idea/sonarlint

# End of https://www.gitignore.io/api/go,intellij+all

script-debug
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
module github.com/lxbot/script-debug

go 1.13

require (
github.com/lxbot/lxlib/v2 v2.0.0-beta.9
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/rs/xid v1.4.0 // indirect
)
44 changes: 44 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
github.com/lxbot/lxlib/v2 v2.0.0-alpha.3 h1:3oVkq1eubZsDdvL/9zywiqOPzKg9TzB7RLMCmdFHbd4=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.3/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.4 h1:/kVrEE6K2xyOh1Vujj0nc8sIf6QOjwi/J04j/mYmgvw=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.4/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.5 h1:v9HuEOYTriZ6Vnk8h7WwJS1AZ3Hn4nW7n5rnuuXLeDs=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.5/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.6 h1:dB3pnXZkAVlmVQ+5PjfUEviUdAaSfUcOJy/ohsE04uM=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.6/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.7 h1:FENSsbLYfbqNoGrzvgQSd7gPA7JHert6vrw9lIPvl/k=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.7/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.8 h1:PI7OEetrX9GfiGlqF8NfNjRvoVOT/3b8OQCfaC28Xzo=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.8/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.9 h1:PTKXiSyUyu/Zaf6+oIk+I3CsCFAFjmLwzxVoM5f6Yr4=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.9/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.10 h1:cOzTRIQU4dAGrZw5soY9PNyoB7lMpJ5BTDIxa9rbET4=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.10/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.11 h1:BTHz0+LCXzdSfKtvJeELM4fXRzPoADXvCaF+lg7zgC0=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.11/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.12 h1:ZntDl1mlMP0hFXR6dMniPS61rprgJTOosKOml7uN4xc=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.12/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.13 h1:ZoZCeD+9SK5vB6m+0XW7qpto8wuOiV3x0UWfJUw2Zcc=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.13/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.14 h1:KU+77yzWdm+9TbNkGCeK3OSmYdnm96a/Vp5X/SnUiUI=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.14/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.15 h1:a16xATV/RaU4wjOi27sUuCuhgXbBmyebYuopjRbA0f8=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.15/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.16 h1:QBuwXGX+ILbG6j+oJdg4Lh3tyQd+lbUE25nwiU8Z36A=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.16/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.17 h1:APuNdz2nXmBXMRxQSbiSvAFAqWT418uYAtxC7VslDsI=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.17/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.18 h1:QAgONx8vxbE+04czogxqDd0g6wV6oPeOorJJcz26igE=
github.com/lxbot/lxlib/v2 v2.0.0-alpha.18/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-beta.7 h1:xTOpNg9L1gsoUhHHdALj5kfW9N9pxvWwhMkW1hWG1Eo=
github.com/lxbot/lxlib/v2 v2.0.0-beta.7/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-beta.8 h1:r6I8YgdfoXa0lbmf2oorxtyycYTJW7QpL597rYRfsts=
github.com/lxbot/lxlib/v2 v2.0.0-beta.8/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/lxbot/lxlib/v2 v2.0.0-beta.9 h1:dUv1TmqtT+1WZS9CHhCgaGLp7fcH8OS1RdCw8l6LvTw=
github.com/lxbot/lxlib/v2 v2.0.0-beta.9/go.mod h1:otQkZ+zWIdaJUuYk3QSgONBkDP3ur1IGbChtIhONO9w=
github.com/mitchellh/mapstructure v1.4.3 h1:OVowDSCllw/YjdLkam3/sm7wEtOy59d8ndGgCcyj8cs=
github.com/mitchellh/mapstructure v1.4.3/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/rs/xid v1.3.0 h1:6NjYksEUlhurdVehpc7S7dk6DAmcKv8V9gG0FsVN2U4=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
70 changes: 21 additions & 49 deletions script.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,34 @@
package main

import (
"fmt"
"plugin"
"strings"
"github.com/lxbot/lxlib/v2"
"github.com/lxbot/lxlib/v2/common"
)

type M = map[string]interface{}

var store *plugin.Plugin
func main() {
script, messageCh := lxlib.NewScript()

func Boot(s *plugin.Plugin, c *chan M) {
store = s
}
for {
message := <-*messageCh

func OnMessage() []func(M) M {
return []func(M) M{
func(msg M) M {
text := msg["message"].(M)["text"].(string)
last := get(msg)
set(msg, text)
if last != "" {
msg["message"].(M)["text"] = "last: "+last+"\n"+text
}
if strings.HasPrefix(text, "reply ") {
msg["mode"] = "reply"
} else {
msg["mode"] = "send"
}
return msg
},
}
}
common.TraceLog("(script)", "lxlib.listen()", "event received", "message:", message)

func get(m M) string {
fn, err := store.Lookup("Get")
if err != nil {
_ = fmt.Errorf("%v", err)
return ""
}
result := fn.(func(string) interface{})(key(m))
if result == nil {
return ""
}
return result.(string)
}
response, err := message.Copy()
if err != nil {
common.ErrorLog("message copy error:", err)
continue
}

key := "last_msg_" + message.User.ID

func set(m M, value string) {
fn, err := store.Lookup("Set")
if err != nil {
_ = fmt.Errorf("%v", err)
return
lastMessage := script.GetStorage(key)
if lastMessage == nil {
lastMessage = ""
}
script.SetStorage(key, message.Contents[0].Text)

script.SendMessage(response.Reply().ResetContents().AddContent("last message: '" + lastMessage.(string) + "'"))
}
fn.(func(string, interface{}))(key(m), value)
}

func key(m M) string {
roomID := m["room"].(M)["id"].(string)
userID := m["user"].(M)["id"].(string)
return "last_msg_" + roomID + "_" + userID
}

0 comments on commit 778213f

Please sign in to comment.