diff --git a/bot/commands/__init__.py b/bot/commands/__init__.py index 24982df..5de936d 100644 --- a/bot/commands/__init__.py +++ b/bot/commands/__init__.py @@ -40,6 +40,7 @@ class CommandProcessor: "contacts": user_commands.ContactsBot, "help": user_commands.HelpCommand, "p": user_commands.PlayPauseCommand, + "e": user_commands.QueueCommand, "u": user_commands.PlayUrlCommand, "sv": user_commands.ServiceCommand, "s": user_commands.StopCommand, diff --git a/bot/commands/user_commands.py b/bot/commands/user_commands.py index e601c87..0413c20 100644 --- a/bot/commands/user_commands.py +++ b/bot/commands/user_commands.py @@ -1,6 +1,7 @@ from __future__ import annotations from typing import List, Optional, TYPE_CHECKING import os +import re from bot.commands.command import Command from bot.player.enums import Mode, State, TrackType @@ -582,6 +583,68 @@ class DownloadCommand(Command): else: return self.translator.translate("Nothing is playing") +class QueueCommand(Command): + @property + def help(self) -> str: + return self.translator.translate( + "QUERY Adds a track to the queue. If no track is playing, plays immediately." + ) + + def __call__(self, arg: str, user: User) -> Optional[str]: + if not arg: + raise errors.InvalidArgumentError + + self.run_async( + self.ttclient.send_message, + self.translator.translate("Searching..."), + user, + ) + + try: + if re.match(r'http[s]?://', arg): + # Kalau URL, langsung stream + tracks = self.module_manager.streamer.get(arg, user.is_admin if user is not None else True) + self.player.add_to_queue(tracks) + + # Kalau gak lagi main apa-apa, langsung play + if not self.player.is_playing(): + self.player.play_next() + + if self.config.general.send_channel_messages: + self.run_async( + self.ttclient.send_message, + self.translator.translate( + "{nickname} added a stream URL to the queue." + ).format(nickname=user.nickname), + type=2, + ) + return self.translator.translate("Added stream URL to the queue.") + else: + # Kalau bukan URL, cari lagu dari service + track_list = self.service_manager.service.search(arg) + self.player.add_to_queue(track_list) + + if not self.player.is_playing(): + self.player.play_next() + + if self.config.general.send_channel_messages: + self.run_async( + self.ttclient.send_message, + self.translator.translate( + "{nickname} added {request} to the queue." + ).format(nickname=user.nickname, request=arg), + type=2, + ) + return self.translator.translate("Added {} to the queue.").format( + track_list[0].name + ) + except errors.NothingFoundError: + return self.translator.translate("Nothing is found for your query") + except errors.ServiceError: + return self.translator.translate( + "The selected service is currently unavailable" + ) + class ChangeLogCommand(Command): @property def help(self) -> str: diff --git a/bot/player/__init__.py b/bot/player/__init__.py index c1307d8..889b509 100644 --- a/bot/player/__init__.py +++ b/bot/player/__init__.py @@ -181,6 +181,25 @@ class Player: raise ValueError() self._player.speed = arg + def add_to_queue(self, tracks: List[Track]) -> None: + """Adds tracks to the queue.""" + self.track_list.extend(tracks) + logging.debug(f"Added {len(tracks)} track(s) to the queue.") + + # If nothing is playing, start playing the next track + if self.state == State.Stopped and len(self.track_list) > 0: + self.play_next() + + def play_next(self) -> None: + """Play the next track in the queue.""" + if len(self.track_list) > 0: + self.track_index = 0 # Start with the first track + self.track = self.track_list[self.track_index] + self._play(self.track.url) + self.state = State.Playing + else: + self.state = State.Stopped + def seek_back(self, step: Optional[float] = None) -> None: step = step if step else self.config.seek_step if step <= 0: diff --git a/changelog.txt b/changelog.txt index d82541a..bc35152 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,6 @@ This change log is written to find out the changes that have been made by Pandora, and the source code still refers to TTMediaBot. 5/5/2025 +Added new command: e, to add either link or new song to the extended track. if nothing is playing, it plays the track directly. Fix user rights to upload file, hopefully... 5/4/2025