From 65e4047915f88076b0bb3aca3a22a9dcb8593f73 Mon Sep 17 00:00:00 2001 From: Branden J Brown Date: Thu, 22 Aug 2024 07:32:43 -0400 Subject: [PATCH] robot, spoken: record traces after emote and effect Fixes #51. --- command/talk.go | 6 +++--- privmsg.go | 2 +- spoken/spoken.go | 8 ++++++-- spoken/spoken_test.go | 5 +++-- tmi.go | 1 - 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/command/talk.go b/command/talk.go index 2089f78..5920108 100644 --- a/command/talk.go +++ b/command/talk.go @@ -29,7 +29,7 @@ func speakCmd(ctx context.Context, robo *Robot, call *Invocation, effect string) } e := call.Channel.Emotes.Pick(rand.Uint32()) s := m + " " + e - if err := robo.Spoken.Record(ctx, call.Channel.Send, m, trace, call.Message.Time(), 0, e, effect); err != nil { + if err := robo.Spoken.Record(ctx, call.Channel.Send, s, trace, call.Message.Time(), 0, m, e, effect); err != nil { robo.Log.ErrorContext(ctx, "couldn't record trace", slog.Any("err", err)) return "" } @@ -72,7 +72,7 @@ func Speak(ctx context.Context, robo *Robot, call *Invocation) { // OwO genyewates an uwu message. // - prompt: Start of the message to use. Optional. func OwO(ctx context.Context, robo *Robot, call *Invocation) { - u := speakCmd(ctx, robo, call, "OwO") + u := speakCmd(ctx, robo, call, "cmd OwO") if u == "" { return } @@ -87,7 +87,7 @@ func AAAAA(ctx context.Context, robo *Robot, call *Invocation) { if call.Args["prompt"] != "" { delete(call.Args, "prompt") } - u := speakCmd(ctx, robo, call, "AAAAA") + u := speakCmd(ctx, robo, call, "cmd AAAAA") if u == "" { return } diff --git a/privmsg.go b/privmsg.go index ce86e24..b84ad7d 100644 --- a/privmsg.go +++ b/privmsg.go @@ -89,7 +89,7 @@ func (robo *Robot) tmiMessage(ctx context.Context, group *errgroup.Group, send c slog.InfoContext(ctx, "speak", slog.String("text", s), slog.String("emote", e), slog.String("effect", f)) se := strings.TrimSpace(s + " " + e) sef := command.Effect(f, se) - if err := robo.spoken.Record(ctx, ch.Send, s, trace, time.Now(), 0, e, f); err != nil { + if err := robo.spoken.Record(ctx, ch.Send, sef, trace, time.Now(), 0, s, e, f); err != nil { slog.ErrorContext(ctx, "record trace failed", slog.Any("err", err)) return } diff --git a/spoken/spoken.go b/spoken/spoken.go index 757b8bd..1aa3e0e 100644 --- a/spoken/spoken.go +++ b/spoken/spoken.go @@ -19,6 +19,9 @@ type History struct { // meta is metadata that may be associated with a generated message. type meta struct { + // Orig is the original generated message, prior to applying any emote + // or effect. + Orig string `json:"orig,omitempty"` // Emote is the emote appended to the message. Emote string `json:"emote,omitempty"` // Effect is the name of the effect applied to the message. @@ -34,7 +37,7 @@ func Open(ctx context.Context, db *sqlitex.Pool) (*History, error) { } // Record records a message with its trace and metadata. -func (h *History) Record(ctx context.Context, tag, message string, trace []string, tm time.Time, cost time.Duration, emote, effect string) error { +func (h *History) Record(ctx context.Context, tag, msg string, trace []string, tm time.Time, cost time.Duration, orig, emote, effect string) error { conn, err := h.db.Take(ctx) defer h.db.Put(conn) if err != nil { @@ -51,6 +54,7 @@ func (h *History) Record(ctx context.Context, tag, message string, trace []strin go panic(fmt.Errorf("spoken: couldn't marshal trace %#v: %w", trace, err)) } m := &meta{ + Orig: orig, Emote: emote, Effect: effect, Cost: cost.Nanoseconds(), @@ -61,7 +65,7 @@ func (h *History) Record(ctx context.Context, tag, message string, trace []strin go panic(fmt.Errorf("spoken: couldn't marshal metadata %#v: %w", m, err)) } st.SetText(":tag", tag) - st.SetText(":msg", message) + st.SetText(":msg", msg) st.SetBytes(":trace", tr) st.SetInt64(":time", tm.UnixNano()) st.SetBytes(":meta", md) diff --git a/spoken/spoken_test.go b/spoken/spoken_test.go index d307c69..f78875e 100644 --- a/spoken/spoken_test.go +++ b/spoken/spoken_test.go @@ -41,7 +41,7 @@ func TestRecord(t *testing.T) { if err != nil { t.Fatal(err) } - err = h.Record(ctx, "kessoku", "bocchi ryo", []string{"1", "2"}, time.Unix(1, 0), time.Second, "xD", "o") + err = h.Record(ctx, "kessoku", "boccho ryo xD", []string{"1", "2"}, time.Unix(1, 0), time.Second, "bocchi ryo", "xD", "o") if err != nil { t.Errorf("couldn't record: %v", err) } @@ -57,7 +57,7 @@ func TestRecord(t *testing.T) { if tag != "kessoku" { t.Errorf("wrong tag recorded: want %q, got %q", "kessoku", tag) } - if msg != "bocchi ryo" { + if msg != "boccho ryo xD" { t.Errorf("wrong message recorded: want %q, got %q", "bocchi ryo", msg) } var tr []string @@ -75,6 +75,7 @@ func TestRecord(t *testing.T) { t.Errorf("couldn't unmarshal metadata from %q: %v", meta, md) } want := map[string]any{ + "orig": "bocchi ryo", "emote": "xD", "effect": "o", "cost": float64(time.Second.Nanoseconds()), diff --git a/tmi.go b/tmi.go index 8760654..a7e6d96 100644 --- a/tmi.go +++ b/tmi.go @@ -171,7 +171,6 @@ func (robo *Robot) clearmsg(ctx context.Context, group *errgroup.Group, msg *tmi // not to say it. // Note that we use the send tag rather than the learn tag for this, // because we are unlearning something that we sent. - // TODO(zeph): the message we see has the emote, so history needs to index it thus trace, tm, err := robo.spoken.Trace(ctx, ch.Send, msg.Trailing) if err != nil { slog.ErrorContext(ctx, "failed to get message trace",