[commands] Allow loading cogs from folders.
Internally, instead of using module objects just use the `__module__` attribute which is the same thing. From preliminary testing this seems to work fine with both regular one-file-per-cog approaches and the folder cog approach. Fixes #126.
This commit is contained in:
parent
b05d8790fc
commit
b6ac856868
@ -602,26 +602,27 @@ class BotBase(GroupMixin):
|
|||||||
if lib is None:
|
if lib is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
lib_name = lib.__name__
|
||||||
|
|
||||||
# find all references to the module
|
# find all references to the module
|
||||||
|
|
||||||
# remove the cogs registered from the module
|
# remove the cogs registered from the module
|
||||||
for cogname, cog in self.cogs.copy().items():
|
for cogname, cog in self.cogs.copy().items():
|
||||||
if inspect.getmodule(cog) is lib:
|
if cog.__module__.startswith(lib_name):
|
||||||
self.remove_cog(cogname)
|
self.remove_cog(cogname)
|
||||||
|
|
||||||
# first remove all the commands from the module
|
# first remove all the commands from the module
|
||||||
for command in self.all_commands.copy().values():
|
for cmd in self.all_commands.copy().values():
|
||||||
if command.module is lib:
|
if cmd.module.startswith(lib_name):
|
||||||
command.module = None
|
if isinstance(cmd, GroupMixin):
|
||||||
if isinstance(command, GroupMixin):
|
cmd.recursively_remove_all_commands()
|
||||||
command.recursively_remove_all_commands()
|
self.remove_command(cmd.name)
|
||||||
self.remove_command(command.name)
|
|
||||||
|
|
||||||
# then remove all the listeners from the module
|
# then remove all the listeners from the module
|
||||||
for event_list in self.extra_events.copy().values():
|
for event_list in self.extra_events.copy().values():
|
||||||
remove = []
|
remove = []
|
||||||
for index, event in enumerate(event_list):
|
for index, event in enumerate(event_list):
|
||||||
if inspect.getmodule(event) is lib:
|
if event.__module__.startswith(lib_name):
|
||||||
remove.append(index)
|
remove.append(index)
|
||||||
|
|
||||||
for index in reversed(remove):
|
for index in reversed(remove):
|
||||||
|
@ -155,7 +155,7 @@ class Command:
|
|||||||
signature = inspect.signature(callback)
|
signature = inspect.signature(callback)
|
||||||
self.params = signature.parameters.copy()
|
self.params = signature.parameters.copy()
|
||||||
self.checks = kwargs.get('checks', [])
|
self.checks = kwargs.get('checks', [])
|
||||||
self.module = inspect.getmodule(callback)
|
self.module = callback.__module__
|
||||||
self.ignore_extra = kwargs.get('ignore_extra', True)
|
self.ignore_extra = kwargs.get('ignore_extra', True)
|
||||||
self.instance = None
|
self.instance = None
|
||||||
self.parent = None
|
self.parent = None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user