From 59e4d6d0af832ab446542c5261d8065bc8cf8466 Mon Sep 17 00:00:00 2001 From: NotaSmartDev Date: Thu, 30 May 2019 11:27:00 +0200 Subject: [PATCH] added a way to unload extensions (#23 ) & prevented the same extension to appear multiple times in enabled file --- checks.py | 33 --------------------------------- executioner.py | 38 +++++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 40 deletions(-) delete mode 100644 checks.py diff --git a/checks.py b/checks.py deleted file mode 100644 index 61b09a0..0000000 --- a/checks.py +++ /dev/null @@ -1,33 +0,0 @@ -import logging -from settings import * -from discord.ext import commands - - -######################################### -# # -# # -# Setting up logging # -# # -# # -######################################### -local_logger = logging.getLogger(__name__) -local_logger.setLevel(LOGGING_LEVEL) -local_logger.addHandler(LOGGING_HANDLER) -local_logger.info(f"Innitalized {__name__} logger") - - -######################################### -# # -# # -# Checks # -# # -# # -######################################### - -def is_runner(): - def check_condition(ctx): - return ctx.message.author.id ==RUNNER_ID - result = commands.check(check_condition) - if result == False: - ctx.send(ERR_UNSUFFICIENT_PRIVILEGE) - return result \ No newline at end of file diff --git a/executioner.py b/executioner.py index 4181938..accb8d0 100644 --- a/executioner.py +++ b/executioner.py @@ -74,9 +74,19 @@ async def add(ctx, extension:str): #if the extension was correctly loaded, adding it to the enabled file try: - #appending new extension to ENABLED_EXTENSIONS_FILE - with open(ENABLED_EXTENSIONS_FILE, "a") as file: - file.write("{}\n".format(extension)) + #fetching laready enabled extensions + with open(ENABLED_EXTENSIONS_FILE, "r") as file: + to_write = "" + for line in file.readlines(): + if line[:-1]==extension: + continue + to_write+=line + + to_write+= f"{extension}\n" + + #writting to file + with open(ENABLED_EXTENSIONS_FILE, "w") as file: + file.write(to_write) except FileNotFoundError as e: #if the file didn't yet exist a new one will be created. This should not happen, only here as a failsafe @@ -90,10 +100,12 @@ async def add(ctx, extension:str): raise e await ctx.send("Successfully added and loadded {}".format(extension)) + + @ext.command() async def rm(ctx, extension:str): try: - bot.unload(extension) + bot.unload_extension(extension) except Exception as e: main_logger.exception(e) @@ -102,18 +114,30 @@ async def rm(ctx, extension:str): #if the extension was correctly unloaded, removing it from the enblaed extension file try: - with open(ENABLED_EXTENSIONS_FILE, "aw") as file: + with open(ENABLED_EXTENSIONS_FILE, "r") as file: lines = [] for line in file.readlines(): - if line == extension: + if line[:-1] == extension: continue lines.append(line) - file.write(lines) + + + #building new file + to_write = "" + for line in lines: + to_write+=line + + with open(ENABLED_EXTENSIONS_FILE, "w") as file: + file.write(to_write) + except Exception as e: main_logger.exception(e) await ctx.send("UnexpectedError:\tReport issue to an admin\n{}".format(e)) raise e + await ctx.send("Successfully removed and unloaded {}".format(extension)) + local_logger.info(f"Disabled and removed {extension}") + #########################################