From 4aa1e5075e4d93100abaed29596d50c39507ccb6 Mon Sep 17 00:00:00 2001 From: Adam Dodman Date: Sun, 3 Dec 2017 21:53:30 +0000 Subject: [PATCH] Add guest check, small refactor --- helpers.go | 26 ++++++++++++++++++-------- lockcheck.go | 35 +++++++++++++++++++++++++++++------ main.go | 22 ++++++++++++++++++---- 3 files changed, 65 insertions(+), 18 deletions(-) diff --git a/helpers.go b/helpers.go index 0118eec..1d42bfc 100644 --- a/helpers.go +++ b/helpers.go @@ -42,7 +42,7 @@ func ClientInfo(clid string) ts3.Command { return ts3.Command{ Command: "clientinfo", Params: map[string][]string{ - "clid": []string{"5"}, + "clid": []string{clid}, }, } } @@ -57,25 +57,35 @@ func ServerGroupDelClient(sgid string, cldbid string) ts3.Command { } } -func SendMessageChannel(isError bool, message string) ts3.Command { - if isError { - message = "'[B][COLOR=#ff0000]" + message + "[/B][/COLOR]" +func ServerGroupAddClient(sgid string, cldbid string) ts3.Command { + return ts3.Command{ + Command: "servergroupaddclient", + Params: map[string][]string{ + "sgid": []string{sgid}, + "cldbid": []string{cldbid}, + }, } +} + +func SendMessageChannel(isError bool, message string) ts3.Command { + //if isError { + // message = "'[B][COLOR=#ff0000]" + message + "[/COLOR][/B]" + //} return ts3.Command{ Command: "sendtextmessage", Params: map[string][]string{ "targetmode": []string{"2"}, "target": []string{"1"}, - "msg": []string{message}, + "msg": []string{"Cockwomble"}, }, } } func SendMessageUser(isError bool, message string, clid string) ts3.Command { - if isError { - message = "'[B][COLOR=#ff0000]" + message + "[/B][/COLOR]" - } + //if isError { + // message = "'[B][COLOR=#ff0000]" + message + "[/COLOR][/B]" + //} return ts3.Command{ Command: "sendtextmessage", diff --git a/lockcheck.go b/lockcheck.go index 13a2c98..2350052 100644 --- a/lockcheck.go +++ b/lockcheck.go @@ -6,7 +6,8 @@ import ( "strings" ) -func doLock(client *ts3.Client, clid string) { +func doLockandGuest(client *ts3.Client, clid string) { + var groupAssigned bool data, err := client.Exec(ClientInfo(clid)) if err != nil { log.Fatal(err) @@ -14,16 +15,38 @@ func doLock(client *ts3.Client, clid string) { cldbid := data.Params[0]["client_database_id"] groups := strings.Split(data.Params[0]["client_servergroups"], ",") + nickname := data.Params[0]["client_nickname"] + for i := range groups { if groups[i] == "9" { - _, err := client.Exec(ServerGroupDelClient("9",cldbid)) - if err != nil { - log.Fatal(err) - } - _, err = client.Exec(SendMessageUser(false, "Removed Lock group from you!", clid)) + _, err := client.Exec(ServerGroupDelClient("9", cldbid)) if err != nil { log.Fatal(err) } + + log.Println("Removed lock group from client " + nickname) + + //_, err = client.Exec(SendMessageChannel(false, "Removed Lock group from you!")) + //if err != nil { + // log.Println(err) + //} + + } + if groups[i] == "8" { + groupAssigned = true } } + + log.Println(groupAssigned) + + if !groupAssigned { + // Guest group missing, needs assigning. + _, err := client.Exec(ServerGroupAddClient("8", cldbid)) + if err != nil { + log.Fatal(err) + } + + log.Println("Gave guest group to client " + nickname) + } + } diff --git a/main.go b/main.go index dc26e06..d24a2e9 100644 --- a/main.go +++ b/main.go @@ -3,16 +3,17 @@ package main import ( "github.com/darfk/ts3" "log" + "time" ) // Username and password in Constants.go, not on github :D func processNotify(client *ts3.Client, notification chan ts3.Notification) { for i := range notification { - //log.Println(i) - if i.Type == "notifyclientmoved" { - // We need to check for lock group - doLock(client, i.Params[0]["clid"]) + switch i.Type { + case "notifyclientmoved", "notifycliententerview": + // We need to check for lock group on every move, and guest check is best done at the same time. + doLockandGuest(client, i.Params[0]["clid"]) } } } @@ -46,6 +47,18 @@ func main() { log.Fatal(err) } + // Start listening to text chat + _, err = client.Exec(ts3.Command{ + Command: "servernotifyregister", + Params: map[string][]string{ + "event": []string{"textchannel"}, + "id": []string{"1"}, + }, + }) + if err != nil { + log.Fatal(err) + } + notification := make(chan ts3.Notification) go processNotify(client, notification) @@ -55,5 +68,6 @@ func main() { }) for { + time.Sleep(500 * time.Millisecond) } }