not-lain commited on
Commit
1bc04be
·
1 Parent(s): 036628e

switch to local audio streaming

Browse files
Files changed (2) hide show
  1. .gitignore +2 -1
  2. app.py +20 -41
.gitignore CHANGED
@@ -1 +1,2 @@
1
- .env
 
 
1
+ .env
2
+ assets/
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import discord
2
  from discord.ext import commands
3
- from discord import app_commands
4
- import yt_dlp # Changed from youtube_dl
5
- import asyncio
6
  import gradio as gr
7
  from dotenv import load_dotenv
8
  import os
@@ -11,33 +9,19 @@ import threading
11
  # Load environment variables
12
  load_dotenv()
13
 
 
 
 
 
 
14
  # Bot configuration
15
  intents = discord.Intents.default()
16
  intents.message_content = True
17
  bot = commands.Bot(command_prefix='!', intents=intents)
18
 
19
- # YouTube DL options
20
- ydl_opts = {
21
- 'format': 'bestaudio/best',
22
- 'postprocessors': [{
23
- 'key': 'FFmpegExtractAudio',
24
- 'preferredcodec': 'mp3',
25
- 'preferredquality': '192',
26
- }],
27
- 'noplaylist': True,
28
- 'nocheckcertificate': True,
29
- 'ignoreerrors': False,
30
- 'logtostderr': False,
31
- 'quiet': True,
32
- 'no_warnings': True,
33
- 'default_search': 'auto',
34
- 'source_address': '0.0.0.0'
35
- }
36
-
37
  # Music bot class
38
  class MusicBot:
39
  def __init__(self):
40
- self.queue = []
41
  self.is_playing = False
42
  self.voice_client = None
43
 
@@ -52,21 +36,17 @@ class MusicBot:
52
  await ctx.send("You need to be in a voice channel!")
53
 
54
  async def play_next(self, ctx):
55
- if len(self.queue) > 0:
56
  self.is_playing = True
57
- url = self.queue.pop(0)
58
-
59
- with yt_dlp.YoutubeDL(ydl_opts) as ydl: # Changed from youtube_dl
60
- info = ydl.extract_info(url, download=False)
61
- if 'entries' in info:
62
- url2 = info['entries'][0]['url']
63
- else:
64
- url2 = info['url']
65
-
66
- self.voice_client.play(discord.FFmpegPCMAudio(url2, **{'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'}),
67
- after=lambda e: asyncio.run_coroutine_threadsafe(self.play_next(ctx), bot.loop))
68
- else:
69
- self.is_playing = False
70
 
71
  music_bot = MusicBot()
72
 
@@ -82,18 +62,17 @@ async def on_ready():
82
  except Exception as e:
83
  print(f"An error occurred while syncing commands: {e}")
84
 
85
- @bot.tree.command(name="play", description="Play a song from URL")
86
- async def play(interaction: discord.Interaction, url: str):
87
  await interaction.response.defer()
88
  ctx = await commands.Context.from_interaction(interaction)
89
  await music_bot.join_voice(ctx)
90
- music_bot.queue.append(url)
91
 
92
  if not music_bot.is_playing:
93
  await music_bot.play_next(ctx)
94
- await interaction.followup.send('Playing music!')
95
  else:
96
- await interaction.followup.send('Added to queue!')
97
 
98
  # Replace the existing skip command with this version
99
  @bot.tree.command(name="skip", description="Skip the current song")
 
1
  import discord
2
  from discord.ext import commands
3
+ from huggingface_hub import hf_hub_download
 
 
4
  import gradio as gr
5
  from dotenv import load_dotenv
6
  import os
 
9
  # Load environment variables
10
  load_dotenv()
11
 
12
+ # Download song
13
+ if os.path.exists('assets') is False:
14
+ os.makedirs('assets', exist_ok=True)
15
+ hf_hub_download("not-lain/assets", "sample.mp3", repo_type="dataset",local_dir="assets")
16
+
17
  # Bot configuration
18
  intents = discord.Intents.default()
19
  intents.message_content = True
20
  bot = commands.Bot(command_prefix='!', intents=intents)
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  # Music bot class
23
  class MusicBot:
24
  def __init__(self):
 
25
  self.is_playing = False
26
  self.voice_client = None
27
 
 
36
  await ctx.send("You need to be in a voice channel!")
37
 
38
  async def play_next(self, ctx):
39
+ if not self.is_playing:
40
  self.is_playing = True
41
+ try:
42
+ audio_source = discord.FFmpegPCMAudio("assets/sample.mp3")
43
+ def after_playing(e):
44
+ self.is_playing = False
45
+ self.voice_client.play(audio_source, after=after_playing)
46
+ except Exception as e:
47
+ print(f"Error playing file: {e}")
48
+ await ctx.send("Error playing the song.")
49
+ self.is_playing = False
 
 
 
 
50
 
51
  music_bot = MusicBot()
52
 
 
62
  except Exception as e:
63
  print(f"An error occurred while syncing commands: {e}")
64
 
65
+ @bot.tree.command(name="play", description="Play the sample music")
66
+ async def play(interaction: discord.Interaction):
67
  await interaction.response.defer()
68
  ctx = await commands.Context.from_interaction(interaction)
69
  await music_bot.join_voice(ctx)
 
70
 
71
  if not music_bot.is_playing:
72
  await music_bot.play_next(ctx)
73
+ await interaction.followup.send('Playing sample music!')
74
  else:
75
+ await interaction.followup.send('Already playing!')
76
 
77
  # Replace the existing skip command with this version
78
  @bot.tree.command(name="skip", description="Skip the current song")