[英]IRC BOT in Python, if in elif in while-loop
Here is the complete code: 这是完整的代码:
#client der mit irc-server kontakt aufnimmt
import time
import socket
from sys import argv
script, mitspieler1, mitspieler2 = argv
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
class funktion:
def main(self):
print "Socket(Client)"
host = "irc.iz-smart.net"
port = 6667
PASS = "7987fshd"
NICK = "Testikus"
USER = "Testikus localhost irc.iz-smart.net :Testikus"
self.login(PASS, NICK, USER, host, port)
print "Verbindung aufgebaut zu {0}(IP:{1})".format(
host, socket.gethostbyname(host)
)
self.haupt_schleife()
def haupt_schleife(self):
while True:
antwort = sock.recv(4096)
join = "JOIN #testblablub \r\n"
print antwort
if antwort[0:4] == "PING":
self.pong(antwort, join)
elif antwort.split()[3] == ":quiz.start":
sock.sendall("PRIVMSG #testblablub Es spielen mit: "
+mitspieler1+" und "+mitspieler2+"\r\n"
)
time.sleep(2)
self.fragen(antwort)
def pong(self, antwort, join):
sock.sendall("PONG " + antwort.split()[1] + "\n")
time.sleep(3)
sock.sendall(join)
sock.sendall("PRIVMSG #testblablub hi \r\n")
def login(self, PASS, NICK, USER, host, port):
sock.connect((host, port))
sock.sendall("PASS "+PASS+"\n")
sock.sendall("NICK "+NICK+"\n")
sock.sendall("USER "+USER+"\n")
def fragen(self, antwort):
sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n")
time.sleep(3)
if antwort.split()[3] == ":blau":
sock.sendall("PRIVMSG #testblablub RISCHTISCH \r\n")
ausfuehren = funktion()
ausfuehren.main()
(sry some strings are written in german, but I think that's not important) (有些字符串是用德语编写的,但是我认为这并不重要)
So my main problem is that I want the function def fragen(self, antwort)
to be run in the def haupt_schleife(self)
function (or method for python's sake). 所以我的主要问题是我希望函数
def fragen(self, antwort)
在def haupt_schleife(self)
函数(或python的方法)中运行。 All of the important stuff is in this def haupt_schleife(self)
. 所有重要的东西都在这个
def haupt_schleife(self)
。 I want that the rest of the quiz-code to be running in the elif-block of the def haupt_schleife(self)
, so that it's a complete seperate thing. 我希望其余的测验代码在
def haupt_schleife(self)
的elif块中运行,因此这是一件完全独立的事情。 The stupid thing is, if I type in "quiz.start" the : sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \\r\\n")
is running with no problems, but it doesn't start the if-statement. 愚蠢的事情是,如果我输入“ quiz.start”,则:
sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \\r\\n")
可以正常运行,但是不会启动if -声明。 It just does nothing else in the channel (just prints "Welche Farbe hat der Himmel ? out). 它只是在频道中什么也没做(仅打印“ Welche Farbe hat der Himmel?”)。
I hope that finaly someone understood this :/ (if you want to run the code you have to type in two names in the command line, because of argv) 我希望最终有人能理解这一点:/(由于argv,如果您要运行代码,则必须在命令行中键入两个名称)
Sending messages should not be done this way: 发送消息不应该做的是这样的:
PRIVMSG #testblablub Es spielen mit:
It should be: 它应该是:
PRIVMSG #testblablub :Es spielen mit ...
Also, USER
should consist of 4 parts: 此外,
USER
应包括4个部分:
<username> <hostname> <servername> :<full name/realname>
In irc, it's important to end each string with \\r\\n
在irc中,以
\\r\\n
结尾的每个字符串很重要
I noticed you sent \\n
sometimes and that will end badly, always end with \\r\\n
! 我注意到您有时会发送
\\n
,并且结局很差,总是以\\r\\n
结尾!
Then comes sock.sendall(...)
, it's supposed to be used on say UDP sockets where you want to broadcast a message on all open sockets, in your case you're sending to and from a server, nothing else.. and it's via TCP so you should be using sock.send(...)
(correct me if i'm wrong here) 然后是
sock.sendall(...)
,它应该用于要在所有打开的套接字上广播消息的UDP套接字上,在这种情况下,您要与服务器之间sock.sendall(...)
发送。它是通过TCP的,所以您应该使用sock.send(...)
(如果我在这里错了,请纠正我)
A side note: 旁注:
PING
, it should only reply with PONG <server-name (usually sent with PING)>\\r\\n
and not be spammed with JOIN
every time, JOIN
should be sent after MOTD is recieved, but sure it's not a problem really.. it will only annoy the sysadmins. PING
,它只应使用PONG <server-name (usually sent with PING)>\\r\\n
答复,而不是每次都用JOIN
进行垃圾邮件, JOIN
应该在收到MOTD之后发送,但是请确保这不是问题。它只会惹恼系统管理员。 In your case it would probably be PONG irc.iz-smart.net\\r\\n
and nothing else, no JOIN
etc. 在您的情况下,可能是
PONG irc.iz-smart.net\\r\\n
,仅此而已,没有JOIN
等。
def haupt_schleife(self):
while True:
antwort = sock.recv(4096)
join = "JOIN #testblablub \r\n"
print antwort
if antwort[0:4] == "PING":
self.pong(antwort, join)
elif antwort.split()[3] == ":quiz.start":
sock.sendall("PRIVMSG #testblablub Es spielen mit: "
+mitspieler1+" und "+mitspieler2+"\r\n"
)
time.sleep(2)
self.fragen(antwort)
elif antwort.split()[3] == ":blau":
sock.sendall("PRIVMSG #testblablub RISCHTISH!\r\n")
Since self.fragen(...)
only get called if the user writes quiz.start
, the if
check inside def fragen(...)
will never be run again.. (because the next time the user write something, it's probably "blau" and that doesn't match ":quiz.start" which is the ONLY way in to the if
check inside fragen(...)
. 由于
self.fragen(...)
仅在用户编写quiz.start
才会被调用,因此def fragen(...)
内部的if
校验将永远不会再运行。.(因为用户下次编写某些内容,可能是“ blau”,与“:quiz.start”不匹配,这是fragen(...)
内部if
检查的唯一方法。
def fragen(self, antwort):
sock.sendall("PRIVMSG #testblablub Welche Farbe hat der Himmel ? \r\n")
time.sleep(3)
So we removed the if
block and made it an elif
in haupt_schleife
instead. 因此,我们删除了
if
块,并将其改为haupt_schleife
的elif
。
This is how the FIRST run/message would get parsed: 这是第一个运行/消息将被解析的方式:
The next run, the message WOULD be green in fragen
but it never reaches there because the only way to get there is via elif antwort.split()[3] == ":quiz.start":
:) 下次运行时,该消息在
fragen
变为绿色,但是它永远不会到达那里,因为到达那里的唯一方法是通过elif antwort.split()[3] == ":quiz.start":
:)
You call the fragen
function when this condition is true: 当满足以下条件时,可以调用
fragen
函数:
elif antwort.split()[3] == ":quiz.start":
Then, in the fragen
function, you enter the if
branch when this condition is true: 然后,在
fragen
函数中,当此条件为true时,输入if
分支:
if antwort.split()[3] == ":blau":
Since the function got called, we know that antwort.split()[3]
will be ":quiz.start"
, hence the if
statement will never be executed. 由于函数已被调用,我们知道
antwort.split()[3]
将为":quiz.start"
,因此if
语句将永远不会执行。
You should recheck the logic behind your application. 您应该重新检查应用程序背后的逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.