-
Notifications
You must be signed in to change notification settings - Fork 2
/
lotus.py
189 lines (146 loc) · 5.69 KB
/
lotus.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
import importlib
import traceback
from typing import Type, List
from configuration.config import config
from core import manager, variables
from core import lotus_interface
from core.lowlevel import mongo_interface
from entities.bot import Bot
from entities.module_function import ModuleFunction
from exceptions.configuration_error import ConfigurationError
from exceptions.kitsu_exception import KitsuException
from exceptions.telegram_exception import TelegramException
from exceptions.unauthorized import Unauthorized
from logger import log
from utils.bot_utils import is_bot_token
_main_bot = None
_overrideable_module_function = [
ModuleFunction("load_dummies", dict, True),
ModuleFunction("on_new_trigger", bool, True),
ModuleFunction("on_trigger_change", bool, True)
]
def get_maker_bot():
global _main_bot
if _main_bot:
return _main_bot
for bot in variables.attached_bots:
if bot.is_maker:
_main_bot = bot
if _main_bot:
return _main_bot
raise KitsuException("Main bot not found!")
def run(threaded: bool = True, idle: bool = True, auto_attach: bool = True):
log.i(f"Starting Kitsu Ultimate version {config['lotus']['version']}")
_load_modules()
if config["startup"]["drop"]["dialogs"]:
log.i("Dropping dialogs")
mongo_interface.drop_dialogs()
if config["startup"]["drop"]["triggers"]:
log.i("Dropping triggers")
mongo_interface.drop_triggers()
if config["startup"]["drop"]["bots"]:
log.i("Dropping bots")
mongo_interface.drop_bots()
if config["startup"]["drop"]["groups"]:
log.i("Dropping groups")
mongo_interface.drop_groups()
if config["startup"]["drop"]["users"]:
log.i("Dropping users")
mongo_interface.drop_users()
if not threaded:
log.i("Running in webhook mode")
raise NotImplementedError()
if auto_attach:
log.d("Auto attaching bots from mongo")
attach_bots_from_manager()
log.i("Running attached bots")
[lotus_interface.run_bot(bot) for bot in variables.attached_bots]
variables.running = True
if idle:
lotus_interface.idle()
def attach_bot_by_token(token: str):
try:
log.d(f"Attaching bot {token}")
b = Bot(token)
variables.attached_bots.append(b)
log.d(f"Bot {b.bot_id} attached successfully")
return True
except Unauthorized:
log.w(f"Wrong bot token: {token}")
except TelegramException as error:
error_name = error.__class__.__name__
log.e(f"Exception '{error_name}' "
f"while initializing the bot: {error}")
return False
def attach_bots_from_manager():
tokens = manager.get_tokens()
if not tokens:
log.i("Not bots found on database, adding default bot")
maker_token = config["defaults"]["maker"]["token"]
owner_id = config["defaults"]["owner"]["id"]
if not maker_token or not owner_id:
raise ConfigurationError("No bots on database and configuration file has no default token nor owner id")
if not mongo_interface.register_bot(maker_token, owner_id):
log.e("Could not even register default bot, interrupting execution abruptly")
exit(1)
attach_bots(manager.get_tokens())
def attach_bots(tokens: list):
log.d(f"Attaching {len(tokens)} bots")
if not isinstance(tokens, list):
log.w("You must pass a list to attach_bots,"
" operation cancelled.")
else:
for token in tokens:
if is_bot_token(token):
attach_bot_by_token(token)
else:
log.w(f"Invalid token {token} skipping.")
def get_attached_bots() -> List[Bot]:
return variables.attached_bots
# noinspection PyPep8Naming
def _load_modules():
for module in config["modules"]["classes"]:
try:
if "." in module:
class_name = module.split(".")[-1]
module = ".".join(module.split(".")[:-1])
LoadedModule = getattr(importlib.import_module(module), class_name)
log.d(f"Loading module: {module} with class {LoadedModule}")
load_module(LoadedModule)
except Exception as err:
log.e(err)
traceback.print_exc()
def load_module(Module: Type):
module = Module()
for function in _overrideable_module_function:
if not _elab_module_fun(function, module, Module):
log.w(f"Incompatible module, stopping load of {Module.__name__}")
break
def _elab_module_fun(function, instance, Module):
if not hasattr(instance, function.name):
if not function.optional:
log.w(f"{Module.__name__} has no function {function.name}")
return function.optional
real_function = getattr(instance, function.name)
if not callable(real_function):
if not function.optional:
log.w(f"{Module.__name__}.{function.name} is not a function")
return function.optional
if function.name == "on_new_trigger":
variables.on_new_trigger_functions.append(real_function)
log.i("Registered callback")
return True
if function.name == "on_trigger_change":
variables.on_trigger_change_functions.append(real_function)
log.i("Registered callback")
return True
return_value = real_function()
if type(return_value) is not function.type:
log.w(f"{Module.__name__}.{function.name} returns '{type(return_value).__name__}',"
f" '{type(function.type).__name__}' is required")
return False
log_string = function.elaborate_data(return_value)
log.i(f"Module: {log_string}")
return True
if __name__ == "__main__":
run()