Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help format #427

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tle/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ def main():
constants.ALLOW_DUEL_SELF_REGISTER = bool(distutils.util.strtobool(allow_self_register))

setup()

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix=commands.when_mentioned_or(';'), intents=intents)
bot = commands.Bot(command_prefix=commands.when_mentioned_or(';'),
intents=intents,
help_command=discord_common.TLEHelpCommand())
cogs = [file.stem for file in Path('tle', 'cogs').glob('*.py')]
for extension in cogs:
bot.load_extension(f'tle.cogs.{extension}')
Expand Down
8 changes: 4 additions & 4 deletions tle/cogs/codeforces.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ async def _gitgud(self, ctx, handle, problem, delta):
@commands.command(brief='Upsolve a problem')
@cf_common.user_guard(group='gitgud')
async def upsolve(self, ctx, choice: int = -1):
"""Request an unsolved problem from a contest you participated in
delta | -300 | -200 | -100 | 0 | +100 | +200 | +300
"""Request an unsolved problem from a contest you participated in\\
delta | -300 | -200 | -100 | 0 | +100 | +200 | +300\\
points | 2 | 3 | 5 | 8 | 12 | 17 | 23
"""
await self._validate_gitgud_status(ctx,delta=None)
Expand Down Expand Up @@ -219,8 +219,8 @@ async def mashup(self, ctx, *args):
@commands.command(brief='Challenge')
@cf_common.user_guard(group='gitgud')
async def gitgud(self, ctx, delta: int = 0):
"""Request a problem for gitgud points.
delta | -300 | -200 | -100 | 0 | +100 | +200 | +300
"""Request a problem for gitgud points.\\
delta | -300 | -200 | -100 | 0 | +100 | +200 | +300\\
points | 2 | 3 | 5 | 8 | 12 | 17 | 23
"""
await self._validate_gitgud_status(ctx, delta)
Expand Down
32 changes: 30 additions & 2 deletions tle/util/discord_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import logging
import functools
import logging
import random
import re

import discord
from discord.ext import commands
Expand All @@ -16,7 +17,6 @@
_SUCCESS_GREEN = 0x28A745
_ALERT_AMBER = 0xFFBF00


def embed_neutral(desc, color=discord.Embed.Empty):
return discord.Embed(description=str(desc), color=color)

Expand Down Expand Up @@ -129,3 +129,31 @@ async def presence_task(_):

presence_task.start()

class TLEHelpCommand(commands.help.DefaultHelpCommand):
@staticmethod
def _reformat(doc):
return '\n\n'.join(
re.sub(r'(?<=[^\\])\n', ' ', paragraph)
for paragraph in doc.split('\n\n')).replace('\\', '')

def add_command_formatting(self, command):
"""Copy of the default method, with added reformatting"""

if command.description:
self.paginator.add_line(command.description, empty=True)

signature = self.get_command_signature(command)
if command.aliases:
self.paginator.add_line(signature)
self.add_aliases_formatting(command.aliases)
else:
self.paginator.add_line(signature, empty=True)

if command.help:
try:
new_help = TLEHelpCommand._reformat(command.help)
self.paginator.add_line(new_help, empty=True)
except RuntimeError:
for line in command.help.splitlines():
self.paginator.add_line(line)
self.paginator.add_line()
Comment on lines +142 to +159
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a bit fragile to depend on the internal implementation, don't you think? 🤔
What if we instead have a decorator on the methods that update the docstring? Or if we don't want to be that granular, we could update command.help for each command in the bot after we have added the cogs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it that fragile? We could fork the thing if that makes you happier. This just felt like the minimal effort route, and I doubt the api of the DefaultHelpCommand would change drastically. What internal implementation do I actually depend on?

What if we instead have a decorator on the methods that update the docstring?

What do you mean exactly?

Or if we don't want to be that granular, we could update command.help for each command in the bot after we have added the cogs.

That feels hackier than what I did here tbh.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What internal implementation do I actually depend on?

That was a bad choice of words, I meant that a piece of logic is copied from the default impl. Also the API will probably not change so this may be fine 🤷

What if we instead have a decorator on the methods that update the docstring?

What do you mean exactly?

def reformat_help(func):
    func.__doc__ = _reformat(func.__doc__)
    return func

Or if we don't want to be that granular, we could update command.help for each command in the bot after we have added the cogs.

That feels hackier than what I did here tbh.

Why though :waturr:
This is just the line new_help = TLEHelpCommand._reformat(command.help) from the PR.

for command in bot.walk_commands():
    command.help = _reformat(command.help)

I probably like the last option the most. What do you think? 👀