forked from Cog-Creators/Red-Lavalink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
player_manager.py
458 lines (388 loc) · 14 KB
/
player_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import asyncio
import datetime
from random import shuffle
from typing import TYPE_CHECKING, Optional
import discord
from discord.backoff import ExponentialBackoff
from discord.voice_client import VoiceProtocol
from . import log, ws_rll_log
from .enums import (
LavalinkEvents,
LavalinkIncomingOp,
LavalinkOutgoingOp,
PlayerState,
TrackEndReason,
)
from .rest_api import Track
from .utils import VoiceChannel
if TYPE_CHECKING:
from . import node
__all__ = ["Player"]
class Player(VoiceProtocol):
"""
The Player class represents the current state of playback.
It also is used to control playback and queue tracks.
The existence of this object guarantees that the bot is connected
to a voice channel.
Attributes
----------
channel: discord.VoiceChannel
The channel the bot is connected to.
queue : list of Track
position : int
The seeked position in the track of the current playback.
current : Track
repeat : bool
shuffle : bool
"""
def __call__(self, client: discord.Client, channel: VoiceChannel):
self.client: discord.Client = client
self.channel: VoiceChannel = channel
return self
def __init__(self, client: discord.Client = None, channel: VoiceChannel = None, node=None):
self.client = client
self.channel = channel
self.guild = channel.guild
self._last_channel_id = channel.id
self.queue = []
self.position = 0
self.current = None # type: Track
self._paused = False
self.repeat = False
self.shuffle = False # Shuffle is done client side now This is a breaking change
self.shuffle_bumped = True
self._is_autoplaying = False
self._auto_play_sent = False
self._volume = 100
self.state = PlayerState.CREATED
self._voice_state = {}
self.connected_at = None
self._connected = False
self._is_playing = False
self._metadata = {}
if node is None:
from .node import get_node
node = get_node()
self.node = node
self._con_delay = None
self._last_resume = None
def __repr__(self):
return (
"<Player: "
f"state={self.state.name}, connected={self.connected}, "
f"guild={self.guild.name!r} ({self.guild.id}), "
f"channel={self.channel.name!r} ({self.channel.id}), "
f"playing={self.is_playing}, paused={self.paused}, volume={self.volume}, "
f"queue_size={len(self.queue)}, current={self.current!r}, "
f"position={self.position}, "
f"length={self.current.length if self.current else 0}, node={self.node!r}>"
)
@property
def is_auto_playing(self) -> bool:
"""
Current status of playback
"""
return self._is_playing and not self._paused and self._is_autoplaying
@property
def is_playing(self) -> bool:
"""
Current status of playback
"""
return self._is_playing and not self._paused and self._connected
@property
def paused(self) -> bool:
"""
Player's paused state.
"""
return self._paused
@property
def volume(self) -> int:
"""
The current volume.
"""
return self._volume
@property
def ready(self) -> bool:
"""
Whether the underlying node is ready for requests.
"""
return self.node.ready
@property
def connected(self) -> bool:
"""
Whether the player is ready to be used.
"""
return self._connected
async def on_voice_server_update(self, data: dict) -> None:
self._voice_state.update({"event": data})
await self._send_lavalink_voice_update(self._voice_state)
async def on_voice_state_update(self, data: dict) -> None:
self._voice_state.update({"sessionId": data["session_id"]})
if (channel_id := data["channel_id"]) is None:
ws_rll_log.info("Received voice disconnect from discord, removing player.")
self._voice_state.clear()
await self.node._remove_player(int(data["guild_id"]))
else:
channel = self.guild.get_channel(int(channel_id))
if channel != self.channel:
if self.channel:
self._last_channel_id = self.channel.id
self.channel = channel
await self._send_lavalink_voice_update({**self._voice_state, "event": data})
async def _send_lavalink_voice_update(self, voice_state: dict):
if {"sessionId", "event"} == self._voice_state.keys():
await self.node.send(
{
"op": LavalinkOutgoingOp.VOICE_UPDATE.value,
"guildId": str(self.guild.id),
"sessionId": voice_state["sessionId"],
"event": voice_state["event"],
}
)
async def wait_until_ready(
self, timeout: Optional[float] = None, no_raise: bool = False
) -> bool:
"""
Waits for the underlying node to become ready.
If no_raise is set, returns false when a timeout occurs instead of propogating TimeoutError.
A timeout of None means to wait indefinitely.
"""
if self.node.ready:
return True
try:
return await self.node.wait_until_ready(timeout=timeout)
except asyncio.TimeoutError:
if no_raise:
return False
else:
raise
async def connect(self, timeout: float = 2.0, reconnect: bool = False, deafen: bool = False):
"""
Connects to the voice channel associated with this Player.
"""
self._last_resume = datetime.datetime.now(datetime.timezone.utc)
self.connected_at = datetime.datetime.now(datetime.timezone.utc)
self._connected = True
self.node._players_dict[self.guild.id] = self
await self.node.refresh_player_state(self)
await self.guild.change_voice_state(
channel=self.channel, self_mute=False, self_deaf=deafen
)
async def move_to(self, channel: discord.VoiceChannel, deafen: bool = False):
"""
Moves this player to a voice channel.
Parameters
----------
channel : discord.VoiceChannel
"""
if channel.guild != self.guild:
raise TypeError(f"Cannot move {self!r} to a different guild.")
if self.channel:
self._last_channel_id = self.channel.id
self.channel = channel
await self.connect(deafen=deafen)
if self.current:
await self.resume(
track=self.current, replace=True, start=self.position, pause=self._paused
)
async def disconnect(self, force=False):
"""
Disconnects this player from it's voice channel.
"""
self._is_autoplaying = False
self._auto_play_sent = False
self._connected = False
if self.state == PlayerState.DISCONNECTING:
return
await self.update_state(PlayerState.DISCONNECTING)
guild_id = self.guild.id
if force:
log.debug("Forcing player disconnect for %r due to player manager request.", self)
self.node.event_handler(
LavalinkIncomingOp.EVENT,
LavalinkEvents.FORCED_DISCONNECT,
{
"guildId": guild_id,
"code": 42069,
"reason": "Forced Disconnect - Do not Reconnect",
"byRemote": True,
"retries": -1,
},
)
voice_ws = self.node.get_voice_ws(guild_id)
if not voice_ws.socket.closed:
await self.guild.change_voice_state(channel=None)
await self.node.destroy_guild(guild_id)
self.node.remove_player(self)
self.cleanup()
def store(self, key, value):
"""
Stores a metadata value by key.
"""
self._metadata[key] = value
def fetch(self, key, default=None):
"""
Returns a stored metadata value.
Parameters
----------
key
Key used to store metadata.
default
Optional, used if the key doesn't exist.
"""
return self._metadata.get(key, default)
async def update_state(self, state: PlayerState):
if state == self.state:
return
ws_rll_log.debug("Player %r changing state: %s -> %s", self, self.state.name, state.name)
self.state = state
if self._con_delay:
self._con_delay = None
async def handle_event(self, event: "node.LavalinkEvents", extra):
"""
Handles various Lavalink Events.
If the event is TRACK END, extra will be TrackEndReason.
If the event is TRACK EXCEPTION, extra will be the string reason.
If the event is TRACK STUCK, extra will be the threshold ms.
Parameters
----------
event : node.LavalinkEvents
extra
"""
log.debug("Received player event for player: %r - %r - %r.", self, event, extra)
if event == LavalinkEvents.TRACK_END:
if extra == TrackEndReason.FINISHED:
await self.play()
else:
self._is_playing = False
elif event == LavalinkEvents.WEBSOCKET_CLOSED:
code = extra.get("code")
if code in (4015, 4014, 4009, 4006, 4000, 1006):
if not self._con_delay:
self._con_delay = ExponentialBackoff(base=1)
async def handle_player_update(self, state: "node.PlayerState"):
"""
Handles player updates from lavalink.
Parameters
----------
state : websocket.PlayerState
"""
if state.position > self.position:
self._is_playing = True
log.debug("Updated player position for player: %r - %ds.", self, state.position // 1000)
self.position = state.position
# Play commands
def add(self, requester: discord.User, track: Track):
"""
Adds a track to the queue.
Parameters
----------
requester : discord.User
User who requested the track.
track : Track
Result from any of the lavalink track search methods.
"""
track.requester = requester
self.queue.append(track)
def maybe_shuffle(self, sticky_songs: int = 1):
if self.shuffle and self.queue: # Keeps queue order consistent unless adding new tracks
self.force_shuffle(sticky_songs)
def force_shuffle(self, sticky_songs: int = 1):
if not self.queue:
return
sticky = max(0, sticky_songs) # Songs to bypass shuffle
# Keeps queue order consistent unless adding new tracks
if sticky > 0:
to_keep = self.queue[:sticky]
to_shuffle = self.queue[sticky:]
else:
to_shuffle = self.queue
to_keep = []
if not self.shuffle_bumped:
to_keep_bumped = [t for t in to_shuffle if t.extras.get("bumped", None)]
to_shuffle = [t for t in to_shuffle if not t.extras.get("bumped", None)]
to_keep.extend(to_keep_bumped)
# Shuffles whole queue
shuffle(to_shuffle)
to_keep.extend(to_shuffle)
# Keep next track in queue consistent while adding new tracks
self.queue = to_keep
async def play(self):
"""
Starts playback from lavalink.
"""
if self.repeat and self.current is not None:
self.queue.append(self.current)
self.current = None
self.position = 0
self._paused = False
if not self.queue:
await self.stop()
else:
self._is_playing = True
track = self.queue.pop(0)
self.current = track
log.debug("Assigned current track for player: %r.", self)
await self.node.play(self.guild.id, track, start=track.start_timestamp, replace=True)
async def resume(
self, track: Track, replace: bool = True, start: int = 0, pause: bool = False
):
log.debug("Resuming current track for player: %r.", self)
self._is_playing = False
self._paused = True
await self.node.play(self.guild.id, track, start=start, replace=replace, pause=True)
await self.pause(True)
await self.pause(pause, timed=1)
async def stop(self):
"""
Stops playback from lavalink.
.. important::
This method will clear the queue.
"""
await self.node.stop(self.guild.id)
self.queue = []
self.current = None
self.position = 0
self._paused = False
self._is_autoplaying = False
self._auto_play_sent = False
async def skip(self):
"""
Skips to the next song.
"""
await self.play()
async def pause(self, pause: bool = True, timed: Optional[int] = None):
"""
Pauses the current song.
Parameters
----------
pause : bool
Set to ``False`` to resume.
timed : Optional[int]
If an int is given the op will be called after it.
"""
if timed is not None:
await asyncio.sleep(timed)
self._paused = pause
await self.node.pause(self.guild.id, pause)
async def set_volume(self, volume: int):
"""
Sets the volume of Lavalink.
Parameters
----------
volume : int
Between 0 and 150
"""
self._volume = max(min(volume, 150), 0)
await self.node.volume(self.guild.id, self.volume)
async def seek(self, position: int):
"""
If the track allows it, seeks to a position.
Parameters
----------
position : int
Between 0 and track length.
"""
if self.current.seekable:
position = max(min(position, self.current.length), 0)
await self.node.seek(self.guild.id, position)