music play + now playing

This commit is contained in:
2023-05-17 01:04:16 +02:00
parent 0749f742c3
commit d09c2b7a89
21 changed files with 410 additions and 143 deletions

View File

@ -15,7 +15,7 @@ class Greetings(Cog):
@default_permissions(administrator=True)
async def redis_set(self, context: ApplicationContext, key: str, value: str):
self.logger.info(f"redis set {value} at {key}")
await self.redis.set(key, value)
await self.redis.set("test", key, value)
await context.respond(f"redis set {value} at {key}")
@ -23,6 +23,6 @@ class Greetings(Cog):
@default_permissions(administrator=True)
async def redis_get(self, context: ApplicationContext, key: str):
self.logger.info(f"redis get {key}")
value = await self.redis.get(key)
value = await self.redis.get("test", key)
await context.respond(f"redis get {key}: {value}")

View File

@ -1,25 +1,91 @@
from logging import Logger
from discord import ApplicationContext, Bot, Cog, slash_command
from discord import ApplicationContext, Bot, Cog, Embed, Interaction, slash_command
from service import QueueManager
from service import PlaybackManager, QueueManager
from usecase import Sources
class Music(Cog):
def __init__(
self, bot: Bot, logger: Logger, queueManager: QueueManager, sources: Sources
self,
bot: Bot,
logger: Logger,
queueManager: QueueManager,
playbackManager: PlaybackManager,
sources: Sources,
):
self.bot = bot
self.logger = logger
self.queueManager = queueManager
self.playbackManager = playbackManager
self.sources = sources
@slash_command(name="play")
async def play(self, context: ApplicationContext, query: str):
async with self.queueManager(context.guild_id) as queue:
interaction = await context.respond(f"searching {query} ...")
entries = await self.sources.processQuery(interaction, query)
queue.add(entries)
if context.author.voice is None:
await context.respond("Not connected to a voice channel")
return
await interaction.edit_original_response(content=entries[0].title.name)
async with self.queueManager(context.guild_id) as queue:
interaction = await context.respond(f"Searching {query}...")
if not isinstance(interaction, Interaction):
return
entries = await self.sources.processQuery(interaction, query)
if entries is None:
await interaction.edit_original_response(content=f"{query} not found")
return
queue.add(entries)
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})",
)
await context.respond(embed=embed)