benediktschmitt/py-ts3

servernotifyregister conflict

Closed this issue · 13 comments

im trying to use

ts3conn.exec_("servernotifyregister", event="server")

and

privateMessage = ts3conn.exec_("servernotifyregister", event="textprivate")

on the same script, but im getting a conflict on the script.. script goes as follow:

def hello_bot(ts3conn):
    while True:
        ts3conn.send_keepalive()
        
        def greetings(msg=None):
            if msg is None:
                msg = "Welcome To Red Ribbon Army Teamspeak."

            # Register for the event.
            ts3conn.exec_("servernotifyregister", event="server")

            playersOnline = driver.find_element_by_id("content")
            try:
                ts3conn.exec_("channeledit", cid=16, channel_description=playersOnline.text)
            except:
                pass

            try:
                event = ts3conn.wait_for_event(timeout=60)
                resp = ts3conn.exec_("clientlist")
                print("Clients on server:",resp.parsed)
            except ts3.query.TS3TimeoutError:
                pass
            else:
                # Greet new clients.
                if event[0]["reasonid"] == "0":
                    print("Client '{}' connected.".format(event[0]["client_nickname"]))
                    ts3conn.exec_("sendtextmessage",targetmode=1, target=event[0]["clid"], msg=msg)
        greetings()
        
        def masspoke(msg):
            for client in ts3conn.exec_("clientlist"):
                ts3conn.exec_("clientpoke", clid=client["clid"], msg=msg)
        #mensagem = "aaa"
        #masspoke(mensagem)
        def checkPvtMsg():
            privateMessage = ts3conn.exec_("servernotifyregister", event="textprivate")
            print("private message: ", privateMessage.parsed)     
        checkPvtMsg()

    return None

with ts3.query.TS3ServerConnection("telnet:/:10101") as ts3conn:
    ts3conn.exec_("use", sid=1)
    ts3conn.exec_("clientupdate", client_nickname="RRABot")
    hello_bot(ts3conn)

and the error i get is this

Traceback (most recent call last):
  File "c:\Users\juanz\Documents\Python Scripts\tsbot.py", line 86, in <module>
    hello_bot(ts3conn)
  File "c:\Users\juanz\Documents\Python Scripts\tsbot.py", line 68, in hello_bot
    greetings()
  File "c:\Users\juanz\Documents\Python Scripts\tsbot.py", line 64, in greetings
    if event[0]["reasonid"] == "0":
KeyError: 'reasonid'

does anyone know how i can solve this conflict problem? thanks in advance

You might want to edit that message, looks like you have your serveradmin username and password in here.
If you're wondering why I'm here again, yes, I'm subscribed to the issues.

To your issue though:
Not all events you get from servernotifyregister have a reasonid.
While this resource is in german, you should still be able to roughly work out which events have a reasonid:
http://yat.qa/ressourcen/server-query-notify

Using that resource we can see notifyserveredited has a reasonid of 10, but notifytextmessage does not have a reasonid.
You may be recieving a notifytextmessage and trying to get the reasonid.

Thank you, but what im trying to do is not call the textprivate in that part of the code, it was supposed to call the server event instead, but idk why the textprivate is overwriting it even tho is in another function, what exactly am i doing wrong?

You are never unsubscribing from textprivate.
When you use servernotifyregister you subscribe to a certain event, but you will keep receiving that event until you unsubscribe (servernotifyunregister)

thank you so much! now it works, do you know how do i print private messages received on the bot?

privateMessage = self.ts3conn.exec_("servernotifyregister", event="textprivate")
print("private message: ", privateMessage.parsed)

gives me [], like no message was received

well, exec_() doesn't actually return the event.
Once you subscribe to events, you will receive them until you either unsubscribe or disconnect.
Basically, you handle events like this:

self.ts3conn.exec_("servernotifyregister", event="textprivate")   # Subscribe to the event
try:
    event = self.ts3conn.wait_for_event(timeout=240)    # Wait for 240 seconds (4 minutes) for an event
    print(event["msg"])    # Or do whatever you want with the event
except ts3.query.TS3TimeoutError:    # Not sure how the exception is actually called
    # Because we waited 4 minutes for the event, we didn't communicate with the server.
    # Usually we time out after 5 or 10 minutes, so we need to send a keepalive
    self.ts3conn.send_keepalive()
self.ts3conn.exec_("servernotifyunregister", event="textprivate") # Unsubscribe if you really want to

You could subscribe to multiple events and access the event.parsed attribute, I think you can get the event name (like notifytextmessage) this way, so no reasonid is needed.
Or, if you know one message has a reasonid and one does not, you could check if the reasonid is there and detemine the message that way. Then you'll have to be careful once you subscribe to more events though.

EDIT:
There is an example in this repo:

while True:

thank you so much! now i get how it works!

well im getting this error with the code you sent hahahaha

TypeError: list indices must be integers or slices, not str

Oh right, because of the parser you always get the result in a list.
Just do event[0]["msg"] - always add that [0] if you get a single result

i did that but now it wont let me send private messages to serverQuery, im checking YaTQA now to see if there is anything i can change..
error

<17:53:11> insufficient client permissions (failed on i_client_private_textmessage_power)

and this error crashed the bot

print("event is: ".format(event[0]["msg"]))    # Or do whatever you want with the event
KeyError: 'msg'

changed msg to reasonmsg, now trying to get the output xD but still cant send msg to private msg with query

it was bad permission, but now im also getting same error for reasonmsg

print("event is: ".format(event[0]["reasonmsg"]))    # Or do whatever you want with the event
KeyError: 'reasonmsg'

Note again, reasonmsg does not exist on some events, and msg does not exist on some events.
Not all events have the same parameters, yat.qa shows the parameter for the events

Thank you! I will be doing a little more digging