Python IRC 机器人无休止地响应

发布于 2025-01-02 20:36:36 字数 1470 浏览 3 评论 0原文

我正在慢慢学习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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

攀登最高峰 2025-01-09 20:36:36

您重复连接到 feedback 的末尾,而不清除它:

feedback=feedback+s.recv(1024)

这将导致 if Feedback.find("say hello Data") != -1: 条件永远真实。

您需要执行类似 feedback="" 的操作或删除上述表达式的串联部分。

You're concatenating onto the end of feedback repeatedly, without clearing it:

feedback=feedback+s.recv(1024)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文