def get_source(self, thing):
"""returns a source object of a thing
thing may be a non-builtin module, class, method, function, traceback, frame, or code object,
or a space separated discord.ext.commands call,
or a period deliminated file/module path as used when importing
"""
if isinstance(thing, str):
if '.' in thing: # import
modules = thing.split('.')
def get_last_attr(prev, attrs):
try:
return prev.callback
except AttributeError:
if not attrs:
return prev
return get_last_attr(getattr(prev, attrs.pop(0)),
attrs)
thing = get_last_attr(__import__(modules.pop(0)), modules)
else: # space delimited command call
names = thing.split()
thing = self.bot.commands[names.pop(0)]
for name in names:
thing = thing.commands[name]
thing = thing.callback
return Source(thing)
评论列表
文章目录