This commit is contained in:
Slipstream 2025-05-30 15:40:20 -06:00
parent 2c10586b73
commit b503db75ec
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD
3 changed files with 19 additions and 13 deletions

View File

View File

@ -7,6 +7,7 @@ import random
import re # For parsing duration
import json
import os
import aiofiles # Import aiofiles
GIVEAWAY_DATA_FILE = "data/giveaways.json"
DATA_DIR = "data"
@ -169,8 +170,9 @@ class GiveawaysCog(commands.Cog, name="Giveaways"):
# "is_nitro_giveaway": bool,
# "ended": bool
# }
self._ensure_data_dir_exists()
self.load_giveaways()
# Ensure data directory exists before loading/saving
self.bot.loop.create_task(self._ensure_data_dir_exists()) # Run asynchronously
self.bot.loop.create_task(self.load_giveaways()) # Run asynchronously
self.check_giveaways_loop.start()
# Persistent views are added in setup_hook
@ -179,8 +181,9 @@ class GiveawaysCog(commands.Cog, name="Giveaways"):
print("Re-adding persistent giveaway views...")
temp_loaded_giveaways = [] # Use a temporary list for loading
try:
with open(GIVEAWAY_DATA_FILE, 'r') as f:
giveaways_data_for_views = json.load(f)
async with aiofiles.open(GIVEAWAY_DATA_FILE, mode='r') as f:
content = await f.read()
giveaways_data_for_views = json.loads(content)
for gw_data in giveaways_data_for_views:
# We only need to re-add views for messages that should have them
is_ended = gw_data.get("ended", False)
@ -196,23 +199,26 @@ class GiveawaysCog(commands.Cog, name="Giveaways"):
print(f"Attempted to re-add views for {len(temp_loaded_giveaways)} giveaways.")
except FileNotFoundError:
print("No giveaway data file found, skipping view re-adding.")
except json.JSONDecodeError:
print("Error decoding giveaway data file. Starting with no active giveaways.")
except Exception as e:
print(f"Error re-adding persistent views: {e}")
def _ensure_data_dir_exists(self):
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
async def _ensure_data_dir_exists(self):
if not os.path.exists(DATA_DIR): # os.path.exists is synchronous, but fine for checking existence
await aiofiles.os.makedirs(DATA_DIR) # Use aiofiles.os for async mkdir
def cog_unload(self):
self.check_giveaways_loop.cancel()
def load_giveaways(self):
async def load_giveaways(self): # Make async
self.active_giveaways = []
self.all_loaded_giveaways = []
try:
with open(GIVEAWAY_DATA_FILE, 'r') as f:
giveaways_data = json.load(f)
async with aiofiles.open(GIVEAWAY_DATA_FILE, mode='r') as f:
content = await f.read()
giveaways_data = json.loads(content)
now = datetime.datetime.now(datetime.timezone.utc)
for gw_data in giveaways_data:
gw_data["end_time"] = datetime.datetime.fromisoformat(gw_data["end_time"])
@ -233,7 +239,7 @@ class GiveawaysCog(commands.Cog, name="Giveaways"):
except Exception as e:
print(f"An unexpected error occurred loading giveaways: {e}")
def save_giveaways(self):
async def save_giveaways(self): # Make async
# Save all giveaways (from self.all_loaded_giveaways or by merging active and ended)
# This ensures that "ended" status and participant lists are preserved.
try:
@ -273,8 +279,8 @@ class GiveawaysCog(commands.Cog, name="Giveaways"):
self.all_loaded_giveaways.append(gw_active.copy())
with open(GIVEAWAY_DATA_FILE, 'w') as f:
json.dump(giveaways_to_save, f, indent=4)
async with aiofiles.open(GIVEAWAY_DATA_FILE, mode='w') as f:
await f.write(json.dumps(giveaways_to_save, indent=4))
# print(f"Saved {len(giveaways_to_save)} giveaways to disk.")
except Exception as e:
print(f"Error saving giveaways: {e}")