Beispiel #1
0
        public void Start()
        {
            Running = true;

            Task.Run(() =>
            {
                var voiceClient = _client.GetVoiceClient(_guildId);

                while (voiceClient.State == MediaConnectionState.Ready && Tracks.Count > 0)
                {
                    var currentSong = Tracks[0];

                    var manifest = Program.YouTubeClient.Videos.Streams.GetManifestAsync(currentSong.Id).Result;

                    if (_stream == null)
                    {
                        VoiceChannel currentChannel = (VoiceChannel)_client.GetChannel(voiceClient.Channel.Id);
                        _stream = DiscordVoiceUtils.GetAudioStream(GetVideoUrl(currentSong.Id, currentChannel.Bitrate));
                    }

                    if (voiceClient.Microphone.CopyFrom(_stream, currentSong.CancellationTokenSource.Token))
                    {
                        _stream = null;
                        Tracks.RemoveAt(0);
                    }
                    else if (currentSong.CancellationTokenSource.IsCancellationRequested)
                    {
                        _stream = null;
                    }
                }

                Running = false;
            });
        }
Beispiel #2
0
        public async Task StartAsync()
        {
            while (true)
            {
                if (Session.State == MediaSessionState.Authenticated && Tracks.Count > 0)
                {
                    MusicTrack currentTrack = Tracks[0];

                    if (currentTrack.DownloadTask != null)
                    {
                        await currentTrack.DownloadTask;
                    }

                    if (CurrentTrackStream == null)
                    {
                        CurrentTrackStream = DiscordVoiceUtils.GetAudioStream(currentTrack.FilePath);
                    }

                    try
                    {
                        SessionStream.CopyFrom(CurrentTrackStream);

                        Tracks.RemoveAt(0);
                        CurrentTrackStream.Flush();
                        CurrentTrackStream = null;
                    }
                    catch { }
                }
                else
                {
                    await Task.Delay(100);
                }
            }
        }
Beispiel #3
0
        private static void Client_OnJoinedVoiceChannel(DiscordSocketClient client, VoiceConnectEventArgs args)
        {
            Console.WriteLine(client.User.ToString() + " has joined " + args.Client.Channel.Id);

            // exploit that lets you speak while muted (https://www.youtube.com/watch?v=PWzPa_BIv9s)
            args.Client.Microphone.Bitrate = ((VoiceChannel)client.GetChannel(args.Client.Channel.Id)).Bitrate;
            args.Client.Microphone.SetSpeakingState(DiscordSpeakingFlags.Soundshare);

            CancellationTokenSource source = new CancellationTokenSource();

            Task.Run(() =>
            {
                while (true)
                {
                    if (GetParticipantCount(client, args.Client.Channel.Id) == 0)
                    {
                        source.Cancel();
                        if (Connect(client))
                        {
                            Console.WriteLine(client.User.ToString() + " is switching channel, due to there being noone in the current one");
                        }
                        else
                        {
                            args.Client.Disconnect();
                        }

                        return;
                    }

                    Thread.Sleep(100);
                }
            });

            while (args.Client.State == MediaConnectionState.Ready && !source.IsCancellationRequested)
            {
                args.Client.Microphone.CopyFrom(DiscordVoiceUtils.GetAudioStream(AudioPath), source.Token);
            }
        }