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) { syncer.OnEventType(event.EventMessage, func(ctx context.Context, evt *event.Event) {
if evt.RoomID.String() == configuration.matrix.room_id { if evt.RoomID.String() == configuration.matrix.room_id {
for _, command := range Chatcommands { prefixes := []string{"!", configuration.matrix.username+" "}
if command.cmd == strings.TrimPrefix(evt.Content.AsMessage().Body, "!") { for _,prefix := range prefixes {
out := command.callback(ctx, evt) if strings.HasPrefix(evt.Content.AsMessage().Body, prefix) {
content := format.RenderMarkdown(out, true, false) found_command := false
content.SetReply(evt) for _, command := range Chatcommands {
client.SendMessageEvent(ctx, evt.RoomID, event.EventMessage, content) 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 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 () { go func () {
for range time.Tick(time.Second * 3) { for range time.Tick(time.Second * 3) {
log.Printf("Scanning for queued messages...") log.Printf("Scanning for queued messages...")