Skip to content

Instantly share code, notes, and snippets.

@jsettlem
Created May 27, 2018 03:20
Show Gist options
  • Save jsettlem/ca3274927f872f14fc8eae821a76e0cf to your computer and use it in GitHub Desktop.
Save jsettlem/ca3274927f872f14fc8eae821a76e0cf to your computer and use it in GitHub Desktop.
import numpy as np
import pygame
import serial
import time
from youtubechat import YoutubeLiveChat, get_live_chat_id_for_stream_now
import re
import os
from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
import random
from tornado.ioloop import IOLoop
from chatty import create
import config
import ChatEventHandler
import threading
import asynctwitch
bot = asynctwitch.Bot(
user = "bikbot2",
oauth = "oauth:xxxxxxxxxxxxxxxxxxxxx",
channel = "pimanrules",
)
@bot.override
async def event_message(message):
processmessage(message.content, message.author.name)
validcommands = ["a", "b", "y", "zl", "up", "down", "left", "right", "u", "d", "l", "r", "ul", "ur", "dl", "dr", "cup", "cdown", "cleft", "cright", "law", "chaos", "map", "stop", "voteban", "jump", "capture", "center"]
clients = []
commands = []
pressed = []
lastpressed = []
last = time.time()
next = time.time()
ctimer = 0
ser = serial.Serial("COM6", 57600, writeTimeout = 0)
livechat_id = get_live_chat_id_for_stream_now("oauth_creds")
chat_obj = YoutubeLiveChat("oauth_creds", [livechat_id])
oldb = 0
law = False
minusPercent = 0
voteban = {}
timeouts = {}
lasttovoteban = ""
class SimpleThing(WebSocket):
def handleConnected(self):
print(self.address, 'connected')
clients.append(self)
def handleClose(self):
clients.remove(self)
server = SimpleWebSocketServer('', 8000, SimpleThing)
def processmessage(msg, author):
if "[" in msg or "]" in msg:
return
if msg.lower().count("map") > 1:
return
if msg.lower().count("voteban") > 1:
return
if author.lower() in timeouts and timeouts[author.lower()] > time.time():
for client in clients:
client.sendMessage("\n\n" + author + " is votebanned for\n" + str(int(timeouts[author.lower()]) - time.time()) + " more seconds. ")
server.serveonce()
return
message = msg.split(",")
commands.append([author, message[0]])
if len(message) > 1:
for thing in message[1:8]:
commands.append(["", thing])
def respond(msgs, chatid):
for msg in msgs:
#print(msg.author.display_name, msg.display_message)
author = msg.author.display_name
'''for client in clients:
client.sendMessage("\n\nYouTube temporarily disabled\nHead to mixer.com/pimanrules")
server.serveonce()'''
processmessage(msg.display_message, author)
def _handle_chat(data):
try:
msg = ''.join(item["text"] for item in data['data']["message"]["message"])
author = data["data"]["user_name"]
processmessage(msg, author)
except:
print("oops")
def mthread():
IOLoop.instance().start()
def tthread():
bot.start()
try:
chat = create(config)
chatevents = ChatEventHandler.Handler(config, chat)
# Tell chat to authenticate with the beam server. It'll throw
# a chatty.errors.NotAuthenticatedError if it fails.
chat.authenticate()
# Listen for incoming messages. When they come in, just print them.
chat.on("message", _handle_chat)
dthread = threading.Thread(target=mthread)
dthread.daemon = True
dthread.start()
tthread = threading.Thread(target=tthread)
tthread.daemon = True
tthread.start()
# Start the tornado event loop.
print("threaded")
fh = open("log.txt", mode="ab", buffering = 0)
chat_obj.start()
chat_obj.subscribe_chat_message(respond)
while(True):
xp = 0
yp = 0
rxp = 0
ryp = 0
mrb = 0b00000000
repeated = False
towrite = ""
newcommand = False
actuallynewcommand = False
ctimer = ctimer - 1
if len(commands) and next < time.time():
msg = commands.pop(0)
author = msg[0]
msg = msg[1]
toban = msg.split(" ", 1)[-1].lower()
#print(voteban, timeouts)
msg = re.sub(r"[,.;@#?!&$]+\ *", " ",msg.lower()).split()
if len(msg) > 1 and msg[0] == "voteban" and author != lasttovoteban:
pressed = []
towrite = "\n\n"
if toban == "pimanrules":
toban = author.lower()
towrite += author + ":\nvoteban " + toban + " "
if toban in voteban:
voteban[toban] += 10
else:
voteban[toban] = 10
if voteban[toban] > 100:
voteban[toban] = 100
towrite += "(" + str(voteban[toban]) + "%)\n"
if voteban[toban] == 100:
if toban in validcommands:
voteban[toban] = 0
if toban in timeouts:
towrite += "The command " + str(toban) + " has been banned for 6 minutes\n"
timeouts[toban] = time.time() + 6*60
else:
towrite += "The command " + str(toban) + " has been banned for 5 minutes\n"
timeouts[toban] = time.time() + 5*60
else:
voteban[toban] = 0
if toban in timeouts:
towrite += "The user " + str(toban) + " has been banned for 30 minutes\n"
timeouts[toban] = time.time() + 30*60
else:
towrite += "The user " + str(toban) + " has been banned for 15 minutes\n"
timeouts[toban] = time.time() + 15*60
newcommand = True
actuallynewcommand = True
lasttovoteban = author
else:
for key in voteban:
voteban[key] -= random.randrange(0,14)
if voteban[key] < 0:
voteban[key] = 0
pressed = set(msg)
if pressed == lastpressed or ("a" in lastpressed and "a" in pressed) or ("b" in lastpressed and "b" in pressed):
repeated = True
lastpressed = pressed
if "capture" in pressed and ctimer > -250:
pressed.remove("capture")
if "capture" in pressed:
ctimer = 2
if author != "":
towrite = "\n\n"
towrite += author + ":\n"
else:
towrite += "\n"
actuallynewcommand = True
last = time.time()
if "map" in pressed:
minusPercent += 25 + random.randrange(0,10)
if minusPercent > 100:
minusPercent = 100
else:
pressed.remove("map")
newcommand = True
towrite += "map (" + str(minusPercent) + "%) "
if minusPercent == 100:
minusPercent = 0
else:
minusPercent -= random.randrange(0,10)
if minusPercent < 0:
minusPercent = 0
for key in set(pressed):
if key in validcommands and key in timeouts and timeouts[key] > time.time():
pressed.remove(key)
towrite += key + " is votebanned for\n" + str(int(timeouts[key]) - time.time()) + " more seconds. "
newcommand = True
if "capture" in pressed and ctimer < 0:
pressed.remove("capture")
next = last + 1 / (len(commands)+1)
if "stop" in pressed or "s" in pressed:
towrite += "Stop "
pressed = []
newcommand = True
if "up" in pressed:
towrite += "↑ "
yp = -127
newcommand = True
if "down" in pressed:
towrite += "↓ "
yp = 127
newcommand = True
if "left" in pressed:
towrite += "← "
xp = -127
newcommand = True
if "right" in pressed:
towrite += "→ "
xp = 127
newcommand = True
if "u" in pressed or "ul" in pressed or "ur" in pressed:
towrite += "slight ↑ "
yp = -50
newcommand = True
if "d" in pressed or "dl" in pressed or "dr" in pressed:
towrite += "slight ↓ "
yp = 50
newcommand = True
if "l" in pressed or "ul" in pressed or "dl" in pressed:
towrite += "slight ← "
xp = -50
newcommand = True
if "r" in pressed or "ur" in pressed or "dr" in pressed:
towrite += "slight → "
xp = 50
newcommand = True
if "a" in pressed or "jump" in pressed:
towrite += "Ⓐ "
mrb = mrb | 0b00010000
newcommand = True
if "b" in pressed :
towrite += "Ⓑ "
mrb = mrb | 0b10000000
newcommand = True
if "y" in pressed:
towrite += "Ⓨ "
mrb = mrb | 0b01000000
newcommand = True
if "zl" in pressed:
towrite += "ZL "
mrb = mrb | 0b00100000
newcommand = True
if "map" in pressed:
newcommand = True
mrb = mrb | 0b00000100
if "center" in pressed:
towrite += "center"
mrb = mrb | 0b00001000
newcommand = True
if "capture" in pressed:
towrite += "◙ "
mrb = mrb | 0b00000010
newcommand = True
if "law" in pressed:
law = True
towrite += "LAW "
newcommand = True
if "chaos" in pressed:
law = False
towrite += "CHAOS "
newcommand = True
if "cup" in pressed and not law:
towrite += "📹↑ "
ryp = -127
newcommand = True
if "cdown" in pressed and not law:
towrite += "📹↓ "
ryp = 127
newcommand = True
if "cleft" in pressed:
towrite += "📹← "
rxp = -127
newcommand = True
if "cright" in pressed:
towrite += "📹→ "
rxp = 127
newcommand = True
if newcommand and actuallynewcommand:
for client in clients:
client.sendMessage(towrite)
if law:
ryp = 127
#print(xp, yp, rxp, ryp, mrb)
if (repeated):
ser.write(bytearray([xp + 128, yp + 128, rxp + 128, ryp + 128, 0]))
ser.read()
ser.write(bytearray([xp + 128, yp + 128, rxp + 128, ryp + 128, 0]))
ser.read()
ser.write(bytearray([xp + 128, yp + 128, rxp + 128, ryp + 128, 0]))
ser.read()
ser.write(bytearray([xp + 128, yp + 128, rxp + 128, ryp + 128, 0]))
ser.read()
ser.write(bytearray([xp + 128, yp + 128, rxp + 128, ryp + 128, mrb]))
ser.read()
server.serveonce()
finally:
print("closing up shop")
pygame.quit()
ser.close()
fh.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment