Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # V 0.2 - 04/02/2015 - Feel free to follow me: http://twitter.com/WeaselZone
- #Define the imports
- import twitch
- import win32api
- import win32con
- import win32com.client as comclt
- import time
- import urllib # for getting data from URL
- import json # parsing json
- import re
- import ctypes
- import datetime
- t = twitch.Twitch() # Reference the twitch script
- username = "weasel_9000" # The username of your bot
- channel = "weasel_9000" # The channel you want your bot to be in
- key = "oauth:XXXXXXXXXXXXXXXXXXX" # oauth-key can be generated at http://twitchapps.com/tmi/
- game = "chess"
- restart_vote_time = 5 # The length of the restart vote
- # Chess variables
- board_coordinates = [57, 154, 600, 690] # upper corner X, Y to lower corner x & y (Lucas Chess)
- field_size = (board_coordinates[2] - board_coordinates[0]) / 8
- # Let's connect to Twitch
- t.twitch_connect(username, key, channel)
- shell = None;
- def key_press(key):
- shell = comclt.Dispatch("WScript.Shell")
- shell.SendKeys(key)
- # click(10,10)
- def click(x, y):
- ctypes.windll.user32.SetCursorPos(x, y)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) # Left Button Down
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) # Left Button Up
- def mouse_move(x, y):
- ctypes.windll.user32.SetCursorPos(x, y)
- # drag(10,10,20,20)
- def drag(startX, startY, endX, endY):
- ctypes.windll.user32.SetCursorPos(startX, startY)
- time.sleep(0.2)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, startX, startY, 0, 0) # Left Button Down
- ctypes.windll.user32.SetCursorPos(endX, endY)
- time.sleep(0.2)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, endX, endY, 0, 0) # Left Button Up
- mouse_move(1, 1) # Move mouse out of window
- def is_moderator(user):
- url = "http://tmi.twitch.tv/group/user/" + channel + "/chatters" # Getting a list of users
- response = urllib.urlopen(url)
- data = json.loads(response.read())
- # print data; # Output the whole response
- chatters = data['chatters']
- moderators = chatters['moderators']
- for x in moderators: # Going through the current moderators
- if user == x:
- return True
- return False
- # ===========================================================
- # ======================= CHESS =============================
- # ===========================================================
- def chess_restart():
- # These commands are for 'Lucas Chess' and have to be adjusted to your program
- click(100, 5) # Click in the window to focus it
- time.sleep(0.2)
- key_press("{ENTER}")
- time.sleep(0.2)
- key_press("{ENTER}")
- click(450, 60) # Click 'Reinit' Button
- time.sleep(1.3)
- key_press("{ENTER}")
- time.sleep(4)
- key_press("{ESC}") # Close the potentially open Tutor window -.-
- time.sleep(0.2)
- key_press("{ENTER}")
- time.sleep(0.2)
- key_press("{ENTER}")
- mouse_move(1, 1) # Move mouse out of the window
- print "DONE: Game restarted."
- t.send_message(channel, 'The game has been restarted. If the game does not react to commands, try !newgame again.')
- #The main loop for chess
- if game == "chess":
- restart_vote_start = 0
- restart_vote = 0
- restart_votes_yes = 0
- restart_votes_no = 0
- restart_vote_username = ""
- while True:
- # Check for active votes
- if restart_vote == 1:
- if datetime.datetime.now()-restart_vote_start >= datetime.timedelta(seconds=restart_vote_time):
- t.send_message(channel, 'Vote ended, here are the results: Yes - ' + str(restart_votes_yes) + ' vote(s), '
- 'No ' + str(restart_votes_no) + ' - vote(s)')
- if restart_votes_yes > restart_votes_no:
- print 'DONE: Vote successful! Restarting the game. Please wait a few seconds.'
- t.send_message(channel, 'Vote successful. Restarting the game!')
- chess_restart()
- restart_vote = 0
- restart_vote_username = ''
- else:
- print 'DONE: Vote failed! Not restarting.'
- t.send_message(channel, 'Vote failed! Not restarting.')
- restart_vote = 0
- # Check for new messages
- new_messages = t.twitch_receive_messages();
- if not new_messages:
- # No new messages...
- continue
- else:
- for message in new_messages:
- # Got new message
- msg = message['message'].lower()
- username = message['username'].lower()
- print("Message from "+username + ": " + msg)
- # Moderator-only commands:
- if is_moderator(username):
- if msg == "!restart": # Restart the current game (forced)
- print username+" is a moderator, accepting !restart command.";
- chess_restart()
- restart_vote = 0
- restart_vote_username = ""
- # Global commands
- if msg.startswith('!help'):
- t.send_message(channel, "To make a move, enter !move and the starting field followed by "
- "the destination field i.e. '!move a2 a3'. To start the vote for a "
- " new game, enter !newgame.")
- if msg.startswith('!accept'):
- key_press("{ENTER}")
- time.sleep(0.2)
- key_press("{ESC}")
- if msg.startswith('!decline'):
- key_press("{ESC}")
- #Starting the vote for a new game
- if msg.startswith('!newgame'):
- if restart_vote_username == username:
- t.send_message(channel, username + ', your last vote failed, '
- 'you cannot start a new vote.')
- else:
- t.send_message(channel, username + ' wants to start a new game.'
- 'Type "!yes" or "!no" to vote! within the next '
- + str(restart_vote_time) + ' seconds.')
- restart_vote_username = username # Set the current user as the person who started it
- restart_vote = 1 # Activate the vote countdown
- restart_votes_yes = 1 # Reset the votes
- restart_votes_no = 0
- restart_vote_start = datetime.datetime.now() # Save current time
- print "DONE: Started new vote."
- if msg == '!yes' and username != restart_vote_username:
- if restart_vote == 1:
- restart_votes_yes += 1
- print "DONE: Vote cast. (yes)"
- if msg == '!no' and username != restart_vote_username:
- if restart_vote == 1:
- restart_votes_no += 1
- print "DONE: Vote cast. (no)"
- if msg.startswith('!move'):
- print "Move command"
- split = msg.split(' ')
- if len(msg) == 11 and len(split[0]) == 5 and len(split[1]) == 2 and len(split[2]) == 2:
- print "Move from " + split[1] + " to " + split[2]
- print split[1][0] + "," + split[1][1]
- print split[2][0] + "," + split[2][1]
- # Are letters & numbers valid?
- if re.match("^[A-Ha-h]*$", split[1][0]) and re.match("^[A-Ha-h]*$", split[2][0]) \
- and re.match("^[1-8]*$", split[1][1]) and re.match("^[1-8]*$", split[2][1]):
- # Executing move command
- print "All ok. Executing command."
- field_start_x = 1
- field_start_y = 1
- field_end_x = 1
- field_end_y = 1
- # Get field
- if re.match("^[Aa]*$", split[1][0]):
- field_start_x = 1
- if re.match("^[Bb]*$", split[1][0]):
- field_start_x = 2
- if re.match("^[Cc]*$", split[1][0]):
- field_start_x = 3
- if re.match("^[Dd]*$", split[1][0]):
- field_start_x = 4
- if re.match("^[Ee]*$", split[1][0]):
- field_start_x = 5
- if re.match("^[Ff]*$", split[1][0]):
- field_start_x = 6
- if re.match("^[Gg]*$", split[1][0]):
- field_start_x = 7
- if re.match("^[Hh]*$", split[1][0]):
- field_start_x = 8
- # Numbers need to be flipped (Lucas Chess start with 8 down to 1 by default)
- if split[1][1] == "8":
- field_start_y = 1
- if split[1][1] == "7":
- field_start_y = 2
- if split[1][1] == "6":
- field_start_y = 3
- if split[1][1] == "5":
- field_start_y = 4
- if split[1][1] == "4":
- field_start_y = 5
- if split[1][1] == "3":
- field_start_y = 6
- if split[1][1] == "2":
- field_start_y = 7
- if split[1][1] == "1":
- field_start_y = 8
- if re.match("^[Aa]*$", split[2][0]):
- field_end_x = 1
- if re.match("^[Bb]*$", split[2][0]):
- field_end_x = 2
- if re.match("^[Cc]*$", split[2][0]):
- field_end_x = 3
- if re.match("^[Dd]*$", split[2][0]):
- field_end_x = 4
- if re.match("^[Ee]*$", split[2][0]):
- field_end_x = 5
- if re.match("^[Ff]*$", split[2][0]):
- field_end_x = 6
- if re.match("^[Gg]*$", split[2][0]):
- field_end_x = 7
- if re.match("^[Hh]*$", split[2][0]):
- field_end_x = 8
- # Numbers need to be flipped (Lucas Chess start with 8 down to 1 by default)
- if split[2][1] == "8":
- field_end_y = 1
- if split[2][1] == "7":
- field_end_y = 2
- if split[2][1] == "6":
- field_end_y = 3
- if split[2][1] == "5":
- field_end_y = 4
- if split[2][1] == "4":
- field_end_y = 5
- if split[2][1] == "3":
- field_end_y = 6
- if split[2][1] == "2":
- field_end_y = 7
- if split[2][1] == "1":
- field_end_y = 8
- # Calculate coordinates
- move_start_x = board_coordinates[0] + (field_start_x * field_size) - (field_size/2)
- move_start_y = board_coordinates[1] + (field_start_y * field_size) - (field_size/2)
- move_end_x = board_coordinates[0] + (field_end_x * field_size) - (field_size/2)
- move_end_y = board_coordinates[1] + (field_end_y * field_size) - (field_size/2)
- print str(field_start_x) + "," + str(field_start_y) + "," + str(field_end_x) + "," + str(field_end_y)
- print str(move_start_x) + "," + str(move_start_y) + "," + str(move_end_x) + "," + str(move_end_y)
- # Doing the move
- click(35, 125) # Click in the window to focus it
- drag(move_start_x, move_start_y, move_end_x, move_end_y)
- print "DONE: Command executed."
- else:
- print "BAD COMMAND: Wrong coordinates"
- t.send_message(channel, username + ': Coordinates are invalid, please check your command.')
- else:
- print "BAD COMMAND: Length command incorrect"
- t.send_message(channel, username + ': Command invalid. Example: !move b2 b4')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement