Skip to content

Commit

Permalink
robot, spoken: record traces after emote and effect
Browse files Browse the repository at this point in the history
Fixes #51.
  • Loading branch information
zephyrtronium committed Aug 22, 2024
1 parent 1a640e1 commit 65e4047
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions command/talk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion privmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
8 changes: 6 additions & 2 deletions spoken/spoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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(),
Expand All @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions spoken/spoken_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand All @@ -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()),
Expand Down
1 change: 0 additions & 1 deletion tmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 65e4047

Please sign in to comment.