I'm currently working on having my Discord music bot create a keyword search menu, similar to the one depicted in the image. I'm utilizing the ytsearch from the ytdlp library to retrieve the top five results for a keyword search. The search process often takes longer than 3 seconds, resulting in unsuccessful responses. I'm curious if any of you have effective strategies to reduce this search time.
I've attempted to reference this article for guidance. Although the code runs well, the issue of timeouts and failed responses persists, and I'm unsure how to address it.
Here is a snippet of my code:
from discord.commands import slash_command
import yt_dlp as youtube_dl
youtube_dl.utils.bug_reports_message = lambda: ''
ytdlopts = {
'format': 'bestaudio/best',
'extractaudio': True,
'outtmpl': 'downloads/%(extractor)s-%(id)s-%(title)s.%(ext)s',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'dump_single_json': True,
'default_search': 'auto',
'postprocessors': [{"key" : "FFmpegExtractAudio", "preferredcodec" : "mp3", "preferredquality" : "256"}],
'buffersize': 16777216,
'source_address': '0.0.0.0' # ipv6 addresses cause issues sometimes
}
ytdl = youtube_dl.YoutubeDL(ytdlopts)
async def song_search(self, ctx):
options = []
if ctx.value:
to_run = partial(ytdl.extract_info, f"ytsearch5:{ctx.value}", download=False)
info_dict = await asyncio.get_event_loop().run_in_executor(None, to_run)
if 'entries' in info_dict:
# Extract up to 5 items from the list of entries
entries = info_dict['entries'][:5]
for entry in entries:
if 'title' in entry:
options.append(entry['title'])
print(options)
return options
@slash_command(name="play", description="play")
@option("url",description="url", autocomplete = song_search)
async def play_(self, ctx, *, url: str):
await ctx.trigger_typing()
vc = ctx.voice_client
if not vc:
await self.join_channel(ctx)
await self.load_source_defer(ctx)
player = self.get_player(ctx)
source = await self.get_music_source(ctx, url)
await player.queue.put(source)
This is the error that appears
['Create Your Own Discord Bot in Python 3.10 Tutorial (2022 Edition)', 'The EASIEST Discord Chat Bot Tutorial On The Internet (Python 3.10) 2023', 'Code a Discord Bot with Python - Host for Free in the Cloud', 'Making a Discord Bot In Python (Part 1: Setup)', 'All you need to know about Buttons in Discord.py & Pycord | Ultimate Python Guide']
Task exception was never retrieved
future: <Task finished name='Task-44' coro=<ApplicationCommandMixin.on_application_command_auto_complete.<locals>.callback() done, defined at C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py:853> exception=NotFound('404 Not Found (error code: 10062): Unknown interaction')>
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 856, in callback
return await command.invoke_autocomplete_callback(ctx)
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 1011, in invoke_autocomplete_callback
return await ctx.interaction.response.send_autocomplete_result(
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\interactions.py", line 1017, in send_autocomplete_result
await self._locked_response(
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\interactions.py", line 1090, in _locked_response
await coro
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\webhook\async_.py", line 219, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction