-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplayer.py
256 lines (184 loc) · 8.03 KB
/
player.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
"""Swish. A standalone audio player and server for bots on Discord.
Copyright (C) 2022 PythonistaGuild <https://github.com/PythonistaGuild>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from __future__ import annotations
import asyncio
import logging
from collections.abc import Callable
from typing import Any, TYPE_CHECKING
import aiohttp
import aiohttp.web
import discord.backoff
from discord.ext.native_voice import native_voice # type: ignore
from .types.payloads import (
PayloadHandlers,
ReceivedPayload,
SentPayloadOp,
VoiceUpdateData,
PlayData,
SetPauseStateData,
SetPositionData,
SetFilterData,
)
if TYPE_CHECKING:
from .app import App
__all__ = (
'Player',
)
LOG: logging.Logger = logging.getLogger('swish.player')
class Player:
def __init__(
self,
websocket: aiohttp.web.WebSocketResponse,
guild_id: str,
) -> None:
self._app: App = websocket['app']
self._websocket: aiohttp.web.WebSocketResponse = websocket
self._guild_id: str = guild_id
self._connector: native_voice.VoiceConnector = native_voice.VoiceConnector()
self._connector.user_id = int(websocket['user_id'])
self._connection: native_voice.VoiceConnection | None = None
self._runner: asyncio.Task[None] | None = None
self._PAYLOAD_HANDLERS: PayloadHandlers = {
'voice_update': self._voice_update,
'destroy': self._destroy,
'play': self._play,
'stop': self._stop,
'set_pause_state': self._set_pause_state,
'set_position': self._set_position,
'set_filter': self._set_filter,
}
self._LOG_PREFIX: str = f'{self._websocket["client_name"]} - Player \'{self._guild_id}\''
self._NO_CONNECTION_MESSAGE: Callable[[str], str] = (
lambda op: f'{self._LOG_PREFIX} attempted \'{op}\' op while internal connection is down.'
)
self._MISSING_KEY_MESSAGE: Callable[[str, str], str] = (
lambda op, key: f'{self._LOG_PREFIX} received \'{op}\' op with missing \'{key}\' key.'
)
# websocket handlers
async def handle_payload(self, payload: ReceivedPayload) -> None:
op = payload['op']
if op not in self._PAYLOAD_HANDLERS:
LOG.error(f'{self._LOG_PREFIX} received payload with unknown \'op\' key.\nPayload: {payload}')
return
LOG.debug(f'{self._LOG_PREFIX} received payload with \'{op}\' op.\nPayload: {payload}')
await self._PAYLOAD_HANDLERS[op](payload['d'])
async def send_payload(self, op: SentPayloadOp, data: Any) -> None:
await self._websocket.send_json({'op': op, 'd': data})
# connection handlers
async def _connect(self) -> None:
loop = asyncio.get_running_loop()
self._connection = await self._connector.connect(loop)
if self._runner is not None:
self._runner.cancel()
self._runner = loop.create_task(self._reconnect_handler())
async def _reconnect_handler(self) -> None:
loop = asyncio.get_running_loop()
backoff = discord.backoff.ExponentialBackoff()
while True:
try:
assert self._connection is not None
await self._connection.run(loop)
except native_voice.ConnectionClosed:
await self._disconnect()
return
except native_voice.ConnectionError:
await self._disconnect()
return
except native_voice.ReconnectError:
retry = backoff.delay()
await asyncio.sleep(retry)
try:
await self._connect()
except asyncio.TimeoutError:
continue
else:
await self._disconnect()
return
async def _disconnect(self) -> None:
if self._connection is None:
return
self._connection.disconnect()
self._connection = None
# payload handlers
async def _voice_update(self, data: VoiceUpdateData) -> None:
if not (session_id := data.get('session_id')):
LOG.error(self._MISSING_KEY_MESSAGE('voice_update', 'session_id'))
return
if not (token := data.get('token')):
LOG.error(self._MISSING_KEY_MESSAGE('voice_update', 'token'))
return
if not (endpoint := data.get('endpoint')):
LOG.error(self._MISSING_KEY_MESSAGE('voice_update', 'endpoint'))
return
self._connector.session_id = session_id
endpoint, _, _ = endpoint.rpartition(':')
endpoint = endpoint.removeprefix('wss://')
self._connector.update_socket(
token,
data['guild_id'],
endpoint
)
await self._connect()
LOG.info(f'{self._LOG_PREFIX} connected to internal voice server \'{endpoint}\'.')
async def _destroy(self) -> None:
await self._disconnect()
LOG.info(f'{self._LOG_PREFIX} has been disconnected.')
del self._websocket['players'][self._guild_id]
async def _play(self, data: PlayData) -> None:
if not self._connection:
LOG.error(self._NO_CONNECTION_MESSAGE('play'))
return
if not (track_id := data.get('track_id')):
LOG.error(self._MISSING_KEY_MESSAGE('play', 'track_id'))
return
# TODO: handle start_time
# TODO: handle end_time
# TODO: handle replace
track_info = self._app._decode_track_id(track_id)
url = await self._app._get_playback_url(track_info['url'])
self._connection.play(url)
LOG.info(f'{self._LOG_PREFIX} started playing track \'{track_info["title"]}\' by \'{track_info["author"]}\'.')
async def _stop(self) -> None:
if not self._connection:
LOG.error(self._NO_CONNECTION_MESSAGE('stop'))
return
if not self._connection.is_playing():
LOG.error(f'{self._LOG_PREFIX} attempted \'stop\' op while no tracks are playing.')
return
self._connection.stop()
LOG.info(f'{self._LOG_PREFIX} stopped the current track.')
async def _set_pause_state(self, data: SetPauseStateData) -> None:
if not self._connection:
LOG.error(self._NO_CONNECTION_MESSAGE('set_pause_state'))
return
if not (state := data.get('state')):
LOG.error(self._MISSING_KEY_MESSAGE('set_pause_state', 'state'))
return
if state != self._connection.is_paused():
self._connection.pause() if state else self._connection.resume()
LOG.info(f'{self._LOG_PREFIX} set its paused state to \'{state}\'.')
async def _set_position(self, data: SetPositionData) -> None:
if not self._connection:
LOG.error(self._NO_CONNECTION_MESSAGE('set_position'))
return
if not self._connection.is_playing():
LOG.error(f'{self._LOG_PREFIX} attempted \'set_position\' op while no tracks are playing.')
return
if not (position := data.get('position')):
LOG.error(self._MISSING_KEY_MESSAGE('set_position', 'position'))
return
# TODO: implement
LOG.info(f'{self._LOG_PREFIX} set its position to \'{position}\'.')
async def _set_filter(self, data: SetFilterData) -> None:
LOG.error(f'{self._LOG_PREFIX} received \'set_filter\' op which is not yet implemented.')