1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
   | import socket 
import RPi.GPIO as GPIO                 
import time                             
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_UP)                     
[/COLOR]
### Options 
SERVER = "irc.twitch.tv"  # server
PORT = 6667  # port
### Options (Edit this)
PASS = "oauth:yc2fz4upxwi6k7tzrv58y1rj7h3s3k"  # bot password can be found on https://twitchapps.com/tmi/
BOT = "BOT"  # Bot's name [NO CAPITALS]
CHANNEL = "pixelle_123"  # Channal name [NO CAPITALS]
OWNER = "your_name"  # Owner's name [NO CAPITALS]
### Functions
def sendMessage(s, message):
    messageTemp = "PRIVMSG #" + CHANNEL + " :" + message
    s.send((messageTemp + "\r\n").encode())
def getUser(line):
    separate = line.split(":", 2)
    user = separate[1].split("!", 1)[0]
    return user
def getMessage(line):
    global message
    try:
        message = (line.split(":", 2))[2]
    except:
        message = ""
    return message
def joinchat():
    readbuffer_join = "".encode()
    Loading = True
    while Loading:
        readbuffer_join = s.recv(1024)
        readbuffer_join = readbuffer_join.decode()
        temp = readbuffer_join.split("\n")
        readbuffer_join = readbuffer_join.encode()
        readbuffer_join = temp.pop()
        for line in temp:
            Loading = loadingCompleted(line)
    sendMessage(s, "Chat room joined!")
    print("Bot has joined " + CHANNEL + " Channel!")
def loadingCompleted(line):
    if ("End of /NAMES list" in line):
        return False
    else:
        return True
### Code runs
s_prep = socket.socket()
s_prep.connect((SERVER, PORT))
s_prep.send(("PASS " + PASS + "\r\n").encode())
s_prep.send(("NICK " + BOT + "\r\n").encode())
s_prep.send(("JOIN #" + CHANNEL + "\r\n").encode())
s = s_prep
joinchat()
readbuffer = ""
def Console(line):
    # gets if it is a user or twitch server
    if "PRIVMSG" in line:
        return False
    else:
        return True
while True:
    try:
        readbuffer = s.recv(1024)
        readbuffer = readbuffer.decode()
        temp = readbuffer.split("\n")
        readbuffer = readbuffer.encode()
        readbuffer = temp.pop()
    except:
        temp = ""
    for line in temp:
        if line == "":
            break
        # So twitch doesn't timeout the bot.
        if "PING" in line and Console(line):
            msgg = "PONG tmi.twitch.tv\r\n".encode()
            s.send(msgg)
            print(msgg)
            break
        # get user
        user = getUser(line)
        # get message send by user
        message = getMessage(line)
        # for you to see the chat from CMD
        print(user + " > " + message)
        # sends private msg to the user (start line)
        PMSG = "/w " + user + " "
################################# Command ##################################
############ Here you can add as meny commands as you wish of ! ############
############################################################################
        if user == OWNER and "!command" in message:
            sendMessage(s, "This can only be used by the owner")
            break
        if "!private" in message:
            sendMessage(s, PMSG + "This is a private message send to the user")
            break
        if "!global" in message:
            sendMessage(s, "This is a global message send to the chat")
            break 
        if GPIO.input(40) == 0:
            sendMessage(s, "Bonjour")                     # j’envoie le message "Bonjour" sur le tchat
            print("Message bonjour envoyé")             # je signal que ma commande est envoyé
            break 
############################################################################ | 
Partager