-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
342 lines (308 loc) · 13.2 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# bot.py
import discord
from discord.ext import commands
import yt_dlp as youtube_dl
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import os
import asyncio
from dotenv import load_dotenv
import lyricsgenius
from commands import setup_commands # Ensure commands.py is in the same directory or properly imported
import logging
from messages import Messages # Ensure Messages is imported
import traceback
# Logging configuration
logging.basicConfig(level=logging.INFO)
# Load environment variables
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
SPOTIFY_CLIENT_ID = os.getenv('SPOTIFY_CLIENT_ID')
SPOTIFY_CLIENT_SECRET = os.getenv('SPOTIFY_CLIENT_SECRET')
GENIUS_ACCESS_TOKEN = os.getenv('GENIUS_ACCESS_TOKEN')
# Initialize Genius API client
genius = lyricsgenius.Genius(GENIUS_ACCESS_TOKEN)
# Define intents
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
intents.voice_states = True # Ensure voice_states intent is enabled
# Initialize the bot
bot = commands.Bot(command_prefix='/', intents=intents)
tree = bot.tree
# Spotify client setup
sp = spotipy.Spotify(client_credentials_manager=SpotifyClientCredentials(
client_id=SPOTIFY_CLIENT_ID, client_secret=SPOTIFY_CLIENT_SECRET))
# Ensure FFmpeg is installed and accessible
ffmpeg_path = 'ffmpeg' # Adjust this path if necessary
# yt-dlp options
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'quiet': True,
'default_search': 'auto',
'nocheckcertificate': True,
'no_warnings': True,
'ignoreerrors': True,
'logtostderr': False,
'skip_download': True,
'source_address': '0.0.0.0',
'noplaylist': True,
'youtube_skip_dash_manifest': True,
'geo_bypass': True,
'geo_bypass_country': 'DE', # Germany
}
ffmpeg_options = {
'options': '-vn -nostdin',
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class BaseSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, url, volume=0.5):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.web_url = data.get('webpage_url')
self.url = url # Original URL used to create the source
class YTDLSource(BaseSource):
@classmethod
async def from_url(cls, url, *, loop=None, stream=True):
"""Create a YTDLSource from a YouTube URL."""
loop = loop or asyncio.get_event_loop()
try:
data = await loop.run_in_executor(
None, lambda: ytdl.extract_info(url, download=not stream)
)
except Exception as e:
print(f"Error in YTDLSource.from_url: {e}")
data = None
if data is None:
raise Exception("Could not extract information from the URL.")
if 'entries' in data:
data = data['entries'][0]
if data is None:
raise Exception("Could not retrieve any data.")
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(
discord.FFmpegPCMAudio(filename, **ffmpeg_options),
data=data,
url=url
)
class SpotifySource(BaseSource):
@classmethod
async def from_spotify_url(cls, url, *, loop=None):
"""Create a SpotifySource from a Spotify URL."""
loop = loop or asyncio.get_event_loop()
try:
data = sp.track(url)
except Exception as e:
print(f"Error fetching track from Spotify: {e}")
data = None
if data is None:
raise Exception("Could not retrieve track information from Spotify.")
track_name = f"{data['name']} {data['artists'][0]['name']}"
youtube_url = await cls.search_youtube_url(track_name)
return await YTDLSource.from_url(youtube_url, loop=loop, stream=True)
@staticmethod
async def search_youtube_url(query):
"""Search YouTube for a track name and return the first result's URL."""
ytdl_opts = {
'format': 'bestaudio/best',
'quiet': True,
'default_search': 'ytsearch1',
'nocheckcertificate': True,
'no_warnings': True,
'ignoreerrors': True,
'logtostderr': False,
'skip_download': True,
'source_address': '0.0.0.0',
'youtube_skip_dash_manifest': True,
}
ytdl_search = youtube_dl.YoutubeDL(ytdl_opts)
loop = asyncio.get_event_loop()
try:
data = await loop.run_in_executor(
None, lambda: ytdl_search.extract_info(query, download=False)
)
except Exception as e:
print(f"Error in SpotifySource.search_youtube_url: {e}")
data = None
if data and 'entries' in data and len(data['entries']) > 0:
return data['entries'][0]['webpage_url']
else:
raise Exception("No results found on YouTube.")
class Music:
def __init__(self, bot):
self.bot = bot
self.queue = []
self.loop = False
self.current = None
self.skip_votes = set()
self.channel = None # Text channel to send messages
self.inactivity_timer = None # Timer for inactivity
self.view = None # Reference to the MusicControls view
async def play_next(self, guild):
"""Play the next song in the queue or handle looping."""
# Cancel any existing inactivity timer
if self.inactivity_timer:
self.inactivity_timer.cancel()
self.inactivity_timer = None
try:
is_looping = False
if self.loop and self.current:
is_looping = True
# Recreate the audio source using the original URL
if isinstance(self.current, YTDLSource):
source = await YTDLSource.from_url(self.current.url, loop=self.bot.loop, stream=True)
elif isinstance(self.current, SpotifySource):
source = await SpotifySource.from_spotify_url(self.current.url, loop=self.bot.loop)
else:
print("Unknown source type. Cannot loop.")
return
self.current = source
elif self.queue:
source = self.queue.pop(0)
self.current = source
is_looping = False # Not looping
else:
self.current = None
# Disable the buttons when playback ends
if self.view:
await self.view.disable_all_items()
self.view = None
# Start inactivity timer
self.inactivity_timer = self.bot.loop.create_task(self.disconnect_after_delay(guild))
return
# Check if voice client is connected
if guild.voice_client is None:
print("Voice client is not connected.")
return
def after_playing(error):
if error:
print(f"Player error: {error}")
coro = self.play_next(guild)
fut = asyncio.run_coroutine_threadsafe(coro, self.bot.loop)
try:
fut.result()
except Exception as e:
print(f"Error in after_playing: {e}")
guild.voice_client.play(self.current, after=after_playing)
# Send new message with controls if not looping
if not is_looping:
# Disable previous view
if self.view:
await self.view.disable_all_items()
embed = Messages.now_playing(self.current.title)
self.view = MusicControls(None, self, guild.voice_client)
message = await self.channel.send(embed=embed, view=self.view)
self.view.message = message # Reference to the message containing the view
else:
# Optionally update the existing message or do nothing
pass # No action needed when looping the same song
except Exception as e:
print(f"Exception in play_next: {e}")
traceback.print_exc()
# Handle the exception gracefully
if self.view:
await self.view.disable_all_items()
self.view = None
self.current = None
# Start inactivity timer
self.inactivity_timer = self.bot.loop.create_task(self.disconnect_after_delay(guild))
async def stop(self, guild):
"""Stop playback and clear the queue."""
if guild.voice_client:
await guild.voice_client.disconnect(force=True)
self.queue.clear()
self.loop = False
self.current = None
# Cancel inactivity timer if it's running
if self.inactivity_timer:
self.inactivity_timer.cancel()
self.inactivity_timer = None
# Disable buttons if they are active
if self.view:
await self.view.disable_all_items()
self.view = None
async def disconnect_after_delay(self, guild):
"""Disconnect the bot after a period of inactivity."""
await asyncio.sleep(180) # Wait for 3 minutes
# Check if the voice client is still connected and not playing
voice_client = guild.voice_client
if not voice_client or not voice_client.is_connected():
return
if voice_client.is_playing() or voice_client.is_paused():
return # Don't disconnect if still playing or paused
await self.stop(guild)
if guild.id in music_instances:
del music_instances[guild.id]
if self.channel:
await self.channel.send("Disconnected due to inactivity.")
# MusicControls View Class
class MusicControls(discord.ui.View):
def __init__(self, interaction, music, voice_client):
super().__init__(timeout=None)
self.interaction = interaction
self.music = music
self.voice_client = voice_client
self.message = None # Will be set when the message is sent
async def interaction_check(self, interaction: discord.Interaction) -> bool:
"""Ensure only users in the same voice channel can interact with the controls."""
if interaction.user.voice and interaction.user.voice.channel == self.voice_client.channel:
return True
else:
await interaction.response.send_message(
"You must be in the same voice channel to use these controls.",
ephemeral=True
)
return False
@discord.ui.button(label='Pause', style=discord.ButtonStyle.primary, emoji='⏸️')
async def pause_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.voice_client.is_playing():
self.voice_client.pause()
await interaction.response.send_message("Playback paused.", ephemeral=True)
else:
await interaction.response.send_message("Nothing is playing.", ephemeral=True)
@discord.ui.button(label='Resume', style=discord.ButtonStyle.primary, emoji='▶️')
async def resume_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.voice_client.is_paused():
self.voice_client.resume()
await interaction.response.send_message("Playback resumed.", ephemeral=True)
else:
await interaction.response.send_message("Playback is not paused.", ephemeral=True)
@discord.ui.button(label='Skip', style=discord.ButtonStyle.primary, emoji='⏭️')
async def skip_button(self, interaction: discord.Interaction, button: discord.ui.Button):
music = self.music
voice_client = self.voice_client
if voice_client.is_playing():
if music.queue or music.loop:
voice_client.stop()
await interaction.response.send_message("Skipped the current song.", ephemeral=True)
else:
await interaction.response.send_message("No more songs in the queue to skip to.", ephemeral=True)
else:
await interaction.response.send_message("Nothing is playing.", ephemeral=True)
@discord.ui.button(label='Loop', style=discord.ButtonStyle.primary, emoji='🔁')
async def loop_button(self, interaction: discord.Interaction, button: discord.ui.Button):
self.music.loop = not self.music.loop
status = "enabled" if self.music.loop else "disabled"
await interaction.response.send_message(f"Looping {status}.", ephemeral=True)
async def disable_all_items(self):
"""Disable all buttons in the view."""
for item in self.children:
item.disabled = True
if self.message:
await self.message.edit(view=self)
music_instances = {}
@bot.event
async def on_ready():
"""Event handler when the bot is ready."""
print("Bot is ready. Setting up commands...")
try:
setup_commands(tree, bot, sp, genius, music_instances, Music, YTDLSource, SpotifySource)
await tree.sync()
print("All commands synced successfully.")
except Exception as e:
print(f"Error during setup or sync: {e}")
print(f'Bot is logged in as {bot.user}')
bot.run(TOKEN)