Discord-Raid-bot/utils/pb_handler.py

191 lines
8.2 KiB
Python
Raw Normal View History

2025-08-22 13:25:34 +00:00
import os
import discord
from config import AUTHORIZED_CHANNEL_ID, BOSS_CONFIG
from utils.helpers import (
parse_damage_amount,
normalize_difficulty,
get_difficulty_display_name,
format_damage_display,
format_datetime,
)
db_manager = None
screenshot_manager = None
def set_managers(db, ss):
2025-08-26 13:02:34 +00:00
"""Injection des managers (appelée une seule fois depuis bot.py)"""
2025-08-22 13:25:34 +00:00
global db_manager, screenshot_manager
db_manager = db
screenshot_manager = ss
async def handle_pb_command(ctx, boss_type, arg1=None, arg2=None):
2025-08-26 13:02:34 +00:00
"""Fonction générique pour gérer toutes les commandes PB avec difficultés"""
2025-08-22 13:25:34 +00:00
if ctx.channel.id != AUTHORIZED_CHANNEL_ID:
return
boss_info = BOSS_CONFIG[boss_type]
difficulties = boss_info['difficulties']
try:
2025-08-26 13:02:34 +00:00
# Pour CvC (pas de difficultés)
2025-08-22 13:25:34 +00:00
if not difficulties:
# Utiliser l'ancienne logique pour CvC avec parsing des montants
if arg1:
damage = parse_damage_amount(arg1)
if damage is not None:
await handle_pb_submission(ctx, boss_type, None, damage)
else: # Username
await show_user_pb(ctx, boss_type, None, arg1)
else: # Montrer son propre PB
await show_user_pb(ctx, boss_type, None, ctx.author.display_name)
return
2025-08-26 13:02:34 +00:00
# Pour Hydra et Chimera (avec difficultés)
2025-08-22 13:25:34 +00:00
if not arg1:
# !pbhydra sans arguments - montrer aide
difficulty_list = " | ".join([d.title() for d in difficulties])
await ctx.send(
2025-08-26 13:02:34 +00:00
f"❌ Please specify difficulty and damage!\n"
2025-08-22 13:25:34 +00:00
f"**Available difficulties:** {difficulty_list}\n"
f"**Shortcuts:** `nm` = Nightmare, `unm` = Ultra Nightmare\n"
f"**Examples:**\n"
f"`!pb{boss_type} normal 1.5M` - Submit PB with screenshot\n"
f"`!pb{boss_type} nm 500K` - Submit Nightmare PB\n"
f"`!pb{boss_type} hard` - Show your Hard PB\n"
f"`!pb{boss_type} brutal username` - Show user's Brutal PB"
)
return
2025-08-26 13:02:34 +00:00
# Normaliser la difficulté (gérer les diminutifs)
2025-08-22 13:25:34 +00:00
normalized_difficulty = normalize_difficulty(arg1)
2025-08-26 13:02:34 +00:00
# Vérifier si arg1 est une difficulté valide
2025-08-22 13:25:34 +00:00
if normalized_difficulty in difficulties:
difficulty = normalized_difficulty
if arg2:
damage = parse_damage_amount(arg2)
if damage is not None:
# !pbhydra normal 1.5M - Soumission PB
await handle_pb_submission(ctx, boss_type, difficulty, damage)
else:
# !pbhydra normal username - Voir PB d'un utilisateur
await show_user_pb(ctx, boss_type, difficulty, arg2)
else:
# !pbhydra normal - Voir son propre PB
await show_user_pb(ctx, boss_type, difficulty, ctx.author.display_name)
else:
2025-08-26 13:02:34 +00:00
# arg1 n'est pas une difficulté valide
2025-08-22 13:25:34 +00:00
difficulty_list = " | ".join([d.title() for d in difficulties])
await ctx.send(
2025-08-26 13:02:34 +00:00
f"❌ Invalid difficulty: `{arg1}`\n"
2025-08-22 13:25:34 +00:00
f"**Available difficulties:** {difficulty_list}\n"
f"**Shortcuts:** `nm` = Nightmare, `unm` = Ultra Nightmare"
)
except Exception as e:
2025-08-26 13:02:34 +00:00
await ctx.send(f"❌ Error: {e}")
2025-08-22 13:25:34 +00:00
async def handle_pb_submission(ctx, boss_type, difficulty, damage):
2025-08-26 13:02:34 +00:00
"""Gère la soumission d'un nouveau PB"""
2025-08-22 13:25:34 +00:00
if not ctx.message.attachments:
2025-08-26 13:02:34 +00:00
await ctx.send("❌ Please attach a screenshot to validate your PB!")
2025-08-22 13:25:34 +00:00
return
attachment = ctx.message.attachments[0]
if not any(attachment.filename.lower().endswith(ext) for ext in ['.png', '.jpg', '.jpeg', '.gif', '.webp']):
2025-08-26 13:02:34 +00:00
await ctx.send("❌ Please attach a valid image file!")
2025-08-22 13:25:34 +00:00
return
username = ctx.author.display_name
current_pb, _, _ = db_manager.get_user_pb(username, boss_type, difficulty)
if damage > current_pb:
# Sauvegarder la nouvelle screenshot
screenshot_filename = await screenshot_manager.save_screenshot(
attachment, username, damage, boss_type, difficulty
)
if screenshot_filename:
2025-08-26 13:02:34 +00:00
# Mettre à jour la base et récupérer l'ancien screenshot
2025-08-22 13:25:34 +00:00
old_screenshot = db_manager.update_user_pb(
username, boss_type, damage, screenshot_filename, difficulty
)
# Supprimer l'ancien screenshot
if old_screenshot:
screenshot_manager.delete_old_screenshot(old_screenshot, boss_type, difficulty)
improvement = damage - current_pb if current_pb > 0 else damage
boss_info = BOSS_CONFIG[boss_type]
difficulty_name = get_difficulty_display_name(difficulty) if difficulty else ""
embed = discord.Embed(
2025-08-26 13:02:34 +00:00
title=f"🎉 NEW {boss_info['name'].upper()} PB! 🎉",
2025-08-22 13:25:34 +00:00
description=f"**{username}** just hit **{format_damage_display(damage)} damage** on {difficulty_name} {boss_info['name']}!",
color=0x00ff00
)
2025-08-26 13:02:34 +00:00
embed.add_field(name="📈 Improvement", value=f"+{format_damage_display(improvement)} damage", inline=True)
2025-08-22 13:25:34 +00:00
embed.set_image(url=attachment.url)
await ctx.send(embed=embed)
else:
2025-08-26 13:02:34 +00:00
await ctx.send("❌ Failed to save screenshot. Please try again.")
2025-08-22 13:25:34 +00:00
else:
difficulty_name = get_difficulty_display_name(difficulty) if difficulty else ""
embed = discord.Embed(
2025-08-26 13:02:34 +00:00
title="💪 Nice attempt!",
2025-08-22 13:25:34 +00:00
description=f"Your damage: **{format_damage_display(damage)}**\nCurrent PB: **{format_damage_display(current_pb)}**",
color=0xffa500
)
embed.add_field(
name="Keep going!",
value=f"You need **{format_damage_display(current_pb - damage + 1)}** more damage for a new {difficulty_name} PB!",
inline=False
)
await ctx.send(embed=embed)
async def show_user_pb(ctx, boss_type, difficulty, username):
"""Affiche le PB d'un utilisateur"""
pb_data = db_manager.get_user_pb(username, boss_type, difficulty)
pb_damage, screenshot_filename, pb_date = pb_data
boss_info = BOSS_CONFIG[boss_type]
difficulty_name = get_difficulty_display_name(difficulty) if difficulty else ""
if pb_damage == 0:
embed = discord.Embed(
title=f"{boss_info['emoji']} {username}'s {difficulty_name} {boss_info['name']} PB",
description="**No record yet**",
color=0x666666
)
embed.add_field(
2025-08-26 13:02:34 +00:00
name="💡 Get started!",
2025-08-22 13:25:34 +00:00
value=f"Use `!pb{boss_type} {difficulty} <damage>` with a screenshot to set your first record!\nAccepts K/M/B suffixes: `1.5M`, `500K`, etc.",
inline=False
)
await ctx.send(embed=embed)
return
embed = discord.Embed(
title=f"{boss_info['emoji']} {username}'s {difficulty_name} {boss_info['name']} PB",
description=f"**{format_damage_display(pb_damage)} damage**",
color=boss_info['color']
)
if pb_date:
formatted_date = format_datetime(pb_date)
if formatted_date:
2025-08-26 13:02:34 +00:00
embed.add_field(name="📅 Record Date", value=formatted_date, inline=False)
2025-08-22 13:25:34 +00:00
# Envoyer la screenshot si elle existe
if screenshot_filename:
screenshot_path = screenshot_manager.get_screenshot_path(screenshot_filename, boss_type, difficulty)
if screenshot_path and os.path.exists(screenshot_path):
file = discord.File(screenshot_path, filename=f"{username}_{boss_type}_{difficulty}_pb.png")
embed.set_image(url=f"attachment://{username}_{boss_type}_{difficulty}_pb.png")
await ctx.send(embed=embed, file=file)
return
await ctx.send(embed=embed)