(非母语英语,很抱歉可能英文不好,我也是编程的新手)。
你好,我试图连接到使用QueryServer的TeamSpeak服务器来制作一个bot。经过几天的苦苦挣扎之后,它才有效,只有一个问题,并且我被这个问题困住了。如何保持python 3脚本(Bot)运行
如果需要检查,这是我使用的TeamSpeak API:http://py-ts3.readthedocs.org/en/latest/api/query.html
这是什么是真正在我的脚本发生摘要:
- 它连接。
- 它检查信道ID(和它自己的客户端ID)
- 它加入的信道
- 脚本结束,因此它断开。
我的问题是:我怎样才能使它不会断开?如何让脚本保持“等待”状态,以便在有人在频道中键入“hi bot”时读取脚本?所有需要阅读文本和回答它们的代码似乎都很容易编程,但是我遇到了一个问题,我无法让机器人“运行”,因为它一结束运行脚本就关闭文件。
更多信息:
我使用Python的3.4.1。
我试着学习线程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)
作为一个方面说明,这不是你如何使用类。你的'Conectar'应该带一个'self'参数,你的主代码应该构造一个'BotPrincipal'实例并且调用该实例的'Conectar'方法等等。这是3个错误组合消除这种工作,这可能不是你想要依赖的东西。 – abarnert 2014-10-17 19:09:16
只需添加到abarnets注释,只需看看[Python的官方文档](https://docs.python.org/3.4/tutorial/classes.html#a-first-look-at-classes)或一些[不同的教程](http://en.wikibooks.org/wiki/A_Beginner's_Python_Tutorial/Classes)。 – Vyktor 2014-10-17 19:25:31
因为我从来没有真正学过任何编程语言,我只是用谷歌来努力,我可以看到我失败的地方。我正在查看该网站,并试图解决这些问题。我很感激帮助。 – Saelyth 2014-10-17 19:39:37