Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Attempt 2 to add search command #4

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions cogs/Slash/Music.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from discord_slash import SlashContext, cog_ext, ComponentContext
from discord_slash.utils.manage_commands import create_option
from discord_slash.utils.manage_components import create_actionrow, create_button, wait_for_component
from discord_slash.utils.manage_components import create_actionrow, create_button, create_select_option, wait_for_component, create_select
from discord_slash.model import ButtonStyle
import discord
from discord.ext import commands
Expand All @@ -10,6 +10,7 @@
from cogs.Shared.Music import Song_queue
import constants.Haruno as Haruno
from cogs.Shared.Music import Song, YTDLSource, VoiceError
from youtube_search import YoutubeSearch


class MusicSlash(commands.Cog):
Expand Down Expand Up @@ -158,7 +159,7 @@ async def _loop_queue(self, ctx: SlashContext):
)
]
)
async def _play(self, ctx: SlashContext, *, song: str):
async def _play(self, ctx: SlashContext, song: str):
global Song_queue

await ctx.invoke(self._join)
Expand All @@ -182,6 +183,44 @@ async def _play(self, ctx: SlashContext, *, song: str):
song = Song(source)
await ctx.send(embed=song.create_embed(ctx.created_at, Haruno.Words.ENQUEUED))

@cog_ext.cog_slash(
name="search", description="Search for Music", guild_ids=guild_ids,
options=[
create_option(
name="song",
description="Search Query",
required=True,
option_type=3
)
]
)
async def _search(self, ctx: SlashContext, song: str):
await ctx.defer()

results = YoutubeSearch(song, max_results=10).to_dict()
text = ""
for i, song in enumerate(results):
text += f"`{i + 1}.` [**{song['title']} - {song['channel']} ({song['duration']})**](https://www.youtube.com/{song['url_suffix']})\n"

embed = discord.Embed(
description=f"**{len(results)} Results:**\n\n{text}", color=Haruno.COLOR
).set_footer(text="このハルノには夢がある ❄️")
select = create_select(
options=[
create_select_option(
f"{song['title']} - {song['channel']}", value=song['url_suffix'], emoji="❄️"
) for song in results],
placeholder="song",
min_values=1,
max_values=1,
)

msg = await ctx.send(embed=embed, components=[create_actionrow(select)])

button_ctx: ComponentContext = await wait_for_component(self.bot, components=[select])
url = f"https://www.youtube.com{button_ctx.selected_options[0]}"
await ctx.invoke(self._play, song=url)

@cog_ext.cog_slash(
name="remove", description="Remove Song from Queue", guild_ids=guild_ids,
options=[
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ youtube_dl
python-dotenv
discord-py-slash-command
asyncpraw
youtube-search