Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # import functions/scripts
- import socket
- import threading
- import os
- import traceback
- import requests
- import sqlite3
- import time
- ### connecting to Twitch IRC // DO NOT CHANGE THIS VALUE
- HOST = "irc.chat.twitch.tv"
- PORT = 6667
- ### User Editable Settings
- NICK = "sp4wnytv" # Bots nickname
- CHAN = "sp4wnyuk" # Channel bot is joining
- PASS = "oauth:3ey8xr1z5xe98j9re2jfdlfomdag1o" #Twitch OAuth
- readbuffer = ""
- MODT = False
- ### DO NOT EDIT THESE VALUES
- CHANNEL_NAME = CHAN
- CHANNEL_NAME = CHANNEL_NAME.lower()
- SLEEP_TIME = 120
- #IRC_CHANNEL = "#" + CHANNEL_NAME
- #APIKey = "RGAPI-c2b5b567-5229-46e9-b7b8-7eb218c1ea8f"
- #HOST2 = "199.9.253.119"
- # Connecting to Twitch IRC by passing credentials and joining a certain channel
- s = socket.socket()
- s.connect((HOST, PORT))
- s.send(bytes("PASS %s\r\n" % PASS, "UTF-8"))
- s.send(bytes("NICK %s\r\n" % NICK, "UTF-8"))
- s.send(bytes("JOIN #%s\r\n" % CHAN, "UTF-8"))
- s.send(bytes("CAP REQ :twitch.tv/membership\r\n", "UTF-8"))
- s.send(bytes("CAP REQ :twitch.tv/commands\r\n", "UTF-8"))
- s.send(bytes("CAP REQ :twitch.tv/tags\r\n", "UTF-8"))
- def socketconnection():
- global s, HOST, PORT, NICK, CHAN
- try:
- s.close()
- s.socket.socket()
- s.connect((HOST, PORT))
- s.send(bytes("PASS %s\r\n" % PASS, "UTF-8"))
- s.send(bytes("NICK %s\r\n" % NICK, "UTF-8"))
- s.send(bytes("JOIN #%s\r\n" % CHAN, "UTF-8"))
- s.send(bytes("CAP REQ :twitch.tv/membership\r\n", "UTF-8"))
- s.send(bytes("CAP REQ :twitch.tv/commands\r\n", "UTF-8"))
- s.send(bytes("CAP REQ :twitch.tv/tags\r\n", "UTF-8"))
- except:
- print(traceback.format_exc())
- #opens channel database or creates new database if one does not already exist
- if os.path.exists("{}DB".format(CHAN)):
- dbcon = sqlite3.connect("{}DB".format(CHAN))
- cursor = dbcon.cursor()
- else:
- dbcon = sqlite3.connect("{}DB".format(CHAN))
- cursor = dbcon.cursor()
- cursor.execute('CREATE TABLE channelPoints(username text PRIMARY KEY,points INT DEFAULT 0)')
- dbcon.commit()
- ### Functions
- def tablereport():
- global dbcon
- global cursor
- for x in cursor.execute('SELECT * from channelPoints ORDER BY points'):
- sendmessage(x)
- def requestpoints(message, username):
- global dbcon
- global cursor
- messagelist = message.split()
- index = messagelist.index('!points')
- try:
- if len(messagelist) == 1:
- u = username
- elif len(messagelist) == 2:
- u = messagelist[index + 1]
- cursor.execute('SELECT points FROM channelPoints WHERE username=?', ((u,)))
- sendmessage('{} currently has {} point(s)'.format(username, cursor.fetchone()[0]))
- except:
- print(traceback.format_exc())
- def givepoints(username, points):
- global dbcon
- global cursor
- try:
- cursor.execute('UPDATE channelPoints SET points=points+? WHERE username=?', (points, username))
- dbcon.commit()
- except:
- print(traceback.format_exc())
- def getuser(line):
- separate = line.split(":", 2)
- user = separate[1].split("!", 1)[0]
- return user
- def sendmessage(text):
- # Method for sending a message
- # message = "PRIVMSG #" + CHAN + " :" + str(text)
- s.send(bytes("PRIVMSG #" + CHAN + " :" + str(text) + "\r\n", "UTF-8"))
- # print("Sent Msg: " + message)
- def console(line):
- # Gets if it is a user or twitch server
- if "PRIVMSG" in line:
- return False
- else:
- return True
- def timeout(user, secs):
- timeout_message = "PRIVMSG #" + CHAN + ": /timeout %s %s\r\n" % (user, secs)
- s.send(bytes(timeout_message), "UTF-8")
- def uptime():
- global CHAN
- url = "https://decapi.me/twitch/uptime?channel={}".format(CHAN)
- page = requests.get(url)
- for x in page:
- if ('is offline' not in x.strip().decode('UTF-8')):
- sendmessage("{} has been online for {}".format(CHAN, x.strip().decode('UTF-8')))
- else:
- sendmessage(x.strip().decode('UTF-8'))
- def followage(username):
- global CHAN
- url = "https://decapi.me/twitch/followage/{}/{}?precision=4".format(CHAN, username)
- page = requests.get(url)
- for x in page:
- # sendmessage(x.decode('UTF-8')) ## output just the value
- if('cannot follow themself' in x.strip().decode('UTF-8')):
- sendmessage("{} you cannot follow yourself.".format(username, x.strip().decode('UTF-8')))
- else:
- sendmessage("{} has been following for {}".format(username, x.strip().decode('UTF-8')))
- def followcount():
- global CHAN
- url = "https://decapi.me/twitch/followcount/{}".format(CHAN)
- page = requests.get(url)
- for x in page:
- #sendmessage(x.decode('UTF-8')) ## output just the value
- sendmessage("{} has {} savages following the channel!".format(CHAN, x.strip().decode('UTF-8')))
- def puppet():
- try:
- while True:
- message = input(' assuming direct control: ')
- sendmessage(message)
- commands(message, 'sp4wnybot')
- except BrokenPipeError:
- socketconnection()
- def commands(message, username):
- if message == "!meme":
- sendmessage("EleGiggle")
- if message == "!sliced":
- sendmessage("**unsheathes katana**")
- if message == "!whoami":
- sendmessage(username)
- if '!test' in message and username == 'sp4wnyuk':
- tablereport()
- if '!points' in message and username == 'sp4wnyuk':
- requestpoints(message, username)
- if '!givepoints' in message and username == 'sp4wnyuk':
- messagelist = message.split()
- index = messagelist.index('!givepoints')
- u = str(messagelist[index + 1]) # username
- p = messagelist[index + 2] # points
- givepoints(u, p)
- if message == '!uptimet':
- uptime()
- if message == '!followage':
- followage(username)
- if message == '!followers':
- followcount()
- sendmessage('sp4wnys bot has joined the channel')
- t = threading.Thread(target=puppet).start()
- def messageloop():
- while True:
- global s, readbuffer, dbcon, cursor
- try:
- readbuffer = readbuffer + s.recv(1024).decode("UTF-8")
- except KeyboardInterrupt:
- raise
- except:
- print(traceback.format_exc())
- temp = str.split(readbuffer, "\r\n")
- # temp = [ str(e.encode('UTF-8')).rstrip() for e in temp ]
- readbuffer = temp.pop()
- for line in temp:
- if line == "":
- break
- if line[0] and console(line) == "PING":
- #hellotwitch = "PONG :tmi.twitch.tv\r\n".encode("UTF-8")
- #s.send(bytes(hellotwitch))
- s.send(line.replace("PING", "PONG").encode("UTF-8"))
- print("PONG")
- break
- if "PRIVMSG" in line:
- parts = str.split(line, ":")
- message = parts[2][:len(parts[2])]
- usernamesplit = str.split(parts[1], "!")
- username = usernamesplit[0]
- print("Chat | " + username + ": " + message)
- else:
- parts = str.split(line, ":")
- try:
- message = parts[2][:len(parts[2`])]
- except:
- message = ""
- usernamesplit = str.split(parts[1], "!")
- username = usernamesplit[0]
- cursor.execute('INSERT OR IGNORE INTO channelPoints (username, points) VALUES (?,?)', (username, '0'))
- givepoints(username, 1)
- dbcon.commit()
- print("Bot | " + username + ": " + message)
- commands(message.lower(), username.lower())
- while True:
- try:
- messageloop()
- except KeyboardInterrupt:
- raise
- except:
- print(traceback.format_exc())
- socketconnection()
- messageloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement