Discord-Raid-bot/utils/ScreenshotManager_class.py

66 lines
2.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
import sqlite3, os
import aiohttp
from datetime import datetime
from config import SCREENSHOTS_BASE_PATH, BOSS_CONFIG
class ScreenshotManager:
def __init__(self, base_path=SCREENSHOTS_BASE_PATH):
self.base_path = base_path
# Create directories for each boss and difficulty
for boss_type in BOSS_CONFIG.keys():
boss_path = os.path.join(base_path, boss_type)
os.makedirs(boss_path, exist_ok=True)
# Create subdirectories for difficulties
for difficulty in BOSS_CONFIG[boss_type]['difficulties']:
difficulty_path = os.path.join(boss_path, difficulty)
os.makedirs(difficulty_path, exist_ok=True)
async def save_screenshot(self, attachment, username, damage, boss_type, difficulty=None):
"""Saves the screenshot locally"""
try:
timestamp = int(datetime.now().timestamp())
file_extension = attachment.filename.split('.')[-1].lower()
filename = f"{username.lower()}_{damage}_{timestamp}.{file_extension}"
if difficulty:
boss_path = os.path.join(self.base_path, boss_type, difficulty)
else:
boss_path = os.path.join(self.base_path, boss_type)
filepath = os.path.join(boss_path, filename)
async with aiohttp.ClientSession() as session:
async with session.get(attachment.url) as resp:
if resp.status == 200:
# Binary mode, no encoding issues
with open(filepath, 'wb') as f:
f.write(await resp.read())
return filename
return None
except Exception as e:
print(f"Screenshot save error: {str(e)}")
return None
def get_screenshot_path(self, filename, boss_type, difficulty=None):
"""Returns the full path to the screenshot"""
if filename:
if difficulty:
return os.path.join(self.base_path, boss_type, difficulty, filename)
else:
return os.path.join(self.base_path, boss_type, filename)
return None
def delete_old_screenshot(self, filename, boss_type, difficulty=None):
"""Deletes the old screenshot"""
if filename:
old_path = self.get_screenshot_path(filename, boss_type, difficulty)
if old_path and os.path.exists(old_path):
try:
os.remove(old_path)
print(f"Old screenshot deleted: {filename}")
except Exception as e:
print(f"Screenshot deletion error: {str(e)}")