Add \'servers\' matrix command (fix #10)

This commit is contained in:
Noah 2024-03-10 22:40:45 -05:00
parent b07e9a78d8
commit 714380477e

View File

@ -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...")