From 75d5a3c6646639ad73f4bba1fa8bdfddf1bd065e Mon Sep 17 00:00:00 2001 From: Robert Date: Wed, 8 Jan 2020 21:18:17 +0100 Subject: [PATCH] Added help command --- bot.py | 20 +++++++++-- cogs/api/inspirobot.py | 2 +- cogs/api/nasa.py | 2 +- cogs/api/steam.py | 2 +- cogs/api/translation.py | 3 ++ cogs/fun/coinflip.py | 2 +- cogs/help.py | 80 +++++++++++++++++++++++++++++++++++++++++ util/embed.py | 17 +++++++-- 8 files changed, 119 insertions(+), 9 deletions(-) create mode 100644 cogs/help.py diff --git a/bot.py b/bot.py index b31d6b5..f04233c 100644 --- a/bot.py +++ b/bot.py @@ -23,7 +23,7 @@ logging.info(f"Total {total}, Failed {failed}\n") client = commands.Bot(command_prefix=config.settings["prefix"], case_insensitive=True) -@client.command() +@client.command(name="load", description="Loads a cog", usage="load ") @commands.is_owner() async def load(ctx : commands.Context, extension : str): try: @@ -32,7 +32,7 @@ async def load(ctx : commands.Context, extension : str): except: await ctx.message.add_reaction("👎") -@client.command() +@client.command(name="unload", description="Unoads a cog", usage="unload ") @commands.is_owner() async def unload(ctx : commands.Context, extension : str): try: @@ -41,7 +41,7 @@ async def unload(ctx : commands.Context, extension : str): except: await ctx.message.add_reaction("👎") -@client.command() +@client.command(name="reload", description="Reoads a cog", usage="reload ") @commands.is_owner() async def reload(ctx : commands.Context, extension : str): try: @@ -59,6 +59,20 @@ async def on_ready(): total = 0 failed = 0 # Load all cogs on startup + for filename in os.listdir("./cogs"): + if filename.endswith(".py"): + total += 1 + cog = f"cogs.{filename[:-3]}" + + try: + client.load_extension(cog) + logging.info(f"Trying {cog}.....Success!") + except Exception as e: + logging.info(f"Trying {cog}.....Failed!") + logging.error(str(e)) + failed += 1 + + # Load "fun" cogs for filename in os.listdir("./cogs/fun"): if filename.endswith(".py"): diff --git a/cogs/api/inspirobot.py b/cogs/api/inspirobot.py index c533b41..36d2428 100644 --- a/cogs/api/inspirobot.py +++ b/cogs/api/inspirobot.py @@ -14,7 +14,7 @@ class Inspirobot(commands.Cog): def __init__(self, client : discord.Client): self.client = client - @commands.command(name="Inspirobot", description="Sends a randomly generated inspirational quote", aliases=["inspiration", "inspiro"]) + @commands.command(name="inspirobot", description="Sends a randomly generated inspirational quote", usage="inspirobot", aliases=["inspiration", "inspiro"]) @commands.cooldown(1, 5) async def inspirobot(self, ctx : commands.Context): image = inspirobot.get_inspirational_quote() diff --git a/cogs/api/nasa.py b/cogs/api/nasa.py index 4875ef4..0ce4322 100644 --- a/cogs/api/nasa.py +++ b/cogs/api/nasa.py @@ -12,7 +12,7 @@ class Nasa(commands.Cog): def __init__(self, client: discord.Client): self.client = client - @commands.command(name="APOD", description="Posts NASA's picture of the day.") + @commands.command(name="apod", description="Posts NASA's picture of the day.", usage="apod") @commands.cooldown(1, 30) async def apod(self, ctx: commands.Context): url = nasa.image_of_the_day() diff --git a/cogs/api/steam.py b/cogs/api/steam.py index 24a301d..5849076 100644 --- a/cogs/api/steam.py +++ b/cogs/api/steam.py @@ -11,7 +11,7 @@ class Steam(commands.Cog): def __init__(self, client: discord.Client): self.client = client - @commands.command(name="SteamLevel", description="Finds the steam level of a user", usage="SteamLevel ", aliases=["level"]) + @commands.command(name="steamlevel", description="Finds the steam level of a user", usage="steamlevel ", aliases=["level"]) @commands.cooldown(1, 2) async def SteamLevel(self, ctx: commands.Context, vanity_url: str): level = steam.get_steam_level(vanity_url) diff --git a/cogs/api/translation.py b/cogs/api/translation.py index 0fefec1..a380696 100644 --- a/cogs/api/translation.py +++ b/cogs/api/translation.py @@ -19,6 +19,9 @@ class Translation(commands.Cog): return response = translation.translate(text, code) + if len(response) == 0: + await ctx.send(embed=embed.make_error_embed(f"The translation API doesn't support **{language}**.")) + return translated = response[0] direction = response[1].split("-") _from = translation.ISO_to_name(direction[0]) diff --git a/cogs/fun/coinflip.py b/cogs/fun/coinflip.py index 9a510e0..ebc0ea7 100644 --- a/cogs/fun/coinflip.py +++ b/cogs/fun/coinflip.py @@ -12,7 +12,7 @@ class Coinflip(commands.Cog): def __init__(self, client: discord.Client): self.client = client - @commands.command(name="coinflip", description="Flips a coin and reacts with the result", aliases=["coin", "flip"]) + @commands.command(name="coinflip", description="Flips a coin and reacts with the result", usage="coin", aliases=["coin", "flip"]) async def coinflip(self, ctx: commands.Context): if random.randint(0, 1) == 0: await ctx.message.add_reaction("🌑") diff --git a/cogs/help.py b/cogs/help.py new file mode 100644 index 0000000..02ef761 --- /dev/null +++ b/cogs/help.py @@ -0,0 +1,80 @@ +import discord +from discord.ext import commands +from util import config, embed, logging + +class SetupHelp(commands.Cog): + def __init__(self, client: discord.Client): + self.client = client + self._original_help_command = client.help_command + client.help_command = Help() + client.help_command.cog = self + +class Help(commands.MinimalHelpCommand): + async def command_not_found(self, string: str): + await self.context.send(embed=embed.make_error_embed("Command not found")) + + async def subcommand_not_found(self, command, string): + await self.context.send(embed=embed.make_error_embed("Command not found")) + + async def send_cog_help(self, cog): + await self.context.send(embed=embed.make_error_embed("Command not found")) + + async def send_group_help(self, group): + await self.context.send(embed=embed.make_error_embed("Command not found")) + + async def send_command_help(self, command): + try: + alias = "" + if command.aliases != []: + for i in range(len(command.aliases)): + if i == len(command.aliases) - 1: + alias = alias + '`' + command.aliases[i] + '`' + else: + alias = alias + '`' + command.aliases[i] + '`' + ', ' + else: + alias = "`None`" + + await self.context.send(embed=embed.make_embed_fields_ninl(command.name, command.description, ("Usage", f"`{config.settings['prefix']}{command.usage}`"), ("Aliases", alias))) + except Exception as e: + logging.error(str(e)) + await self.context.send(embed=embed.make_error_embed("Command not found")) + + + async def send_bot_help(self, mapping): + # get list of commands + cmds = [] + prefix = config.settings['prefix'] + + for cog, cog_commands in mapping.items(): + cmds = cmds + cog_commands + + newCmds = [] + for item in cmds: + newCmds.append(str(item)) + newCmds = sorted(newCmds) + + finalCmds = [] + for item in newCmds: + try: + finalCmds.append(item) + except: + pass + + cmdString = "" + if len(finalCmds) != 0: + for i in range(len(finalCmds)): + if i == len(finalCmds)-1: + cmdString = cmdString + '`' + finalCmds[i] + '`' + else: + cmdString = cmdString + '`' + finalCmds[i] + '`' + ', ' + + + if cmdString != "": + await self.context.send(embed=embed.make_embed_field('Help', f'To get further information about a command use `{prefix}help `', "Commands", cmdString, inline=False)) + else: + await self.context.send(embed=embed.make_error_embed("No commands found.")) + + + +def setup(client: discord.Client): + client.add_cog(SetupHelp(client)) \ No newline at end of file diff --git a/util/embed.py b/util/embed.py index a665ae0..7fef2f5 100644 --- a/util/embed.py +++ b/util/embed.py @@ -9,7 +9,20 @@ def make_embed(title: str, desc: str) -> discord.Embed: embed = discord.Embed(title=title, description=desc, colour=int(config.settings["color"], 16)) return embed -def make_embed_field(title: str, desc: str, field_name: str, field_val: str) -> discord.Embed: +def make_embed_field(title: str, desc: str, field_name: str, field_val: str, inline: bool = True) -> discord.Embed: embed = discord.Embed(title=title, description=desc, colour=int(config.settings["color"], 16)) - embed.add_field(name=field_name, value=field_val) + embed.add_field(name=field_name, value=field_val, inline=inline) + return embed + +def make_embed_fields(title: str, desc: str, *fields: tuple) -> discord.Embed: + embed = discord.Embed(title=title, description=desc, colour=int(config.settings["color"], 16)) + for name, value in fields: + embed.add_field(name=name, value=value) + return embed + + +def make_embed_fields_ninl(title: str, desc: str, *fields: tuple) -> discord.Embed: + embed = discord.Embed(title=title, description=desc, colour=int(config.settings["color"], 16), inline=False) + for name, value in fields: + embed.add_field(name=name, value=value) return embed \ No newline at end of file