From f64007a4d201bdfe5d28869c1ea66d20b1bdfbbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C3=84lgmyr?= Date: Tue, 5 Jan 2021 20:07:37 +0100 Subject: [PATCH] Customize help command --- tle/__main__.py | 6 ++++-- tle/cogs/codeforces.py | 8 ++++---- tle/util/discord_common.py | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/tle/__main__.py b/tle/__main__.py index 6ca3be7e..0ee83dd9 100644 --- a/tle/__main__.py +++ b/tle/__main__.py @@ -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}') diff --git a/tle/cogs/codeforces.py b/tle/cogs/codeforces.py index 1a23e400..1f03b6eb 100644 --- a/tle/cogs/codeforces.py +++ b/tle/cogs/codeforces.py @@ -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) @@ -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) diff --git a/tle/util/discord_common.py b/tle/util/discord_common.py index 9f763868..bb6ba8c5 100644 --- a/tle/util/discord_common.py +++ b/tle/util/discord_common.py @@ -1,7 +1,8 @@ import asyncio -import logging import functools +import logging import random +import re import discord from discord.ext import commands @@ -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) @@ -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()