El titulo ya lo dice pero voy a explayarme en que, despues de unos segundos (aprox 70) o hasta la mitad de la cancion deja de funcionar el bot. Soy algo nuevo en esto de programacion asi que, quizas, mi codigo esta bastante desorganizado, alguna ayuda?
The title already says it but I’m going to elaborate that, after a few seconds (approximately 70) or even halfway through the song, the bot stops working. I’m somewhat new to programming so, perhaps, my code is quite disorganized, any help? (yes, I used google translate)
import discord
from discord.ext import commands
import yt_dlp
import config
import asyncio
bot = commands.Bot(command_prefix='m!', intents=discord.Intents.all())
@bot.event
async def on_ready():
print(f'{bot.user} lista para reproducir música')
ffmpeg_options = {'options': '-vn'}
ydl_opts = {
'format': 'bestaudio/best',
'quiet': False,
'noplaylist': False,
'ignoreerrors': True,
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
class Queue:
def __init__(self):
self.queue = []
def add(self, item):
self.queue.append(item)
def remove(self):
if self.queue:
return self.queue.pop(0)
return None
queue = Queue()
@bot.command()
async def ping(ctx):
await ctx.send("!Pong")
@bot.command()
async def join(ctx):
if ctx.author.voice is None or ctx.author.voice.channel is None:
await ctx.send("¡Necesitas estar en un canal de voz para usar este comando!")
return
voice_channel = ctx.author.voice.channel
if ctx.voice_client is not None:
await ctx.voice_client.move_to(voice_channel)
else:
await voice_channel.connect()
await ctx.send(f"Me he unido a {voice_channel}")
@bot.command()
async def leave(ctx):
if ctx.voice_client is not None:
await ctx.voice_client.disconnect()
await ctx.send("Bye Bye")
else:
await ctx.send("No estoy en un canal de voz")
@bot.command()
async def play(ctx, *, query):
if ctx.author.voice is None or ctx.author.voice.channel is None:
await ctx.send("¡Necesitas estar en un canal de voz para usar este comando!")
return
voice_channel = ctx.author.voice.channel
try:
vc = ctx.voice_client
if not vc:
await voice_channel.connect()
vc = ctx.voice_client
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(f"ytsearch:{query}", download=False)
url = info['entries'][0]['url']
queue.add(url)
if not vc.is_playing():
play_next(ctx)
await ctx.send(f"Reproduciendo: {info['entries'][0]['title']}")
else:
await ctx.send(f"{info['entries'][0]['title']} añadida a la cola de reproducción")
except Exception as e:
print(f"Error al reproducir audio: {e}")
await ctx.send("Ocurrió un error al reproducir el audio.")
@bot.command()
async def skip(ctx):
if ctx.voice_client is None or not ctx.voice_client.is_playing():
await ctx.send("No hay nada reproduciéndose que se pueda saltar.")
return
ctx.voice_client.stop()
await ctx.send("Canción saltada. ¡Siguiente canción en reproducción!")
play_next(ctx)
def play_next(ctx):
url = queue.remove()
if url:
ctx.voice_client.play(discord.FFmpegPCMAudio(url), after=lambda e: play_next(ctx))
@bot.command()
async def disconnect(ctx):
if ctx.voice_client is None:
await ctx.send("No estoy en un canal de voz.")
return
await ctx.voice_client.disconnect()
await ctx.send("Me he desconectado del canal de voz.")
@bot.event
async def on_voice_state_update(member, before, after):
if bot.user in member.guild.voice_channels and len(member.guild.voice_channels) == 1:
await asyncio.sleep(20)
if len(member.guild.voice_channels) == 1 and bot.user in member.guild.voice_channels:
await member.guild.voice_client.disconnect()
bot.run(config.TOKEN)
Reproducir musica o canciones completas
Mikusita UwU is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.