Skip to content

Commit

Permalink
added a way to unload extensions (#23 ) & prevented the same extensio…
Browse files Browse the repository at this point in the history
…n to appear multiple times in enabled file
  • Loading branch information
s0lst1ce committed May 30, 2019
1 parent 8589c25 commit 59e4d6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
33 changes: 0 additions & 33 deletions checks.py

This file was deleted.

38 changes: 31 additions & 7 deletions executioner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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}")



#########################################
Expand Down

0 comments on commit 59e4d6d

Please sign in to comment.