toqueteos / ts3

TeamSpeak 3 Server Query Library in Go.
https://pkg.go.dev/github.com/toqueteos/ts3
MIT License
13 stars 2 forks source link

Register on Servernotifications does not work (like in example) #7

Open sushi86 opened 7 years ago

sushi86 commented 7 years ago

First of all thank you for this nice API, good work =)

I'm not that experienced in golang yet, for me as a learning project I started to work on a ts3 bot with your API.

I will try to see each connect and disconnect for every client on my server and then notify another programm.

Unfortunately I was not able to register. Your example doesn't work. Following Line is broken: ch := conn.Chans()

Maybe you can update your examples?

Would be really cool =)

sushi86 commented 7 years ago

Ok maybe I was to early with my issue. I figured it out bei myself somehow...

Here is my code:


package main

import (
    "fmt"
    "time"

    "github.com/toqueteos/ts3"
    "log"
)

func main() {
    conn, err := ts3.Dial(":10011", false)
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()

    bot(conn)
}

// bot is a simple bot that checks version, signs in and sends a text message to
// channel#1 then exits.
func bot(conn *ts3.Conn) {
    defer conn.Cmd("quit")

    conn.NotifyFunc(func (e string, s string) {
        fmt.Println(e + " " + s)
    })

    var cmds = []string{
        // Show version
        "version",
        // Login
        "login serveradmin 123456",
        // Choose virtual server
        "use 1",
        // Update nickname
        `clientupdate client_nickname=Sushi\sBot`,
        // "clientlist",
        // Send message to channel with id=1
        `sendtextmessage targetmode=2 target=1 msg=Big\sBrother\sis\snow\swatching\syou`,
        // Register to notify
        "servernotifyregister event=server",
    }

    for _, s := range cmds {
        // Send command and wait for its response
        r,_ := conn.Cmd(s)
        // Display as:
        //     > request
        //     response
        fmt.Printf("> %s\n%s", s, r)
        // Wait a bit after each command so we don't get banned. By default you
        // can issue 10 commands within 3 seconds.  More info on the
        // WHITELISTING AND BLACKLISTING section of TS3 ServerQuery Manual
        // (http://goo.gl/OpJXz).
        time.Sleep(350 * time.Millisecond)
    }

    for { select {} }
}

The important part is of course to register to the server notify: "servernotifyregister event=server", and register the notifyFunc: conn.NotifyFunc(func (e string, s string) { fmt.Println(e + " " + s) }) At least I made the program run forever: for { select {} }

Of course there are more elegant ways, but I'm a beginner ;)

Maybe you can suggest a more professional way within your examples