internal VoiceConnectionInvalidatedEventArgs(Shard shard, DiscordVoiceConnection connection,
                                              VoiceConnectionInvalidationReason reason, string errorMessage = null)
     : base(shard, connection)
 {
     Reason       = reason;
     ErrorMessage = errorMessage;
 }
示例#2
0
        /// <exception cref="InvalidOperationException">Thrown if this voice connection is not connected.</exception>
        /// <exception cref="OperationCanceledException"></exception>
        internal async Task DisconnectWithReasonAsync(VoiceConnectionInvalidationReason reason,
                                                      CancellationToken?cancellationToken = null)
        {
            if (!isConnected)
            {
                throw new InvalidOperationException("The voice connection is not connected!");
            }

            await CloseAndInvalidate(WebSocketCloseStatus.NormalClosure, "Closing normally...",
                                     reason, null, cancellationToken).ConfigureAwait(false);
        }
        /// <summary>
        /// Ensures that: the WebSocket is closed, the UDP socket is closed, the user has left the voice channel,
        /// and the connection is invalidated.
        /// </summary>
        /// <exception cref="OperationCanceledException"></exception>
        async Task CloseAndInvalidate(WebSocketCloseStatus webSocketCloseCode, string webSocketCloseDescription,
                                      VoiceConnectionInvalidationReason reason, string errorMessage = null,
                                      CancellationToken?cancellationToken = null)
        {
            Task leaveChannelTask = EnsureUserLeftVoiceChannel(cancellationToken ?? CancellationToken.None);

            Task webSocketDisconnectTask = EnsureWebSocketIsClosed(webSocketCloseCode,
                                                                   webSocketCloseDescription, cancellationToken);

            EnsureUdpSocketIsClosed();

            await webSocketDisconnectTask.ConfigureAwait(false);

            await leaveChannelTask.ConfigureAwait(false);

            Invalidate(reason, errorMessage);
        }
        void Invalidate(VoiceConnectionInvalidationReason reason, string errorMessage = null)
        {
            if (isValid)
            {
                isValid      = false;
                isConnecting = false;
                isConnected  = false;

                voiceState = null;

                if (!isDisposed)
                {
                    connectingCancellationSource?.Cancel();
                }

                log?.LogVerbose("[Invalidate] Invalidating voice connection...");

                Shard.Voice.RemoveVoiceConnection(guildId);

                OnInvalidated?.Invoke(this, new VoiceConnectionInvalidatedEventArgs(Shard, this, reason, errorMessage));
            }
        }