Add chatcommand argument parsing

This commit is contained in:
Noah 2024-03-15 18:27:02 -05:00
parent 25512974d2
commit 85ab2a963b

View File

@ -3,15 +3,15 @@ package main
import ( import (
"context" "context"
"log"
"time"
"strings"
"fmt" "fmt"
"log"
"strings"
"time"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mautrix/event" "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
"maunium.net/go/mautrix/format" "maunium.net/go/mautrix/format"
"maunium.net/go/mautrix/id"
) )
// MatrixMessage stores information about a matrix message. // MatrixMessage stores information about a matrix message.
@ -19,10 +19,13 @@ type MatrixMessage struct {
text string text string
} }
// MatrixCommandCallback is a function that gets called when the bot is issued a command over Matrix
type MatrixCommandCallback func(ctx context.Context, evt *event.Event, args []string) string
// MatrixCommand stores information about a matrix chat command // MatrixCommand stores information about a matrix chat command
type MatrixCommand struct { type MatrixCommand struct {
cmd string cmd string
callback func(ctx context.Context, evt *event.Event) string callback MatrixCommandCallback
description string description string
} }
@ -38,10 +41,10 @@ func sendMessage(msg MatrixMessage) {
} }
// registerChatCommand // registerChatCommand
func registerChatCommand(cmd string, description string, callback func(ctx context.Context, evt *event.Event) string) { func registerChatCommand(cmd string, description string, callback MatrixCommandCallback) {
Chatcommands = append(Chatcommands, MatrixCommand{ Chatcommands = append(Chatcommands, MatrixCommand{
cmd: cmd, cmd: cmd,
callback: callback, callback: callback,
description: description, description: description,
}) })
} }
@ -53,9 +56,9 @@ func initMatrix() {
log.Fatal(err) log.Fatal(err)
} }
resp, err := client.Login(context.Background(), &mautrix.ReqLogin{ resp, err := client.Login(context.Background(), &mautrix.ReqLogin{
Type: "m.login.password", Type: "m.login.password",
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username}, Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: configuration.matrix.username},
Password: configuration.matrix.password, Password: configuration.matrix.password,
StoreCredentials: true, StoreCredentials: true,
}) })
if err != nil { if err != nil {
@ -81,14 +84,16 @@ func initMatrix() {
}) })
syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) { syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
if evt.RoomID.String() == configuration.matrix.room_id { if evt.RoomID.String() == configuration.matrix.room_id {
prefixes := []string{"!", configuration.matrix.username+" "} prefixes := []string{"!", configuration.matrix.username + " "}
for _,prefix := range prefixes { for _, prefix := range prefixes {
if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) { if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
msg := evt.Content.AsMessage().Body
cmd := strings.TrimPrefix(strings.Split(msg, " ")[0], prefix)
found_command := false found_command := false
for _, command := range Chatcommands { for _, command := range Chatcommands {
if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, prefix) { if command.cmd == cmd {
found_command = true found_command = true
out := command.callback(ctx, evt) out := command.callback(ctx, evt, strings.Split(strings.TrimPrefix(msg, prefix+cmd), " "))
content := format.RenderMarkdown(out, true, false) content := format.RenderMarkdown(out, true, false)
content.SetReply(evt) content.SetReply(evt)
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content) client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
@ -103,30 +108,30 @@ func initMatrix() {
} }
}) })
registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event) string { registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event, args []string) string {
text := "" text := ""
for _,cmd := range Chatcommands { for _, cmd := range Chatcommands {
text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description) text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description)
} }
return text return text
}) })
registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event) string { registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event, args []string) string {
text := "⚪️ Getting sites...\n" text := "⚪️ Getting sites...\n"
sites, err := getAllSites() sites, err := getAllSites()
if err.code != 0 { if err.code != 0 {
return text+"🔴 Failed to get sites!" return text + "🔴 Failed to get sites!"
} }
for _, site := range sites { for _, site := range sites {
text = fmt.Sprintf("%s- %s\n", text, site.name) text = fmt.Sprintf("%s- %s\n", text, site.name)
} }
if len(sites) == 0 { if len(sites) == 0 {
text = text+"*No sites found*" text = text + "*No sites found*"
} }
return text return text
}) })
go func () { go func() {
for range time.Tick(time.Second * 6) { for range time.Tick(time.Second * 6) {
log.Printf("Scanning for queued messages...") log.Printf("Scanning for queued messages...")
for _, msg := range MatrixOut { for _, msg := range MatrixOut {