Skip to content

Commit

Permalink
command: record spoken message traces
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Aug 18, 2024
1 parent bac9855 commit 148dffc
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 2 additions & 0 deletions command/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/zephyrtronium/robot/brain"
"github.com/zephyrtronium/robot/channel"
"github.com/zephyrtronium/robot/privacy"
"github.com/zephyrtronium/robot/spoken"
)

// Robot is the bot state as is visible to commands.
Expand All @@ -14,4 +15,5 @@ type Robot struct {
Channels map[string]*channel.Channel // TODO(zeph): syncmap[string]channel.Channel
Brain brain.Brain
Privacy *privacy.List
Spoken *spoken.History
}
22 changes: 14 additions & 8 deletions command/talk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/zephyrtronium/robot/brain"
)

func speakCmd(ctx context.Context, robo *Robot, call *Invocation) string {
func speakCmd(ctx context.Context, robo *Robot, call *Invocation, effect string) string {
t := call.Message.Time()
r := call.Channel.Rate.ReserveN(call.Message.Time(), 1)
cancel := func() { r.CancelAt(t) }
Expand All @@ -19,28 +19,34 @@ func speakCmd(ctx context.Context, robo *Robot, call *Invocation) string {
cancel()
return ""
}
// TODO(zeph): record trace
m, _, err := brain.Speak(ctx, robo.Brain, call.Channel.Send, call.Args["prompt"])
m, trace, err := brain.Speak(ctx, robo.Brain, call.Channel.Send, call.Args["prompt"])
if err != nil {
robo.Log.ErrorContext(ctx, "couldn't speak", "err", err.Error())
cancel()
return ""
}
if call.Channel.Block.MatchString(m) {
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 {
robo.Log.ErrorContext(ctx, "couldn't record trace", slog.Any("err", err))
cancel()
return ""
}
if call.Channel.Block.MatchString(s) {
robo.Log.WarnContext(ctx, "generated blocked message",
slog.String("in", call.Channel.Name),
slog.String("text", m),
slog.String("emote", e),
)
cancel()
return ""
}
e := call.Channel.Emotes.Pick(rand.Uint32())
slog.InfoContext(ctx, "speak", "in", call.Channel.Name, "text", m, "emote", e)
return m + " " + e
}

func Speak(ctx context.Context, robo *Robot, call *Invocation) {
u := speakCmd(ctx, robo, call)
u := speakCmd(ctx, robo, call, "")
if u == "" {
return
}
Expand All @@ -53,7 +59,7 @@ func Speak(ctx context.Context, robo *Robot, call *Invocation) {
}

func OwO(ctx context.Context, robo *Robot, call *Invocation) {
u := speakCmd(ctx, robo, call)
u := speakCmd(ctx, robo, call, "OwO")
if u == "" {
return
}
Expand All @@ -77,7 +83,7 @@ var owoRep = strings.NewReplacer(
)

func AAAAA(ctx context.Context, robo *Robot, call *Invocation) {
u := speakCmd(ctx, robo, call)
u := speakCmd(ctx, robo, call, "AAAAA")
if u == "" {
return
}
Expand Down
1 change: 1 addition & 0 deletions privmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (robo *Robot) command(ctx context.Context, ch *channel.Channel, m *message.
Channels: robo.channels,
Brain: robo.brain,
Privacy: robo.privacy,
Spoken: robo.spoken,
}
inv := command.Invocation{
Channel: ch,
Expand Down

0 comments on commit 148dffc

Please sign in to comment.