49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package matrixbotlib
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"maunium.net/go/mautrix"
|
|
"maunium.net/go/mautrix/event"
|
|
)
|
|
|
|
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")
|
|
)
|
|
|
|
//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{}, ErrCmdParseNoPrefix
|
|
|
|
}
|
|
if len(bodyS) < 2 {
|
|
return []string{}, ErrCmdParseNoCmd
|
|
}
|
|
|
|
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 {
|
|
log.Printf("error joining room %v\n", evt.RoomID)
|
|
} else {
|
|
log.Printf("joined room %v\n", evt.RoomID)
|
|
}
|
|
}
|
|
})
|
|
}
|