diff options
| author | 2020-05-19 20:21:58 +0300 | |
|---|---|---|
| committer | 2020-05-19 20:21:58 +0300 | |
| commit | c71895c99366f5096442420edde10c70e562a4dd (patch) | |
| tree | 23679427c84e9ecb1dfb1cb3453f850826069795 | |
| parent | Source: Created initial cog layout + setup function (diff) | |
Source: Create converter for source object converting
| -rw-r--r-- | bot/cogs/source.py | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/bot/cogs/source.py b/bot/cogs/source.py index 4897a16e3..76f75f83b 100644 --- a/bot/cogs/source.py +++ b/bot/cogs/source.py @@ -1,10 +1,39 @@ -from discord.ext.commands import Cog +from typing import Union + +from discord.ext.commands import BadArgument, Cog, Context, Converter, Command, HelpCommand from bot.bot import Bot +class SourceConverted(Converter): + """Convert argument to help command, command or Cog.""" + + async def convert(self, ctx: Context, argument: str) -> Union[HelpCommand, Command, Cog]: + """ + Convert argument into source object. + + Order how arguments is checked: + 1. When argument is `help`, return bot help command + 2. When argument is valid command, return this command + 3. When argument is valid Cog, return this Cog + 4. Otherwise raise `BadArgument` error + """ + if argument.lower() == "help": + return ctx.bot.help_command + + command = ctx.bot.get_command(argument) + if command: + return command + + cog = ctx.bot.get_cog(argument) + if cog: + return cog + + raise BadArgument(f"Unable to convert `{argument}` to help command, command or cog.") + + class Source(Cog): - """Cog of Python Discord project source information.""" + """Cog of Python Discord projects source information.""" def __init__(self, bot: Bot): self.bot = bot |