matrixbotlib/util.go

49 lines
1.2 KiB
Go
Raw Normal View History

2022-07-17 17:22:28 -04:00
package matrixbotlib
import (
"errors"
"fmt"
2022-07-17 17:38:17 -04:00
"log"
2022-07-17 17:22:28 -04:00
"strings"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/event"
)
2022-07-17 17:38:17 -04:00
var (
//ErrCmdParseNoPrefix means no prefix was found
ErrCmdParseNoPrefix = errors.New("no prefix found")
//ErrCmdParseNoCmd means no actual commands were found
ErrCmdParseNoCmd = errors.New("nothing to parse from command")
)
2022-07-17 17:22:28 -04:00
//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) {
2022-07-17 18:22:57 -04:00
return []string{}, ErrCmdParseNoPrefix
2022-07-17 17:22:28 -04:00
}
if len(bodyS) < 2 {
2022-07-17 18:22:57 -04:00
return []string{}, ErrCmdParseNoCmd
2022-07-17 17:22:28 -04:00
}
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 {
2022-07-17 17:38:17 -04:00
log.Printf("error joining room %v\n", evt.RoomID)
2022-07-17 17:22:28 -04:00
} else {
2022-07-17 17:38:17 -04:00
log.Printf("joined room %v\n", evt.RoomID)
2022-07-17 17:22:28 -04:00
}
}
})
}