Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import time
import requests
# The imports below import all necessary files containing things like keys, usernames and functions
from Socket import openSocket, sendMessage, joinRoom, getMessage, getUser
import settings
import functions
import TwitchAPITools
import CSTeams
def LaunchBot():
"""This funtion is called when the bot is initially ran. It then calls the mainBot function
when the bot is initialised. This function is partly taken and adapted from this video
into a function: https://youtu.be/T8DLwACpe3o(Bad Nidalee, July 2015).
Any modifications are pointed out in the in-line comments."""
s = (openSocket()) # s is a socket object
joinRoom(s)
readBuffer = ""
poll_url = "" # This is a modification to original code and defines an empty string
googToken = functions.getAccessToken() # This is also a modification and gets an OAuth
# Google access token
mainBot(s, readBuffer, poll_url, googToken) # This is slightly modified as two extra
# arguments are passed: poll_url and googToken
return
def mainBot(s, readBuffer, poll_url, googToken):
"""This function is called when the connection to Twitch has been initialised.
It was also taken from https://youtu.be/T8DLwACpe3o(Bad Nidalee, July 2015) with
our bot's functions added in to the for loop."""
################################################################################################################
###################### Until the next line of hashtags, this code was not written by us.########################
while True: # Infinite loop.
readBuffer = readBuffer + (s.recv(1024)).decode() # there is a slight modification with the addition of the
# decode() function, again because this did not work in a current version of Python.
temp = readBuffer.split("\r\n") # there's a modification here as the video imported the string module and used
# "string.split(readBuffer, '\r\n')"
readBuffer = temp.pop()
# Main code goes in here. "line" is the text typed in to chat
for line in temp: # For loop which runs every time a new chat message is received
user = getUser(line) # Sets the user variable to the username of the person that typed in the chat
message = getMessage(line) # Sets the message string to the message typed in the chat
print(message)
if "PING" in line: # Responds to PINGs from Twitch IRC which checks bot is active
s.send(bytes(line.replace("PING", "PONG"), "utf-8")) # slightly modified to work with this version of
# Python. Added bytes() function and "utf-8" encoding as a bytes object was
# required to use the send() function, not a string.
print("PONG sent") # debug tool
########## Everything below this line was written by us and not taken from elsewhere unless explicitly stated otherwise.#########
############################################################################################################
if "!name" in message: # Tells user their username
nameResponse = "Your name is: " + user
sendMessage(s, nameResponse)
if "!discord" in message:
response = "Join my Discord server: " + settings.DiscordInvLink
sendMessage(s, response)
if "!weather" in message:
response = functions.getWeather(message, user) # calls the getWeather function with args message and user before assigning it to the response variable.
sendMessage(s, response)
if "https://www.strawpoll.me" in message: # Checks if someone posts a strawpoll link
message = getMessage(line)
if user == settings.IDENT:
botDupe = "" # This if statement prevents the bot from replying to its own strawpoll links
sendMessage(s, botDupe)
else:
poll_url = getMessage(line) # This sets the users URL link as a variable
pollResponse = "A new poll was created by: " + user
sendMessage(s, pollResponse)
time.sleep(1) # This sleep is to just break up the bot responses a bit
pollResponse = "Type ""!poll"" to vote on the poll and Type ""!results"" to see the result of the poll!"
sendMessage(s, pollResponse)
if "!results" in message:
if user == settings.IDENT:
botDupe = ""
sendMessage(s, botDupe)
else: # This is going to post the URL with the "/r" which is the result URL
resultResponse = "You can view the results here: " + poll_url + "/r"
sendMessage(s, resultResponse)
if "!poll" in message:
if user == settings.IDENT:
botDupe = ""
sendMessage(s, botDupe)
else: # This is just going to repost the poll link
pollAsk = "You can vote on the poll here: " + poll_url
sendMessage(s, pollAsk)
if "!songrequest" in message:
response = functions.addSongToPlaylist(message, user, googToken) # calls the getWeather function with args message, user & googToken before assigning it to the response variable
sendMessage(s, response)
if "!time" in message:
response = functions.Date_time(message, user) # calls the getWeather function with args message and user before assigning it to the response variable
sendMessage(s, response)
if "!rps" in message:
response=functions.Rps_game(message,s)
sendMessage(s,response)
if "!score" in message:
teamName = "" # creates an empty string and assigns it to teamName
for team in CSTeams.ReturnTeams(): # for each team in the outputted list from ReturnTeams()
if team in message: # if the correct format of the team's name is in the message
teamName = team # assign teamName with the team in the message
if teamName == "": # checks to see if a team was supplied or not, i.e. not changed from the empty string
response = "@" + user + ", enter !score teamname. You did not specify a team name." # assigns an error message to response variable
else: # i.e. if a team name was supplied
response = functions.SearchForTeam(teamName, user) # calls the SearchForTeam func with the teamName and user as args and assigns its output to response
sendMessage(s, response) # sends the response to the Twitch stream chat.
if "!ban" in message:
TwitchAPITools.BanUser(message, user, s) # calls the BanUser func with args message, user and s
# there is a separate function to ban users from Discord as the Twitch works differently.
LaunchBot()