Murgeye/teamspeak3-python-api

msg only to first Client

Closed this issue · 3 comments

Hello, I have a Question. If I connect multiple Clients on the Server my Welcome Message, when the Bot has started does not work.
Only the First Client Joined the Server gets the Welcome Message. The Other ones don't.

def SendMessageToClients(clients, message):
        for client in clients:
                try:
                        if client["client_type"]!="1":
                                ts3conn.sendtextmessage(definitions.TextMessageTargetMode.CLIENT, client["clid"], message)
                                print(client)
                except Exception as e:
                        print(e)

SendMessageToClients(bot_api.ts3conn.clientlist(), "Bot is now Online")

The Second Client Connected Gets a Invalid Client Type Error in the Server Console in the Teamspeak Program, the other ones don't.
g

Also if I try to send two Messages to one Client like:


  ts3conn.sendtextmessage(definitions.TextMessageTargetMode.CLIENT, client["clid"], message)
  ts3conn.sendtextmessage(definitions.TextMessageTargetMode.CLIENT, client["clid"], message)

Then the First Message connects and the other causes the same Error: Invalid Client Type

I already tried time.sleep after each message, but it didn't work. In the Python Console, there are no Errors.

thank you for your help

Hey,

I have tried to integrate your code into the example given in the README and it seems to work for me ... Could you check this snippet and tell me if this works for you? Please also post the output from the bot for the print(client) statement I have move to the top? For me, both connected clients get two messages as soon as the bot connects. By the way, the invalid client type message is from your teamspeak client, not the bot. Could it be that you (maybe accidentally by pressing some hotkey) tried to perform an invalid action on the bot (e.g., showing the connection info?).

from ts3API.TS3Connection import TS3Connection
import ts3API.Events as Events

HOST = "localhost"
PORT = 10011 # Default Port
USER = 'serveradmin' # Default login
PASS = '<password>'
SID = 1 # Virtual server id

def on_event(sender, **kw):
    """
    Event handling method
    """
    # Get the parsed event from the dictionary
    event = kw["event"]
    print(type(event))
    """
    # This generates output for every event. Remove the comment if you want more output
    for attr, value in event.__dict__.items():
        print("\t"+attr+":", value)
    """
    if isinstance(event, Events.ClientBannedEvent):
        print("Client was banned!")
        print("\tClient ID:", event.client_id)
        print("\tReason Message:", event.reason_msg)
        print("\tInvokerID:", event.invoker_id)
        print("\tInvokerName:", event.invoker_name)
        print("\tBantime:", event.ban_time)
    if isinstance(event, Events.ClientKickedEvent):
        print("Client was kicked!")
        print("\tClient ID:", event.client_id)
        print("\tReason Message:", event.reason_msg)
        print("\tInvokerID:", event.invoker_id)
        print("\tInvokerName:", event.invoker_name)

    if isinstance(event, Events.ClientLeftEvent):
        print("Client left!")
        print("\tClient ID:", event.client_id)
        print("\tReason Message:", event.reason_msg)
    if type(event) is Events.TextMessageEvent:
        # Prevent the client from sending messages to itself
        if event.invoker_id != int(ts3conn.whoami()["client_id"]):
            ts3conn.sendtextmessage(targetmode=1, target=event.invoker_id, msg="I received your message!")

def SendMessageToClients(clients, message):
        for client in clients:
                print(client)
                try:
                        if client["client_type"]!="1":
                                print("Trying to message", client)
                                ts3conn.sendtextmessage(1, client["clid"], message)
                                ts3conn.sendtextmessage(1, client["clid"], message)
                except Exception as e:
                        print(e)

# Connect to the Query Port
ts3conn = TS3Connection(HOST, PORT)
# Login with query credentials
ts3conn.login(USER, PASS)
# Choose a virtual server
ts3conn.use(sid=SID)
# Register for server wide events
ts3conn.register_for_server_events(on_event) 
# Register for private messages
ts3conn.register_for_private_messages(on_event)
# Test Bot Online message
SendMessageToClients(ts3conn.clientlist(), "Bot is now Online") 
# Start the loop to send connection keepalive messages
ts3conn.start_keepalive_loop()

Just to be sure it's not that flooding protection: You added the bot's IP addresse to the query whitelist?

Hello, Thank you for your fast answer. I figured out, that my Spam Blocker was Blocking the Message on the Second Client. So it wasn't your fault. Thank you for your help, I will now close the Issue.

Ok, thanks for reporting back! Feel free to open an issue if you need any help.