forked from DSharpPlus/DSharpPlus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLavalinkGuildConnection.cs
382 lines (324 loc) · 16.5 KB
/
LavalinkGuildConnection.cs
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
// This file is part of the DSharpPlus project.
//
// Copyright (c) 2015 Mike Santiago
// Copyright (c) 2016-2023 DSharpPlus Contributors
//
// 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.
using System;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus.AsyncEvents;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Lavalink.Entities;
using DSharpPlus.Lavalink.EventArgs;
using Newtonsoft.Json;
namespace DSharpPlus.Lavalink
{
internal delegate void ChannelDisconnectedEventHandler(LavalinkGuildConnection node);
/// <summary>
/// Represents a Lavalink connection to a channel.
/// </summary>
public sealed class LavalinkGuildConnection
{
/// <summary>
/// Triggered whenever Lavalink updates player status.
/// </summary>
public event AsyncEventHandler<LavalinkGuildConnection, PlayerUpdateEventArgs> PlayerUpdated
{
add { this._playerUpdated.Register(value); }
remove { this._playerUpdated.Unregister(value); }
}
private readonly AsyncEvent<LavalinkGuildConnection, PlayerUpdateEventArgs> _playerUpdated;
/// <summary>
/// Triggered whenever playback of a track starts.
/// <para>This is only available for version 3.3.1 and greater.</para>
/// </summary>
public event AsyncEventHandler<LavalinkGuildConnection, TrackStartEventArgs> PlaybackStarted
{
add { this._playbackStarted.Register(value); }
remove { this._playbackStarted.Unregister(value); }
}
private readonly AsyncEvent<LavalinkGuildConnection, TrackStartEventArgs> _playbackStarted;
/// <summary>
/// Triggered whenever playback of a track finishes.
/// </summary>
public event AsyncEventHandler<LavalinkGuildConnection, TrackFinishEventArgs> PlaybackFinished
{
add { this._playbackFinished.Register(value); }
remove { this._playbackFinished.Unregister(value); }
}
private readonly AsyncEvent<LavalinkGuildConnection, TrackFinishEventArgs> _playbackFinished;
/// <summary>
/// Triggered whenever playback of a track gets stuck.
/// </summary>
public event AsyncEventHandler<LavalinkGuildConnection, TrackStuckEventArgs> TrackStuck
{
add { this._trackStuck.Register(value); }
remove { this._trackStuck.Unregister(value); }
}
private readonly AsyncEvent<LavalinkGuildConnection, TrackStuckEventArgs> _trackStuck;
/// <summary>
/// Triggered whenever playback of a track encounters an error.
/// </summary>
public event AsyncEventHandler<LavalinkGuildConnection, TrackExceptionEventArgs> TrackException
{
add { this._trackException.Register(value); }
remove { this._trackException.Unregister(value); }
}
private readonly AsyncEvent<LavalinkGuildConnection, TrackExceptionEventArgs> _trackException;
/// <summary>
/// Triggered whenever Discord Voice WebSocket connection is terminated.
/// </summary>
public event AsyncEventHandler<LavalinkGuildConnection, WebSocketCloseEventArgs> DiscordWebSocketClosed
{
add { this._webSocketClosed.Register(value); }
remove { this._webSocketClosed.Unregister(value); }
}
private readonly AsyncEvent<LavalinkGuildConnection, WebSocketCloseEventArgs> _webSocketClosed;
/// <summary>
/// Gets whether this channel is still connected.
/// </summary>
public bool IsConnected => !Volatile.Read(ref this._isDisposed) && this.Channel != null;
private bool _isDisposed = false;
/// <summary>
/// Gets the current player state.
/// </summary>
public LavalinkPlayerState CurrentState { get; }
/// <summary>
/// Gets the voice channel associated with this connection.
/// </summary>
public DiscordChannel Channel => this.VoiceStateUpdate.Channel;
/// <summary>
/// Gets the guild associated with this connection.
/// </summary>
public DiscordGuild Guild => this.Channel.Guild;
/// <summary>
/// Gets the Lavalink node associated with this connection.
/// </summary>
public LavalinkNodeConnection Node { get; }
internal string GuildIdString => this.GuildId.ToString(CultureInfo.InvariantCulture);
internal ulong GuildId => this.Channel.Guild.Id;
internal VoiceStateUpdateEventArgs VoiceStateUpdate { get; set; }
internal TaskCompletionSource<bool> VoiceWsDisconnectTcs { get; set; }
internal LavalinkGuildConnection(LavalinkNodeConnection node, DiscordChannel channel, VoiceStateUpdateEventArgs vstu)
{
this.Node = node;
this.VoiceStateUpdate = vstu;
this.CurrentState = new LavalinkPlayerState();
this.VoiceWsDisconnectTcs = new TaskCompletionSource<bool>();
Volatile.Write(ref this._isDisposed, false);
this._playerUpdated = new AsyncEvent<LavalinkGuildConnection, PlayerUpdateEventArgs>("LAVALINK_PLAYER_UPDATE", this.Node.Discord.EventErrorHandler);
this._playbackStarted = new AsyncEvent<LavalinkGuildConnection, TrackStartEventArgs>("LAVALINK_PLAYBACK_STARTED", this.Node.Discord.EventErrorHandler);
this._playbackFinished = new AsyncEvent<LavalinkGuildConnection, TrackFinishEventArgs>("LAVALINK_PLAYBACK_FINISHED", this.Node.Discord.EventErrorHandler);
this._trackStuck = new AsyncEvent<LavalinkGuildConnection, TrackStuckEventArgs>("LAVALINK_TRACK_STUCK", this.Node.Discord.EventErrorHandler);
this._trackException = new AsyncEvent<LavalinkGuildConnection, TrackExceptionEventArgs>("LAVALINK_TRACK_EXCEPTION", this.Node.Discord.EventErrorHandler);
this._webSocketClosed = new AsyncEvent<LavalinkGuildConnection, WebSocketCloseEventArgs>("LAVALINK_DISCORD_WEBSOCKET_CLOSED", this.Node.Discord.EventErrorHandler);
}
/// <summary>
/// Disconnects the connection from the voice channel.
/// </summary>
/// <param name="shouldDestroy">Whether the connection should be destroyed on the Lavalink server when leaving.</param>
public Task DisconnectAsync(bool shouldDestroy = true)
=> this.DisconnectInternalAsync(shouldDestroy);
internal async Task DisconnectInternalAsync(bool shouldDestroy, bool isManualDisconnection = false)
{
if (!this.IsConnected && !isManualDisconnection)
throw new InvalidOperationException("This connection is not valid.");
Volatile.Write(ref this._isDisposed, true);
if (shouldDestroy)
await this.Node.SendPayloadAsync(new LavalinkDestroy(this)).ConfigureAwait(false);
if (!isManualDisconnection)
{
await this.SendVoiceUpdateAsync().ConfigureAwait(false);
this.ChannelDisconnected?.Invoke(this);
}
}
internal async Task SendVoiceUpdateAsync()
{
var vsd = new VoiceDispatch
{
OpCode = 4,
Payload = new VoiceStateUpdatePayload
{
GuildId = this.GuildId,
ChannelId = null,
Deafened = false,
Muted = false
}
};
var vsj = JsonConvert.SerializeObject(vsd, Formatting.None);
await (this.Channel.Discord as DiscordClient).SendRawPayloadAsync(vsj).ConfigureAwait(false);
}
/// <summary>
/// Searches for specified terms.
/// </summary>
/// <param name="searchQuery">What to search for.</param>
/// <param name="type">What platform will search for.</param>
/// <returns>A collection of tracks matching the criteria.</returns>
public Task<LavalinkLoadResult> GetTracksAsync(string searchQuery, LavalinkSearchType type = LavalinkSearchType.Youtube)
=> this.Node.Rest.GetTracksAsync(searchQuery, type);
/// <summary>
/// Loads tracks from specified URL.
/// </summary>
/// <param name="uri">URL to load tracks from.</param>
/// <returns>A collection of tracks from the URL.</returns>
public Task<LavalinkLoadResult> GetTracksAsync(Uri uri)
=> this.Node.Rest.GetTracksAsync(uri);
/// <summary>
/// Loads tracks from a local file.
/// </summary>
/// <param name="file">File to load tracks from.</param>
/// <returns>A collection of tracks from the file.</returns>
public Task<LavalinkLoadResult> GetTracksAsync(FileInfo file)
=> this.Node.Rest.GetTracksAsync(file);
/// <summary>
/// Queues the specified track for playback.
/// </summary>
/// <param name="track">Track to play.</param>
public async Task PlayAsync(LavalinkTrack track)
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
this.CurrentState.CurrentTrack = track;
await this.Node.SendPayloadAsync(new LavalinkPlay(this, track)).ConfigureAwait(false);
}
/// <summary>
/// Queues the specified track for playback. The track will be played from specified start timestamp to specified end timestamp.
/// </summary>
/// <param name="track">Track to play.</param>
/// <param name="start">Timestamp to start playback at.</param>
/// <param name="end">Timestamp to stop playback at.</param>
public async Task PlayPartialAsync(LavalinkTrack track, TimeSpan start, TimeSpan end)
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
if (start.TotalMilliseconds < 0 || end <= start)
throw new ArgumentException("Both start and end timestamps need to be greater or equal to zero, and the end timestamp needs to be greater than start timestamp.");
this.CurrentState.CurrentTrack = track;
await this.Node.SendPayloadAsync(new LavalinkPlayPartial(this, track, start, end)).ConfigureAwait(false);
}
/// <summary>
/// Stops the player completely.
/// </summary>
public async Task StopAsync()
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
await this.Node.SendPayloadAsync(new LavalinkStop(this)).ConfigureAwait(false);
}
/// <summary>
/// Pauses the player.
/// </summary>
public async Task PauseAsync()
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
await this.Node.SendPayloadAsync(new LavalinkPause(this, true)).ConfigureAwait(false);
}
/// <summary>
/// Resumes playback.
/// </summary>
public async Task ResumeAsync()
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
await this.Node.SendPayloadAsync(new LavalinkPause(this, false)).ConfigureAwait(false);
}
/// <summary>
/// Seeks the current track to specified position.
/// </summary>
/// <param name="position">Position to seek to.</param>
public async Task SeekAsync(TimeSpan position)
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
await this.Node.SendPayloadAsync(new LavalinkSeek(this, position)).ConfigureAwait(false);
}
/// <summary>
/// Sets the playback volume. This might incur a lot of CPU usage.
/// </summary>
/// <param name="volume">Volume to set. Needs to be greater or equal to 0, and less than or equal to 100. 100 means 100% and is the default value.</param>
public async Task SetVolumeAsync(int volume)
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
if (volume < 0 || volume > 1000)
throw new ArgumentOutOfRangeException(nameof(volume), "Volume needs to range from 0 to 1000.");
await this.Node.SendPayloadAsync(new LavalinkVolume(this, volume)).ConfigureAwait(false);
}
/// <summary>
/// Adjusts the specified bands in the audio equalizer. This will alter the sound output, and might incur a lot of CPU usage.
/// </summary>
/// <param name="bands">Bands adjustments to make. You must specify one adjustment per band at most.</param>
public async Task AdjustEqualizerAsync(params LavalinkBandAdjustment[] bands)
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
if (bands?.Any() != true)
return;
if (bands.Distinct(new LavalinkBandAdjustmentComparer()).Count() != bands.Count())
throw new InvalidOperationException("You cannot specify multiple modifiers for the same band.");
await this.Node.SendPayloadAsync(new LavalinkEqualizer(this, bands)).ConfigureAwait(false);
}
/// <summary>
/// Resets the audio equalizer to default values.
/// </summary>
public async Task ResetEqualizerAsync()
{
if (!this.IsConnected)
throw new InvalidOperationException("This connection is not valid.");
await this.Node.SendPayloadAsync(new LavalinkEqualizer(this, Enumerable.Range(0, 15).Select(x => new LavalinkBandAdjustment(x, 0)))).ConfigureAwait(false);
}
internal Task InternalUpdatePlayerStateAsync(LavalinkState newState)
{
this.CurrentState.LastUpdate = newState.Time;
this.CurrentState.PlaybackPosition = newState.Position;
return this._playerUpdated.InvokeAsync(this, new PlayerUpdateEventArgs(this, newState.Time, newState.Position));
}
internal Task InternalPlaybackStartedAsync(string track)
{
var ea = new TrackStartEventArgs(this, LavalinkUtilities.DecodeTrack(track));
return this._playbackStarted.InvokeAsync(this, ea);
}
internal Task InternalPlaybackFinishedAsync(TrackFinishData e)
{
if (e.Reason != TrackEndReason.Replaced)
this.CurrentState.CurrentTrack = default;
var ea = new TrackFinishEventArgs(this, LavalinkUtilities.DecodeTrack(e.Track), e.Reason);
return this._playbackFinished.InvokeAsync(this, ea);
}
internal Task InternalTrackStuckAsync(TrackStuckData e)
{
var ea = new TrackStuckEventArgs(this, e.Threshold, LavalinkUtilities.DecodeTrack(e.Track));
return this._trackStuck.InvokeAsync(this, ea);
}
internal Task InternalTrackExceptionAsync(TrackExceptionData e)
{
var ea = new TrackExceptionEventArgs(this, e.Error, LavalinkUtilities.DecodeTrack(e.Track));
return this._trackException.InvokeAsync(this, ea);
}
internal Task InternalWebSocketClosedAsync(WebSocketCloseEventArgs e)
=> this._webSocketClosed.InvokeAsync(this, e);
internal event ChannelDisconnectedEventHandler ChannelDisconnected;
}
}