Atltvhead
Published © MIT

Snap Like You're Thanos!

Too many people in your Twitch.tv chatroom? Why not ban half of them, with a snap! Well, maybe just time them out, it's a Marvel movie.

IntermediateFull instructions provided8 hours1,859
Snap Like You're Thanos!

Things used in this project

Hardware components

Dishwasher Infinity Gauntlet
×1
ESP32
×1
Silicon Wire 18 gauge
×1
Conductive Silver fabric
×1
3D printed Files in file section
×1
Sweatband/wristband
×1
Lipo Battery for the esp32
×1

Story

Read more

Schematics

Touch Circuit

Just a simple esp32 touch circuit!

Code

Snap_Glove.ino

Arduino
Error opening file.

Code snippet #1

Plain text
import cfg
import socket
import time
import random
import math

Code snippet #2

Python
def timeout(sock, user, secs):
    """
    Time out a user for a set period of time.
    Keyword arguments:
    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))
    
def untimeout(sock,user):
    chat(sock,"/unban {}".format(user))

Code snippet #3

Python
s = socket.socket()
s.connect((cfg.HOST,cfg.PORT))
s.send("PASS {}\r\n".format(cfg.PASS).encode("utf-8"))
s.send("NICK {}\r\n".format(cfg.NICK).encode("utf-8"))
s.send("JOIN {}\r\n".format(cfg.CHAN).encode("utf-8"))

CHAT_MSG=re.compile(r"^:\w+!\w+@\w+.tmi.twitch.tv PRIVMSG #\w+ :")

Code snippet #5

Python
while True:    
        response = s.recv(1024).decode("utf-8")
        if response == "PING :tmi.twitch.tv\r\n":
            s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8")) 
            print('sent')

Code snippet #6

Python
        else:
            username = re.search(r"\w+", response).group(0) # return the entire match
            message = CHAT_MSG.sub("", response)
            # s.send(s,username +" + " + message)
            print(username + ": " + message)
           

Code snippet #7

 if username in vips: # will need to edit out tvheadbot, atltvhead, tmi from the messages collected! before doing the thanos glove
                # if its tvhead bot do nothing
                ind = vips.index(username)
                vipCount = vipc[ind]
                vipCount = vipCount + 1
                vipc[ind] = vipCount
                print(vips)
                print(vipc)
                # print(username + " has spoken " + str(vipCount) + " times.")
                
            else:
                vips.append(username)
                vipc.append(1)

Code snippet #8

Python
if message.strip() == "SNAP" and username == "tvheadbot":
                # these operations move half of the chat to a new list for removal
                oglength = len(vips)
                delenght = math.ceil((oglength-3)/2)

                while len(vrem) < delenght:
                    userToMove = random.choice(vips)
                    if userToMove == "tmi" or userToMove == "tvheadbot" or userToMove == "atltvhead":
                        # do nothing
                        print("Encountered either Tmi or tvheadbot or atltvhead")
                    else:
                        if userToMove in vrem:
                            print("User " + userToMove + " is already to be timed out.")
                        else:
                            vrem.append(userToMove)

Code snippet #9

                # this is where I do the timeout
                for q in vrem:
                    # print(q)
                    timeout(s, q, 10)
                    time.sleep(1 / cfg.RATE)
                # remember to wipe out vrem  I want to unban / untimout
                

                
        time.sleep(1 / cfg.RATE)

Code snippet #10

C/C++
#include <WiFi.h>
#include <IRCClient.h>

/*-----------------------------------------------------------------------------------*/
#define ssid1         "Wifi"
#define password1     "Password"

#define IRC_SERVER   "irc.chat.twitch.tv"
#define IRC_PORT     6667
#define IRC_NICK     "Bot_Name"
#define IRC_CHAN     "#Channel_Name"
#define IRC_PASS     "oauth:####"


WiFiClient wiFiClient;
IRCClient client(IRC_SERVER, IRC_PORT, wiFiClient);

Code snippet #12

C/C++
int thresholdTwo = 60;
bool touch2detected = false;
byte touchCountTwo = 0;
unsigned long touchTimeTwo;
unsigned long oldTouchTimeTwo;


void gotTouch2(){
  //figure out calibration here!
//get time and incriment timer
touchTimeTwo = millis();
touchCountTwo++;
// if counter is above a certain # in a certain timeout decrease sensitivity (time is going to be half second)
if(touchCountTwo >=3 && (touchTimeTwo-oldTouchTimeTwo)<=400 && thresholdTwo > 20){
  thresholdTwo=thresholdTwo-1;
  // reset count
  touchCountTwo=0;
  }
// if counter is below a # and a certain timeout increase sensitivity (time is going to be 2 min?)
else if((touchTimeTwo-oldTouchTimeTwo)>=60000){ // touchCount<1 && probably doesn't need the <1 touch count. if it creates too sensitive of a sensor, it will be backed off imediatly after. it should jut create more triggers after the high five
  thresholdTwo++;
  // reset counter
  touchCountTwo=0;
  }
// if counter is below # and timer is between sensitivity triggers touch detected
else if(400<(touchTimeTwo-oldTouchTimeTwo) && (touchTimeTwo-oldTouchTimeTwo)<60000){
  touch2detected = true;
  delay(500);
  // reset counter
  }
// time saved to new variable and reset
oldTouchTimeTwo = touchTimeTwo;
} 

Code snippet #13

Plain text
void setup() {
  WiFi.begin(ssid1, password1);
  client.setCallback(callback);
  client.setSentCallback(debugSentCallback);

  touchAttachInterrupt(T7,gotTouch2,thresholdTwo);

}

Code snippet #14

Plain text
void loop() {
  // put your main code here, to run repeatedly:

    if (!client.connected()) {
    //Serial.println("Attempting IRC connection...");
    // Attempt to connect
    if (client.connect(IRC_NICK, IRC_CHAN, IRC_PASS)) {
    //Serial.println("connected");
      client.sendMessage(IRC_CHAN, "Hello everyone! I'm TvheadBot, a construct, assistant within the head of Atltvhead. If you have any questions type !help , and I'll post a link to all the channel commands. Let's Tune into Good Vibes! <3");
    } else {
      //Serial.println("failed... try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
    return;
  }

Code snippet #15

Plain text
client.loop();
  

  //Time Outs or Emote Only 
  if(touch2detected){
    touch2detected = false;
    if(snapboo){
      snapboo = false;
      client.sendMessage(IRC_CHAN,"SNAP");
      
    } else if(emoteboo){
      emoteboo = false;
      client.sendMessage(IRC_CHAN,"IT'S EMOTE TIME!");
    } else{
      client.sendMessage(IRC_CHAN,"High Five Mode Initiated");
    }
  }
}

Code snippet #16

Plain text
void callback(IRCMessage ircMessage) {
//Serial.println("In CallBack");
  // PRIVMSG ignoring CTCP messages
  if (ircMessage.command == "PRIVMSG" && ircMessage.text[0] != '\001') {
    //Serial.println("Passed private message.");
    String message("<" + ircMessage.nick + "> " + ircMessage.text);
    
    if(ircMessage.text == "sk" && ircMessage.nick == "username"){
      snapboo = true;
      client.sendMessage(IRC_CHAN,"The Snap has been unlocked!");
    }
    else if(ircMessage.text == "ek" && ircMessage.nick == "username"){
      emoteboo = true;
      client.sendMessage(IRC_CHAN,"Emote only ready!");
    }
    return;
  }
}

Credits

Atltvhead

Atltvhead

3 projects • 10 followers

Comments