1
0
mirror of https://github.com/Adam-Ant/TeamspeakBot-Go synced 2024-11-05 05:36:23 +00:00

Add guest check, small refactor

This commit is contained in:
Adam Dodman 2017-12-03 21:53:30 +00:00
parent 0bf6f71dad
commit 4aa1e5075e
3 changed files with 65 additions and 18 deletions

View File

@ -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",

View File

@ -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)
}
}

22
main.go
View File

@ -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)
}
}