Files
discord_bot/lib/CogsPrepare.py
2022-08-28 19:25:39 +03:00

41 lines
1.1 KiB
Python

import logging
from os import listdir
from disnake.ext import commands
"""
Loads, unloads Cogs files
cog_list: return list of cog filenames
"""
def cog_list():
cogs_list = []
for _filename in listdir('./cogs'):
if _filename.endswith('.py'):
cogs_list.append(_filename[:-3])
return cogs_list
async def work_with_cogs(what_do, bot):
for _filename in cog_list():
if what_do == "load":
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 as error:
logging.error(f'Error: {_filename} failed to load properly.', error)
except commands.ExtensionError:
logging.error(f'Error: unknown error with {_filename}')
elif what_do == 'unload':
bot.unload_extension(f'cogs.{_filename}')
elif what_do == 'reload':
bot.reload_extension(f'cogs.{_filename}')