title: How to keep a python 3 script (Bot) running
body: <p><em>(Not native English, sorry for probably broken English. I'm also a newbie at programming).</em><br>
  Hello, I'm trying to connect to a TeamSpeak server using the QueryServer to make a bot. After days of struggling with it... it works, with only 1 problem, and I'm stuck with that one.</p>
  
  <p>If you need to check, this is the TeamSpeak API that I'm using: <a href="http://py-ts3.readthedocs.org/en/latest/api/query.html" rel="nofollow">http://py-ts3.readthedocs.org/en/latest/api/query.html</a></p>
  
  <p>And this is the summary of what actually happens in my script:</p>
  
  <ol>
  <li>It connects.</li>
  <li>It checks for channel ID (and it's own client ID)</li>
  <li>It joins the channel</li>
  <li>Script ends so it disconnects.</li>
  </ol>
  
  <p>My question is: How can I make it doesn't disconnects? How can I make the script stay in a "waiting" state so it can read if someone types "hi bot" in the channel? All the code needed to read texts and answer to them seems easy to program, however I'm facing a problem where I can't keep the bot "running" since it closes the file as soon as it ends running the script.</p>
  
  <p><strong>More info:</strong><br>
  I am using <strong>Python 3.4.1</strong>.<br>
  I tried learning Threading <a href="http://www.tutorialspoint.com/python/python_multithreading.htm" rel="nofollow">http://www.tutorialspoint.com/python/python_multithreading.htm</a> but either M'm dumb or it doesn't work the way I though it would.<br>
  In the API there's a function named <code>on_event</code> that I would like to keep running all the time. The bot code should only be run once and then stay "waiting" until an event happens. How should i do that? No clue.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>import ts3
  import telnetlib
  import time
  
  class BotPrincipal:
      def Conectar(ts3conn):
          MiID = [i["client_id"] for i in ts3conn.whoami()]
          ChannelToJoin = "[Pruebas] Bots"
          ts3conn.on_event = BotPrincipal.EventHappened()
          try: 
              BuscandoIDCanal = ts3conn.channelfind(pattern=ChannelToJoin) 
              IDCanal = [i["cid"] for i in BuscandoIDCanal]
              if not IDCanal: 
                  print("No channel found with that name")
                  return None
              else:
                  MiID = str(MiID).replace("'", "")
                  MiID = str(MiID).replace("]", "")
                  MiID = str(MiID).replace("[", "")
                  IDCanal = str(IDCanal).replace("'", "")
                  IDCanal = str(IDCanal).replace("]", "")
                  IDCanal = str(IDCanal).replace("[", "")                
                  print("ID de canal " + ChannelToJoin + ": " + IDCanal)
                  print("ID de cliente " + Nickname + ": " + MiID)
              try:
                  print("Moving you into: " + ChannelToJoin)
                  ts3conn.clientmove(cid=IDCanal, clid=MiID) #entra al canal
                  try:
                      print("Asking for notifications from: " + ChannelToJoin)
                      ts3conn.servernotifyregister(event="channel", id_=IDCanal)
                      ts3conn.servernotifyregister(event="textchannel", id_=IDCanal)
                  except ts3.query.TS3QueryError:
                      print("You have no permission to use the telnet command: servernotifyregister")
                  print("------- Bot Listo -------")
              except ts3.query.TS3QueryError:
                  print("You have no permission to use the telnet command: clientmove")
          except ts3.query.TS3QueryError:
              print("Error finding ID for " + ChannelToJoin + ". telnet: channelfind")
  
      def EventHappened():
          print("Doesn't work")
  
  # Data needed #
  USER = "thisisafakename"
  PASS = "something"
  HOST = "111.111.111.111"
  PORT = 10011
  SID = 1
  
  if __name__ == "__main__":    
      with ts3.query.TS3Connection(HOST, PORT) as ts3conn:
          ts3conn.login(client_login_name=USER, client_login_password=PASS)
          ts3conn.use(sid=SID)
          print("Connected to "+HOST)
          BotPrincipal.Conectar(ts3conn)
  </code></pre>