Skip to content

Commit

Permalink
refactor help system
Browse files Browse the repository at this point in the history
  • Loading branch information
ClemenPine committed Jan 28, 2024
1 parent 9c75aa8 commit 447a010
Showing 1 changed file with 48 additions and 26 deletions.
74 changes: 48 additions & 26 deletions cmds/help.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
import os
import glob
from discord import Message
from importlib import import_module
from more_itertools import windowed
from more_itertools import divide

from util import parser

WIDTH = 6
INDENT = ' '

def exec(message: Message):
lines = ['Usage: `!cmini (command) [args]`']
lines.append('```')

with os.scandir('cmds') as it:
entries = sorted(
[entry for entry in it
if not entry.is_symlink() and
entry.name.endswith('.py')],
key=lambda x:x.name)
command = parser.get_arg(message)
commands = sorted(x.replace('/', '.')[5:-3] for x in glob.glob('cmds/*.py'))

if command:
if not command in commands:
return f"Unknown command `{command}`"

for entry in entries:
file = entry.name
mod = import_module(f'cmds.{file[:-3]}')
mod = import_module(f'cmds.{command}')

if all(hasattr(mod, func) for func in ['exec', 'desc', 'use']):
if hasattr(mod, 'use'):
use = mod.use()
desc = mod.desc().split()

lines.append(use)
lines += [
INDENT + ' '.join(x) for x in windowed(
desc, n=WIDTH, step=WIDTH, fillvalue=''
)
]

lines.append('```')

return '\n'.join(lines)
else:
use = f"{command} [args]"

if hasattr(mod, 'desc'):
desc = mod.desc()
else:
desc = "..."

return (
f'Help page for `{command}`:'
f'```\n'
f'{use}\n'
f'{desc}\n'
f'```'
)

else:
cmds = []
for cmd in commands:
mod = import_module(f'cmds.{cmd}')

if not all(hasattr(mod, x) for x in ['exec', 'desc', 'use']):
continue

cmds.append(cmd)

lines = ['Usage: `!cmini (command) [args]`']
lines.append('```')

cols = divide(2, cmds)

for row in zip(*cols):
lines.append("".join(x.ljust(16) for x in row))

lines.append('```')
return '\n'.join(lines)

0 comments on commit 447a010

Please sign in to comment.