Move matrix chat commands to their own file (fix #36)

This commit is contained in:
Noah 2024-04-10 15:59:41 -05:00
parent 8875a40fea
commit 4ecd4b06b8
2 changed files with 109 additions and 91 deletions

View File

@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"fmt"
"log" "log"
"strings" "strings"
"time" "time"
@ -32,23 +31,11 @@ type MatrixCommand struct {
// Output queue // Output queue
var MatrixOut []MatrixMessage var MatrixOut []MatrixMessage
// ChatCommands
var Chatcommands []MatrixCommand
// sendMessage sends a message through Matrix // sendMessage sends a message through Matrix
func sendMessage(msg MatrixMessage) { func sendMessage(msg MatrixMessage) {
MatrixOut = append(MatrixOut, msg) 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. // initMatrix starts a Matrix bot.
func initMatrix() { func initMatrix() {
client, err := mautrix.NewClient(configuration.matrix.homeserver, "", "") 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 { registerChatCommands() // Register all the chat commands defined inside matrix_commands.go
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."
})
go func() { go func() {
for range time.Tick(time.Second * 2) { for range time.Tick(time.Second * 2) {

108
matrix_commands.go Normal file
View File

@ -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."
},
},
}
}