170 lines
5.2 KiB
Python
170 lines
5.2 KiB
Python
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")
|
|
DATA_FILE = "data.json"
|
|
|
|
# Datenformat:
|
|
# { "guild_id": { "user_id": { "mic": <int>, "deaf": <int> } } }
|
|
|
|
|
|
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):
|
|
"""kind: 'mic' oder 'deaf'"""
|
|
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
|
|
|
|
|
|
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:
|
|
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}]"
|
|
)
|
|
|
|
elif just_mic_muted:
|
|
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}]"
|
|
)
|
|
|
|
|
|
@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
|
|
|
|
# Sortieren nach Gesamt-Mutes (mic + deaf)
|
|
def total(entry):
|
|
if isinstance(entry, dict):
|
|
return entry.get("mic", 0) + entry.get("deaf", 0)
|
|
return entry # Fallback für altes Format
|
|
|
|
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)
|