diff --git a/.gitignore b/.gitignore index e1febb3..3deac2e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ /audio/*/ /test2.py /user.db -/*.json +*.json *.pyc diff --git a/test.py b/test.py index b5eaab8..27208a2 100644 --- a/test.py +++ b/test.py @@ -11,6 +11,8 @@ from discord.ext import commands from dislash import InteractionClient, Option, OptionType +bot_owner = 386629192743256065 + class DB: @staticmethod def _prepare_db(guild: int): @@ -65,6 +67,50 @@ class DB: logging.error("Failed to read sqlite table", error) +class Cogs_prepare: + @staticmethod + def _cog_list(): + cogs_list = [] + for filename in listdir('./cogs'): + if filename.endswith('.py'): + cogs_list.append(filename[:-3]) + return cogs_list + + @staticmethod + async def _cogs_load(ctx): + for filename in Cogs_prepare._cog_list(): + if filename.endswith('.py'): + bot.load_extension(f'cogs.{filename[:-3]}') + print(f'Load cog {filename[:-3]}') + else: + print(f'Unable to load {filename[:-3]}') + + await ctx.reply('Cogs loaded') + + @staticmethod + async def _cogs_unload(ctx): + for filename in Cogs_prepare._cog_list(): + bot.unload_extension(f'cogs.{filename[:-3]}') + print(f'Unload cog {filename[:-3]}') + + await ctx.reply('Cogs unloaded') + + @staticmethod + async def _cog_load(ctx, cog): + try: + bot.load_extension(f'cogs.{cog}') + await ctx.reply(f'Loaded cog {cog}') + + except commands.ExtensionNotFound: + await ctx.send(f"Error: {cog} couldn't be found to load.") + + except commands.ExtensionFailed: + await ctx.send(f'Error: {cog} failed to load properly.') + + except commands.ExtensionError: + await ctx.send(f'Error: unknown error with {cog}') + + threading.current_thread().name = "main" logging.basicConfig(stream=sys.stdout, filemode='w', level='INFO', format='%(asctime)s - %(levelname)s - %(threadName)s - %(message)s') @@ -88,12 +134,22 @@ intents.members = True bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents) inter_client = InteractionClient(bot) -for filename in listdir('./cogs'): - if filename.endswith('.py'): - bot.load_extension(f'cogs.{filename[:-3]}') - print(f'Load cog {filename[:-3]}') - else: - print(f'Unable to load {filename[:-3]}') + +print(Cogs_prepare._cog_list()) + +for filename in Cogs_prepare._cog_list(): + try: + bot.load_extension(f'cogs.{filename}') + logging.info(f'Loaded cog {filename}') + + except commands.ExtensionNotFound: + logging.error(f"Error: {filename} couldn't be found to load.") + + except commands.ExtensionFailed: + logging.error(f'Error: {filename} failed to load properly.') + + except commands.ExtensionError: + logging.error(f'Error: unknown error with {filename[:-3]}') class Commands: