如何保持python 3脚本(Bot)运行

Sae*_*yth 5 python api bots

(不是母语英语,对不起可能是英语不好.我也是编程的新手).
您好,我正在尝试使用QueryServer连接到TeamSpeak服务器来制作机器人.经过几天的挣扎......它有效,只有一个问题,我坚持使用那一个.

如果您需要检查,这是我正在使用的TeamSpeak API:http://py-ts3.readthedocs.org/en/latest/api/query.html

这是我的脚本中实际发生的事情的摘要:

  1. 它连接起来.
  2. 它检查通道ID(以及它自己的客户端ID)
  3. 它加入了频道
  4. 脚本结束,因此断开连接.

我的问题是:如何让它不断开?如何让脚本保持"等待"状态,以便在有人在频道中键入"hi bot"时它可以读取?阅读文本和回答它们所需的所有代码似乎都很容易编程,但是我遇到了一个问题,我无法让机器人"运行",因为它一旦结束运行脚本就会关闭文件.

更多信息:
我使用的是Python 3.4.1.
我试过学习Threading http://www.tutorialspoint.com/python/python_multithreading.htm,但要么是哑巴,要么它不会像我那样工作.
在API中有一个名为的函数on_event,我希望它一直在运行.机器人代码应该只运行一次然后保持"等待"直到事件发生.我该怎么做?没有线索.

码:

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)
Run Code Online (Sandbox Code Playgroud)

aba*_*ert 3

快速浏览一下 API,您似乎需要显式告诉ts3conn对象等待事件。似乎有几种方法可以做到这一点,但ts3conn.recv(True)似乎是最明显的:

recv_forever如果为真,则阻塞直到所有未获取的响应都已收到或永远阻塞。

据推测,当每个命令进入时,它都会调用您的on_event处理程序,然后当您从该处理程序返回时,它将永远等待下一个命令。

我不知道你是否需要这里的线程,但文档让它recv_in_thread听起来像你可能:

recv()在线程中调用。servernotifyregister如果您使用过并且希望接收事件,这非常有用。

您可能想要获取 servernotify 事件和命令,我猜这个库的编写方式您需要线程来实现吗?如果是这样,只需调用ts3conn.recv_in_thread()而不是ts3conn.recv(True). (如果您查看源代码,您会发现所做的只是启动一个后台线程并调用self.recv(True)该线程。)