LapisDeploy/matrix_commands.go

109 lines
2.8 KiB
Go
Raw Normal View History

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