From 504567a33442b9dd1ec24946e7f932aa48fef0a8 Mon Sep 17 00:00:00 2001 From: Adriene Hutchins Date: Mon, 2 Mar 2020 15:47:58 -0500 Subject: [PATCH] Turned extend into command group --- extensions/__pycache__/core.cpython-38.pyc | Bin 6245 -> 6599 bytes extensions/core.py | 64 +++++++++++---------- extensions/developer.py | 2 + main.py | 17 +++++- 4 files changed, 51 insertions(+), 32 deletions(-) diff --git a/extensions/__pycache__/core.cpython-38.pyc b/extensions/__pycache__/core.cpython-38.pyc index 74bd1c3cf1d6aa1b791e9fa17f409ab719c96d12..1b06e3fd6e66ee26717f962144c849d872e499e0 100644 GIT binary patch delta 3133 zcma)8U2I%O6`r~Od-um{d%f$#c6yzpNq6h6)7YeGpdwXiaZuuf=C=;vcD*yPuf4u^ zJNIsWt{kmUjMN7r1oMPis}4eeA|5K`si+cFDwP)=ctAp2i5Dcq6DUt0cnN1_Z*0ej z5G(DOzjMya`Q|%k?q3i6cB*hbpSKbGe%QY9T{`lk!k@bajE=)Uynqp+XbuxJrw}}+ z5{0O1XilR#(LTgPC&rpKXHav=A||m&Wde(7uB06F*!wHHV=RU44bYUIW3T4aK} zBw~Rh4fdi*k6b5{>3oSCA}<4eLuB_^RGs1l<*n{9znhX1)yIhC`Lud&yZ{5F8rZ^yYGE7N zIR8e#Xx()^--}%Lep&w$#yS4bsEp=8mq{ValB~PfS!ww5HzOME=ljfdSGfKH{$U%0 z2yqE5;U(n*L=@Rqp{K6vx0F?dXmUnd(qT?tS6@ML)<|13ZLPG;02~Hr+NmbqFjtkk z_yi)^8H7OsOgU~?2P&HD)@rd;tGTZ8lH65i5(X z>iJRCc0BI1E2z0&ai2q3lL>p5@p$7qeStc5ZzLND1tzGcU7>*0QP9U zM$B7~G#hwTlN>;j10q$d+5cp>j#&rj!`Sdb#}A@dftyxqH7DNdG&_t@KWg1{T0w(Q z;=pky1Ln{hkUp?bkKe7;W`k(a^B0`!jnHYcZg9;5&GA~zw$HR^Bcw1X=fD^Y(+qS3 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p= str: # function lifted from StackOverflow """Return the given bytes as a human friendly KB, MB, GB, or TB string.""" @@ -129,16 +112,37 @@ Number of extensions present: {len(ctx.bot.cogs)} ping = (after - before) * 1000 await pong.edit(content="`PING discordapp.com {}ms`".format(int(ping))) - @commands.command() + @commands.group(aliases=['extensions', 'ext'], + invoke_without_command=True) + @commands.is_owner() + async def extend(self, ctx, name:str = None): + + # Provides status of extension + if name is not None: + status = "is" if f'extensions.{name}' in self.extensions_list else "is not" + msg = f"**extensions.{name}** {status} currently loaded and/or existent." + + # Handles empty calls + else: + msg = ( + "**Nothing was provided!**\n\n" + "Please provide an extension name for status, " + "or provide a subcommand." + ) + + # Sends completed message + await ctx.send(msg) + + @extend.command(aliases=['le', 'l']) @commands.is_owner() async def load(self, ctx, name: str): """Load an extension into the bot.""" m = await ctx.send(f'Loading {name}') - extension_name = 'extensions.{0}'.format(name) - if extension_name not in self.settings['extensions']: + extension_name = f'extensions.{name}' + if extension_name not in self.extensions_list: try: self.bot.load_extension(extension_name) - self.settings['extensions'].append(extension_name) + self.extensions_list.append(extension_name) await m.edit(content='Extension loaded.') except Exception as e: await m.edit( @@ -146,34 +150,34 @@ Number of extensions present: {len(ctx.bot.cogs)} else: await m.edit(content='Extension already loaded.') - @commands.command(aliases=["ule", "ul"]) + @extend.command(aliases=["ule", "ul"]) @commands.is_owner() async def unload(self, ctx, name: str): """Unload an extension from the bot.""" m = await ctx.send(f'Unloading {name}') - extension_name = 'extensions.{0}'.format(name) - if extension_name in self.settings['extensions']: + extension_name = f'extensions.{name}' + if extension_name in self.extensions_list: self.bot.unload_extension(extension_name) - self.settings['extensions'].remove(extension_name) + self.extensions_list.remove(extension_name) await m.edit(content='Extension unloaded.') else: await m.edit(content='Extension not found or not loaded.') - @commands.command(aliases=["rle", "rl"]) + @extend.command(aliases=["rle", "rl"]) @commands.is_owner() async def reload(self, ctx, name: str): """Reload an extension of the bot.""" m = await ctx.send(f'Reloading {name}') - extension_name = 'extensions.{0}'.format(name) - if extension_name in self.settings['extensions']: + extension_name = f'extensions.{name}' + if extension_name in self.extensions_list: self.bot.unload_extension(extension_name) try: self.bot.load_extension(extension_name) await m.edit(content='Extension reloaded.') except Exception as e: - self.settings['extensions'].remove(extension_name) + self.extensions_list.remove(extension_name) await m.edit( content=f'Failed to reload extension\n`{type(e).__name__}: {e}`') else: diff --git a/extensions/developer.py b/extensions/developer.py index 1937081..99c63db 100644 --- a/extensions/developer.py +++ b/extensions/developer.py @@ -38,6 +38,7 @@ class Developer(commands.Cog): def _cleanup_code(self, content): """Automatically removes code blocks from the code.""" + # remove ```py\n``` if content.startswith('```') and content.endswith('```'): return '\n'.join(content.split('\n')[1:-1]) @@ -47,6 +48,7 @@ class Developer(commands.Cog): def _get_syntax_error(self, err): """Returns SyntaxError formatted for repl reply.""" + return '```py\n{0.text}{1:>{0.offset}}\n{2}: {0}```'.format( err, '^', diff --git a/main.py b/main.py index cd0b904..203bdb7 100644 --- a/main.py +++ b/main.py @@ -29,7 +29,8 @@ class Bot(commands.Bot): # Initializes parent class super().__init__(self._get_prefix_new, **options) - # Get Config Values + # Setup + self.extensions_list = [] with open('config.json') as f: self.config = json.load(f) self.prefix = self.config['PREFIX'] @@ -45,6 +46,18 @@ class Bot(commands.Bot): # Logging print('Initialization complete.\n\n') + def _init_extensions(self): + """Initializes extensions.""" + + for ext in os.listdir('extensions'): + if ext.endswith('.py'): + try: + bot.load_extension(f'extensions.{ext[:-3]}') + self.extensions_list.append( + f'extensions.{ext[:-3]}') + except Exception as e: + print(e) + async def _get_prefix_new(self, bot, msg): """More flexible check for prefix.""" @@ -68,7 +81,7 @@ class Bot(commands.Bot): # NOTE Extension Entry Point # Loads core, which loads all other extensions - self.load_extension('extensions.core') + self._init_extensions() # Logging msg = "CONNECTED!\n"