Simplify the inheritance of the bot (#3822)

This commit is contained in:
Michael H 2020-05-06 21:08:20 -04:00 committed by GitHub
parent 51f7d6cea2
commit 981661ea68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,14 +63,17 @@ def _is_submodule(parent, child):
return parent == child or child.startswith(parent + ".") return parent == child or child.startswith(parent + ".")
# barely spurious warning caused by our intentional shadowing # Order of inheritance here matters.
# d.py autoshardedbot should be at the end
# all of our mixins should happen before,
# and must include a call to super().__init__ unless they do not provide an init
class RedBase( class RedBase(
commands.GroupMixin, dpy_commands.bot.BotBase, RPCMixin commands.GroupMixin, RPCMixin, dpy_commands.bot.AutoShardedBot
): # pylint: disable=no-member ): # pylint: disable=no-member # barely spurious warning caused by shadowing
"""Mixin for the main bot class. """
The historical reasons for this mixin no longer apply
This exists because `Red` inherits from `discord.AutoShardedClient`, which and only remains temporarily to not break people
is something other bot classes may not want to have as a parent class. relying on the publicly exposed bases existing.
""" """
def __init__(self, *args, cli_flags=None, bot_dir: Path = Path.cwd(), **kwargs): def __init__(self, *args, cli_flags=None, bot_dir: Path = Path.cwd(), **kwargs):
@ -640,6 +643,9 @@ class RedBase(
await self.rpc.initialize(self.rpc_port) await self.rpc.initialize(self.rpc_port)
async def start(self, *args, **kwargs): async def start(self, *args, **kwargs):
"""
Overridden start which ensures cog load and other pre-connection tasks are handled
"""
cli_flags = kwargs.pop("cli_flags") cli_flags = kwargs.pop("cli_flags")
await self.pre_flight(cli_flags=cli_flags) await self.pre_flight(cli_flags=cli_flags)
return await super().start(*args, **kwargs) return await super().start(*args, **kwargs)
@ -1237,12 +1243,6 @@ class RedBase(
await asyncio.sleep(delay) await asyncio.sleep(delay)
await _delete_helper(message) await _delete_helper(message)
class Red(RedBase, discord.AutoShardedClient):
"""
You're welcome Caleb.
"""
async def logout(self): async def logout(self):
"""Logs out of Discord and closes all connections.""" """Logs out of Discord and closes all connections."""
await super().logout() await super().logout()
@ -1273,6 +1273,13 @@ class Red(RedBase, discord.AutoShardedClient):
sys.exit(self._shutdown_mode) sys.exit(self._shutdown_mode)
# This can be removed, and the parent class renamed as a breaking change
class Red(RedBase):
"""
Our subclass of discord.ext.commands.AutoShardedBot
"""
class ExitCodes(IntEnum): class ExitCodes(IntEnum):
# This needs to be an int enum to be used # This needs to be an int enum to be used
# with sys.exit # with sys.exit