Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement Invokable & BaseContext #1606

Closed
wants to merge 83 commits into from
Closed
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
352446b
Initial Implementation of Invokables
Middledot Aug 26, 2022
0cbe0e7
Integrate invokables into app commands
Middledot Aug 27, 2022
915534e
Merge branch 'master' into invokable
Middledot Aug 27, 2022
70ed7d5
Make fully working with app commands
Middledot Aug 27, 2022
2fcd736
Spelling is close enough
Middledot Aug 27, 2022
c2a41e4
Integrate ext.commands + typehinting fixes
Middledot Aug 27, 2022
c15d97a
New baseline methods
Middledot Aug 27, 2022
3ce64b7
Some cleanup before standard context impl
Middledot Aug 27, 2022
1ea0342
Remove some unused imports and vars
Middledot Aug 29, 2022
633b987
Implement BaseContext
Middledot Aug 29, 2022
4d67d64
Finish integrating BaseContext
Middledot Aug 29, 2022
f9b69be
Remove test file
Middledot Aug 29, 2022
cf1aee8
Move parents and root_parent
Middledot Aug 29, 2022
63e230e
Implement reinvoke (ctx & cmd) and extra ctx attrs
Middledot Aug 29, 2022
36085e3
Some Runtime fixes
Middledot Aug 29, 2022
07247a3
Last minute fixes
Middledot Aug 29, 2022
b4b6fdd
Make docs great again
Middledot Aug 29, 2022
b4aed1c
Merge from master
Middledot Aug 29, 2022
cffb506
revert: the unfix
Middledot Aug 29, 2022
ae466b5
refactor: remove parent checks reference
Middledot Aug 29, 2022
8751a22
fix: p*
Middledot Aug 29, 2022
2f3d00b
refactor: remove redundant defintions
Middledot Aug 29, 2022
731864e
fix: error hierarchy and docs
Middledot Aug 29, 2022
6a988a4
Merge branch 'master' into invokable
Middledot Aug 29, 2022
23bdc50
Smaller fixes
Middledot Aug 29, 2022
5230946
Uncommit unrelated things
Middledot Aug 30, 2022
62077e8
Make more concise imports
Middledot Aug 30, 2022
4175066
Merge branch 'master' into invokable
Middledot Sep 10, 2022
810fac3
Merge branch 'master' into invokable
Middledot Oct 1, 2022
048d5e2
fix: more cog-setting errors
Middledot Oct 1, 2022
098fc0b
chore: use more generic cog typehints
Middledot Oct 1, 2022
a5cc699
Merge branch 'master' into invokable
Middledot Oct 15, 2022
4fe2c68
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 15, 2022
87aded3
Merge branch 'master' into invokable
Lulalaby Oct 16, 2022
ab89462
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 16, 2022
da6a19c
Merge branch 'master' into invokable
BobDotCom Oct 16, 2022
99aac98
Merge branch 'master' into invokable
BobDotCom Oct 17, 2022
d8b03ee
Fix typehints
BobDotCom Oct 17, 2022
3fd4d08
Fix more typehints
BobDotCom Oct 17, 2022
88c90ca
fix(commands): apply suggestions
Middledot Oct 25, 2022
b413d44
Merge https://github.com/Pycord-Development/pycord into invokable
Middledot Nov 12, 2022
656cf37
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 12, 2022
c495966
refactor(commands): the other function rename
Middledot Nov 12, 2022
d1af15e
Merge branch 'invokable' of https://github.com/Middledot/pycord into …
Middledot Nov 12, 2022
f7de4b4
Merge branch 'master' into invokable
BobDotCom Nov 14, 2022
ccc61b7
refactor(commands): avoid 'type: ignore'
Middledot Nov 15, 2022
36e9a9a
fix(ext.commands): add missing import
Middledot Nov 15, 2022
2110284
fix(commands): move typehints
Middledot Nov 15, 2022
3f0c906
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 15, 2022
20d255c
chore(workflows): update list of allowed words
Middledot Nov 15, 2022
8dcc5d6
Merge branch 'invokable' of https://github.com/Middledot/pycord into …
Middledot Nov 15, 2022
b51552a
fix(commands): move typehints again
Middledot Nov 15, 2022
e1e78ab
Merge branch 'master' into invokable
BobDotCom Nov 15, 2022
4a8e38e
fix(commands, ext.commands): apply suggestions
Middledot Dec 18, 2022
3969c05
Merge branch 'master' into invokable
Middledot Dec 18, 2022
00acc4e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 18, 2022
d88d03f
Merge branch 'master' into invokable
Lulalaby Jan 5, 2023
a035f93
Merge branch 'master' into invokable
VincentRPS Mar 17, 2023
57064b0
Merge branch 'master' into invokable
plun1331 Apr 3, 2023
ddb0c74
Merge branch 'master' into invokable
Lulalaby Apr 3, 2023
a5f81fe
Merge branch 'master' into invokable
Lulalaby Apr 18, 2023
ca20e63
Merge branch 'master' into invokable
Middledot Apr 29, 2023
791aef4
fix(commands): silly error where enabled is False
Middledot Apr 29, 2023
c0ea4e1
fix(commands): `.help` is text exclusive
Middledot Apr 29, 2023
9141c45
chore(commands): efficientify the copy func
Middledot Apr 29, 2023
85abb41
chore(errors): deprecate ACInvokeError and ACError
Middledot Apr 29, 2023
d8bf4bc
chore(commands): add last dispatch_error changes
Middledot Apr 29, 2023
9c90f4a
Merge branch 'master' into invokable
Lulalaby May 8, 2023
e4484a2
Merge branch 'master' into invokable
Middledot Jun 10, 2023
ce44ccb
fix(cmds): fix 3 bugs
Middledot Jun 11, 2023
75a4709
Merge branch 'master' into invokable
Middledot Jun 11, 2023
45c2e96
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 11, 2023
d9d536c
chore(*): cleanup and changelog
Middledot Jun 27, 2023
ef8da9a
Merge branch 'master' into invokable
Middledot Jun 27, 2023
3645e79
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 27, 2023
e115305
fix(commands): missing things (@error & cooldowns)
Middledot Jul 9, 2023
0ac4b47
style(pre-commit): auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jul 9, 2023
6d369f9
Merge branch 'master' into invokable
Middledot Jul 9, 2023
c4154a1
Merge branch 'master' into invokable
Middledot Jul 14, 2023
2ca481b
Merge branch 'master' into invokable
Middledot Aug 12, 2023
931db35
Merge branch 'master' into invokable
Middledot Oct 29, 2023
d899980
Merge branch 'master' into invokable
Middledot Nov 6, 2023
4621811
Merge branch 'master' into invokable
Middledot Jan 16, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements/dev.txt
- run:
codespell --ignore-words-list="groupt,nd,ot,ro,falsy,BU" \
codespell --ignore-words-list="groupt,nd,ot,ro,falsy,BU,invokable" \
--exclude-file=".github/workflows/codespell.yml"
bandit:
runs-on: ubuntu-latest
Expand Down
26 changes: 26 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ These changes are available on the `master` branch, but have not yet been releas
- Added `suppress` and `allowed_mentions` parameters to `Webhook` and
`InteractionResponse` edit methods.
([#2138](https://github.com/Pycord-Development/pycord/pull/2138))
- Added `_parse_arguments` function to slash commands to parse arguments instead of in
`prepare`. ([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Added the `parents`, `root_parent`, and `cog_name` attribute to slash commands.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Added the `error` decorator to slash commands.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Added the `cooldown_after_parsing` parameter & attribute to slash commands.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Added the `reinvoke` function to `ApplicationContext`.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Added the `invoked_with`, `invoked_parents`, `invoked_subcommand`,
`subcommand_passed`, and `command_failed` parameters & attributes to
`ApplicationContext`.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Added the `source` attribute to `ext.commands.Context` & `ApplicationContext` for a
common way to either retrieve the message or the interaction that triggered the
command. ([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Documented `ContextMenuCommand`.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))

### Changed

Expand All @@ -93,6 +112,13 @@ These changes are available on the `master` branch, but have not yet been releas
([#2087](https://github.com/Pycord-Development/pycord/pull/2087))
- Typehinted `command_prefix` and `help_command` arguments properly.
([#2099](https://github.com/Pycord-Development/pycord/pull/2099))
- Renamed `_invoke` in application commands to `invoke`.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Deprecated `ext/commands/cooldowns` in favour of `commands/cooldowns`.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))
- Deprecated `ApplicationCommandError` & `ApplicationCommandInvokeError` in favour of
`CommandError` & `CommandInvokeError` respectively.
([#1606](https://github.com/Pycord-Development/pycord/pull/1606))

### Removed

Expand Down
19 changes: 10 additions & 9 deletions discord/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
ApplicationCommand,
ApplicationContext,
AutocompleteContext,
BaseContext,
MessageCommand,
SlashCommand,
SlashCommandGroup,
Expand Down Expand Up @@ -862,8 +863,9 @@ async def process_application_commands(
)

ctx = await self.get_application_context(interaction)
if command:
if command and not ctx.command:
ctx.command = command

await self.invoke_application_command(ctx)

async def on_application_command_auto_complete(
Expand Down Expand Up @@ -1212,7 +1214,6 @@ async def on_application_command_error(
)

# global check registration
# TODO: Remove these from commands.Bot

def check(self, func):
"""A decorator that adds a global check to the bot. A global check is similar to a :func:`.check` that is
Expand All @@ -1222,8 +1223,8 @@ def check(self, func):
.. note::

This function can either be a regular function or a coroutine. Similar to a command :func:`.check`, this
takes a single parameter of type :class:`.Context` and can only raise exceptions inherited from
:exc:`.ApplicationCommandError`.
takes a single parameter of type :class:`.BaseContext` and can only raise exceptions inherited from
:exc:`.CommandError`.

Example
-------
Expand Down Expand Up @@ -1282,14 +1283,14 @@ def check_once(self, func):

.. note::

When using this function the :class:`.Context` sent to a group subcommand may only parse the parent command
When using this function the :class:`.BaseContext` sent to a group subcommand may only parse the parent command
and not the subcommands due to it being invoked once per :meth:`.Bot.invoke` call.

.. note::

This function can either be a regular function or a coroutine. Similar to a command :func:`.check`,
this takes a single parameter of type :class:`.Context` and can only raise exceptions inherited from
:exc:`.ApplicationCommandError`.
this takes a single parameter of type :class:`.BaseContext` and can only raise exceptions inherited from
:exc:`.CommandError`.

Example
-------
Expand Down Expand Up @@ -1318,7 +1319,7 @@ def before_invoke(self, coro):
A pre-invoke hook is called directly before the command is
called. This makes it a useful function to set up database
connections or any type of set up required.
This pre-invoke hook takes a sole parameter, a :class:`.Context`.
This pre-invoke hook takes a sole parameter, a :class:`.BaseContext`.

.. note::

Expand Down Expand Up @@ -1348,7 +1349,7 @@ def after_invoke(self, coro):
A post-invoke hook is called directly after the command is
called. This makes it a useful function to clean-up database
connections or any type of clean up required.
This post-invoke hook takes a sole parameter, a :class:`.Context`.
This post-invoke hook takes a sole parameter, a :class:`.BaseContext`.

.. note::

Expand Down
47 changes: 24 additions & 23 deletions discord/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
from . import errors
from .commands import (
ApplicationCommand,
ApplicationContext,
BaseContext,
Invokable,
SlashCommandGroup,
_BaseCommand,
)
Expand Down Expand Up @@ -230,7 +231,7 @@ def __new__(cls: type[CogMeta], *args: Any, **kwargs: Any) -> CogMeta:

# Either update the command with the cog provided defaults or copy it.
# r.e type ignore, type-checker complains about overriding a ClassVar
new_cls.__cog_commands__ = tuple(c._update_copy(cmd_attrs) if not hasattr(c, "add_to") else c for c in new_cls.__cog_commands__) # type: ignore
new_cls.__cog_commands__ = tuple(c.copy(cmd_attrs) if not hasattr(c, "add_to") else c for c in new_cls.__cog_commands__) # type: ignore

name_filter = lambda c: (
"app"
Expand Down Expand Up @@ -313,7 +314,7 @@ def __new__(cls: type[CogT], *args: Any, **kwargs: Any) -> CogT:
# To do this, we need to interfere with the Cog creation process.
return super().__new__(cls)

def get_commands(self) -> list[ApplicationCommand]:
def get_commands(self) -> list[Invokable]:
r"""
Returns
--------
Expand Down Expand Up @@ -345,12 +346,12 @@ def description(self) -> str:
def description(self, description: str) -> None:
self.__cog_description__ = description

def walk_commands(self) -> Generator[ApplicationCommand, None, None]:
def walk_commands(self) -> Generator[Invokable, None, None]:
"""An iterator that recursively walks through this cog's commands and subcommands.

Yields
------
Union[:class:`.Command`, :class:`.Group`]
Union[:class:`.Invokable`]
A command or group from the cog.
"""
for command in self.__cog_commands__:
Expand Down Expand Up @@ -440,7 +441,7 @@ def cog_unload(self) -> None:
"""

@_cog_special_method
def bot_check_once(self, ctx: ApplicationContext) -> bool:
def bot_check_once(self, ctx: BaseContext) -> bool:
"""A special method that registers as a :meth:`.Bot.check_once`
check.

Expand All @@ -449,45 +450,45 @@ def bot_check_once(self, ctx: ApplicationContext) -> bool:

Parameters
----------
ctx: :class:`.Context`
ctx: :class:`.BaseContext`
The invocation context.
"""
return True

@_cog_special_method
def bot_check(self, ctx: ApplicationContext) -> bool:
def bot_check(self, ctx: BaseContext) -> bool:
"""A special method that registers as a :meth:`.Bot.check`
check.

This function **can** be a coroutine and must take a sole parameter,
``ctx``, to represent the :class:`.Context` or :class:`.ApplicationContext`.
``ctx``, to represent a subclass of :class:`BaseContext` (either :class:`.Context`
or :class:`.ApplicationContext`).

Parameters
----------
ctx: :class:`.Context`
ctx: :class:`.BaseContext`
The invocation context.
"""
return True

@_cog_special_method
def cog_check(self, ctx: ApplicationContext) -> bool:
def cog_check(self, ctx: BaseContext) -> bool:
"""A special method that registers as a :func:`~discord.ext.commands.check`
for every command and subcommand in this cog.

This function **can** be a coroutine and must take a sole parameter,
``ctx``, to represent the :class:`.Context` or :class:`.ApplicationContext`.
``ctx``, to represent a subclass of :class:`BaseContext` (either :class:`.Context`
or :class:`.ApplicationContext`).

Parameters
----------
ctx: :class:`.Context`
ctx: :class:`.BaseContext`
The invocation context.
"""
return True

@_cog_special_method
async def cog_command_error(
self, ctx: ApplicationContext, error: Exception
) -> None:
async def cog_command_error(self, ctx: BaseContext, error: Exception) -> None:
"""A special method that is called whenever an error
is dispatched inside this cog.

Expand All @@ -498,37 +499,37 @@ async def cog_command_error(

Parameters
----------
ctx: :class:`.ApplicationContext`
ctx: :class:`.BaseContext`
The invocation context where the error happened.
error: :class:`ApplicationCommandError`
The error that happened.
"""

@_cog_special_method
async def cog_before_invoke(self, ctx: ApplicationContext) -> None:
async def cog_before_invoke(self, ctx: BaseContext) -> None:
"""A special method that acts as a cog local pre-invoke hook.

This is similar to :meth:`.ApplicationCommand.before_invoke`.
This is similar to :meth:`.Invokable.before_invoke`.

This **must** be a coroutine.

Parameters
----------
ctx: :class:`.ApplicationContext`
ctx: :class:`.BaseContext`
The invocation context.
"""

@_cog_special_method
async def cog_after_invoke(self, ctx: ApplicationContext) -> None:
async def cog_after_invoke(self, ctx: BaseContext) -> None:
"""A special method that acts as a cog local post-invoke hook.

This is similar to :meth:`.ApplicationCommand.after_invoke`.
This is similar to :meth:`.BaseContext.after_invoke`.

This **must** be a coroutine.

Parameters
----------
ctx: :class:`.ApplicationContext`
ctx: :class:`.BaseContext`
The invocation context.
"""

Expand Down
1 change: 1 addition & 0 deletions discord/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@

from .context import *
from .core import *
from .mixins import *
from .options import *
from .permissions import *
Loading