-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
96 additions
and
158 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import json | ||
import logging | ||
import asyncio | ||
|
||
logger = logging.getLogger('DiscordBot') | ||
|
||
class DataManager: | ||
def __init__(self, data_file): | ||
self.data_file = data_file | ||
self.lock = asyncio.Lock() | ||
|
||
async def load_data(self): | ||
async with self.lock: | ||
try: | ||
with open(self.data_file, 'r') as f: | ||
content = f.read().strip() | ||
if not content: | ||
logger.warning("Fichier de données vide, création d'une nouvelle structure") | ||
return {"config": {}, "stats": {}} | ||
return json.loads(content) | ||
except FileNotFoundError: | ||
logger.warning(f"Fichier {self.data_file} non trouvé, création d'une nouvelle structure") | ||
return {"config": {}, "stats": {}} | ||
except json.JSONDecodeError: | ||
logger.error(f"Erreur de décodage JSON dans {self.data_file}. Voulez vous créer une nouvelle structure ? Toutes les données du bot seront perdues (O/N)") | ||
response = input() | ||
if response.lower() in ['o', 'oui', 'y', 'yes']: | ||
return {"config": {}, "stats": {}} | ||
else: | ||
logger.error("Arrêt du bot") | ||
exit(1) | ||
|
||
async def save_data(self, data): | ||
async with self.lock: | ||
try: | ||
with open(self.data_file, 'w') as f: | ||
json.dump(data, f, indent=4) | ||
logger.debug("Données sauvegardées avec succès") | ||
except Exception as e: | ||
logger.error(f"Erreur lors de la sauvegarde des données: {e}") | ||
|
||
async def get_guild_config(self, guild_id: str) -> dict: | ||
data = await self.load_data() | ||
if guild_id not in data['config']: | ||
data['config'][guild_id] = { | ||
"send_public": True, | ||
"send_message": True, | ||
"enable_reactions": True | ||
} | ||
await self.save_data(data) | ||
return data['config'][guild_id] | ||
|
||
async def update_stats(self, guild_id: str, user_id: str, stat: str): | ||
data = await self.load_data() | ||
if guild_id not in data['stats']: | ||
data['stats'][guild_id] = {} | ||
if user_id not in data['stats'][guild_id]: | ||
data['stats'][guild_id][user_id] = {'count_69': 0} | ||
|
||
data['stats'][guild_id][user_id][stat] += 1 | ||
await self.save_data(data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import logging | ||
import re | ||
|
||
def setup_logging(): | ||
logging.basicConfig( | ||
level=logging.INFO, | ||
format='%(asctime)s [%(levelname)s] %(message)s', | ||
handlers=[ | ||
logging.StreamHandler() | ||
] | ||
) | ||
|
||
def extract_number_and_sum(message: str): | ||
numbers = list(map(int, re.findall(r'\b\d+\b', message))) | ||
total = sum(numbers) | ||
return numbers, total | ||
|