-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
471 lines (397 loc) · 17.1 KB
/
__main__.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
from __future__ import annotations
import asyncio
import base64
import json
import os
import sys
import time
import urllib.parse
from dataclasses import dataclass
from typing import Any, Optional
import aiohttp
import discord
from discord import (ButtonStyle, Guild, Interaction, Message, VoiceChannel,
VoiceClient, app_commands)
from discord.ext import tasks
from discord.ui import Button, View
from discord.utils import MISSING
from dotenv import load_dotenv
from loguru import logger
class BotConfig:
def __init__(self) -> None:
load_dotenv()
self.token = os.getenv("BOT_TOKEN")
self.guild_id = os.getenv("GUILD_ID")
if not self.guild_id or not self.token:
raise ValueError(
".env is not valid. Please provide GUILD_ID and BOT_TOKEN."
)
try:
self.guild_id = int(self.guild_id)
except ValueError:
raise ValueError("GUILD_ID must be a valid integer.")
self.guild = discord.Object(id=self.guild_id)
@dataclass
class SongMetadata:
query: str
session: aiohttp.ClientSession
BASE_URL: str = "https://itunes.apple.com/search"
async def fetch(self) -> dict:
try:
async with self.session.get(
self.BASE_URL,
params={"term": self.query, "media": "music", "limit": "1"},
headers={"Accept": "application/json"},
) as response:
return await response.json(content_type=None)
except Exception as e:
logger.error(f"Error fetching iTunes data: {e}")
return {"results": []}
async def get_song(self) -> Optional[dict]:
try:
data = await self.fetch()
return data["results"][0] if data.get("results") else None
except Exception as e:
logger.error(f"Error parsing song data: {e}")
return
@property
async def artwork(self) -> str | False:
song = await self.get_song()
if not song:
return False
return song.get(
"artworkUrl100", song.get("artworkUrl60", song.get("artworkUrl30", False))
)
class RadioBot:
def __init__(self, bot: Client) -> None:
self.base_url: str = "https://play5.newradio.it/player/license/3992"
self.bot: Client = bot
self.stream_url: Optional[str] = None
self.track_name_url: Optional[str] = None
self.current_track: str = "Unknown Track"
self.session: Optional[aiohttp.ClientSession] = None
self.track_info_updater: Optional[TrackInfoUpdater] = None
self.active_views: set[RadioControlView] = set()
self.file = discord.File("assets/thumbnail.png", filename="thumbnail.png")
self.placeholder = True
async def setup(self) -> None:
self.session = aiohttp.ClientSession()
self.track_info_updater = TrackInfoUpdater(self)
try:
await self.get_dynamic_url()
initial_track = await self.track_info_updater.fetch_track_name()
self.current_track = initial_track
await self.update_presence()
except Exception as e:
logger.error(f"Failed to fetch initial track info: {e}")
self.track_info_updater.start_updater()
logger.info("RadioBot setup completed")
async def cleanup(self) -> None:
if self.session and not self.session.closed:
await self.session.close()
logger.info("RadioBot cleanup completed")
async def get_dynamic_url(self) -> None:
timestamp = int(time.time() * 1000)
params: dict[str, Any] = {"_": timestamp}
async with self.session.get(self.base_url, params=params) as response:
response.raise_for_status()
data = await response.text()
decoded_data = base64.b64decode(data).decode("utf-8")
stream_data = json.loads(decoded_data)
self.stream_url = stream_data["streams"][0][0]["url"]
self.track_name_url = stream_data["streams"][0][0]["textUrl"]
logger.info("Stream URL and track name URL updated successfully.")
async def create_player_embed(
self,
status: str = "Now Playing",
description: Optional[str] = None,
color: discord.Color = discord.Color.green(),
) -> discord.Embed:
metadata = SongMetadata(self.current_track or description, self.session)
artwork = await metadata.artwork
if not artwork:
logger.warning(
f"No metadata found for {self.current_track or description}."
)
self.placeholder = not artwork
embed = discord.Embed(title="🎵 Radio ATAC Controls", color=color)
embed.add_field(
name=status,
value=f"```{description or self.current_track}```",
inline=False,
)
embed.set_footer(text="❤️🧡 Radio ATAC • Musica della città ❤️🧡")
embed.set_thumbnail(url=artwork or f"attachment://{self.file.filename}")
return embed
async def update_all_player_messages(
self,
track_name: str,
status: str = "Now Playing",
color: discord.Color = discord.Color.green(),
) -> None:
update_tasks = []
for view in list(self.active_views):
try:
if view.message:
embed = await self.create_player_embed(
status=status, description=track_name, color=color
)
update_tasks.append(
view.message.edit(
embed=embed,
attachments=[self.file] if self.placeholder else MISSING,
)
)
except (discord.NotFound, AttributeError):
self.active_views.discard(view)
continue
if update_tasks:
await asyncio.gather(*update_tasks, return_exceptions=True)
async def update_presence(self, track_name: Optional[str] = None) -> None:
if not self.bot.is_ready():
return
track_name = track_name or self.current_track
activity = discord.Activity(
type=discord.ActivityType.listening,
name=f"{track_name}",
state="📻 Radio ATAC",
details="🎶 Musica della città",
)
await self.bot.change_presence(activity=activity)
async def play_stream(self, voice_client: VoiceClient) -> bool:
await self.get_dynamic_url()
if self.stream_url:
ffmpeg_options = {
"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5",
"options": "-vn -b:a 192k",
}
voice_client.stop()
voice_client.play(
discord.FFmpegPCMAudio(self.stream_url, **ffmpeg_options),
after=lambda e: logger.error(f"Stream error: {e}") if e else None,
)
await self.update_presence()
return True
return False
async def start_stream(self, interaction: Interaction) -> bool:
if not isinstance(interaction.guild, Guild):
await interaction.response.send_message(
"This command can only be used in a guild.", ephemeral=True
)
return False
if not interaction.user.voice:
await interaction.response.send_message(
"Please join a voice channel first.", ephemeral=True
)
logger.warning("Attempted to start stream without user in voice channel.")
return False
if interaction.guild.voice_client:
if interaction.guild.voice_client.channel == interaction.user.voice.channel:
view = RadioControlView(self)
embed = await self.create_player_embed()
await interaction.response.send_message(
embed=embed,
file=self.file if self.placeholder else MISSING,
view=view,
ephemeral=True,
)
view.message = await interaction.original_response()
self.active_views.add(view)
logger.info("Sent radio controls for existing stream.")
return True
else:
await interaction.response.send_message(
"I'm already in another voice channel! Please use /leave first.",
ephemeral=True,
)
logger.info("Attempted to join while bot was in another channel.")
return False
await interaction.response.defer()
voice_channel = interaction.user.voice.channel
if not isinstance(voice_channel, VoiceChannel):
await interaction.followup.send(
"Cannot join this type of voice channel.", ephemeral=True
)
return False
voice_client = await voice_channel.connect()
if await self.play_stream(voice_client):
view = RadioControlView(self)
embed = await self.create_player_embed()
message = await interaction.followup.send(
embed=embed,
file=self.file if self.placeholder else MISSING,
view=view,
ephemeral=True,
)
view.message = message
self.active_views.add(view)
logger.info(f"Radio stream started in channel: {voice_channel.name}")
return True
else:
await interaction.followup.send(
content="Failed to retrieve stream URL.", ephemeral=True
)
logger.error("Stream URL is not set.")
return False
class TrackInfoUpdater:
def __init__(self, radio_bot: RadioBot) -> None:
self.radio_bot = radio_bot
async def fetch_track_name(self) -> str:
if not self.radio_bot.track_name_url:
return "Unknown Track"
async with self.radio_bot.session.get(
f"https://play5.newradio.it{self.radio_bot.track_name_url}"
) as response:
response.raise_for_status()
data = await response.text()
parsed_data = urllib.parse.parse_qs(data)
track_name = parsed_data.get("title", ["Unknown Track"])[0]
return track_name
@tasks.loop(seconds=5)
async def update_track_name(self) -> None:
try:
track_name = await self.fetch_track_name()
if track_name != self.radio_bot.current_track:
self.radio_bot.current_track = track_name
await asyncio.gather(
self.radio_bot.update_presence(track_name),
self.radio_bot.update_all_player_messages(track_name),
return_exceptions=True,
)
logger.info(f"Updated track name to: {track_name}")
except Exception as e:
logger.error(f"Error updating track name: {e}")
def start_updater(self) -> None:
self.update_track_name.start()
class RadioControlView(View):
def __init__(self, radio_bot: RadioBot) -> None:
super().__init__(timeout=None)
self.radio_bot = radio_bot
self.message: Optional[Message] = None
@discord.ui.button(label="", style=ButtonStyle.primary, emoji="⏸️")
async def pause_button(self, interaction: Interaction, button: Button) -> None:
if not isinstance(interaction.guild, Guild):
return
if (
interaction.guild.voice_client
and interaction.guild.voice_client.is_playing()
):
interaction.guild.voice_client.pause()
await self.radio_bot.update_all_player_messages(
self.radio_bot.current_track,
status="Paused",
color=discord.Color.orange(),
)
await interaction.response.defer()
logger.info("Radio paused.")
@discord.ui.button(label="", style=ButtonStyle.success, emoji="⏯️")
async def resume_button(self, interaction: Interaction, button: Button) -> None:
if not isinstance(interaction.guild, Guild):
return
vc = interaction.guild.voice_client
if not vc:
if not interaction.user.voice:
await interaction.response.send_message(
"Please join a voice channel first.", ephemeral=True
)
return
if not isinstance(interaction.user.voice.channel, VoiceChannel):
await interaction.response.send_message(
"Cannot join this type of voice channel.", ephemeral=True
)
return
await interaction.response.defer()
vc = await interaction.user.voice.channel.connect()
if await self.radio_bot.play_stream(vc):
await self.radio_bot.update_all_player_messages(
self.radio_bot.current_track, color=discord.Color.green()
)
logger.info("Radio resumed with new connection.")
return
elif vc.is_paused():
vc.resume()
await self.radio_bot.update_all_player_messages(
self.radio_bot.current_track,
status="Now Playing",
color=discord.Color.green(),
)
await self.radio_bot.update_presence()
await interaction.response.defer()
logger.info("Radio resumed.")
else:
await interaction.response.defer()
logger.info("Radio already playing.")
@discord.ui.button(label="", style=ButtonStyle.danger, emoji="⏹️")
async def stop_button(self, interaction: Interaction, button: Button) -> None:
if not isinstance(interaction.guild, Guild):
return
if interaction.guild.voice_client:
vc = interaction.guild.voice_client
vc.stop()
await vc.disconnect()
# Update all player messages to indicate stopped state
await self.radio_bot.update_all_player_messages(
"Disconnected from voice channel",
status="Stopped",
color=discord.Color.red(),
)
await interaction.response.defer()
logger.info("Radio stopped and disconnected from voice channel.")
class Client(discord.Client):
def __init__(self, *, intents: discord.Intents) -> None:
super().__init__(intents=intents)
self.tree = app_commands.CommandTree(self)
self.radio_bot = RadioBot(self)
self.config = BotConfig()
async def setup_hook(self) -> None:
await self.tree.sync(guild=self.config.guild)
await self.radio_bot.setup()
logger.info("Commands registered and synced to guild.")
async def close(self) -> None:
await self.radio_bot.cleanup()
await super().close()
async def on_ready(self) -> None:
logger.info(f"Logged in as {self.user}")
def setup_bot() -> Client:
intents = discord.Intents.default()
intents.message_content = True
return Client(intents=intents)
client = setup_bot()
@client.tree.command(
name="join", description="Join the voice channel and start streaming"
)
async def join(interaction: discord.Interaction):
await client.radio_bot.start_stream(interaction)
logger.info(
f"Join command executed for channel: {interaction.user.voice.channel.name if interaction.user.voice else 'None'}"
)
@client.tree.command(name="leave", description="Leave the voice channel")
async def leave(interaction: Interaction) -> None:
if not isinstance(interaction.guild, Guild):
await interaction.response.send_message(
"This command can only be used in a guild.", ephemeral=True
)
return
if interaction.guild.voice_client:
await interaction.guild.voice_client.disconnect()
await interaction.response.send_message(
"Disconnected from the voice channel.", ephemeral=True
)
logger.info("Disconnected from the voice channel.")
else:
await interaction.response.send_message(
"I'm not in a voice channel.", ephemeral=True
)
@client.tree.command(
name="radio", description="Show radio controls and join voice channel"
)
async def radio_control(interaction: Interaction) -> None:
await client.radio_bot.start_stream(interaction)
logger.info("Radio command executed")
if __name__ == "__main__":
if os.name == "nt":
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
try:
client.run(client.config.token)
except KeyboardInterrupt:
sys.exit()