-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
107 lines (89 loc) · 3.12 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from random import random
import datetime
import twitch
import gpt_2_simple as gpt2
import time
import re
import queue
#textchannels = ["dougdougw","summit1g","jericho","morrolantv","macilus","n0thing","loltyler1"]
textchannels = ["ashlinpanda"]
bots = ["Nightbot"]
run_name = 'master'
chats = {}
logs = {}
global lastmsg
global msg
# Strips links so the bot doesn't advertise
strip_links = re.compile(r'^.*((http)s?|www\.\w+\.(com|org|net|ca|me|co)|[\d\w]+\.(com|org|net|ca|me|co)).*$', flags=re.MULTILINE|re.IGNORECASE)
# Strips mentions to prevent harassment
strip_ats = re.compile(r'^.*@[\d\w_]+.*$', flags=re.MULTILINE)
# Strip out all the bad words chat says
strip_profanity = re.compile(r'^.*.*$'
, flags=re.MULTILINE|re.IGNORECASE)
def handle_message(message: twitch.chat.Message) -> None:
# if not isLink(message.text) and not isBot(message.sender):
# with open(message.channel+".txt","a+", encoding='utf-8') as log:
# log.write(message.text+"\n")
# print("[{}] {}: {}".format(message.channel, message.sender, message.text))
global lastmsg, msg
if not msg.empty():
roll = random()
print("Rolled {} ({})".format(roll, msg.full()))
if random() <= 1/15:
now = datetime.datetime.now()
if lastmsg < now:
text = msg.get(block=False)
message.chat.send(text + "\r\n")
print(text)
lastmsg = datetime.datetime.now() + datetime.timedelta(0,10)
if "WoodenLongboard" in message.text:
print("{}: {}".format(message.sender, message.text))
def isLink(str):
return "http://" in str or "https://" in str
def isBot(user):
return user in bots
def close_logs():
for log in logs:
log.close()
def sanitize_message(txt):
txt = txt.replace("<|startoftext|>", "")
txt = txt.replace("<|endoftext|>", "")
txt = strip_links.sub("", txt)
txt = strip_profanity.sub("", txt)
txt = strip_ats.sub("", txt)
return txt
def genmsg(sess):
while True:
msg = gpt2.generate(sess, run_name=run_name,
length=100,
temperature=0.8,
prefix="<|startoftext|>",
truncate="<|endoftext|>",
include_prefix=False,
return_as_list=True)[0]
msg = sanitize_message(msg)
if msg != "":
break
return msg
def main():
helix = twitch.Helix('', use_cache=True)
global lastmsg, msg
lastmsg = datetime.datetime.now()
msg = queue.Queue(100)
sess = gpt2.start_tf_sess()
gpt2.load_gpt2(sess, run_name=run_name)
while not msg.full():
newmsg = genmsg(sess)
print(newmsg)
msg.put(newmsg)
for channel in textchannels:
chat = twitch.Chat(channel="#"+channel, nickname='WoodenLongboard', oauth="",
helix=helix)
chats[channel] = chat
chats[channel].subscribe(handle_message)
print("Finished init")
while True:
if not msg.full():
msg.put(genmsg(sess))
if __name__ == '__main__':
main()