dj-embe/cog/music.py

92 lines
3.1 KiB
Python
Raw Normal View History

2023-04-27 23:19:18 +00:00
from logging import Logger
2023-05-06 01:16:26 +00:00
2023-05-16 23:04:16 +00:00
from discord import ApplicationContext, Bot, Cog, Embed, Interaction, slash_command
2023-05-06 01:16:26 +00:00
2023-05-16 23:04:16 +00:00
from service import PlaybackManager, QueueManager
2023-04-27 23:19:18 +00:00
from usecase import Sources
class Music(Cog):
def __init__(
2023-05-16 23:04:16 +00:00
self,
bot: Bot,
logger: Logger,
queueManager: QueueManager,
playbackManager: PlaybackManager,
sources: Sources,
2023-04-27 23:19:18 +00:00
):
self.bot = bot
self.logger = logger
self.queueManager = queueManager
2023-05-16 23:04:16 +00:00
self.playbackManager = playbackManager
2023-04-27 23:19:18 +00:00
self.sources = sources
@slash_command(name="play")
async def play(self, context: ApplicationContext, query: str):
2023-05-16 23:04:16 +00:00
if context.author.voice is None:
await context.respond("Not connected to a voice channel")
return
2023-04-27 23:19:18 +00:00
async with self.queueManager(context.guild_id) as queue:
2023-05-16 23:04:16 +00:00
interaction = await context.respond(f"Searching {query}...")
if not isinstance(interaction, Interaction):
return
2023-05-06 01:16:26 +00:00
entries = await self.sources.processQuery(interaction, query)
2023-05-16 23:04:16 +00:00
if entries is None:
await interaction.edit_original_response(content=f"{query} not found")
return
2023-05-06 01:16:26 +00:00
queue.add(entries)
2023-05-16 23:04:16 +00:00
await self.playbackManager.registerQueue(
context.guild_id,
queue,
context.channel,
context.author.voice.channel,
)
if entries[0].playlist is not None:
await interaction.edit_original_response(
content=f"{len(entries)} songs added to queue from playlist {entries[0].playlist.name}"
)
else:
await interaction.edit_original_response(
content=f"{entries[0].title.name} was added to queue"
)
@slash_command(name="nowplaying")
async def nowPlaying(self, context: ApplicationContext):
assert self.bot.user
async with self.queueManager(context.guild_id) as queue:
entry = queue.nowPlaying()
if entry is None:
await context.respond("No song playing")
return
requester = await self.bot.get_or_fetch_user(entry.requester)
assert requester is not None
embed = (
Embed(title=entry.title.name, url=entry.title.url)
.add_field(
name="Artist", value=f"[{entry.artist.name}]({entry.artist.url})"
)
.set_author(
name="Now Playing", icon_url=self.bot.user.display_avatar.url
)
.set_image(url=entry.thumbnail)
.set_footer(
text=requester.display_name, icon_url=requester.display_avatar.url
)
)
if entry.playlist is not None:
embed.add_field(
name="Playlist",
value=f"[{entry.playlist.name}]({entry.playlist.url})",
)
2023-04-27 23:19:18 +00:00
2023-05-16 23:04:16 +00:00
await context.respond(embed=embed)