33import asyncio
44from datetime import timedelta , datetime
55from config import os
6- import fastf1 as f1api
7- import classement as ldb
6+ import yt_dlp
87import json
9- from error_embed import info_embed , no_prono
8+ from error_embed import info_embed
109
1110
1211def ensure_file_exists (path ):
@@ -23,8 +22,8 @@ async def help(interaction: discord.Interaction):
2322 )
2423
2524 # Commandes utilisateurs
26- embed .add_field (
27- name = "/help" , value = "📖 Affiche cette liste d’aide" , inline = False )
25+ embed .add_field (name = "/help" ,
26+ value = "📖 Affiche cette liste d’aide" , inline = False )
2827 embed .add_field (name = "/pronos_course" ,
2928 value = "🏁 Enregistre ou modifie ton pronostic pour la **course**" , inline = False )
3029 embed .add_field (name = "/pronos_qualif" ,
@@ -35,8 +34,10 @@ async def help(interaction: discord.Interaction):
3534 value = "🏆 Affiche le classement général" , inline = False )
3635 embed .add_field (name = "/presentation" ,
3736 value = "🤖 Laisse le bot se présenter et choisis-lui un nom" , inline = False )
38- embed .add_field (
39- name = "/rules" , value = "📏 Affiche le règlement d’utilisation du bot" , inline = False )
37+ embed .add_field (name = "/rules" ,
38+ value = "📏 Affiche le règlement d’utilisation du bot" , inline = False )
39+ embed .add_field (name = "/play_music" ,
40+ value = "🎶 Joue une chanson demander (Reservé au Booster)" , inline = False )
4041
4142 # Commandes admin
4243 embed .add_field (
@@ -164,3 +165,59 @@ async def presentation_bot(interaction: discord.Interaction):
164165 else :
165166 logger .info (
166167 f"Présentation par { interaction .user .name } dans { interaction .channel .name } sur { interaction .guild .name } " )
168+
169+
170+ async def music_play (interaction : discord .Interaction , song_name : str ):
171+ intents = discord .Intents .default ()
172+ intents .message_content = True
173+ intents .voice_states = True
174+
175+ if not interaction .guild .voice_client :
176+ if interaction .user .voice :
177+ channel = interaction .user .voice .channel
178+ await channel .connect ()
179+ await interaction .response .send_message ("✅ Rejoint le salon vocal" , ephemeral = True )
180+ else :
181+ await interaction .response .send_message ("❌ Tu dois être dans un salon vocal" , ephemeral = True )
182+ return
183+
184+ voice = interaction .guild .voice_client
185+
186+ if not voice :
187+ if interaction .user .voice :
188+ channel = interaction .user .voice .channel
189+ voice = await channel .connect ()
190+ else :
191+ await interaction .response .send_message ("❌ Tu dois être dans un salon vocal" , ephemeral = True )
192+ return
193+
194+ await interaction .response .send_message (f"🔍 Recherche : **{ song_name } **" , ephemeral = True )
195+
196+ ydl_opts = {'format' : 'bestaudio' , 'noplaylist' : 'True' }
197+ with yt_dlp .YoutubeDL (ydl_opts ) as ydl :
198+ try :
199+ info = ydl .extract_info (f"ytsearch:{ song_name } " , download = False )['entries' ][0 ]
200+ stream_url = info ['url' ]
201+ found_title = info .get ('title' , song_name )
202+ except Exception as e :
203+ await interaction .followup .send (f"❌ Impossible de lire la musique. Erreur : { e } " )
204+ return
205+
206+ # Définir la fonction avant de l'utiliser
207+ async def auto_leave_if_idle (voice_client , delay = 300 ):
208+ await asyncio .sleep (delay )
209+ if not voice_client .is_playing () and not voice_client .is_paused ():
210+ await voice_client .disconnect ()
211+ logger .info ("⏹️ Déconnexion automatique après 5 minutes d'inactivité." )
212+ await interaction .followup .send ("⏹️ Déconnexion automatique après 5 minutes d'inactivité.**" )
213+ # Ici, interaction n'est plus garanti safe. Tu peux notifier ailleurs si besoin.
214+
215+ def after_playing (err ):
216+ if err :
217+ print (f"Erreur de lecture : { err } " )
218+ asyncio .create_task (auto_leave_if_idle (voice ))
219+
220+ audio_source = discord .FFmpegPCMAudio (stream_url )
221+ voice .play (audio_source , after = after_playing )
222+
223+ await interaction .followup .send (f"🎶 Lecture : **{ found_title } **" )
0 commit comments