我正在尝试使用python创建一个irc机器人,但出现错误。当我尝试检查发送的消息时,PRIVMSG不会将消息发送到聊天室,但是会输出测试打印语句。如果我只是在收到任何消息后尝试发送消息,它将发送消息。我在代码中注释了这种情况。这是我第一次尝试这样做,如果我对使用python套接字没有经验,请对不起。
#connect.py
#connects to twitch irc and presents a class to b
import cfg
import socket
import time
def ban(sock, user):
chat(sock, ".ban {}".format(user))
def timeout(sock, user, secs=600):
chat(sock, ".timeout {}".format(user, secs))
# network functions go here
s = socket.socket()
s.connect((cfg.HOST, cfg.PORT))
s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8"))
s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8"))
s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8"))
while True:
time.sleep(0.1)
response = s.recv(1024).decode("utf-8")
print(response)
if response == "PING :tmi.twitch.tv\r\n":
s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
elif "hi" in response:
#prints Condition Worked but doesn't send message
print("Condition Worked")
s.send(("PRIVMSG #garrett_the_savage :hello"+"\r\n").encode('utf-8'))
else
#sends a message when anything is received from the socket
s.send(("PRIVMSG #garrett_the_savage :hello"+"\r\n").encode('utf-8'))
编辑:我有一个单独的文件cfg.py,其中包含一些用于连接的变量。