Skip to content

Commit

Permalink
Fix permissions hook removal (#2234)
Browse files Browse the repository at this point in the history
Some in-progress work slipped through #2149, and I figure it should be fixed before RC2.

I've also just decided to allow discovery of permissions hooks from superclasses as well. We should try to be more aware of the possibility of cog superclasses moving forward.

Signed-off-by: Toby Harradine <[email protected]>
  • Loading branch information
Tobotimus authored Oct 14, 2018
1 parent 6022c0f commit 7cd765d
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions redbot/core/bot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
import inspect
import os
import logging
from collections import Counter
Expand Down Expand Up @@ -236,20 +237,13 @@ def remove_cog(self, cogname: str):
if cog is None:
return

for when in ("before", "after"):
for cls in inspect.getmro(cog.__class__):
try:
hook = getattr(cog, f"_{cog.__class__.__name__}__red_permissions_{when}")
hook = getattr(cog, f"_{cls.__name__}__permissions_hook")
except AttributeError:
pass
else:
self.remove_permissions_hook(hook, when)

try:
hook = getattr(cog, f"_{cog.__class__.__name__}__red_permissions_before")
except AttributeError:
pass
else:
self.remove_permissions_hook(hook)
self.remove_permissions_hook(hook)

super().remove_cog(cogname)

Expand Down Expand Up @@ -390,10 +384,17 @@ def add_cog(self, cog: commands.Cog):
)
if not hasattr(cog, "requires"):
commands.Cog.__init__(cog)

for cls in inspect.getmro(cog.__class__):
try:
hook = getattr(cog, f"_{cls.__name__}__permissions_hook")
except AttributeError:
pass
else:
self.add_permissions_hook(hook)

for attr in dir(cog):
_attr = getattr(cog, attr)
if attr == f"_{cog.__class__.__name__}__permissions_hook":
self.add_permissions_hook(_attr)
if isinstance(_attr, discord.ext.commands.Command) and not isinstance(
_attr, commands.Command
):
Expand Down

0 comments on commit 7cd765d

Please sign in to comment.