matrixbotlib/util.go

40 lines
1.0 KiB
Go
Raw Normal View History

2022-07-17 17:22:28 -04:00
package matrixbotlib
import (
"errors"
"fmt"
"strings"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
//ParseCommand takes a Event and pulls a command + args if available
func ParseCommand(evt *event.Event, prefix string) ([]string, error) {
body := evt.Content.AsMessage().Body
bodyS := strings.Split(body, " ")
if bodyS[0] != fmt.Sprintf("!%v", prefix) {
return []string{}, errors.New("no prefix found")
}
if len(bodyS) < 2 {
return []string{}, errors.New("nothing to parse from command")
}
return bodyS[1:], nil
}
//AcceptAllRoomInvites adds a handler to join all rooms invited to
func AcceptAllRoomInvites(client *mautrix.Client) {
syncer := client.Syncer.(*mautrix.DefaultSyncer)
syncer.OnEventType(event.StateMember, func(source mautrix.EventSource, evt *event.Event) {
if evt.Content.AsMember().Membership.IsInviteOrJoin() {
_, err := client.JoinRoomByID(evt.RoomID)
if err != nil {
fmt.Printf("error joining room %v", evt.RoomID)
} else {
fmt.Printf("joined room %v", evt.RoomID)
}
}
})
}