Python IRC 机器人无休止地响应
我正在慢慢学习Python并翻阅教程。我觉得我对其工作原理有了基本的了解。
作为一个创意项目,我想制作一个 IRC 机器人。我不想使用预先建立的框架。我并不打算重新发明轮子,我只是想从头开始构建一个机器人,既作为学习项目,又作为创意的出口。在我对如何执行此操作的一分钟了解中,我不断遇到的一个问题是我的机器人似乎无限地响应我的命令。代码如下:
HOST="irc.durd.net"
PORT=6667
NICK="Data"
IDENT="data"
REALNAME="databot"
HOME='#zela'
feedback=""
s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
s.send("JOIN #zela" + "\r\n")
s.send("PRIVMSG nickserv identify ac}pcut]eobosbec" + "\r\n")
def sendmsg(chan, msg):
s.send("PRIVMSG "+ chan +" :"+ msg +"\n")
def ping():
s.send("PONG :pingis\n")
while 1:
feedback=feedback+s.recv(1024)
print (feedback)
if feedback.find("PING :") != -1:
ping()
if feedback.find("say hello Data") != -1:
sendmsg("#zela", "Hello Data!")
if feedback.find("tell us a joke Data") != -1:
sendmsg("#zela", "but Captain, I don't know how.")
if feedback.find("terminate Data") != -1:
s.send("QUIT\r\n")
现在,一切正常,数据正确响应他的命令。当我告诉他退出时,他甚至退出了。不幸的是,一旦触发其中一个命令,每当我说出新内容时,他就会重复自己,然后永远重复自己,直到我将他关闭。
<~Jordan> say hello Data
<Data> Hello Data!
<~Jordan> a
<Data> Hello Data!
<~Jordan> a
<~Jordan> a
<Data> Hello Data!
<Data> Hello Data!
<Data> Hello Data!
<Data> Hello Data!
我已经测试过服务器是否正在向机器人重新发送消息,但事实并非如此,所以我认为这是我的错,并且我在这里可能缺少一些非常简单的东西。
I am slowly learning Python and thumbing through the tutorials. I feel I have a basic understanding of how it works.
As a creative project I want to make an IRC bot. I don't want to use a pre-established framework. I do not intend to reinvent the wheel, I simply want to construct a bot from the ground-up as both a learning project and a creative outlet. One issue I am encountering consistently in my minute knowledge of how to do this is that my bot seems to be responding to my commands ad infinitum. Here's the code:
HOST="irc.durd.net"
PORT=6667
NICK="Data"
IDENT="data"
REALNAME="databot"
HOME='#zela'
feedback=""
s=socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
s.send("JOIN #zela" + "\r\n")
s.send("PRIVMSG nickserv identify ac}pcut]eobosbec" + "\r\n")
def sendmsg(chan, msg):
s.send("PRIVMSG "+ chan +" :"+ msg +"\n")
def ping():
s.send("PONG :pingis\n")
while 1:
feedback=feedback+s.recv(1024)
print (feedback)
if feedback.find("PING :") != -1:
ping()
if feedback.find("say hello Data") != -1:
sendmsg("#zela", "Hello Data!")
if feedback.find("tell us a joke Data") != -1:
sendmsg("#zela", "but Captain, I don't know how.")
if feedback.find("terminate Data") != -1:
s.send("QUIT\r\n")
Now, everything works fine and Data responds to his commands correctly. He even quits when I tell him to. Unfortunately, once one of these commands is triggered, he will repeat himself every time I say something new, and then repeat himself forever until I turn him off.
<~Jordan> say hello Data
<Data> Hello Data!
<~Jordan> a
<Data> Hello Data!
<~Jordan> a
<~Jordan> a
<Data> Hello Data!
<Data> Hello Data!
<Data> Hello Data!
<Data> Hello Data!
I have tested to see if the server is resending messages to the bot, and it is not, so I figure it's my fault and there is probably something very simple I am missing here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您重复连接到
feedback
的末尾,而不清除它:这将导致
if Feedback.find("say hello Data") != -1:
条件永远真实。您需要执行类似
feedback=""
的操作或删除上述表达式的串联部分。You're concatenating onto the end of
feedback
repeatedly, without clearing it:This will cause the
if feedback.find("say hello Data") != -1:
condition to always be true.You need to do something like
feedback=""
or remove the concatenation part of the above expression.