creates management room now
This commit is contained in:
parent
7a8ee6dd94
commit
9f558ac277
@ -1,8 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
@ -12,13 +12,15 @@ type Config struct {
|
|||||||
Userid string
|
Userid string
|
||||||
Server string
|
Server string
|
||||||
Token string
|
Token string
|
||||||
|
Name string
|
||||||
Owner string
|
Owner string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Bot struct {
|
type Bot struct {
|
||||||
Client *mautrix.Client
|
Client *mautrix.Client
|
||||||
Rooms map[string]int
|
Rooms map[string]int
|
||||||
Conf *Config
|
Conf *Config
|
||||||
|
ManagementRoomID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -26,6 +28,7 @@ func main() {
|
|||||||
Userid: "@testbot:saintnet.tech",
|
Userid: "@testbot:saintnet.tech",
|
||||||
Server: "matrix.saintnet.tech",
|
Server: "matrix.saintnet.tech",
|
||||||
Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg",
|
Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg",
|
||||||
|
Name: "TestBot",
|
||||||
Owner: "@stryan:saintnet.tech",
|
Owner: "@stryan:saintnet.tech",
|
||||||
}
|
}
|
||||||
b := NewBot(c)
|
b := NewBot(c)
|
||||||
@ -40,13 +43,12 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
fmt.Println("Syncing enabled")
|
log.Println("Syncing enabled")
|
||||||
resp, err := b.Client.JoinedRooms()
|
resp, err := b.Client.JoinedRooms()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
for _, v := range resp.JoinedRooms {
|
for _, v := range resp.JoinedRooms {
|
||||||
//fmt.Printf("Bot is in Room %v\n", v)
|
|
||||||
|
|
||||||
mem, err := b.Client.JoinedMembers(v)
|
mem, err := b.Client.JoinedMembers(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -54,7 +56,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
if len(mem.Joined) > 1 {
|
if len(mem.Joined) > 1 {
|
||||||
b.Rooms[v] = len(mem.Joined)
|
b.Rooms[v] = len(mem.Joined)
|
||||||
fmt.Printf("Bot is in Room %v\n", v)
|
log.Printf("%v is in Room %v\n", b.Conf.Name, v)
|
||||||
} else {
|
} else {
|
||||||
_, err := b.Client.LeaveRoom(v)
|
_, err := b.Client.LeaveRoom(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -64,14 +66,17 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Bot was in Room %v, left due to being last one there\n", v)
|
log.Printf("%v was in Room %v, left due to being last one there\n", b.Conf.Name, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
fmt.Println("Begining main loop")
|
err = b.createManagementRoom()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("Begining main loop")
|
||||||
for {
|
for {
|
||||||
time.Sleep(2 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
fmt.Println("Checking")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -81,15 +86,43 @@ func NewBot(c *Config) *Bot {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
b := &Bot{Client: client, Rooms: make(map[string]int), Conf: c}
|
b := &Bot{Client: client, Rooms: make(map[string]int), Conf: c, ManagementRoomID: "!fxioaRvcDbKKhCpHrZ:saintnet.tech"}
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) createManagementRoom() error {
|
||||||
|
if b.ManagementRoomID != "" {
|
||||||
|
log.Printf("%v already has management room %v\n", b.Conf.Name, b.ManagementRoomID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
log.Printf("Creating Management Room for %v\n", b.Conf.Name)
|
||||||
|
respCreateRoom, err := b.Client.CreateRoom(&mautrix.ReqCreateRoom{
|
||||||
|
Preset: "trusted_private_chat",
|
||||||
|
Visibility: "private",
|
||||||
|
Name: "TestBot Control Room",
|
||||||
|
Invite: []string{b.Conf.Owner},
|
||||||
|
IsDirect: true,
|
||||||
|
})
|
||||||
|
b.ManagementRoomID = respCreateRoom.RoomID
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
log.Printf("%v created management room %v\n", b.Conf.Name, respCreateRoom.RoomID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (b *Bot) handleMessage(e *mautrix.Event) {
|
func (b *Bot) handleMessage(e *mautrix.Event) {
|
||||||
if e.Sender == b.Conf.Userid {
|
if e.Sender == b.Conf.Userid {
|
||||||
return //we don't care about our own messages
|
return //we don't care about our own messages
|
||||||
}
|
}
|
||||||
body := e.Content.Body
|
body := e.Content.Body
|
||||||
|
if body == "stop" {
|
||||||
|
b.Client.SendText(e.RoomID, "Shutting down...")
|
||||||
|
b.Client.StopSync()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := b.Client.SendText(e.RoomID, body)
|
_, err := b.Client.SendText(e.RoomID, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -100,7 +133,7 @@ func (b *Bot) handleMember(e *mautrix.Event) {
|
|||||||
mbr := e.Content.Membership
|
mbr := e.Content.Membership
|
||||||
if mbr == mautrix.MembershipInvite {
|
if mbr == mautrix.MembershipInvite {
|
||||||
if e.Sender == b.Conf.Owner && e.GetStateKey() == b.Conf.Userid {
|
if e.Sender == b.Conf.Owner && e.GetStateKey() == b.Conf.Userid {
|
||||||
fmt.Println("Joining room:", e.RoomID)
|
log.Printf("%v joining room: %v\n", b.Conf.Name, e.RoomID)
|
||||||
_, err := b.Client.JoinRoom(e.RoomID, "", nil)
|
_, err := b.Client.JoinRoom(e.RoomID, "", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
@ -109,13 +142,13 @@ func (b *Bot) handleMember(e *mautrix.Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if mbr == mautrix.MembershipJoin {
|
if mbr == mautrix.MembershipJoin {
|
||||||
log.Println("A user joined a room we care about")
|
log.Printf("A user joined a room %v cares about\n", b.Conf.Name)
|
||||||
b.Rooms[e.RoomID] = b.Rooms[e.RoomID] + 1
|
b.Rooms[e.RoomID] = b.Rooms[e.RoomID] + 1
|
||||||
}
|
}
|
||||||
if mbr == mautrix.MembershipLeave && e.Sender != b.Conf.Userid {
|
if mbr == mautrix.MembershipLeave && e.Sender != b.Conf.Userid {
|
||||||
b.Rooms[e.RoomID] = b.Rooms[e.RoomID] - 1
|
b.Rooms[e.RoomID] = b.Rooms[e.RoomID] - 1
|
||||||
if b.Rooms[e.RoomID] < 2 {
|
if b.Rooms[e.RoomID] < 2 {
|
||||||
log.Println("Last in room, leaving")
|
log.Printf("%v is last in a room, leaving.\n", b.Conf.Name)
|
||||||
_, err := b.Client.LeaveRoom(e.RoomID)
|
_, err := b.Client.LeaveRoom(e.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
Loading…
Reference in New Issue
Block a user