Skip to content

Commit

Permalink
feat: added page-ified help command
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyvy-vi committed Dec 10, 2020
1 parent 37462c0 commit fd4a28f
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ venv.bak/
.mypy_cache/
.dmypy.json
dmypy.json

.vscode
# Pyre type checker
.pyre/
# TOKEN
Expand Down
3 changes: 2 additions & 1 deletion src/bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def __init__(self):
def setup(self):
for cog in COGS:
self.load_extension(f'src.cogs.{cog}')
# self.load_extension('jishaku')
print(' Setup Complete')

def run(self, version):
Expand Down Expand Up @@ -91,7 +92,7 @@ async def on_error(self, err, *args, **kwargs):
await args[0].send("Something went wrong")

await self.stdout.send(f"```An Error Occured in {self.guild} -\n{err}```")
raise
raise err

async def on_command_error(self, ctx, exc):
if any([isinstance(exc, error) for error in IGNORE_EXCEPTIONS]):
Expand Down
2 changes: 1 addition & 1 deletion src/cogs/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async def roll_dice(self, ctx, die_string: str):
await ctx.send("ERROR- Can't roll that many die. Try again later")

@command(name='slap', aliases=['hit'])
async def slap_member(self, ctx, member: Member, *, reason: Optional[str] = "no reason"):
async def slap_member(self, ctx, member: Member, *, reason: Optional[str]):
await ctx.send(f'{ctx.author.display_name} slapped {member.mention} for {reason}')

@slap_member.error
Expand Down
66 changes: 63 additions & 3 deletions src/cogs/help.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,79 @@
from discord.utils import get
from discord.ext.commands import Cog
from discord.ext.commands import command
from discord.ext.menus import MenuPages, ListPageSource

from discord import Embed, Color
from typing import Optional


def syntax(command):
cmd_and_aliases = "|".join([str(command), *command.aliases])
params = []

for key, value in command.params.items():
if key not in ("self", "ctx"):
params.append(f"[{key}]" if "Optional" in str(value) else f"<{key}>")
params = " ".join(params)
return f"```{cmd_and_aliases} {params}```"


class HelpMenu(ListPageSource):
def __init__(self, ctx, data):
self.ctx = ctx

super().__init__(data, per_page=3)

async def write_page(self, menu, fields=[]):
offset = (menu.current_page * self.per_page) + 1
len_data = len(self.entries)

embed = Embed(title="Help",
description="This is a help embed",
color=self.ctx.author.colour)
embed.set_thumbnail(url=self.ctx.guild.me.avatar_url)
embed.set_footer(text=f"{offset:,} - {min(len_data, offset*self.per_page-1):,} of {len_data:,} commands.")

for name, value in fields:
embed.add_field(name=name, value=value, inline=False)

return embed

async def format_page(self, menu, entries):
fields = []

for entry in entries:
fields.append((entry.brief or "No Description", syntax(entry)))

return await self.write_page(menu, fields)


class Help(Cog):
def __init__(self, bot):
self.bot = bot
self.bot.remove_command('help')

@command(name="Help")
async def cmd_help(self, ctx, command):
embed = Embed(title=f'Help on `{command}`',
description=syntax(command),
color=Color.green())
embed.add_field(name='Command description', value=command.help)
await ctx.send(embed=embed)

@command(name="help")
async def show_help(self, ctx, cmd: Optional[str]):
"""Shows this help"""
if cmd is None:
pass
menu = MenuPages(source=HelpMenu(ctx, list(self.bot.commands)),
clear_reactions_after=True,
timeout=60.0)
await menu.start(ctx)

else:
pass
if (command := get(self.bot.commands, name=cmd)):
await self.cmd_help(ctx, command)
else:
await ctx.send("That command doesn't exist...")

@Cog.listener()
async def on_ready(self):
Expand Down

0 comments on commit fd4a28f

Please sign in to comment.