forked from ender-null/polaris.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.py
executable file
·209 lines (166 loc) · 6.68 KB
/
loader.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import importlib
import logging
import os
from multiprocessing import Process
from shutil import copyfile
from time import sleep
import firebase_admin
from firebase_admin import credentials, db, storage
from polaris.bot import Bot
from polaris.types import AutosaveDict
from polaris.utils import (catch_exception, load_plugin_list, set_logger,
wait_until_received)
# Loads the bots from the /bots/ #
def get_bots():
botlist = []
for bot in bots:
try:
botlist.append(Bot(bot))
except Exception as e:
catch_exception(e)
logging.error(' [Failed] "%s" failed to initialize' % bot)
return botlist
# Imports all plugin modules to a list. #
def import_plugins(enabled_plugins):
plugins = []
logging.debug('Importing plugins...')
for plugin in enabled_plugins:
try:
plugins.append(importlib.import_module('polaris.plugins.' + plugin))
logging.debug(' [OK] %s ' % (plugin))
except Exception as e:
logging.error(' [Failed] %s: %s ' % (plugin, str(e)))
logging.info(' Loaded: ' + str(len(plugins)) + '/' + str(len(enabled_plugins)))
return plugins
# EXPERIMENTAL!! Adds, removes and configures bots #
def setup():
def bot_config(name):
config = AutosaveDict('bots/%s.json' % name)
i = 1
finished = False
while not finished:
text = 'Editing /bots/%s.json\n' % name
if 'bindings' in config:
bindings = config['bindings']
else:
bindings = 'telegram-bot-api'
if 'bindings_token' in config:
bindings_token = config['bindings_token']
else:
bindings_token = 'YOUR TELEGRAM BOT TOKEN'
if 'command_start' in config:
command_start = config['command_start']
else:
command_start = '/'
if 'owner' in config:
owner = config['owner']
else:
owner = 0
if 'debug' in config:
debug = config['debug']
else:
debug = False
if 'language' in config:
language = config['language']
else:
language = 'default'
if 'plugins' in config:
plugins = config['plugins']
else:
plugins = load_plugin_list()
if 'api_keys' in config:
api_keys = config['api_keys']
else:
api_keys = {}
text += '(0) Finish editing\n'
text += '(1) bindings: %s\n' % bindings
text += '(2) bindings_token: %s\n' % bindings_token
text += '(3) command_start: %s\n' % command_start
text += '(4) owner: %s\n' % owner
text += '(5) debug: %s\n' % debug
text += '(6) language: %s\n' % language
text += '(7) plugins: %s\n' % plugins
text += '(8) api_keys: %s\n' % api_keys
option = int(input(text))
if option == 0:
print('Saving all values to "bots/%s.json".' % name)
config.bindings = bindings
config.bindings_token = bindings_token
config.command_start = command_start
config.owner = owner
config.debug = debug
config.language = language
config.plugins = plugins
config.api_keys = api_keys
finished = True
elif option == 1:
option = input('What bindings want to use? (Current value: "%s")\n' % bindings)
config.bindings = option
logging.warning('BOT SETUP NOT YET IMPLEMENTED')
botlist = []
for filename in os.listdir('bots'):
if filename.endswith('.json'):
botlist.append(filename[:-5])
logging.info('Found %s bots configuration files.' % len(botlist))
user_input = input('Want to edit them or set up another one? (y)es / (n)o\n')
if user_input.lower() == 'y' or user_input.lower() == 'yes':
ok = False
while not ok:
user_input = input('Edit existing bot configuration or add a new one? (e)dit / (a)dd / (r)emove / (f)inish\n')
if user_input.lower() == 'e' or user_input.lower() == 'edit':
ok = True
bots = 'What bot config want to edit?\n'
botlist = []
for filename in os.listdir('bots'):
if filename.endswith('.json'):
botlist.append(filename[:-5])
bots += '\t(%s) %s\n' % (len(botlist), botlist[len(botlist)-1])
user_input = input(bots)
bot_config(botlist[int(user_input)-1])
elif user_input.lower() == 'a' or user_input.lower() == 'add':
ok = True
name = input('How do you want to name the new bot? (lowercase alphanumeric characters, please)\n')
bot_config(name)
elif user_input.lower() == 'f' or user_input.lower() == 'finish':
ok = True
logging.info('Let\'s Go!')
elif user_input.lower() == 'r' or user_input.lower() == 'remove':
ok = True
bots = 'What bot config want to REMOVE?\n'
botlist = []
for filename in os.listdir('bots'):
if filename.endswith('.json'):
botlist.append(filename[:-5])
bots += '\t(%s) %s\n' % (len(botlist), botlist[len(botlist)-1])
user_input = input(bots)
os.remove('bots/%s.json' % botlist[int(user_input)-1])
print('Removed "bots/%s.json".' % botlist[int(user_input)-1])
# Now let's start doing stuff. #
# setup()
copyfile('bot.log', 'bot.old.log')
set_logger()
cred = credentials.Certificate('serviceAccountKey.json')
default_app = firebase_admin.initialize_app(cred, {
'databaseURL': 'https://polaris-bot.firebaseio.com/',
'storageBucket': 'polaris-bot.appspot.com'
})
bots = wait_until_received('bots')
logging.info('Looking for bot configurations in Firebase...')
botlist = get_bots()
try:
for bot in botlist:
logging.info('Initializing [%s] bot...' % bot.name)
bot.start()
while True:
exited = 0
for bot in botlist:
if not bot.started:
exited += 1
sleep(1)
if exited == len(botlist):
logging.info('All bots have exited, finishing.')
break
except KeyboardInterrupt:
for bot in botlist:
logging.debug('Exiting [%s] bot...' % bot.name)
bot.started = False