0

I have a command with a decorator @commands.has_role("admin") and I need to get that role. This is what I have tried:

filtered = await self.filter_commands(self.context.bot.commands, sort=True) 
checks = [command.checks for command in filtered]
for check in checks:
  for c in check:
    print(c.role) 

but returns AttributeError: 'function' object has no attribute 'role'. How do I get the value of that decorator? This decorator is not custom, it is part of discord.py(discord.ext.commands)

Daraan
  • 1,797
  • 13
  • 24
L8R
  • 401
  • 5
  • 21
  • Does this answer your question? [How do I get the value of an argument passed to a decorator in Python?](https://stackoverflow.com/questions/48917991/how-do-i-get-the-value-of-an-argument-passed-to-a-decorator-in-python) – zvi Oct 11 '22 at 12:19
  • 1
    No. The decorator is not custom. – L8R Oct 11 '22 at 12:21
  • The value isn't necessarily *stored* anywhere; it may only be used long enough to create the decorator that its applied to you function. – chepner Oct 11 '22 at 12:23
  • Looking at the source code the role is not stored in a acessible variable, it's in the locals of the function. – Daraan Oct 11 '22 at 12:24
  • 1
    But when I use `c.__code__.co_varnames`, it returns `('ctx', 'role')` @chepner – L8R Oct 11 '22 at 12:25
  • does `c.__closure__[0 or more].cell_contents` provide you with something – Daraan Oct 11 '22 at 12:26

1 Answers1

0

Sometimes necessary values are stored in attributes of the wrapper. Not for the discord wrapper.

Decorators make use of closures. To find their closed values use

function.__closure__[0].cell_contents

function here is c

Daraan
  • 1,797
  • 13
  • 24