Skip to content

Commit

Permalink
Add has_attachment built in check
Browse files Browse the repository at this point in the history
  • Loading branch information
tandemdude committed Jun 5, 2021
1 parent 8ed0da1 commit b267726
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
47 changes: 47 additions & 0 deletions lightbulb/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"bot_has_guild_permissions",
"has_permissions",
"bot_has_permissions",
"has_attachment",
"check",
]

Expand Down Expand Up @@ -234,6 +235,22 @@ async def _bot_has_permissions(ctx: context.Context, *, permissions: hikari.Perm
return True


async def _has_attachment(
ctx: context.Context,
*,
allowed_extensions: typing.Optional[typing.Sequence[str]] = None
) -> bool:
if not ctx.attachments:
raise errors.MissingRequiredAttachment("Missing attachment(s) required to run the command")

if allowed_extensions is not None:
for attachment in ctx.attachments:
if not any(attachment.filename.endswith(ext) for ext in allowed_extensions):
raise errors.MissingRequiredAttachment("Missing attachment(s) required to run the command")

return True


def guild_only() -> typing.Callable[[T_inv], T_inv]:
"""
A decorator that prevents a command from being used in direct messages.
Expand Down Expand Up @@ -484,6 +501,36 @@ def decorate(command: T_inv) -> T_inv:
return decorate


def has_attachment(*extensions: str):
"""
A decorator that prevents the command from being used if the invocation message
does not include any attachments.
Args:
*extensions (:obj:`str`): If specified, attachments with a different file extension
will cause the check to fail.
Example:
.. code-block:: python
@checks.has_attachment(".yaml", ".json")
@bot.command()
async def foobar(ctx):
print(ctx.attachments[0].filename)
Note:
If ``extensions`` is specified then all attachments must conform to the restriction.
"""

def decorate(command: T_inv) -> T_inv:
_check_check_decorator_above_commands_decorator(command)
command.add_check(functools.partial(_has_attachment, allowed_extensions=extensions if extensions else None))
return command

return decorate


def check(check_func: typing.Callable[[context.Context], typing.Coroutine[typing.Any, typing.Any, bool]]):
"""
A decorator which adds a custom check function to a command. The check function must be a coroutine (async def)
Expand Down
11 changes: 11 additions & 0 deletions lightbulb/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,17 @@ def __init__(self, text: str, permissions: hikari.Permissions) -> None:
"""Permission(s) the bot is missing."""


class MissingRequiredAttachment(CheckFailure):
"""
Error raised when the command invocation message is missing an attachment, or an
attachment with the correct file extension.
"""

def __init__(self, text) -> None:
self.text: str = text
"""The error text."""


class CommandInvocationError(CommandError):
"""
Error raised if an error is encountered during command invocation. This will only be raised
Expand Down

0 comments on commit b267726

Please sign in to comment.