Files
Vocard/voicelink/views/search.py
Choco b503385542 Refactor modal usage and error handling in views
Replaced custom Modal class with BaseModal in embed_builder.py for consistency and maintainability. Updated modal instantiation to use custom_id and improved value handling. Removed redundant on_error methods from help.py, pagination.py, and search.py, and delegated error handling to superclass in controller.py for cleaner error management.
2025-10-06 15:56:30 +08:00

63 lines
2.4 KiB
Python

"""MIT License
Copyright (c) 2023 - present Vocard Development
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import discord
from ..objects import Track
class SearchDropdown(discord.ui.Select):
def __init__(self, tracks: list[Track], texts: list[str]) -> None:
self.view: SearchView
self.texts: list[str] = texts
super().__init__(
placeholder=texts[0],
min_values=1, max_values=len(tracks),
options=[
discord.SelectOption(label=f"{i}. {track.title[:50]}", description=f"{track.author[:50]} · {track.formatted_length}")
for i, track in enumerate(tracks, start=1)
]
)
async def callback(self, interaction: discord.Interaction) -> None:
self.disabled = True
self.placeholder = self.texts[1]
await interaction.response.edit_message(view=self.view)
self.view.values = self.values
self.view.stop()
class SearchView(discord.ui.View):
def __init__(self, tracks: list[Track], texts: list[str]) -> None:
super().__init__(timeout=60)
self.response: discord.Message = None
self.values: list[str] = None
self.add_item(SearchDropdown(tracks, texts))
async def on_timeout(self):
for child in self.children:
child.disabled = True
try:
await self.response.edit(view=self)
except:
pass