Попытка отправить сообщение в чат Twitch - PullRequest
0 голосов
/ 16 октября 2018

Я уже настроил бота, где он реагирует на определенные команды, такие как использование текста в речь и проигрывание mp3-клипов, когда чат использует команды, однако я не могу понять, как заставить его отвечатьОТПРАВКА НАЗАД сообщение в чат, когда, скажем, кто-то говорит «Привет нечеткий».Я получаю TypeError: требуется байтоподобный объект, а не 'str' .Я, очевидно, делаю что-то не так, но другие боты, которые я проверил, все закодированы по-другому и стараются не делать заново все это.Спасибо за любую предложенную помощь !!!

# Config portion
import socket
import time
import re
import pyttsx3
import soundfx


HOST = "irc.chat.twitch.tv"  # the twitch irc server
PORT = 6667  # always use port 6667
NICK = "fuzzybottgaming"  # twitch username, lowercase
PASS = "oauth:[]"  # your twitch OAuth token
CHAN = "#[my channel]"  # the channel you want to join

# Message Rate
RATE = (20 / 30)  # messages per second

#BANN HAMMER
PATT = [
    r"swear",
    # ...
    r"some_pattern"
    ]

# bot.py portion
# Network functions go here

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"))





def chat(sock, msg):
    '''
    Send a chat message to the server.
    sock -- the socket over which to send the message
    msg -- the message to be sent
    '''
    sock.send("PRIVMSG #{} :{}".format(CHAN, msg))


def ban(sock, user):
    '''
    Ban a user from the current channel.
    sock -- the socket over which to send the ban command
    user -- the user to be banned
    '''
    chat(sock, ".ban {}".format(user))


def timeout(sock, user, secs=10):
    '''
    Time out a user for a set period of time
    sock -- the socket over which to send the timeout command
    user -- the user to be timed out
    secs -- the length of the timeout in seconds (default 600)
    '''
    chat(sock, ".timeout {}".format(user, secs))





# Make sure you prefix the quotes with an 'r'
CHAT_MSG = re.compile(r"^:\w+!\w+@\w+\.tmi\.twitch\.tv PRIVMSG #\w+ :")


while True:
    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"))
       print("Pong")
    else:
        username = re.search(r"\w+", response).group(0)  # return the entire 
        match
        message = CHAT_MSG.sub("", response)
        print(username + ": " + message)

    if 'hello fuzzy' in message:
        s.send("Hello to you too " + username).encode("utf-8")

    #TTS and MP3 sound effects section taken out#
        for pattern in PATT:
            if re.match(pattern,message):
                ban(s, username)
                break
    time.sleep(1 / RATE)

1 Ответ

0 голосов
/ 17 октября 2018

Я понял, где я ошибся.У меня был дополнительный '#' для CHAN, в чате def мне нужно было добавить '\ r \ n' и '.encode ("utf-8")'

...