From 94ba404912b9a7e7488a4e0e966aaa2ce91c8dc0 Mon Sep 17 00:00:00 2001 From: HigherOrderLogic <73709188+HigherOrderLogic@users.noreply.github.com> Date: Mon, 25 Apr 2022 19:31:08 +0700 Subject: [PATCH] Add `cog_load` and `cog_unload` method (#34) * Add `cog_load` and `cog_unload` method * Move `cog_load` to end * Resolve conflict --- docs/ext/commands/api.rst | 5 +++++ revolt/ext/commands/cog.py | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/ext/commands/api.rst b/docs/ext/commands/api.rst index b7b8d51..1f473a0 100755 --- a/docs/ext/commands/api.rst +++ b/docs/ext/commands/api.rst @@ -19,6 +19,11 @@ Command .. autoclass:: revolt.ext.commands.Command :members: +Cog +~~~~ +.. autoclass:: revolt.ext.commands.Cog + :members: + command ~~~~~~~~ .. autodecorator:: revolt.ext.commands.command diff --git a/revolt/ext/commands/cog.py b/revolt/ext/commands/cog.py index 70f5854..02a229c 100755 --- a/revolt/ext/commands/cog.py +++ b/revolt/ext/commands/cog.py @@ -32,18 +32,30 @@ class Cog(metaclass=CogMeta): _commands: list[Command] qualified_name: str - def _inject(self, client: CommandsClient): + def cog_load(self): + """A special method that is called when the cog gets loaded.""" + pass + + def cog_unload(self): + """A special method that is called when the cog gets removed.""" + pass + + def _inject(self, client: CommandsClient): client.cogs[self.qualified_name] = self for command in self._commands: command.cog = self client.add_command(command) + self.cog_load() + def _uninject(self, client: CommandsClient): for name, command in client.all_commands.copy().items(): if command in self._commands: del client.all_commands[name] + self.cog_unload() + @property def commands(self) -> list[Command]: return self._commands