Skip to content

Commit

Permalink
robot, command: add echoing
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrtronium committed Aug 20, 2024
1 parent ddee7ac commit 61dfe70
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
25 changes: 25 additions & 0 deletions command/echo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package command

import (
"context"
"log/slog"
)

// EchoIn sends a plain text message to any channel.
// - in: Name of the channel to send to.
// - msg: Message to send.
func EchoIn(ctx context.Context, robo *Robot, call *Invocation) {
t := call.Args["in"]
ch := robo.Channels[t]
if ch == nil {
robo.Log.WarnContext(ctx, "echo into unknown channel", slog.String("target", t))
return
}
ch.Message(ctx, "", call.Args["msg"])
}

// Echo sends a plain text message to the channel in which it is invoked.
// - msg: Message to send.
func Echo(ctx context.Context, robo *Robot, call *Invocation) {
call.Channel.Message(ctx, "", call.Args["msg"])
}
44 changes: 37 additions & 7 deletions privmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,31 @@ func (robo *Robot) tmiMessage(ctx context.Context, group *errgroup.Group, send c
}

func (robo *Robot) command(ctx context.Context, ch *channel.Channel, m *message.Received, from, cmd string) {
if from == robo.tmi.owner {
// TODO(zeph): check owner and moderator commands
}
if ch.Mod[from] || m.IsModerator {
// TODO(zeph): check moderator commands
var c *twitchCommand
var args map[string]string
level := "any"
switch {
case from == robo.tmi.owner:
c, args = findTwitch(twitchOwner, cmd)
if c != nil {
level = "owner"
break
}
fallthrough
case ch.Mod[from], m.IsModerator:
c, args = findTwitch(twitchMod, cmd)
if c != nil {
level = "mod"
break
}
fallthrough
default:
c, args = findTwitch(twitchAny, cmd)
}
c, args := findTwitch(twitchAny, cmd)
if c == nil {
return
}
slog.InfoContext(ctx, "regular command", slog.String("name", c.name), slog.Any("args", args))
slog.InfoContext(ctx, "command", slog.String("level", level), slog.String("name", c.name), slog.Any("args", args))
r := command.Robot{
Log: slog.Default(),
Channels: robo.channels,
Expand Down Expand Up @@ -295,6 +309,22 @@ func findTwitch(cmds []twitchCommand, text string) (*twitchCommand, map[string]s
return nil, nil
}

var twitchOwner = []twitchCommand{
{
parse: regexp.MustCompile(`^(?i:in\s+(?<in>#\S+)\s+echo)\s+(?<msg>.*)`),
fn: command.EchoIn,
name: "echo-in",
},
}

var twitchMod = []twitchCommand{
{
parse: regexp.MustCompile(`^(?i:echo)\s+(?<msg>.*)`),
fn: command.Echo,
name: "echo",
},
}

var twitchAny = []twitchCommand{
{
parse: regexp.MustCompile(`^(?i:OwO|uwu)`),
Expand Down

0 comments on commit 61dfe70

Please sign in to comment.