Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Twitch IRC聊天机器人已成功连接,但未检测到命令_Python_Python 3.x_Irc_Twitch_Chatbot - Fatal编程技术网

Python Twitch IRC聊天机器人已成功连接,但未检测到命令

Python Twitch IRC聊天机器人已成功连接,但未检测到命令,python,python-3.x,irc,twitch,chatbot,Python,Python 3.x,Irc,Twitch,Chatbot,我开始使用Python制作一个简单的Twitch聊天机器人。它连接良好,还可以查看其他人在聊天中发送的消息。然而,我的问题是,当使用命令时,我似乎无法检测到它们。我可以获取聊天记录的用户名和信息,甚至可以将它们打印到控制台,但是将它们传递到chat()函数没有任何作用 另一个怪癖是,机器人有时会随机发送包含PONG响应的聊天信息 我想我遗漏了一些明显的东西,可能是我在chat中检查命令的方式,或者我的整个chat()功能 import re from time import sleep impo

我开始使用Python制作一个简单的Twitch聊天机器人。它连接良好,还可以查看其他人在聊天中发送的消息。然而,我的问题是,当使用命令时,我似乎无法检测到它们。我可以获取聊天记录的用户名和信息,甚至可以将它们打印到控制台,但是将它们传递到
chat()
函数没有任何作用

另一个怪癖是,机器人有时会随机发送包含PONG响应的聊天信息

我想我遗漏了一些明显的东西,可能是我在chat中检查命令的方式,或者我的整个
chat()
功能

import re
from time import sleep
import socket


HOST = "irc.twitch.tv"
PORT = 6667
NICK = "bot_name"
PASS = "oauth:..."
CHAN = "#channel"
RATE = (20 / 30)
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")


def main_loop():
    try:
        s = socket.socket()
        s.connect((HOST, PORT))
        s.send("PASS {}\r\n".format(PASS).encode("utf-8"))
        s.send("NICK {}\r\n".format(NICK).encode("utf-8"))
        s.send("JOIN {}\r\n".format(CHAN).encode("utf-8"))
        connected = True
    except Exception as e:
        print(str(e))
        connected = False

    while connected:
        response = s.recv(1024).decode("utf-8")
        if response == "PING :tmi.twitch.tv\r\n":
            s.send("PONG :tmi.twitch.tv\r\n".encode())
            print("PONG")
        else:
            username = re.search(r"\w+", response).group(0)
            message = CHAT_MSG.sub("", response)

            if message == "!test":
                chat(s, "Testing command received!")

            print(username + ": " + message)
        sleep(1)


def chat(sock, msg):
    sock.send("PRIVMSG {} :{}".format(CHAN, msg).encode())


if __name__ == "__main__":
    main_loop()

如果其他人也有同样的问题,我找到了解决办法!通过
chat()
函数传递的消息必须在字符串末尾添加
\r\n
,要检查的命令是
!测试\r\n
。我确信这正是IRC工作的方式,但是这个添加现在让机器人能够响应命令