I am trying to connect to a TeamSpeak server using QueryServer to make a bot. I got advice from this thread , however I still need help.
This is the TeamSpeak API that I use.
Before editing, this was a summary of what actually happened in my script (1 connection):
- He is connecting.
- It checks the channel identifier (and the client’s own identifier)
- He joins the channel and begins to read everything
- If someone speaks about a specific command, he executes the command and then disconnects.
How can I make it not disconnect? How can I make the script stand by so that it continues to read after the command is executed?
I am using Python 3.4.1 .
I tried to learn Threading, but either I'm dumb or it doesn't work the way I thought. There is another “error” when you expect events, if I do not start anything with the command, it turns off after 60 seconds.
#Librerias import ts3 import threading import datetime from random import choice, sample # Data needed # USER = "thisisafakename" PASS = "something" HOST = "111.111.111.111" PORT = 10011 SID = 1 class BotPrincipal: def __init__(self, manejador=False): self.ts3conn = ts3.query.TS3Connection(HOST, PORT) self.ts3conn.login(client_login_name=USER, client_login_password=PASS) self.ts3conn.use(sid=SID) channelToJoin = Bot.GettingChannelID("TestingBot") try: #Login with a client that is ok self.ts3conn.clientupdate(client_nickname="The Reader Bot") self.MyData = self.GettingMyData() self.MoveUserToChannel(ChannelToJoin, Bot.MyData["client_id"]) self.suscribirEvento("textchannel", ChannelToJoin) self.ts3conn.on_event = self.manejadorDeEventos self.ts3conn.recv_in_thread() except ts3.query.TS3QueryError: #Name already exists, 2nd client connect with this info self.ts3conn.clientupdate(client_nickname="The Writer Bot") self.MyData = self.GettingMyData() self.MoveUserToChannel(ChannelToJoin, Bot.MyData["client_id"]) def __del__(self): self.ts3conn.close() def GettingMyData(self): respuesta = self.ts3conn.whoami() return respuesta.parsed[0] def GettingChannelID(self, nombre): respuesta = self.ts3conn.channelfind(pattern=ts3.escape.TS3Escape.unescape(nombre)) return respuesta.parsed[0]["cid"] def MoveUserToChannel(self, idCanal, idUsuario, passCanal=None): self.ts3conn.clientmove(cid=idCanal, clid=idUsuario, cpw=passCanal) def suscribirEvento(self, tipoEvento, idCanal): self.ts3conn.servernotifyregister(event=tipoEvento, id_=idCanal) def SendTextToChannel(self, idCanal, mensajito="Error"): self.ts3conn.sendtextmessage(targetmode=2, target=idCanal, msg=mensajito) #This works print("test") #PROBLEM HERE This doesn't work. Why? the line above did work def manejadorDeEventos(sender, event): message = event.parsed[0]['msg'] if "test" in message: #This works Bot.SendTextToChannel(ChannelToJoin, "This is a test") #This works if __name__ == "__main__": Bot = BotPrincipal() threadprincipal = threading.Thread(target=Bot.__init__) threadprincipal.start()
Before using 2 bots, I tested running SendTextToChannel when it connects and it works fine, which allows me to do whatever I want after it sends the text to the channel. The error that causes all python code to stop occurs only if it is called by the manejadorDeEventos function
Edit 1 - experiment with threads.
I confused this with a lot of time with threads, getting the result when 2 clients connect at the same time. Somehow I think that 1 of them reads events, and the other answers. the script no longer closes and that wins, but having a clone connection doesn't look very good.
Edit 2 - Updated code and actual status of the problem.
I managed to make the double connection more or less “perfect”, but it disconnects if nothing happens in the room for 60 seconds. Tried using Threading.timer, but I can't get it to work. All question code has been updated for him.
I need an answer that helps me do reading from the channel and answer it without the need to connect a second bot to it (for example, actually ...) And I would give extra points if the answer also helps me understand a simple way to do a request to the server every 50 seconds so that it does not disconnect.