-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.py
More file actions
123 lines (100 loc) · 3.34 KB
/
Main.py
File metadata and controls
123 lines (100 loc) · 3.34 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Main script where everything starts.
from SimpleWebSocketServer import SimpleWebSocketServer
from pathlib import Path
from utils import Logger
from utils import ConsoleListener
from twitch import Connection
from server import SocketServer
import threading
import atexit
import json
import re
lurkbot = None
listener = None
class LurkBot:
def __init__(self, config, listener):
self.username = config['username']
self.channel = config['channel']
self.oauth = config['oauth']
self.passwd = config['pass']
self.ip = config['IP']
self.port = config['PORT']
self.settings = {}
self.keywords = []
self.listener = listener
self.config = config
# Save channel settings.
settings = config['settings']
for setting in settings:
self.settings[setting['channel']] = setting
# Save keywords.
keywords = config['keywords'].split(',')
for keyword in keywords:
self.keywords.append(re.compile('\\b' + keyword + '\\b'))
# Create a new connection.
self.connection = Connection.Connection('wss://irc-ws.chat.twitch.tv')
# Give this object to the connection class
self.connection.init(self)
# Connect.
self.connection.connect()
# Create a new socket server.
self.server = SimpleWebSocketServer(self.ip, int(self.port), SocketServer.SocketServer)
# Create a new thread for the socket server
self.serverThread = threading.Thread(target=self.server.serveforever)
# Make the thread daemon
self.serverThread.setDaemon(True)
# Set the lurkbot object
SocketServer.setLurkBot(self)
# Start the thread.
self.serverThread.start()
def Main():
# Register our exit hook.
atexit.register(onExit)
if (not Path('./config/config.json').is_file()):
config = {}
# Ask for the username.
print('Please enter your username : ', end='')
config['username'] = input().lower()
# Ask for the channel name.
print('Please enter the channel name you want to join : ', end='')
config['channel'] = input().lower()
# Ask for the oauth token.
print('Please enter your oauth token : ', end='')
config['oauth'] = input()
# Ask for password for the events socket.
print('Please enter a custom password for the event socket : ', end='')
config['pass'] = input()
# Ask for an IP for the events socket.
print('Please enter your server IP address : ', end='')
config['IP'] = input()
# Ask for a PORT for the events socket.
print('Please enter a port for the socket : ', end='')
config['PORT'] = input()
# Set an empty array for settings.
config['settings'] = []
# Set keywords to get pinged for.
config['keywords'] = config['channel']
# Save our settings in a config file.
open('./config/config.json', 'w').write(json.dumps(config))
# Print bot details.
Logger.writeLine('')
Logger.writeLine('LurkBot Version: 3.0')
Logger.writeLine('Creator: ScaniaTV')
Logger.writeLine('')
# Start the console listener.
global listener
listener = ConsoleListener.ConsoleListener()
listener.start()
# Start the bot.
global lurkbot
lurkbot = LurkBot(json.loads(open('./config/config.json', 'r').read()), listener)
def onExit():
Logger.writeLine('Socket server closing since the thread is daemon...')
if (lurkbot != None):
Logger.writeLine('Closing connection with Twitch...')
lurkbot.connection.close()
if (listener != None):
Logger.writeLine('Terminating console listener...')
listener.kill()
Logger.writeLine('Bye.')
Main()