我正在尝试使用 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 的单独文件,其中包含一些用于连接的变量。