From 4ecd4b06b815f60ddedea67470772f965a2e2b41 Mon Sep 17 00:00:00 2001 From: Noah Date: Wed, 10 Apr 2024 15:59:41 -0500 Subject: [PATCH] Move matrix chat commands to their own file (fix #36) --- matrix.go | 92 +------------------------------------- matrix_commands.go | 108 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 91 deletions(-) create mode 100644 matrix_commands.go diff --git a/matrix.go b/matrix.go index 299da37..69a2e37 100644 --- a/matrix.go +++ b/matrix.go @@ -3,7 +3,6 @@ package main import ( "context" - "fmt" "log" "strings" "time" @@ -32,23 +31,11 @@ type MatrixCommand struct { // Output queue var MatrixOut []MatrixMessage -// ChatCommands -var Chatcommands []MatrixCommand - // sendMessage sends a message through Matrix func sendMessage(msg MatrixMessage) { MatrixOut = append(MatrixOut, msg) } -// registerChatCommand -func registerChatCommand(cmd string, description string, callback MatrixCommandCallback) { - Chatcommands = append(Chatcommands, MatrixCommand{ - cmd: cmd, - callback: callback, - description: description, - }) -} - // initMatrix starts a Matrix bot. func initMatrix() { client, err := mautrix.NewClient(configuration.matrix.homeserver, "", "") @@ -108,84 +95,7 @@ func initMatrix() { } }) - registerChatCommand("help", "Show the help dialouge", func(ctx context.Context, evt *event.Event, args []string) string { - text := "" - for _, cmd := range Chatcommands { - text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description) - } - return text - }) - - registerChatCommand("sites", "Display all available sites", func(ctx context.Context, evt *event.Event, args []string) 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.getName()) - } - if len(sites) == 0 { - text = text + "*No sites found*" - } - return text - }) - - registerChatCommand("restart", "Restart a site.", func(ctx context.Context, evt *event.Event, args []string) string { - if len(args) < 2 { - return "šŸ”“ Invalid site." - } - site, found, err := getSite(args[1]) - if err != nil { - return "šŸ”“ Failed to get site " + args[2] + "!" - } else if !found { - return "šŸ”“ Invalid site." - } - text := "āšŖļø Restarting server..." - derr := site.Restart() - if derr.code != 0 { - derr.SendOverMatrix() - return text + "\nšŸ”“ Failed to restart site!" - } - return text + "\nšŸŸ¢ Successfully restarted site." - }) - - registerChatCommand("start", "Start a site.", func(ctx context.Context, evt *event.Event, args []string) string { - if len(args) < 2 { - return "šŸ”“ Invalid site." - } - site, found, err := getSite(args[1]) - if err != nil { - return "šŸ”“ Failed to get site " + args[2] + "!" - } else if !found { - return "šŸ”“ Invalid site." - } - - text := "āšŖļø Starting server..." - if derr := site.Start(); derr.code != 0 { - derr.SendOverMatrix() - return text + "\nšŸ”“ Failed to stop site!" - } - return text + "\nšŸŸ¢ Successfully started site." - }) - - registerChatCommand("stop", "Stop a site.", func(ctx context.Context, evt *event.Event, args []string) string { - if len(args) < 2 { - return "šŸ”“ Invalid site." - } - site, found, err := getSite(args[1]) - if err != nil { - return "šŸ”“ Failed to get site " + args[2] + "!" - } else if !found { - return "šŸ”“ Invalid site." - } - text := "āšŖļø Stopping server..." - if derr := site.Stop(); derr.code != 0 { - derr.SendOverMatrix() - return text + "\nšŸ”“ Failed to stop site!" - } - return text + "\nšŸŸ¢ Successfully stopped site." - }) + registerChatCommands() // Register all the chat commands defined inside matrix_commands.go go func() { for range time.Tick(time.Second * 2) { diff --git a/matrix_commands.go b/matrix_commands.go new file mode 100644 index 0000000..2ca2d87 --- /dev/null +++ b/matrix_commands.go @@ -0,0 +1,108 @@ +package main + +import ( + "context" + "fmt" + "maunium.net/go/mautrix/event" +) + +var Chatcommands = []MatrixCommand{} + +func registerChatCommands() { + Chatcommands = []MatrixCommand{ + { + cmd: "help", + description: "Show the help dialouge", + callback: func(ctx context.Context, evt *event.Event, args []string) string { + text := "" + for _, cmd := range Chatcommands { + text = fmt.Sprintf("%s- %s\n %s\n", text, cmd.cmd, cmd.description) + } + return text + }, + }, + { + cmd: "sites", + description: "Display all available sites", + callback: func(ctx context.Context, evt *event.Event, args []string) 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.getName()) + } + if len(sites) == 0 { + text = text + "*No sites found*" + } + return text + }, + }, + { + cmd: "restart", + description: "Restart a site.", + callback: func(ctx context.Context, evt *event.Event, args []string) string { + if len(args) < 2 { + return "šŸ”“ Invalid site." + } + site, found, err := getSite(args[1]) + if err != nil { + return "šŸ”“ Failed to get site " + args[2] + "!" + } else if !found { + return "šŸ”“ Invalid site." + } + text := "āšŖļø Restarting server..." + derr := site.Restart() + if derr.code != 0 { + derr.SendOverMatrix() + return text + "\nšŸ”“ Failed to restart site!" + } + return text + "\nšŸŸ¢ Successfully restarted site." + }, + }, + { + cmd: "start", + description: "Start a site.", + callback: func(ctx context.Context, evt *event.Event, args []string) string { + if len(args) < 2 { + return "šŸ”“ Invalid site." + } + site, found, err := getSite(args[1]) + if err != nil { + return "šŸ”“ Failed to get site " + args[2] + "!" + } else if !found { + return "šŸ”“ Invalid site." + } + + text := "āšŖļø Starting server..." + if derr := site.Start(); derr.code != 0 { + derr.SendOverMatrix() + return text + "\nšŸ”“ Failed to stop site!" + } + return text + "\nšŸŸ¢ Successfully started site." + }, + }, + { + cmd: "stop", + description: "Stop a site.", + callback: func(ctx context.Context, evt *event.Event, args []string) string { + if len(args) < 2 { + return "šŸ”“ Invalid site." + } + site, found, err := getSite(args[1]) + if err != nil { + return "šŸ”“ Failed to get site " + args[2] + "!" + } else if !found { + return "šŸ”“ Invalid site." + } + text := "āšŖļø Stopping server..." + if derr := site.Stop(); derr.code != 0 { + derr.SendOverMatrix() + return text + "\nšŸ”“ Failed to stop site!" + } + return text + "\nšŸŸ¢ Successfully stopped site." + }, + }, + } +}