not-lain commited on
Commit
4580906
·
1 Parent(s): 932c444
Files changed (1) hide show
  1. app.py +11 -55
app.py CHANGED
@@ -50,33 +50,16 @@ class MusicBot:
50
  url = self.queue.pop(0)
51
 
52
  try:
53
- ydl_opts = {
54
- 'format': 'bestaudio/best',
55
- 'cookiefile': 'cookies.txt',
56
- 'quiet': True,
57
- 'no_warnings': True,
58
- 'extract_flat': False, # Changed this to False for proper extraction
59
- 'postprocessors': [{
60
- 'key': 'FFmpegExtractAudio',
61
- 'preferredcodec': 'opus',
62
- }]
63
- }
64
-
65
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
66
  info = ydl.extract_info(url, download=False)
67
- if 'entries' in info: # It's a playlist
68
- audio_url = info['entries'][0]['url']
69
- title = info['entries'][0].get('title', 'Unknown title')
70
- else: # It's a single video
71
- audio_url = info['url']
72
- title = info.get('title', 'Unknown title')
73
 
74
- # Updated FFmpeg options for better streaming
75
  ffmpeg_options = {
76
  'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
77
- 'options': '-vn -filter:a "volume=0.5"'
78
  }
79
-
80
  audio_source = discord.FFmpegPCMAudio(audio_url, **ffmpeg_options)
81
 
82
  def after_playing(error):
@@ -85,36 +68,18 @@ class MusicBot:
85
  print(f"Playback error: {error}")
86
  asyncio.run_coroutine_threadsafe(self.play_next(ctx), bot.loop)
87
 
88
- self.voice_client.play(audio_source, after=after_playing)
 
 
 
89
  await ctx.send(f"Now playing: {title}")
90
 
91
- except yt_dlp.utils.DownloadError as e:
92
- print(f"YT-DLP Error for URL {url}: {e}")
93
- self.is_playing = False
94
- await ctx.send("Error playing the song: Could not extract audio stream")
95
- await self.play_next(ctx)
96
  except Exception as e:
97
- print(f"General Error playing URL {url}: {e}")
98
  self.is_playing = False
99
- await ctx.send("An unexpected error occurred while playing the song")
100
  await self.play_next(ctx)
101
 
102
- async def get_queue_info(self):
103
- if not self.queue:
104
- return "Queue is empty"
105
-
106
- try:
107
- queue_info = []
108
- with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
109
- for i, url in enumerate(self.queue, 1):
110
- info = ydl.extract_info(url, download=False)
111
- title = info.get('title', 'Unknown title')
112
- queue_info.append(f"{i}. {title}")
113
-
114
- return "\n".join(queue_info)
115
- except Exception as e:
116
- return f"Error fetching queue info: {str(e)}"
117
-
118
  music_bot = MusicBot()
119
 
120
  @bot.event
@@ -170,15 +135,6 @@ async def leave(interaction: discord.Interaction):
170
  else:
171
  await interaction.response.send_message('Bot is not in a voice channel!')
172
 
173
- @bot.tree.command(name="queue", description="Show the current music queue")
174
- async def queue(interaction: discord.Interaction):
175
- if music_bot.is_playing:
176
- await interaction.response.defer()
177
- queue_info = await music_bot.get_queue_info()
178
- await interaction.followup.send(f"**Current Queue:**\n{queue_info}")
179
- else:
180
- await interaction.response.send_message("No music is currently playing!")
181
-
182
  def run_discord_bot():
183
  bot.run(os.getenv('DISCORD_TOKEN'))
184
 
 
50
  url = self.queue.pop(0)
51
 
52
  try:
53
+ with yt_dlp.YoutubeDL(self.ydl_opts) as ydl:
 
 
 
 
 
 
 
 
 
 
 
 
54
  info = ydl.extract_info(url, download=False)
55
+ audio_url = info['url']
56
+ title = info.get('title', 'Unknown title')
 
 
 
 
57
 
58
+ # Create FFmpeg audio source
59
  ffmpeg_options = {
60
  'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
61
+ 'options': '-vn -bufsize 64k'
62
  }
 
63
  audio_source = discord.FFmpegPCMAudio(audio_url, **ffmpeg_options)
64
 
65
  def after_playing(error):
 
68
  print(f"Playback error: {error}")
69
  asyncio.run_coroutine_threadsafe(self.play_next(ctx), bot.loop)
70
 
71
+ # Apply volume transformation and play
72
+ transformed_source = discord.PCMVolumeTransformer(audio_source, volume=0.5)
73
+ self.voice_client.play(transformed_source, after=after_playing)
74
+
75
  await ctx.send(f"Now playing: {title}")
76
 
 
 
 
 
 
77
  except Exception as e:
78
+ print(f"Error playing URL {url}: {e}")
79
  self.is_playing = False
80
+ await ctx.send(f"Error playing the song: {str(e)}")
81
  await self.play_next(ctx)
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  music_bot = MusicBot()
84
 
85
  @bot.event
 
135
  else:
136
  await interaction.response.send_message('Bot is not in a voice channel!')
137
 
 
 
 
 
 
 
 
 
 
138
  def run_discord_bot():
139
  bot.run(os.getenv('DISCORD_TOKEN'))
140