Skip to content

Commit

Permalink
Customize help command
Browse files Browse the repository at this point in the history
  • Loading branch information
algmyr committed Jan 5, 2021
1 parent 8ed3840 commit f64007a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
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()

0 comments on commit f64007a

Please sign in to comment.