added error handling

This commit is contained in:
phixxy 2024-01-30 03:38:16 -08:00
parent 4bdd2a7203
commit 137afd389c

View file

@ -8,40 +8,45 @@ import aiohttp
import src.logger as logger import src.logger as logger
load_dotenv() load_dotenv()
discord_token = os.getenv('discord_token') discord_token = os.getenv('discord_token')
logger = logger.logging.getLogger("bot") logger = logger.logging.getLogger("bot")
intents = discord.Intents.all() intents = discord.Intents.all()
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents) bot = commands.Bot(command_prefix='!', intents=intents)
async def folder_setup(): async def folder_setup():
folder_names = ["tmp", "extensions", "data"] try:
for folder_name in folder_names: folder_names = ["tmp", "extensions", "data"]
if not os.path.exists(folder_name): for folder_name in folder_names:
os.mkdir(folder_name) if not os.path.exists(folder_name):
return folder_names os.mkdir(folder_name)
return folder_names
except Exception as e:
logger.error(f"Error setting up folders: {e}")
async def delete_all_files(path): async def delete_all_files(path):
for filename in os.listdir(path): try:
if os.path.isdir(path+filename): for filename in os.listdir(path):
shutil.rmtree(path+filename) if os.path.isdir(path+filename):
elif os.path.isfile(path+filename): shutil.rmtree(path+filename)
os.remove(path+filename) elif os.path.isfile(path+filename):
os.remove(path+filename)
except Exception as e:
logger.error(f"Error deleting files: {e}")
@tasks.loop(seconds=1) # Run the task every second @tasks.loop(seconds=1) # Run the task every second
async def task_loop(): async def task_loop():
current_time = time.localtime() try:
#Run daily tasks current_time = time.localtime()
if current_time.tm_hour == 0 and current_time.tm_min == 0 and current_time.tm_sec == 0: #Run daily tasks
try: if current_time.tm_hour == 0 and current_time.tm_min == 0 and current_time.tm_sec == 0:
pass await delete_all_files("tmp/")
#await delete_all_files("tmp/") logger.info("Deleted tmp/ files.")
#logger.info("Deleted tmp/ files.") except Exception as e:
except Exception as error: logger.error(f"Error in task loop: {e}")
logger.exception("Failed to delete files!")
async def create_session(): async def create_session():
return aiohttp.ClientSession() return aiohttp.ClientSession()
@ -62,17 +67,29 @@ async def on_disconnect():
@bot.event @bot.event
async def on_ready(): async def on_ready():
bot.logger = logger try:
await delete_all_files("tmp/") bot.logger = logger
# Import plugins from extensions folder await delete_all_files("tmp/")
for plugin_file in os.listdir('extensions/'): # Import plugins from extensions folder
if plugin_file[0] != '_' and plugin_file[-3:] == '.py': for plugin_file in os.listdir('extensions/'):
await bot.load_extension(f'extensions.{plugin_file[:-3]}') if plugin_file[0] != '_' and plugin_file[-3:] == '.py':
logger.info('We have logged in as {0.user}'.format(bot)) await bot.load_extension(f'extensions.{plugin_file[:-3]}')
task_loop.start() logger.info('We have logged in as {0.user}'.format(bot))
task_loop.start()
except Exception as e:
logger.error(f"Error in on_ready: {e}")
raise
@bot.event @bot.event
async def on_message(ctx): async def on_message(ctx):
await bot.process_commands(ctx) try:
await bot.process_commands(ctx)
except commands.CommandNotFound:
pass
except Exception as e:
logger.error(f"Error processing commands: {e}")
bot.run(discord_token, root_logger=True) try:
bot.run(discord_token, root_logger=True)
except Exception as e:
logger.critical(f"Fatal error running bot: {e}")