-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
125 lines (95 loc) · 3.83 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from asyncio import run, gather, set_event_loop_policy
from client import ClientLarge, ClientSmall, Client
from utils.update_handler import UpdateHandler
from utils.models import BotType, BotData
from utils.log import Logger, LogLevel
from uvloop import EventLoopPolicy
from utils.models import Project
from os.path import exists
from aiofiles import open
from tomllib import loads
from os import walk
set_event_loop_policy(EventLoopPolicy())
try:
from regnalrb import is_compiled as regnalrb_is_compiled
assert regnalrb_is_compiled()
except (ImportError, AssertionError):
print('ERROR: the /reg/nal rust bindings have not been compiled\nplease run `maturin develop -rm regnalrb/Cargo.toml`')
exit()
async def main() -> None:
if not exists('project.toml'):
print('ERROR: project.toml not found, please copy it from project.toml.example and fill it out!')
exit()
async with open('project.toml', 'r') as f:
base_project = loads(await f.read())
auto_logstream_padding: list = (
[base_project['parseable']['logstream']]
if base_project['parseable']['logstream_padding'] == -1
else []
)
log = Logger(
url=base_project['parseable']['base_url'],
logstream=base_project['parseable']['logstream'],
logstream_padding=base_project['parseable']['logstream_padding'],
token=base_project['parseable']['token'],
log_level=LogLevel(base_project['config']['log_level'])
)
await log.logstream_init()
bots: dict[str, Client] = {}
extensions = next(walk('extensions'))[1]
if base_project['developer']['dev_mode']:
extensions = list(
set(extensions) &
set(base_project['developer']['dev_extensions'])
)
bot_dirs = next(walk('bots'))[1]
bot_data_array: dict[str, BotData] = {}
for dir in bot_dirs:
if not exists(f'bots/{dir}/bot.toml'):
log.error(f'bot.toml not found in bots/{dir}')
continue
async with open(f'bots/{dir}/bot.toml', 'r') as f:
bot_data = BotData.model_validate(loads(await f.read()))
bot_data_array.update({dir: bot_data})
if auto_logstream_padding and bot_data.enabled:
auto_logstream_padding.append(bot_data.logstream)
if auto_logstream_padding:
log.logstream_padding = max([len(s) for s in auto_logstream_padding])
for dir, bot_data in bot_data_array.items():
if not bot_data.enabled:
log.info(f'skipping {dir} because it is disabled')
continue
proj = base_project.copy()
proj['bot'] = bot_data
match bot_data.type:
case BotType.LARGE: bots[dir] = ClientLarge(Project.model_validate(proj))
case BotType.SMALL: bots[dir] = ClientSmall(Project.model_validate(proj))
case _: raise ValueError(f'invalid bot type {bot_data.type}')
bots[dir].log.logstream_padding = log.logstream_padding
# ? have to load as extension because python ownership bullshit
bots[dir].load_extension('client.commands')
bots[dir].enabled_extensions = {
extension for extension in extensions
if extension not in bot_data.disabled_extensions
}
for extension in bots[dir].enabled_extensions.copy():
bots[dir].load_extension(f'extensions.{extension}')
if bot_data.custom_extension:
bots[dir].load_extension(f'bots.{dir}')
log.info(f'prepared {dir} for launch')
updater = UpdateHandler(
log,
bots,
base_project,
base_project['config']['github_secret']
)
log.info('starting clients')
await gather(
updater.initialize(),
*[client.start() for client in bots.values()]
)
if __name__ == '__main__':
try:
run(main())
except KeyboardInterrupt:
pass