TwitchBot/tbot.py

101 lines
3.6 KiB
Python

import twitch
import datetime
import re
import json
with open(".twitchcreds") as file:
botSettings = json.loads(file.read())#
UserID = botSettings[0]['OAUTH']
PREFIX = '!'
BOTID = '[BOT]'
#User limits
TIMEOUT = 30
MSG_LIMIT = 5
userstats = {}
permitURL = []
class UserData:
def __init__(self, username):
self.username = username
self.mod = username in botSettings[1]["MODS"]
def isURL(message):
rule = re.compile("http(|s)[;:]\/\/.*\..*")
return rule.findall(message)
def handle_message(message: twitch.chat.Message) -> None:
#print(message.chat
if message.text.startswith(PREFIX + 'views'):
message.chat.send(f'{BOTID} @{message.user.display_name}, you have {message.user.view_count} views.')
if message.text.startswith(PREFIX + "hello"):
message.chat.send(f"{BOTID} Hello @{message.user.display_name}, how are you?")
if message.text.startswith(PREFIX + "author"):
message.chat.send(f"{BOTID} I am written by an idiot who knows enough to cause trouble ;)")
if message.text.startswith(PREFIX + "uptime"):
pass # FIND WAY TO DO THIS WHEN ITS NOT 7AM
if message.user.display_name in botSettings[1]["MODS"]:
if message.text.startswith(PREFIX + "setgame"):
# Do logic for setting the game
message.chat.send(f"{BOTID} Changing game to {message.text.strip(PREFIX + 'setgame').strip()}")
if message.text.startswith(PREFIX + "permit"):
print(message.text)
msg = message.text.replace(PREFIX + "permit","").strip()
print(msg)
if '@' in message.text:
msg = msg.replace('@','')
message.chat.send(f"permitting {msg}")
permitURL.append(msg)
print(permitURL)
# User limits
if message.user.display_name not in userstats:
timeOut = int(datetime.datetime.now().timestamp())
userstats[message.user.display_name] = [0, timeOut, timeOut + TIMEOUT]
userstats[message.user.display_name][0] += 1
else:
userstats[message.user.display_name][0] += 1
currTime = int(datetime.datetime.now().timestamp())
if currTime >= userstats[message.user.display_name][1]:
userstats[message.user.display_name][1] = currTime
userstats[message.user.display_name][1] = currTime + TIMEOUT
userstats[message.user.display_name][0] = 0
if currTime < userstats[message.user.display_name][2] and userstats[message.user.display_name][0] > MSG_LIMIT:
message.chat.send(f"{BOTID} Timing out {message.user.display_name} for 15 seconds, please no spam :(")
message.chat.send(f"/timeout {message.user.display_name} 15")
print(f"{userstats[message.user.display_name][2]}:{currTime}")
if message.user.display_name not in permitURL and isURL(message.text):
print("Timout user for 1 second to remove URL message, needs to be fixed")
message.chat.send(f"/timeout {message.user.display_name} 1")
message.chat.send(f"{BOTID} @{message.user.display_name} Please do not post messages with URL's unless permitted")
else:
if isURL(message.text):
permitURL.remove(message.user.display_name)
else:
pass
# print(userstats)
def main():
chat = twitch.Chat(channel='#aztecdude1',
nickname='AzBot',
oauth=UserID,
helix=twitch.Helix(client_id=botSettings[0]['CLIENTID'], use_cache=True))
chat.subscribe(handle_message)
chat.send(f"{BOTID} I am online now")
if __name__ == '__main__':
main()
#print(isURL("https://google.com"))
print(test.mod)