From ab3fc01559b70856bcdecab92b23f7b2c0e72333 Mon Sep 17 00:00:00 2001 From: Krishnan Shankar Date: Fri, 12 Feb 2021 18:55:04 +0000 Subject: [PATCH 1/8] Added custom help command functionality :)) --- src/cogs/help.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.py | 1 + 2 files changed, 70 insertions(+) create mode 100644 src/cogs/help.py diff --git a/src/cogs/help.py b/src/cogs/help.py new file mode 100644 index 0000000..27342e0 --- /dev/null +++ b/src/cogs/help.py @@ -0,0 +1,69 @@ +from typing import Optional + +from discord.ext import commands +from discord.utils import get +from helpers.helper_funcs import create_embed + + +def syntax(client, command): + cmd_and_aliases = client.command_prefix[0] + "|".join([str(command), *command.aliases]) + params = [] + for key, value in command.params.items(): + if key not in ("self", "context"): + params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>") + params = " ".join(params) + return f"```{cmd_and_aliases} {params}```" + + +class Help(commands.Cog, name="help", description="Get info about the bot and how to use it"): + def __init__(self, client): + self.client = client + self.client.remove_command("help") + self.cogs = [c for c in self.client.cogs.keys()] + + async def help(self, context): + fields = [[f"All `{cog}` Commands:", "".join([f" • `{cmd}`\n" for cmd in self.client.get_cog(cog).get_commands()]).rstrip()] for cog in self.cogs] + embed = create_embed( + "John \"Not a robot\" Peter: Help", + description=self.client.description, + fields=fields + ) + await context.send(embed=embed) + + async def cmd_help(self, client, context, command): + command = get(self.client.commands, name=command) + embed = create_embed("John \"Not a robot\" Peter: Help", description=f"Help with the `{command}` command", fields=[ + ["Syntax", syntax(client, command)], + ["Brief Description", command.brief if command.brief else "None"], + ["Description", command.description if command.description else "None"], + ["Aliases", ", ".join(command.aliases) if command.aliases else "None"] + ]) + await context.send(embed=embed) + + async def cat_help(self, context, category): + embed = create_embed( + "John \"Not a robot\" Peter: Help", + description=f"Help with the `{category}` category", + fields=[ + ["Description:", self.client.get_cog(category).description if self.client.get_cog(category).description else None], + ["Commands", "".join([f" • `{cmd}` - {cmd.brief}\n" for cmd in self.client.get_cog(category).get_commands()]).rstrip()] + ] + ) + await context.send(embed=embed) + + @commands.command(name="help", brief="Get help about the commands and categories of the bot", description="This will allow you to get the descriptions, aliases, and syntax of commands, and the description and list of commands in a category. This command can optionally be used with a command or category name.") + async def show_help(self, context, name: Optional[str]): + self.cogs = [c for c in self.client.cogs.keys()] + if name is None: + await self.help(context) + return + if name in self.cogs: + await self.cat_help(context, name) + if get(self.client.commands, name=name): + await self.cmd_help(self.client, context, name) + else: + await context.send(embed=create_embed("John \"Not a robot\" Peter: Help", description=f"The command `{name}` does not exist!")) + + +def setup(client): + client.add_cog(Help(client)) \ No newline at end of file diff --git a/src/main.py b/src/main.py index 652629b..84aaa69 100644 --- a/src/main.py +++ b/src/main.py @@ -68,6 +68,7 @@ def command_prefix(bot, message): "cogs.badge", "cogs.showcase", "cogs.gold", + "cogs.help" ] loaded_cogs = [] statuses = [ From 425450649a50732998e19dd3ba5601ad75783639 Mon Sep 17 00:00:00 2001 From: Krishnan Shankar Date: Fri, 12 Feb 2021 19:01:22 +0000 Subject: [PATCH 2/8] Added custom help command functionality :DD --- src/cogs/help.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/cogs/help.py b/src/cogs/help.py index 27342e0..7015d27 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -2,8 +2,25 @@ from discord.ext import commands from discord.utils import get -from helpers.helper_funcs import create_embed +import discord +def create_embed(title=None, description=None, author=None, fields=None, image=None, thumbnail=None, color=discord.Color.teal()): + if title: + embed = discord.Embed(title=title, color=color) + else: + embed = discord.Embed(color=color) + if description: + embed.description = description + if author: + embed.set_author(name="For " + author.name, icon_url=author.avatar_url) + if fields: + for field in fields: + embed.add_field(name=field[0], value=field[1], inline=field[2] if len(field) > 2 else False) + if image: + embed.set_image(url=image) + if thumbnail: + embed.set_thumbnail(url=thumbnail) + return embed def syntax(client, command): cmd_and_aliases = client.command_prefix[0] + "|".join([str(command), *command.aliases]) From 25244ba8b88665c7bd3d55233f255f0dc4960e44 Mon Sep 17 00:00:00 2001 From: Krishnan Shankar Date: Fri, 12 Feb 2021 19:18:38 +0000 Subject: [PATCH 3/8] This might help :D Fixes the name issue --- src/cogs/help.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cogs/help.py b/src/cogs/help.py index 7015d27..941ddbb 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -41,7 +41,7 @@ def __init__(self, client): async def help(self, context): fields = [[f"All `{cog}` Commands:", "".join([f" • `{cmd}`\n" for cmd in self.client.get_cog(cog).get_commands()]).rstrip()] for cog in self.cogs] embed = create_embed( - "John \"Not a robot\" Peter: Help", + f"{self.client.user}: Help", description=self.client.description, fields=fields ) @@ -49,7 +49,7 @@ async def help(self, context): async def cmd_help(self, client, context, command): command = get(self.client.commands, name=command) - embed = create_embed("John \"Not a robot\" Peter: Help", description=f"Help with the `{command}` command", fields=[ + embed = create_embed(f"{self.client.user}: Help", description=f"Help with the `{command}` command", fields=[ ["Syntax", syntax(client, command)], ["Brief Description", command.brief if command.brief else "None"], ["Description", command.description if command.description else "None"], @@ -59,7 +59,7 @@ async def cmd_help(self, client, context, command): async def cat_help(self, context, category): embed = create_embed( - "John \"Not a robot\" Peter: Help", + f"{self.client.user}: Help", description=f"Help with the `{category}` category", fields=[ ["Description:", self.client.get_cog(category).description if self.client.get_cog(category).description else None], @@ -79,7 +79,7 @@ async def show_help(self, context, name: Optional[str]): if get(self.client.commands, name=name): await self.cmd_help(self.client, context, name) else: - await context.send(embed=create_embed("John \"Not a robot\" Peter: Help", description=f"The command `{name}` does not exist!")) + await context.send(embed=create_embed(f"{self.client.user}: Help", description=f"The command `{name}` does not exist!")) def setup(client): From 36d2e90b0ceda08511fc942c5c3108236be888bd Mon Sep 17 00:00:00 2001 From: Krishnan Shankar Date: Fri, 12 Feb 2021 19:50:01 +0000 Subject: [PATCH 4/8] A fix? --- src/cogs/help.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cogs/help.py b/src/cogs/help.py index 941ddbb..23a5407 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -39,7 +39,13 @@ def __init__(self, client): self.cogs = [c for c in self.client.cogs.keys()] async def help(self, context): - fields = [[f"All `{cog}` Commands:", "".join([f" • `{cmd}`\n" for cmd in self.client.get_cog(cog).get_commands()]).rstrip()] for cog in self.cogs] + fields = [] + for cog in self.cogs: + cmds = [] + if self.client.get_cog(cog).get_commands(): + for cmd in self.client.get_cog(cog).get_commands(): + cmds.append(f" • `{cmd}`\n") + fields.append([f"All `{cog}` commands:", "".join(cmds).rstrip()]) embed = create_embed( f"{self.client.user}: Help", description=self.client.description, From 9e201d1794e5e8677edb8f71a10569008083cf79 Mon Sep 17 00:00:00 2001 From: Lola Date: Fri, 12 Feb 2021 14:03:22 -0600 Subject: [PATCH 5/8] `context` -> `ctx` to match rest of codebase --- src/cogs/help.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/cogs/help.py b/src/cogs/help.py index 23a5407..a52d3eb 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -1,8 +1,9 @@ from typing import Optional +import discord from discord.ext import commands from discord.utils import get -import discord + def create_embed(title=None, description=None, author=None, fields=None, image=None, thumbnail=None, color=discord.Color.teal()): if title: @@ -38,7 +39,7 @@ def __init__(self, client): self.client.remove_command("help") self.cogs = [c for c in self.client.cogs.keys()] - async def help(self, context): + async def help(self, ctx): fields = [] for cog in self.cogs: cmds = [] @@ -51,9 +52,9 @@ async def help(self, context): description=self.client.description, fields=fields ) - await context.send(embed=embed) + await ctx.send(embed=embed) - async def cmd_help(self, client, context, command): + async def cmd_help(self, client, ctx, command): command = get(self.client.commands, name=command) embed = create_embed(f"{self.client.user}: Help", description=f"Help with the `{command}` command", fields=[ ["Syntax", syntax(client, command)], @@ -61,9 +62,9 @@ async def cmd_help(self, client, context, command): ["Description", command.description if command.description else "None"], ["Aliases", ", ".join(command.aliases) if command.aliases else "None"] ]) - await context.send(embed=embed) + await ctx.send(embed=embed) - async def cat_help(self, context, category): + async def cat_help(self, ctx, category): embed = create_embed( f"{self.client.user}: Help", description=f"Help with the `{category}` category", @@ -72,20 +73,20 @@ async def cat_help(self, context, category): ["Commands", "".join([f" • `{cmd}` - {cmd.brief}\n" for cmd in self.client.get_cog(category).get_commands()]).rstrip()] ] ) - await context.send(embed=embed) + await ctx.send(embed=embed) @commands.command(name="help", brief="Get help about the commands and categories of the bot", description="This will allow you to get the descriptions, aliases, and syntax of commands, and the description and list of commands in a category. This command can optionally be used with a command or category name.") - async def show_help(self, context, name: Optional[str]): + async def show_help(self, ctx, name: Optional[str]): self.cogs = [c for c in self.client.cogs.keys()] if name is None: - await self.help(context) + await self.help(ctx) return if name in self.cogs: - await self.cat_help(context, name) + await self.cat_help(ctx, name) if get(self.client.commands, name=name): - await self.cmd_help(self.client, context, name) + await self.cmd_help(self.client, ctx, name) else: - await context.send(embed=create_embed(f"{self.client.user}: Help", description=f"The command `{name}` does not exist!")) + await ctx.send(embed=create_embed(f"{self.client.user}: Help", description=f"The command `{name}` does not exist!")) def setup(client): From 1a3e0d894087245a18d2de09bb94bd2d660d6dfc Mon Sep 17 00:00:00 2001 From: Krishnan Shankar Date: Fri, 12 Feb 2021 20:13:17 +0000 Subject: [PATCH 6/8] Using classic BotHelpCommand() --- src/cogs/help.py | 82 +++++------------------------------------------- src/main.py | 4 +++ 2 files changed, 12 insertions(+), 74 deletions(-) diff --git a/src/cogs/help.py b/src/cogs/help.py index a52d3eb..0e82add 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -1,11 +1,8 @@ -from typing import Optional - -import discord from discord.ext import commands -from discord.utils import get - +import discord -def create_embed(title=None, description=None, author=None, fields=None, image=None, thumbnail=None, color=discord.Color.teal()): +def create_embed(title=None, description=None, author=None, fields=None, image=None, thumbnail=None, + color=discord.Color.teal()): if title: embed = discord.Embed(title=title, color=color) else: @@ -23,71 +20,8 @@ def create_embed(title=None, description=None, author=None, fields=None, image=N embed.set_thumbnail(url=thumbnail) return embed -def syntax(client, command): - cmd_and_aliases = client.command_prefix[0] + "|".join([str(command), *command.aliases]) - params = [] - for key, value in command.params.items(): - if key not in ("self", "context"): - params.append(f"[{key}]" if "NoneType" in str(value) else f"<{key}>") - params = " ".join(params) - return f"```{cmd_and_aliases} {params}```" - - -class Help(commands.Cog, name="help", description="Get info about the bot and how to use it"): - def __init__(self, client): - self.client = client - self.client.remove_command("help") - self.cogs = [c for c in self.client.cogs.keys()] - - async def help(self, ctx): - fields = [] - for cog in self.cogs: - cmds = [] - if self.client.get_cog(cog).get_commands(): - for cmd in self.client.get_cog(cog).get_commands(): - cmds.append(f" • `{cmd}`\n") - fields.append([f"All `{cog}` commands:", "".join(cmds).rstrip()]) - embed = create_embed( - f"{self.client.user}: Help", - description=self.client.description, - fields=fields - ) - await ctx.send(embed=embed) - - async def cmd_help(self, client, ctx, command): - command = get(self.client.commands, name=command) - embed = create_embed(f"{self.client.user}: Help", description=f"Help with the `{command}` command", fields=[ - ["Syntax", syntax(client, command)], - ["Brief Description", command.brief if command.brief else "None"], - ["Description", command.description if command.description else "None"], - ["Aliases", ", ".join(command.aliases) if command.aliases else "None"] - ]) - await ctx.send(embed=embed) - - async def cat_help(self, ctx, category): - embed = create_embed( - f"{self.client.user}: Help", - description=f"Help with the `{category}` category", - fields=[ - ["Description:", self.client.get_cog(category).description if self.client.get_cog(category).description else None], - ["Commands", "".join([f" • `{cmd}` - {cmd.brief}\n" for cmd in self.client.get_cog(category).get_commands()]).rstrip()] - ] - ) - await ctx.send(embed=embed) - - @commands.command(name="help", brief="Get help about the commands and categories of the bot", description="This will allow you to get the descriptions, aliases, and syntax of commands, and the description and list of commands in a category. This command can optionally be used with a command or category name.") - async def show_help(self, ctx, name: Optional[str]): - self.cogs = [c for c in self.client.cogs.keys()] - if name is None: - await self.help(ctx) - return - if name in self.cogs: - await self.cat_help(ctx, name) - if get(self.client.commands, name=name): - await self.cmd_help(self.client, ctx, name) - else: - await ctx.send(embed=create_embed(f"{self.client.user}: Help", description=f"The command `{name}` does not exist!")) - - -def setup(client): - client.add_cog(Help(client)) \ No newline at end of file +class BotHelpCommand(commands.MinimalHelpCommand): + async def send_pages(self): + destination = self.get_destination() + embed = create_embed("SOBot: Help", description="".join(self.paginator.pages)) + await destination.send(embed=embed) \ No newline at end of file diff --git a/src/main.py b/src/main.py index 84aaa69..0953503 100644 --- a/src/main.py +++ b/src/main.py @@ -14,6 +14,8 @@ from utils.commands import OnlyAllowedInChannels, RequiresVoiceChannel from utils.exceptions import BugReport +from cogs.help import BotHelpCommand + has_bot_started = False logging.basicConfig(level=logging.WARNING) @@ -55,6 +57,8 @@ def command_prefix(bot, message): ) logging.basicConfig(level=logging.INFO) +bot.help_command = BotHelpCommand() + initial_cogs = [ "cogs.team-builder", "cogs.cleverbot", From 7b8ef5a848b8762ff7d091791ddc875a1cd6c464 Mon Sep 17 00:00:00 2001 From: Lola Date: Fri, 12 Feb 2021 16:40:35 -0600 Subject: [PATCH 7/8] don't load help cog, as it's no longer a cog --- src/main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main.py b/src/main.py index 0953503..49361fd 100644 --- a/src/main.py +++ b/src/main.py @@ -11,11 +11,10 @@ from discord.ext.commands import MissingAnyRole, BadArgument, ExpectedClosingQuoteError, CommandInvokeError from raygun4py import raygunprovider +from cogs.help import BotHelpCommand from utils.commands import OnlyAllowedInChannels, RequiresVoiceChannel from utils.exceptions import BugReport -from cogs.help import BotHelpCommand - has_bot_started = False logging.basicConfig(level=logging.WARNING) @@ -72,7 +71,6 @@ def command_prefix(bot, message): "cogs.badge", "cogs.showcase", "cogs.gold", - "cogs.help" ] loaded_cogs = [] statuses = [ From 2ff52a7a3c10fc2c7e648d7a3510d54f2eafa6b4 Mon Sep 17 00:00:00 2001 From: Lola Date: Fri, 12 Feb 2021 16:44:43 -0600 Subject: [PATCH 8/8] 'SOBot' -> 'John Peter' --- src/cogs/help.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cogs/help.py b/src/cogs/help.py index 0e82add..23db1ae 100644 --- a/src/cogs/help.py +++ b/src/cogs/help.py @@ -1,5 +1,6 @@ -from discord.ext import commands import discord +from discord.ext import commands + def create_embed(title=None, description=None, author=None, fields=None, image=None, thumbnail=None, color=discord.Color.teal()): @@ -23,5 +24,5 @@ def create_embed(title=None, description=None, author=None, fields=None, image=N class BotHelpCommand(commands.MinimalHelpCommand): async def send_pages(self): destination = self.get_destination() - embed = create_embed("SOBot: Help", description="".join(self.paginator.pages)) + embed = create_embed("John Peter: Help", description="".join(self.paginator.pages)) await destination.send(embed=embed) \ No newline at end of file