Skip to content

Commit

Permalink
simplify command help by shoving everything in Command objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick80835 committed Sep 15, 2020
1 parent 8f70e23 commit 8551503
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
3 changes: 3 additions & 0 deletions ubot/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

class Command:
def __init__(self, func: FunctionType, args: dict):
self.module = func.__module__.split(".")[-1]
self.function = func

self.pattern = args.get("pattern")
self.simple_pattern = args.get("simple_pattern", None)
self.raw_pattern = args.get("raw_pattern", None)
self.pattern_extra = args.get("pattern_extra", "")
self.extra = args.get("extra", None)
self.help = args.get('help', None)
2 changes: 1 addition & 1 deletion ubot/command_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ async def handle_outgoing(self, event):
try:
await command.function(event)
except Exception as exception:
await event.reply(f"`An error occurred in {command.command.__name__}: {exception}`")
await event.reply(f"`An error occurred in {command.function.__name__}: {exception}`")
raise exception
17 changes: 0 additions & 17 deletions ubot/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class Loader():
aioclient = ClientSession()
thread_pool = ThreadPoolExecutor()

help_dict = {}
loaded_modules = []
all_modules = []

Expand All @@ -38,7 +37,6 @@ def load_all_modules(self):

def reload_all_modules(self):
self.command_handler.outgoing_commands = []
self.help_dict = {}

errors = ""

Expand All @@ -55,11 +53,6 @@ def reload_all_modules(self):

def add(self, pattern: str = None, **args):
def decorator(func):
if func.__module__.split(".")[-1] in self.help_dict:
self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]]
else:
self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]]

args["pattern"] = args.get("pattern", pattern)
self.command_handler.outgoing_commands.append(Command(func, args))

Expand All @@ -72,11 +65,6 @@ def add_list(self, pattern: list = None, **args):

def decorator(func):
for pattern in pattern_list:
if func.__module__.split(".")[-1] in self.help_dict:
self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]]
else:
self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]]

this_args = args.copy()
this_args["pattern"] = pattern
self.command_handler.outgoing_commands.append(Command(func, this_args))
Expand All @@ -90,11 +78,6 @@ def add_dict(self, pattern: dict = None, **args):

def decorator(func):
for pattern, extra in pattern_dict.items():
if func.__module__.split(".")[-1] in self.help_dict:
self.help_dict[func.__module__.split(".")[-1]] += [[pattern, args.get('help', None)]]
else:
self.help_dict[func.__module__.split(".")[-1]] = [[pattern, args.get('help', None)]]

this_args = args.copy()
this_args["pattern"] = pattern
this_args["extra"] = args.get('extra', extra)
Expand Down
31 changes: 16 additions & 15 deletions ubot/modules/_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@ async def reload_modules(event):
@ldr.add("help")
async def help_cmd(event):
if event.args:
for key, value in ldr.help_dict.items():
for info in value:
if event.args == info[0]:
if info[1]:
await event.edit(f"Help for **{info[0]}**: __{info[1]}__")
return

await event.edit(f"**{info[0]}** doesn't have a help string.")
for command in ldr.command_handler.outgoing_commands:
if event.args == command.pattern:
if command.help:
await event.edit(f"Help for **{command.pattern}**: __{command.help}__")
return

help_string = ""
await event.edit(f"**{command.pattern}** doesn't have a help string.")
return

for key, value in ldr.help_dict.items():
help_string += f"\n**{key}**: "
for info in value:
help_string += f"{info[0]}, "
help_string = help_string.rstrip(", ")
help_dict = {}

await event.edit(f"**Available commands:**\n{help_string}")
for command in ldr.command_handler.outgoing_commands:
if command.module in help_dict:
help_dict[command.module].append(command.pattern)
else:
help_dict[command.module] = [command.pattern]

help_string = "\n".join([f"**{module}**: {', '.join(pattern_list)}" for module, pattern_list in help_dict.items()])

await event.edit(f"**Available commands:**\n\n{help_string}")


@ldr.add("sysd")
Expand Down

0 comments on commit 8551503

Please sign in to comment.