如何保持Python3脚本(Bot)的运行

2024-06-06 11:06:07 发布

您现在位置:Python中文网/ 问答频道 /正文

(不是英语母语,很抱歉可能是蹩脚的英语。我也是个编程新手)。
你好,我正在尝试使用QueryServer连接到TeamSpeak服务器以生成机器人程序。经过几天的挣扎。。。它很管用,只有一个问题,而我却被那一个困住了。在

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

以下是我剧本中实际发生的事情的总结:

  1. 它连接在一起。在
  2. 它检查通道ID(以及它自己的客户端ID)
  3. 它加入了英吉利海峡
  4. 脚本结束,因此断开连接。在

我的问题是:我怎样才能使它不断开?如何使脚本保持在“等待”状态,以便在有人在频道中键入“hi bot”时可以读取?阅读文本和回复文本所需的所有代码似乎都很容易编程,但我面临的问题是,我无法让bot“运行”,因为它在运行脚本时会立即关闭文件。在

更多信息:
我使用的是python3.4.1
我试着学习线程http://www.tutorialspoint.com/python/python_multithreading.htm,但要么我很蠢,要么它不能像我想象的那样工作。
在API中有一个名为on_event的函数,我想一直运行它。bot代码应该只运行一次,然后保持“等待”直到事件发生。我该怎么做?没有线索。在

代码:

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)

Tags: 代码脚本eventidforbotqueryreplace
1条回答
网友
1楼 · 发布于 2024-06-06 11:06:07

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

Blocks untill all unfetched responses have been received or forever, if recv_forever is true.

大概在每个命令进入时,它将调用您的on_event处理程序,然后当您从该处理程序返回时,它将返回到永远等待下一个命令的状态。在

我不知道您是否需要这里的线程,但是^{}的文档使它听起来像您可能:

Calls recv() in a thread. This is useful, if you used servernotifyregister and you expect to receive events.

您可能希望同时获得servernotify事件和命令,我想这个库的编写方式需要线程来实现这一点?如果是这样,只需调用ts3conn.recv_in_thread(),而不是ts3conn.recv(True)。(如果您查看the source,所做的就是启动一个后台线程并在该线程上调用self.recv(True)。)

相关问题 更多 >