diff options
| author | 2019-09-26 18:42:22 -0700 | |
|---|---|---|
| committer | 2019-10-01 18:25:31 -0700 | |
| commit | 4cb7b2d8f0c81b351ea71a316a4df8ae55c0c010 (patch) | |
| tree | e2f1cb42a91e6e030d05dfe941f23bd856ca4b78 | |
| parent | Raise ValueError in respect_role_hierarchy instead of logging errors (diff) | |
Adjust type annotations of decorators
* Always return None from inner function
* Change annotation of self parameter to Cog
Diffstat (limited to '')
| -rw-r--r-- | bot/decorators.py | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/bot/decorators.py b/bot/decorators.py index 25d1b694d..935df4af0 100644 --- a/bot/decorators.py +++ b/bot/decorators.py @@ -3,13 +3,13 @@ import random from asyncio import Lock, sleep from contextlib import suppress from functools import wraps -from typing import Any, Callable, Container, Optional, Union +from typing import Callable, Container, Union from weakref import WeakValueDictionary from discord import Colour, Embed, Member from discord.errors import NotFound from discord.ext import commands -from discord.ext.commands import CheckFailure, Context +from discord.ext.commands import CheckFailure, Cog, Context from bot.constants import ERROR_REPLIES, RedirectOutput from bot.utils.checks import with_role_check, without_role_check @@ -78,7 +78,7 @@ def locked() -> Callable: func.__locks = WeakValueDictionary() @wraps(func) - async def inner(self: Callable, ctx: Context, *args, **kwargs) -> Optional[Any]: + async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None: lock = func.__locks.setdefault(ctx.author.id, Lock()) if lock.locked(): embed = Embed() @@ -93,7 +93,7 @@ def locked() -> Callable: return async with func.__locks.setdefault(ctx.author.id, Lock()): - return await func(self, ctx, *args, **kwargs) + await func(self, ctx, *args, **kwargs) return inner return wrap @@ -108,14 +108,16 @@ def redirect_output(destination_channel: int, bypass_roles: Container[int] = Non """ def wrap(func: Callable) -> Callable: @wraps(func) - async def inner(self: Callable, ctx: Context, *args, **kwargs) -> Any: + async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None: if ctx.channel.id == destination_channel: log.trace(f"Command {ctx.command.name} was invoked in destination_channel, not redirecting") - return await func(self, ctx, *args, **kwargs) + await func(self, ctx, *args, **kwargs) + return if bypass_roles and any(role.id in bypass_roles for role in ctx.author.roles): log.trace(f"{ctx.author} has role to bypass output redirection") - return await func(self, ctx, *args, **kwargs) + await func(self, ctx, *args, **kwargs) + return redirect_channel = ctx.guild.get_channel(destination_channel) old_channel = ctx.channel @@ -158,7 +160,7 @@ def respect_role_hierarchy(target_arg: Union[int, str] = 0) -> Callable: """ def wrap(func: Callable) -> Callable: @wraps(func) - async def inner(self: Callable, ctx: Context, *args, **kwargs) -> Any: + async def inner(self: Cog, ctx: Context, *args, **kwargs) -> None: try: target = kwargs[target_arg] except KeyError: @@ -171,7 +173,8 @@ def respect_role_hierarchy(target_arg: Union[int, str] = 0) -> Callable: if not isinstance(target, Member): log.trace("The target is not a discord.Member; skipping role hierarchy check.") - return await func(self, ctx, *args, **kwargs) + await func(self, ctx, *args, **kwargs) + return cmd = ctx.command.name actor = ctx.author @@ -185,6 +188,6 @@ def respect_role_hierarchy(target_arg: Union[int, str] = 0) -> Callable: "someone with an equal or higher top role." ) else: - return await func(self, ctx, *args, **kwargs) + await func(self, ctx, *args, **kwargs) return inner return wrap |