更多信息:我使用的是 Python 3.4.1.我尝试学习线程http://www.tutorialspoint.com/python/python_multithreading.htm但要么我很笨,要么它不像我想象的那样工作.在 API 中有一个名为 on_event 的函数,我想一直运行它.机器人代码应该只运行一次,然后保持等待"直到事件发生.我该怎么做?没有线索.代码:导入ts3导入 telnetlib导入时间BotPrincipal 类:def Conectar(ts3conn):MiID = [i["client_id"] for i in ts3conn.whoami()]ChannelToJoin = "[Pruebas] 机器人"ts3conn.on_event = BotPrincipal.EventHappened()尝试:BuscandoIDCanal = ts3conn.channelfind(pattern=ChannelToJoin)IDCanal = [i["cid"] for i in BuscandoIDCanal]如果不是 IDCanal:print("没有找到同名频道")返回无别的: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)打印(客户 ID" + 昵称 + :" + MiID)尝试:print("让你进入:" + ChannelToJoin)ts3conn.clientmove(cid=IDCanal, clid=MiID) #内运河尝试:print("请求通知来自:" + ChannelToJoin)ts3conn.servernotifyregister(event="channel", id_=IDCanal)ts3conn.servernotifyregister(event="textchannel", id_=IDCanal)除了 ts3.query.TS3QueryError:print("您无权使用telnet命令:servernotifyregister")打印(-------机器人列表-------")除了 ts3.query.TS3QueryError:print("您无权使用telnet命令:clientmove")除了 ts3.query.TS3QueryError:打印(错误查找+ ChannelToJoin +"的ID.telnet:channelfind")def EventHappened():打印(不起作用")#需要的数据#USER = "thisisafakename"PASS = "东西"主机 = "111.111.111.111"端口 = 10011SID = 1如果 __name__ == "__main__":使用 ts3.query.TS3Connection(HOST, PORT) 作为 ts3conn:ts3conn.login(client_login_name=USER, client_login_password=PASS)ts3conn.use(sid=SID)print("连接到"+HOST)BotPrincipal.Conectar(ts3conn) 解决方案 从对 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).)(Not native English, sorry for probably broken English. I'm also a newbie at programming).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.If you need to check, this is the TeamSpeak API that I'm using: http://py-ts3.readthedocs.org/en/latest/api/query.htmlAnd this is the summary of what actually happens in my script:It connects.It checks for channel ID (and it's own client ID)It joins the channelScript ends so it disconnects.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.More info:I am using Python 3.4.1.I tried learning Threading http://www.tutorialspoint.com/python/python_multithreading.htm but either M'm dumb or it doesn't work the way I though it would.In the API there's a function named on_event 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.Code:import ts3import telnetlibimport timeclass 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 = 10011SID = 1if __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) 解决方案 From a quick glimpse at the API, it looks like you need to explicitly tell the ts3conn object to wait for events. There seem to be a few ways to do it, but ts3conn.recv(True) seems like the most obvious: Blocks untill all unfetched responses have been received or forever, if recv_forever is true.Presumably as each command comes in, it will call your on_event handler, then when you return from that it will go back to waiting forever for the next command.I don't know if you need threads here or not, but the docs for recv_in_thread make it sound like you might: Calls recv() in a thread. This is useful, if you used servernotifyregister and you expect to receive events.You presumably want to get both servernotify events and also commands, and I guess the way this library is written you need threads for that? If so, just call ts3conn.recv_in_thread() instead of ts3conn.recv(True). (If you look at the source, all that does is start a background thread and call self.recv(True) on that thread.) 这篇关于如何保持python 3脚本(Bot)运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 00:52
查看更多