-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessage.go
More file actions
52 lines (44 loc) · 1.08 KB
/
message.go
File metadata and controls
52 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"context"
"slices"
"strings"
"time"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
"github.com/hionay/rubyChan/command"
"github.com/hionay/rubyChan/history"
)
const cmdPrefix = "!"
func parseMessage(cli *mautrix.Client, store *history.HistoryStore) func(context.Context, *event.Event) {
st := time.Now()
return func(ctx context.Context, evt *event.Event) {
raw := strings.TrimSpace(evt.Content.AsMessage().Body)
nick := evt.Sender.Localpart()
store.Add(evt.RoomID, history.HistoryMessage{
Sender: nick,
Body: raw,
Timestamp: evt.Timestamp,
})
// Ignore commands from the history
ts := time.UnixMilli(evt.Timestamp)
if ts.Before(st) || evt.Sender == cli.UserID {
return
}
body, ok := strings.CutPrefix(raw, cmdPrefix)
if !ok {
return
}
fields := strings.Fields(body)
if len(fields) == 0 {
return
}
name, args := fields[0], fields[1:]
for _, cmd := range command.Registry {
if name == cmd.Name() || slices.Contains(cmd.Aliases(), name) {
cmd.Execute(ctx, cli, evt, args)
return
}
}
}
}