Permalink
Cannot retrieve contributors at this time
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?
chatbot/TwitchAPITools.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
62 lines (53 sloc)
2.13 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import settings | |
import requests | |
import Socket | |
def BanUser(message, user, s): | |
messageList = message.split(" ") | |
modID = GetUserID(user) | |
streamerID = GetUserID(settings.CHANNEL) | |
mod = CheckModStatus(modID, streamerID) | |
if mod is True and len(messageList) < 3: | |
message = "@" + user + ", you need to specify both the user to ban and a reason for the ban e.g. !ban user swearing" | |
Socket.sendMessage(s, message) | |
return | |
else: | |
if mod == True: | |
userToBan = messageList[1] | |
message = "/ban " + userToBan | |
messageResponse = "Successfully banning " + userToBan | |
Socket.sendMessage(s, messageResponse) | |
if len(messageList) == 3: | |
reason = messageList[2] | |
else: | |
length = len(messageList) | |
reason = "" | |
for i in list(range(0, length)): | |
print(reason) | |
if i <= 1: | |
continue | |
else: | |
reason = reason + messageList[i] | |
file = open("BanList.txt", "a") | |
reasonForBan = userToBan + " was banned by: " + user + " for: " + reason + "\n" | |
file.write(reasonForBan) | |
file.close() | |
else: | |
message = "@" + user + ", you are not a moderator for this channel." | |
Socket.sendMessage(s, message) | |
def CheckModStatus(modID, streamerID): | |
url = "https://api.twitch.tv/kraken/users/" + str(modID) + "/chat/channels/" + str(streamerID) + "?api_version=5" | |
print(url) | |
headers = {"Client-ID": settings.TWITCHCLIENTID, "Authorization": settings.PASS} | |
response = requests.get(url=url, headers=headers).json() | |
badges = str(response['badges']) | |
if "moderator" in badges: | |
return True | |
else: | |
return False | |
def GetUserID(user): | |
headers = {"Client-ID": settings.TWITCHCLIENTID, "Authorization": settings.PASS} | |
url = "https://api.twitch.tv/helix/users?login=" + user | |
response = requests.get(url=url, headers=headers) | |
response = response.json() | |
userID = response['data'][0]['id'] | |
return userID | |