Files
Vocard/voicelink/queue.py
Choco 8a663a939b Vocard v2.6.9 Update: A lot new features, bug fixes, and code clean-up (#34)
* Update docker-compose.yml

No dotenv file needed. All environment values are now defined in docker-compose

* Update RU.json

* Updated docker-compose

* Update README.md

* Resolved issue where playback didn’t start after user joined channel 247

* Added logging system

* Updated logging system

* Update player.py

* Added 24/7 required intent

* Update main.py

* Added logs folder

* Updated logging system

* Updated logging system

* Added some typehint

* Removed error_log

* Renamed a valuable

* Removed unnecessary value

* Fixed some bugs

* Added bot status settings

* Added autocomplete in playtop

* Added a new emoji in Loop button

* Optimized code

* Update requirements.txt

* Fixed autoplay with empty queue

This commit addresses the issue where autoplay was being activated even when the history queue had no tracks. The problem has been resolved to ensure smooth user experience

* Enhanced Loop Control Button Functionality

* Updated the loop button in controller

* Fixed activity update

Resolved the issue where activity updates were not reflected when the value remained the same.

* Added sync function in debug view

* Optimized code

* Added version in the debug

* Added nodes panel in debug view

* Merged .env into settings.json

* Rewritten ipc_client

* Separated the dashboard from the project

* Removed some necessary requirements

* Remove support for replit

* Update README.md

* Update settings Example.json

* Added get_recommendations into node class

* Saved log file when updating

* Rewritten some method in ipc_client

* Written swap and remove method in queue

* Added remove_track and swap_track method in player

* Update methods.py

* Added secure protocol in ipc client

* Fixed some bugs

* Added some method in ipc cilent

* Update methods.py

* Update methods.py

* Added playlist method

* Optimized Spotify client code

* Added Category class in Spotify client

* Added settings method in ipc client

* Fixed some bugs

* Send track requester_id in initPlayer

* Fixed some bugs

* Fixed some bugs

* Optimized some code

* Dump discord.py

* Added start and end position for each track

* Update methods.py

* Fixed a bug

* Fixed some bugs

* Fixed IPC client creation issue

* Rewrote filters

* Fixed some bugs

* Added filter event in ipc method

* Added filter selector in music controller

* Added inbox method

* Optimized some code

* Updated zh-TW local_lang

* Fixed some bugs

* Delete supervisord.conf

* Fixed some bugs

* Updated wording in IPC client

* Update pool.py

* Fixed no song suggestions when spotify_client is None

* Fixed connection problem from the dashboard

* Fixed some bugs

* Added voice status (#33)

* Updated the placeholder name for clarity

* Added voice status

* Dump bot version

* Added global template for voice status

* Added new placeholder

* Updated settings view

* Updated version tag

* Improved codes

* Fixed some bugs

* Supported clear queue endpoint in ipc

* Update methods.py

* Update README.md

* Remove PLACEHOLDERS.MD

---------

Co-authored-by: Azarath7 <158289825+Azarath7@users.noreply.github.com>
2024-09-11 11:51:51 +08:00

219 lines
7.5 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.
"""
from .exceptions import QueueFull, OutofList
from .objects import Track
from .enums import LoopType
from typing import Optional, Tuple, List, Callable, Dict
from itertools import cycle
from discord import Member
class LoopTypeCycle:
def __init__(self) -> None:
self._cycle = cycle(LoopType)
self.current = next(self._cycle)
def next(self) -> LoopType:
self.current = next(self._cycle)
return self.current
def set_mode(self, value: LoopType) -> LoopType:
while next(self._cycle) != value:
pass
self.current = value
return value
@property
def mode(self) -> LoopType:
return self.current
def __str__(self) -> str:
return self.current.name.capitalize()
class Queue:
def __init__(self, size: int, allow_duplicate: bool, get_msg: Callable[[str], str]) -> None:
self._queue: List[Track] = []
self._position: int = 0
self._size: int = size
self._repeat: LoopTypeCycle = LoopTypeCycle()
self._repeat_position: int = 0
self._allow_duplicate: bool = allow_duplicate
self.get_msg = get_msg
def get(self) -> Optional[Track]:
track = None
try:
track = self._queue[self._position - 1 if self._repeat.mode == LoopType.track else self._position]
if self._repeat.mode != LoopType.track:
self._position += 1
except:
if self._repeat.mode == LoopType.queue:
try:
track = self._queue[self._repeat_position]
self._position = self._repeat_position + 1
except IndexError:
self._repeat.set_mode(LoopType.off)
return track
def put(self, item: Track) -> int:
if self.count >= self._size:
raise QueueFull(self.get_msg("voicelinkQueueFull").format(self._size))
self._queue.append(item)
return self.count
def put_at_front(self, item: Track) -> int:
if self.count >= self._size:
raise QueueFull(self.get_msg("voicelinkQueueFull").format(self._size))
self._queue.insert(self._position, item)
return 1
def put_at_index(self, index: int, item: Track) -> None:
if self.count >= self._size:
raise QueueFull(self.get_msg("voicelinkQueueFull").format(self._size))
return self._queue.insert(self._position - 1 + index, item)
def skipto(self, index: int) -> None:
if not 0 < index <= self.count:
raise OutofList(self.get_msg("voicelinkOutofList"))
else:
self._position += index - 1
def backto(self, index: int) -> None:
if not self._position - index >= 0:
raise OutofList(self.get_msg("voicelinkOutofList"))
else:
self._position -= index
def history_clear(self, is_playing: bool) -> None:
self._queue[:self._position - 1 if is_playing else self._position] = []
self._position = 1 if is_playing else 0
def clear(self) -> None:
del self._queue[self._position:]
def replace(self, queue_type: str, replacement: list) -> None:
if queue_type == "queue":
self.clear()
self._queue += replacement
elif queue_type == "history":
self._queue[:self._position] = replacement
def swap(self, track_index1: int, track_index2: int) -> Tuple[Track, Track]:
try:
adjusted_position = self._position - 1
self._queue[adjusted_position + track_index1], self._queue[adjusted_position + track_index2] = self._queue[adjusted_position + track_index2], self._queue[adjusted_position + track_index1]
return self._queue[adjusted_position + track_index1], self._queue[adjusted_position + track_index2]
except IndexError:
raise OutofList(self.get_msg("voicelinkOutofList"))
def move(self, target: int, to: int) -> Optional[Track]:
if not 0 < target <= self.count or not 0 < to:
raise OutofList(self.get_msg("voicelinkOutofList"))
try:
item = self._queue[self._position + target - 1]
self._queue.remove(item)
self.put_at_index(to, item)
return item
except:
raise OutofList(self.get_msg("voicelinkOutofList"))
def remove(self, index: int, index2: int = None, member: Member = None) -> Dict[int, Track]:
pos = self._position - 1
if index2 is None:
index2 = index
elif index2 < index:
index, index2 = index2, index
try:
removed_tracks: Dict[str, Track] = {}
for i, track in enumerate(self._queue[pos + index: pos + index2 + 1]):
if member and track.requester != member:
continue
self._queue.remove(track)
removed_tracks[pos + index + i] = track
return removed_tracks
except:
raise OutofList(self.get_msg("voicelinkOutofList"))
def history(self, incTrack: bool = False) -> List[Track]:
if incTrack:
return self._queue[:self._position]
return self._queue[:self._position - 1]
def tracks(self, incTrack: bool = False) -> List[Track]:
if incTrack:
return self._queue[self._position - 1:]
return self._queue[self._position:]
@property
def count(self) -> int:
return len(self._queue[self._position:])
@property
def repeat(self) -> str:
return self._repeat.mode.name.capitalize()
@property
def is_empty(self) -> bool:
try:
self._queue[self._position]
except:
return True
return False
class FairQueue(Queue):
def __init__(self, size: int, allow_duplicate: bool, get_msg) -> None:
super().__init__(size, allow_duplicate, get_msg)
self._set = set()
def put(self, item: Track) -> int:
if len(self._queue) >= self._size:
raise QueueFull(self.get_msg("voicelinkQueueFull").format(self._size))
tracks = self.tracks(incTrack=True)
lastIndex = len(tracks)
for track in reversed(tracks):
if track.requester == item.requester:
break
lastIndex -= 1
self._set.clear()
for track in tracks[lastIndex:]:
if track.requester in self._set:
break
lastIndex += 1
self._set.add(track.requester)
self.put_at_index(lastIndex, item)
return lastIndex