From 714380477ee5bdca0fd9386dc46bd44418bcb812 Mon Sep 17 00:00:00 2001
From: Noah <noah.matthew@metzger.cc>
Date: Sun, 10 Mar 2024 22:40:45 -0500
Subject: [PATCH] Add \'servers\' matrix command (fix #10)

---
 matrix.go | 35 +++++++++++++++++++++++++++++------
 1 file changed, 29 insertions(+), 6 deletions(-)

diff --git a/matrix.go b/matrix.go
index bdefff1..5b71911 100644
--- a/matrix.go
+++ b/matrix.go
@@ -80,12 +80,23 @@ func initMatrix() {
 	})	
 	syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
 		if evt.RoomID.String() == configuration.matrix.room_id {
-			for _, command := range Chatcommands {
-				if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, "!") {
-					out := command.callback(ctx, evt)
-					content := format.RenderMarkdown(out, true, false)
-					content.SetReply(evt)
-					client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
+			prefixes := []string{"!", configuration.matrix.username+" "}
+			for _,prefix := range prefixes {
+				if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
+					found_command := false
+					for _, command := range Chatcommands {
+						if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, prefix) {
+							found_command = true
+							out := command.callback(ctx, evt)
+							content := format.RenderMarkdown(out, true, false)
+							content.SetReply(evt)
+							client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content)
+						}
+					}
+					if !found_command {
+						client.SendText(ctx, evt.RoomID, "🔴 Command not found")
+					}
+					break
 				}
 			}
 		}
@@ -99,6 +110,18 @@ func initMatrix() {
 		return text
 	})
 
+	registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event) string {
+		text := "⚪️ Getting sites...\n"
+		sites, err := getAllSites()
+		if err.code != 0 {
+			return text+"🔴 Failed to get sites!"
+		}
+		for _, site := range sites {
+			text = fmt.Sprintf("%s- %s\n", text, site)
+		}
+		return text
+	})
+
 	go func () {
 		for range time.Tick(time.Second * 3) {
 			log.Printf("Scanning for queued messages...")