ChessDebate/slowbot.py

93 lines
3.0 KiB
Python
Raw Normal View History

2025-06-16 13:56:28 +02:00
import discord
from discord import app_commands
from discord.ext import commands
import asyncio
import json
intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
intents.members = True
2025-06-16 14:04:27 +02:00
bot = commands.Bot(intents=intents)
2025-06-16 13:56:28 +02:00
config = {
"slowmode_duration": 20,
"affected_roles": [],
}
def save_config():
with open("config.json", "w") as f:
json.dump(config, f)
def load_config():
global config
try:
with open("config.json", "r") as f:
config = json.load(f)
except FileNotFoundError:
save_config()
@bot.event
async def on_ready():
print(f"{bot.user} est en ligne.")
load_config()
try:
synced = await bot.tree.sync()
print(f"Commands synced: {len(synced)}")
except Exception as e:
print(f"Erreur lors de la sync des commandes : {e}")
2025-06-16 14:04:27 +02:00
@bot.tree.command(name="setslowmode", description="Définit la durée du slowmode dans les threads (en minutes)")
2025-06-16 13:56:28 +02:00
@app_commands.checks.has_permissions(manage_threads=True)
2025-06-16 14:04:27 +02:00
@app_commands.describe(minutes="Durée du slowmode en minutes")
async def setslowmode(interaction: discord.Interaction, minutes: int):
config["slowmode_duration"] = minutes
2025-06-16 13:56:28 +02:00
save_config()
updated_threads = 0
failed_threads = []
for thread in interaction.guild.threads:
if not thread.archived:
try:
2025-06-16 14:04:27 +02:00
await thread.edit(slowmode_delay=minutes*60)
2025-06-16 13:56:28 +02:00
updated_threads += 1
except Exception as e:
failed_threads.append(thread.name)
2025-06-16 14:04:27 +02:00
message = f"✅ Slowmode par défaut défini à {seconds} minutes.\n"
2025-06-16 13:56:28 +02:00
message += f"🧵 Slowmode mis à jour dans {updated_threads} thread(s)."
if failed_threads:
message += f"\n⚠️ Impossible de modifier : {', '.join(failed_threads)}"
await interaction.response.send_message(message, ephemeral=True)
@bot.tree.command(name="help", description="Affiche la liste des commandes disponibles")
async def help_command(interaction: discord.Interaction):
help_text = """
**📋 Commandes Slash :**
- `/setslowmode <seconds>` : Définit la durée du slowmode dans les threads.
- `/help` : Affiche cette aide.
"""
await interaction.response.send_message(help_text, ephemeral=True)
@bot.event
async def on_thread_create(thread):
try:
await thread.edit(slowmode_delay=config["slowmode_duration"])
print(f"✅ Slowmode de {config['slowmode_duration']}s appliqué à {thread.name}")
except Exception as e:
print(f"❌ Impossible de définir le slowmode sur {thread.name} : {e}")
@bot.tree.error
async def on_app_command_error(interaction: discord.Interaction, error):
if isinstance(error, app_commands.MissingPermissions):
await interaction.response.send_message("❌ Permissions insuffisantes pour cette commande.", ephemeral=True)
else:
await interaction.response.send_message(f"❌ Une erreur est survenue : {error}", ephemeral=True)
print(f"Erreur commande slash : {error}")
2025-06-16 14:04:27 +02:00
bot.run("MTM4MTk4ODA2NjUwMzQzMDI0Nw.G_7FMX.DMZ32kmoTHRnGHaQ1TEc_o1EwHAt-Tvkkb-ADQ")