forked from SFTtech/cyberbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta_plugin.py
68 lines (54 loc) · 2.45 KB
/
meta_plugin.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
import asyncio
HELP_DESC = """
!listplugins\t\t-\tlist addable plugins
!addplugin plugin [...]\t-\tadd plugin(s) (--all for all)
!remplugin plugin [...]\t-\tremove plugin(s)
"""
blacklisted = ["help" "meta"]
async def register_to(plugin):
"""
TODO: don't add plugins twice, don't remove meta plugin etc
"""
async def listplugins_callback(room, event):
available = plugin.mroom.bot.available_plugins
pluginlist = ""
for k, v in available.items():
indentet = "\t" + v[:-1].replace("\n", "\n\t") + v[-1]
pluginlist += f"{k}:\n{indentet}\n"
await plugin.send_html(f"""<pre><code>{pluginlist}</pre></code>""")
listplugins_handler = plugin.CommandHandler("listplugins", listplugins_callback)
plugin.add_handler(listplugins_handler)
async def addplugin_callback(room, event):
curr_plugins = [p.pluginname for p in plugin.mroom.plugins]
args = event.source["content"]["body"].split()
plugin.log.info(f"Adding Plugins {args}")
if len(args) > 1 and args[1] == "--all":
await asyncio.gather(
*(
plugin.mroom.add_plugin(pname)
for pname in plugin.mroom.bot.available_plugins
if pname not in curr_plugins
)
)
else:
await asyncio.gather(
*(plugin.mroom.add_plugin(pname) for pname in args[1:])
)
addplugin_handler = plugin.CommandHandler("addplugin", addplugin_callback)
plugin.add_handler(addplugin_handler)
async def remplugin_callback(room, event):
args = plugin.extract_args(event)
torem = list(filter(lambda x: x not in blacklisted, args[1:]))
await asyncio.gather(*(plugin.mroom.remove_plugin(pname) for pname in torem))
remplugin_handler = plugin.CommandHandler("remplugin", remplugin_callback)
plugin.add_handler(remplugin_handler)
# async def reload_callback(room, event):
# # if some plugins are still in the register_to funciton, they will not
# # be stopped :(
# await plugin.mroom.bot.read_plugins() # look for new available plugins
# await asyncio.gather(*(p.stop_all_tasks() for p in plugin.mroom.plugins)) # stop running tasks
# await plugin.mroom.load_plugins()
# # await plugin.send_text("Reloaded Plugins.")
#
# reload_handler = plugin.CommandHandler("reload", reload_callback)
# plugin.add_handler(reload_handler)