import discord from discord import app_commands from discord.ext import commands import json import os from dotenv import load_dotenv load_dotenv() TOKEN = os.getenv("DISCORD_TOKEN") MILESTONE_CHANNEL_ID = int(os.getenv("MILESTONE_CHANNEL_ID", 0)) DATA_FILE = "data.json" # Datenformat: # { "guild_id": { "user_id": { "mic": , "deaf": } } } MILESTONES = [10, 25, 50, 100, 250, 500, 1000] MILESTONE_MESSAGES = { 10: ("🔇", "Erste Schritte in die Stille — **{name}** hat sich **10x** gemutet!"), 25: ("😶", "**{name}** hält langsam die Klappe... **25 Mutes** erreicht!"), 50: ("🤫", "**{name}** ist auf dem Weg zur Legende — **50 Mutes**!"), 100: ("💯", "**{name}** hat die **100 Mutes** geknackt! Ein wahrer Schweiger."), 250: ("🏅", "**{name}** erreicht **250 Mutes** — Professionelles Schweigen!"), 500: ("🎖️", "**{name}** mit satten **500 Mutes**. Respekt."), 1000: ("👑", "**{name}** ist der unbestrittene **MUTE-KÖNIG** mit **1000 Mutes**!"), } def load_data() -> dict: if os.path.exists(DATA_FILE): with open(DATA_FILE, "r", encoding="utf-8") as f: return json.load(f) return {} def save_data(data: dict): with open(DATA_FILE, "w", encoding="utf-8") as f: json.dump(data, f, indent=2, ensure_ascii=False) def get_user_entry(data: dict, guild_id: int, user_id: int) -> dict: return data.get(str(guild_id), {}).get(str(user_id), {"mic": 0, "deaf": 0}) def increment(data: dict, guild_id: int, user_id: int, kind: str) -> int: """Zählt hoch und gibt den neuen Gesamtwert (mic + deaf) zurück.""" gid = str(guild_id) uid = str(user_id) if gid not in data: data[gid] = {} if uid not in data[gid]: data[gid][uid] = {"mic": 0, "deaf": 0} data[gid][uid][kind] += 1 return data[gid][uid]["mic"] + data[gid][uid]["deaf"] async def check_milestone(guild: discord.Guild, member: discord.Member, new_total: int): """Prüft ob ein Meilenstein erreicht wurde und postet ggf. eine Nachricht.""" if new_total not in MILESTONES: return emoji, template = MILESTONE_MESSAGES[new_total] message = template.format(name=member.display_name) # Ziel-Channel: konfigurierter Channel → System-Channel → erstes Text-Channel channel = None if MILESTONE_CHANNEL_ID: channel = guild.get_channel(MILESTONE_CHANNEL_ID) if channel is None: channel = guild.system_channel if channel is None: channel = next( (c for c in guild.text_channels if c.permissions_for(guild.me).send_messages), None, ) if channel is None: print(f"[MILESTONE] Kein Channel gefunden für: {message}") return embed = discord.Embed( title=f"{emoji} Meilenstein erreicht!", description=message, color=discord.Color.gold(), ) embed.set_thumbnail(url=member.display_avatar.url) embed.set_footer(text=f"Gesamt: {new_total} Mutes") await channel.send(embed=embed) intents = discord.Intents.default() intents.voice_states = True intents.members = True bot = commands.Bot(command_prefix="!", intents=intents) mute_data = load_data() @bot.event async def on_ready(): print(f"Bot gestartet als {bot.user} (ID: {bot.user.id})") try: synced = await bot.tree.sync() print(f"{len(synced)} Slash-Commands synchronisiert.") except Exception as e: print(f"Fehler beim Sync: {e}") @bot.event async def on_voice_state_update( member: discord.Member, before: discord.VoiceState, after: discord.VoiceState, ): if member.bot: return guild = member.guild afk_channel = guild.afk_channel # AFK-Channel ausschließen if afk_channel is not None: if (after.channel is not None and after.channel.id == afk_channel.id) or \ (before.channel is not None and before.channel.id == afk_channel.id): return # Full Mute (Deafen): Nutzer hat sich gerade taubgestellt just_deafened = (not before.self_deaf) and after.self_deaf # Nur Mikrofon gemutet: self_mute wurde aktiviert, aber NICHT deafen just_mic_muted = (not before.self_mute) and after.self_mute and (not after.self_deaf) if just_deafened: new_total = increment(mute_data, guild.id, member.id, "deaf") save_data(mute_data) entry = get_user_entry(mute_data, guild.id, member.id) print( f"[DEAF] {member.display_name} hat sich taubgestellt " f"(deaf={entry['deaf']}x, mic={entry['mic']}x) [{guild.name}]" ) await check_milestone(guild, member, new_total) elif just_mic_muted: new_total = increment(mute_data, guild.id, member.id, "mic") save_data(mute_data) entry = get_user_entry(mute_data, guild.id, member.id) print( f"[MIC] {member.display_name} hat nur sein Mikrofon gemutet " f"(mic={entry['mic']}x, deaf={entry['deaf']}x) [{guild.name}]" ) await check_milestone(guild, member, new_total) @bot.tree.command(name="mutescore", description="Zeigt das Mute-Leaderboard des Servers.") @app_commands.describe(limit="Wie viele Plätze anzeigen? (Standard: 10)") async def mutescore(interaction: discord.Interaction, limit: int = 10): limit = max(1, min(limit, 25)) guild = interaction.guild guild_data: dict = mute_data.get(str(guild.id), {}) if not guild_data: await interaction.response.send_message( "Noch keine Mutes aufgezeichnet.", ephemeral=True ) return def total(entry): if isinstance(entry, dict): return entry.get("mic", 0) + entry.get("deaf", 0) return entry sorted_entries = sorted(guild_data.items(), key=lambda x: total(x[1]), reverse=True) top = sorted_entries[:limit] medals = {1: "🥇", 2: "🥈", 3: "🥉"} lines = [] for rank, (uid, counts) in enumerate(top, start=1): member = guild.get_member(int(uid)) name = member.display_name if member else f"Unbekannt ({uid})" medal = medals.get(rank, f"**#{rank}**") if isinstance(counts, dict): mic = counts.get("mic", 0) deaf = counts.get("deaf", 0) lines.append( f"{medal} {name} — " f"**{mic + deaf}x** gesamt " f"*(🎙️ {mic}x Mikro / 🔇 {deaf}x Deaf)*" ) else: lines.append(f"{medal} {name} — **{counts}x** gemutet") embed = discord.Embed( title="Mute-Leaderboard", color=discord.Color.blurple(), ) embed.description = "\n".join(lines) embed.set_footer(text=f"Top {len(top)} von {len(sorted_entries)} Nutzern | 🎙️ = nur Mikro | 🔇 = Deaf") await interaction.response.send_message(embed=embed) @bot.tree.command(name="mymutes", description="Zeigt deine eigenen Mute-Stats.") async def mymutes(interaction: discord.Interaction): entry = get_user_entry(mute_data, interaction.guild.id, interaction.user.id) mic = entry.get("mic", 0) deaf = entry.get("deaf", 0) await interaction.response.send_message( f"Deine Mute-Stats:\n" f"🎙️ Nur Mikrofon gemutet: **{mic}x**\n" f"🔇 Taubgestellt (Deaf): **{deaf}x**\n" f"📊 Gesamt: **{mic + deaf}x**", ephemeral=True, ) bot.run(TOKEN)