diff --git a/bot.py b/bot.py index 33bec18..488d2af 100755 --- a/bot.py +++ b/bot.py @@ -28,7 +28,7 @@ bot = commands.Bot(command_prefix=determine_prefix, ) -asyncio.run(work_with_cogs('load', bot, cog_list())) +asyncio.run(work_with_cogs('load', bot, asyncio.run(cog_list()))) @bot.event @@ -50,7 +50,9 @@ async def on_ready(): choices=[ OptionChoice("load", "load"), OptionChoice("unload", "unload"), - OptionChoice("reload", "reload") + OptionChoice("reload", "reload"), + OptionChoice("enable", "enable"), + OptionChoice("disable", "disable"), ] ), Option( @@ -61,15 +63,18 @@ async def on_ready(): ] ) @commands.is_owner() -async def slash_cogs(inter, what_do, cog: str = cog_list()) -> None: - print(type(inter.guild.text_channels)) +async def slash_cogs(inter, what_do, cog: str = asyncio.run(cog_list())) -> None: await work_with_cogs(what_do, bot, cog) - await inter.response.send_message(f'Cogs are {what_do}ed', ephemeral=True) + await inter.response.send_message(f'Cog {cog} is {what_do}ed', ephemeral=True) @slash_cogs.autocomplete('cog') -async def _cog_opt(inter: disnake.ApplicationCommandInteraction, current: str) -> List[OptionChoice]: - _list = cog_list() +async def _cog_opt(inter: disnake.ApplicationCommandInteraction, current: str, what_do) -> List[OptionChoice]: + _what = ['load', 'reload', 'unload', 'disable'] + if what_do in _what: + _list = await cog_list() + elif what_do == 'enable': + _list = await cog_list('./cogs/disabled/') return [ OptionChoice(name=choice, value=choice) for choice in _list if current.lower() in choice.lower() diff --git a/lib/CogsPrep.py b/lib/CogsPrep.py index 944a44c..b66cb4c 100644 --- a/lib/CogsPrep.py +++ b/lib/CogsPrep.py @@ -6,15 +6,18 @@ cog_list: return list of cog filenames work_with_cogs: loads, reloads and unloads cogs files """ +import os import traceback from os import listdir +from typing import List + from disnake.ext import commands from .Logger import logger -def cog_list(): +async def cog_list(fold='./cogs') -> List[str]: cogs_list = [] - for _filename in listdir('./cogs'): + for _filename in listdir(fold): if _filename.endswith('.py'): cogs_list.append(_filename[:-3]) return cogs_list @@ -44,3 +47,11 @@ async def work_with_cogs(what_do, bot, cog): elif what_do == 'reload': bot.reload_extension(f'cogs.{_filename}') logger.info(f'Cog {_filename} reloaded') + elif what_do == 'disable': + bot.unload_extension(f'cogs.{_filename}') + os.rename(f'cogs/{_filename}.py', f'cogs/disabled/{_filename}.py') + logger.info(f'Cog {_filename} stopped and disabled') + elif what_do == 'enable': + os.rename(f'cogs/disabled/{_filename}.py', f'cogs/{_filename}.py') + bot.load_extension(f'cogs.{_filename}') + logger.info(f'Cog {_filename} started and enabled')