-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot_runtime.py
More file actions
47 lines (36 loc) · 1.25 KB
/
bot_runtime.py
File metadata and controls
47 lines (36 loc) · 1.25 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
import asyncio
import threading
import time
class BotWrapper:
def __init__(self, bot_cls, name, sentient=True):
self.bot_cls = bot_cls
self.name = name
self.sentient = sentient
self.bot = None
self.mlmcbot = None
self.loop = None
async def init(self):
self.loop = asyncio.get_event_loop()
instance = self.bot_cls(self.name, self.sentient)
self.bot = instance
self.mlmcbot = instance
def start_bot(self):
asyncio.run(self.init())
while True:
time.sleep(0.25)
def create_run_bot(bot_cls):
def run_bot(name, sentient=True):
bot_wrapper = BotWrapper(bot_cls, name, sentient=sentient)
bot_thread = threading.Thread(target=bot_wrapper.start_bot)
bot_thread.start()
return bot_wrapper
return run_bot
def launch_default_bot_group(run_bot, bot_configs, startup_delay=0, startup_message=None):
if startup_message:
print(startup_message)
wrappers = []
for index, config in enumerate(bot_configs):
wrappers.append(run_bot(config["name"], config.get("sentient", True)))
if index < len(bot_configs) - 1 and startup_delay > 0:
time.sleep(startup_delay)
return wrappers