From a8e8f35a9f5c6e6b90ca378e0bf81b93ddc0d98c Mon Sep 17 00:00:00 2001 From: iyad-f <128908811+iyad-f@users.noreply.github.com> Date: Wed, 9 Oct 2024 01:05:58 +0530 Subject: [PATCH 1/2] added the reload ext command --- cogs/admin.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ core/bot.py | 17 ++++++++------ 2 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 cogs/admin.py diff --git a/cogs/admin.py b/cogs/admin.py new file mode 100644 index 0000000..e067a85 --- /dev/null +++ b/cogs/admin.py @@ -0,0 +1,61 @@ +# SPDX-License-Identifier: MIT + + +# Imports. + + +from disnake.ext import commands +from disnake.ext.commands import errors + +import core + + +class Admin(commands.Cog): + @commands.command(name='reloadext') + async def reload_extension( + self, ctx: commands.Context[core.IgKnite], name: str + ) -> None: + try: + ctx.bot.reload_extension(name) + except Exception as e: + if isinstance(e, errors.ExtensionNotLoaded): + msg = f'`{name}` extension is not loaded.' + elif isinstance(e, errors.ExtensionNotFound): + msg = f'No extension with name `{name}` exists.' + elif isinstance(e, errors.ExtensionFailed): + msg = f'Failed to load `{name}` extension.' + elif isinstance(e, errors.NoEntryPointError): + msg = f'Setup function is not defined in `{name}` file.' + # getting here shouldnt happen, just making sure msg + # doesnt have an Unbound type. + else: + msg = f'Something went wrong while loading `{name}` extension.' + + embed = core.TypicalEmbed(description=msg, is_error=True) + await ctx.send(embed=embed) + else: + embed = core.TypicalEmbed( + description=f'Successfully reloaded `{name}` extension.' + ) + await ctx.send(embed=embed) + + @reload_extension.error + async def reload_ext( + self, ctx: commands.Context[core.IgKnite], error: errors.CommandError + ) -> None: + if isinstance(error, errors.MissingRequiredArgument): + embed = core.TypicalEmbed( + description='Please provide an extension name.', is_error=True + ) + await ctx.send(embed=embed) + # a check failure would be raised when someone who is not + # an owner uses the command but we dont wanna catch it, + # since we dont wanna send any messages if someone who + # isn't an owner uses the command. + + async def cog_check(self, ctx: commands.Context[core.IgKnite]) -> bool: + return await ctx.bot.is_owner(ctx.author) + + +def setup(bot: core.IgKnite) -> None: + bot.add_cog(Admin()) diff --git a/core/bot.py b/core/bot.py index 6486bc0..4036a26 100644 --- a/core/bot.py +++ b/core/bot.py @@ -13,16 +13,23 @@ # Set up a custom class for core functionality. -class IgKnite(commands.AutoShardedInteractionBot): +class IgKnite(commands.Bot): """ - A subclassed version of `commands.AutoShardedInteractionBot`.\n + A subclassed version of `commands.AutoShardedBot`.\n Basically works as the core class for all-things IgKnite! """ def __init__( self, *args, ignored_extensions: Optional[Set[str]] = None, **kwargs ) -> None: - super().__init__(*args, **kwargs) + super().__init__( + command_prefix=commands.when_mentioned_or('.'), + strip_after_prefix=True, + case_insensitive=True, + # owner_ids={}, # provide this if you dont want a api call to fetch the owner + *args, + **kwargs, + ) to_load = EXTENTIONS if ignored_extensions is not None: @@ -62,10 +69,6 @@ async def on_guild_join(self, _: disnake.Guild) -> None: async def on_guild_remove(self, _: disnake.Guild) -> None: await self._update_presence() - async def on_message(self, message: disnake.Message) -> None: - if message.author == self.user: - return - async def on_message_delete(self, message: disnake.Message) -> None: keychain.snipeables.append(message) await asyncio.sleep(25) From fdd74f67c1929272e63ec551f1e0174d65709cbe Mon Sep 17 00:00:00 2001 From: iyad-f <128908811+iyad-f@users.noreply.github.com> Date: Wed, 9 Oct 2024 02:16:13 +0530 Subject: [PATCH 2/2] fix for wrong bot class --- core/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/bot.py b/core/bot.py index 4036a26..6f13e63 100644 --- a/core/bot.py +++ b/core/bot.py @@ -13,7 +13,7 @@ # Set up a custom class for core functionality. -class IgKnite(commands.Bot): +class IgKnite(commands.AutoShardedBot): """ A subclassed version of `commands.AutoShardedBot`.\n Basically works as the core class for all-things IgKnite!