-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathreconnect.diff
249 lines (225 loc) · 7.87 KB
/
reconnect.diff
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
diff --git a/examples/py_hikari_lightbulb/lavalink_voice.py b/examples/py_hikari_lightbulb/lavalink_voice.py
index 75ce9bd..ee81b75 100644
--- a/examples/py_hikari_lightbulb/lavalink_voice.py
+++ b/examples/py_hikari_lightbulb/lavalink_voice.py
@@ -11,20 +11,17 @@ from lavalink_rs.model.player import ConnectionInfo
class LavalinkVoice(VoiceConnection):
- __slots__ = [
+ __slots__ = (
"lavalink",
"player",
- "__channel_id",
- "__guild_id",
"__session_id",
- "__is_alive",
- "__shard_id",
- "__on_close",
"__owner",
- ]
+ "__should_disconnect",
+ )
lavalink: LavalinkClient
player: PlayerContext
+
def __init__(
self,
lavalink_client: LavalinkClient,
@@ -33,68 +30,56 @@ class LavalinkVoice(VoiceConnection):
channel_id: hikari.Snowflake,
guild_id: hikari.Snowflake,
session_id: str,
- is_alive: bool,
shard_id: int,
owner: VoiceComponent,
- on_close: t.Any,
) -> None:
+ super().__init__(channel_id, guild_id, shard_id)
+
self.player = player
self.lavalink = lavalink_client
- self.__channel_id = channel_id
- self.__guild_id = guild_id
self.__session_id = session_id
- self.__is_alive = is_alive
- self.__shard_id = shard_id
self.__owner = owner
- self.__on_close = on_close
-
- @property
- def channel_id(self) -> hikari.Snowflake:
- """Return the ID of the voice channel this voice connection is in."""
- return self.__channel_id
- @property
- def guild_id(self) -> hikari.Snowflake:
- """Return the ID of the guild this voice connection is in."""
- return self.__guild_id
+ self.__should_disconnect: bool = True
- @property
- def is_alive(self) -> bool:
- """Return `builtins.True` if the connection is alive."""
- return self.__is_alive
- @property
- def shard_id(self) -> int:
- """Return the ID of the shard that requested the connection."""
- return self.__shard_id
+ @staticmethod
+ async def reconnect(player: PlayerContext, endpoint: str, token: str, session_id: str) -> None:
+ update_player = UpdatePlayer()
+ connection_info = ConnectionInfo(
+ endpoint, token, session_id
+ )
+ connection_info.fix()
+ update_player.voice = connection_info
- @property
- def owner(self) -> VoiceComponent:
- """Return the component that is managing this connection."""
- return self.__owner
+ await player.update_player(update_player, True)
async def disconnect(self) -> None:
"""Signal the process to shut down."""
- self.__is_alive = False
- await self.lavalink.delete_player(self.__guild_id)
- await self.__on_close(self)
-
- async def join(self) -> None:
- """Wait for the process to halt before continuing."""
+ if self.__should_disconnect:
+ await self.lavalink.delete_player(self.guild_id)
+ else:
+ self.__should_disconnect = True
async def notify(self, event: hikari.VoiceEvent) -> None:
"""Submit an event to the voice connection to be processed."""
- if isinstance(event, hikari.VoiceServerUpdateEvent):
- # Handle the bot being moved frome one channel to another
- assert event.raw_endpoint
- update_player = UpdatePlayer()
- connection_info = ConnectionInfo(
- event.raw_endpoint, event.token, self.__session_id
- )
- connection_info.fix()
- update_player.voice = connection_info
- await self.player.update_player(update_player, True)
+ if not isinstance(event, hikari.VoiceServerUpdateEvent):
+ return
+
+ # TODO handle this better
+ # https://discord.com/developers/docs/topics/gateway-events#voice-server-update
+ assert event.raw_endpoint
+
+ # Handle the bot being moved frome one channel to another
+ await LavalinkVoice.reconnect(self.player, event.raw_endpoint, event.token, self.__session_id)
+
+
+ @property
+ def owner(self) -> VoiceComponent:
+ """Return the component that is managing this connection."""
+ return self.__owner
+
@classmethod
async def connect(
@@ -106,9 +91,19 @@ class LavalinkVoice(VoiceConnection):
player_data: t.Any,
deaf: bool = True,
) -> LavalinkVoice:
+ try:
+ conn = client.voice.connections.get(guild_id)
+ if conn:
+ assert isinstance(conn, LavalinkVoice)
+ conn.__should_disconnect = False
+ await client.voice.disconnect(guild_id)
+ except:
+ pass
+
voice: LavalinkVoice = await client.voice.connect_to(
guild_id,
channel_id,
+ disconnect_existing=False,
voice_connection_type=LavalinkVoice,
lavalink_client=lavalink_client,
player_data=player_data,
@@ -117,13 +112,13 @@ class LavalinkVoice(VoiceConnection):
return voice
+
@classmethod
async def initialize(
cls,
channel_id: hikari.Snowflake,
endpoint: str,
guild_id: hikari.Snowflake,
- on_close: t.Any,
owner: VoiceComponent,
session_id: str,
shard_id: int,
@@ -132,14 +127,19 @@ class LavalinkVoice(VoiceConnection):
**kwargs: t.Any,
) -> LavalinkVoice:
del user_id
- lavalink_client = kwargs["lavalink_client"]
-
- player = await lavalink_client.create_player_context(
- guild_id, endpoint, token, session_id
- )
+ lavalink_client = kwargs["lavalink_client"]
player_data = kwargs["player_data"]
+ player = lavalink_client.get_player_context(guild_id)
+
+ if player:
+ await LavalinkVoice.reconnect(player, endpoint, token, session_id)
+ else:
+ player = await lavalink_client.create_player_context(
+ guild_id, endpoint, token, session_id
+ )
+
if player_data:
player.data = player_data
@@ -149,10 +149,8 @@ class LavalinkVoice(VoiceConnection):
channel_id=channel_id,
guild_id=guild_id,
session_id=session_id,
- is_alive=True,
shard_id=shard_id,
owner=owner,
- on_close=on_close,
)
return self
diff --git a/examples/py_hikari_lightbulb/plugins/music_basic.py b/examples/py_hikari_lightbulb/plugins/music_basic.py
index da463c3..1cb43e1 100644
--- a/examples/py_hikari_lightbulb/plugins/music_basic.py
+++ b/examples/py_hikari_lightbulb/plugins/music_basic.py
@@ -33,27 +33,13 @@ async def _join(ctx: Context) -> t.Optional[hikari.Snowflake]:
channel_id = voice_state.channel_id
- voice = ctx.bot.voice.connections.get(ctx.guild_id)
-
- if not voice:
- await LavalinkVoice.connect(
- ctx.guild_id,
- channel_id,
- ctx.bot,
- ctx.bot.lavalink,
- (ctx.channel_id, ctx.bot.rest),
- )
- else:
- assert isinstance(voice, LavalinkVoice)
-
- await LavalinkVoice.connect(
- ctx.guild_id,
- channel_id,
- ctx.bot,
- ctx.bot.lavalink,
- (ctx.channel_id, ctx.bot.rest),
- old_voice=voice,
- )
+ await LavalinkVoice.connect(
+ ctx.guild_id,
+ channel_id,
+ ctx.bot,
+ ctx.bot.lavalink,
+ (ctx.channel_id, ctx.bot.rest),
+ )
return channel_id
@@ -96,7 +82,7 @@ async def leave(ctx: Context) -> None:
await ctx.respond("Not in a voice channel")
return None
- await voice.disconnect()
+ await ctx.bot.voice.disconnect(ctx.guild_id)
await ctx.respond("Left the voice channel")