Exemple #1
0
        public override async void Execute(IMessage message)
        {
            if (!message.Content.Contains(" "))
            {
                DiscordNETWrapper.SendEmbed(HelpMenu, message.Channel).Wait();
                return;
            }

            SocketGuild         g       = Program.GetGuildFromChannel(message.Channel);
            ISocketAudioChannel channel = g.VoiceChannels.FirstOrDefault(x => x.Users.Select(y => y.Id).Contains(message.Author.Id));

            string videoURL = message.Content.Split(' ')[1];

            if (!videoURL.StartsWith("https://www.youtube.com/watch?"))
            {
                DiscordNETWrapper.SendText("That doesn't look like a youtube video link :thinking:", message.Channel).Wait();
                return;
            }

            if (channel != null)
            {
                try { channel.DisconnectAsync().Wait(); } catch { }

                IAudioClient client = await channel.ConnectAsync();

                using (Process P = MultiMediaHelper.GetAudioStreamFromYouTubeVideo(videoURL, "mp3"))
                    using (MemoryStream mem = new MemoryStream())
                    {
                        while (true)
                        {
                            Task.Delay(1001).Wait();
                            if (string.IsNullOrWhiteSpace(P.StandardError.ReadLine()))
                            {
                                break;
                            }
                        }
                        P.StandardOutput.BaseStream.CopyTo(mem);
                        using (WaveStream naudioStream = WaveFormatConversionStream.CreatePcmStream(new StreamMediaFoundationReader(mem)))
                            MultiMediaHelper.SendAudioAsync(client, naudioStream).Wait();
                    }

                try { channel.DisconnectAsync().Wait(); } catch { }
            }
            else
            {
                DiscordNETWrapper.SendText("You are not in an AudioChannel on this server!", message.Channel).Wait();
                return;
            }
        }
        public void PlayAudio(WaveStream w, IMessage m)
        {
            SocketGuild         g       = Program.GetGuildFromChannel(m.Channel);
            ISocketAudioChannel channel = g.VoiceChannels.FirstOrDefault(x => x.Users.Select(y => y.Id).Contains(m.Author.Id));

            if (channel != null)
            {
                try { channel.DisconnectAsync().Wait(); } catch { }

                IAudioClient client = channel.ConnectAsync().Result;
                using (WaveStream naudioStream = WaveFormatConversionStream.CreatePcmStream(w))
                    MultiMediaHelper.SendAudioAsync(client, naudioStream).Wait();

                try { channel.DisconnectAsync().Wait(); } catch { }
            }
            else
            {
                DiscordNETWrapper.SendText("You are not in an AudioChannel on this server!", m.Channel).Wait();
            }

            w.Dispose();
        }
        public WaveStream AudioFromVoice(EditNull n, IMessage m, ulong userID = 0, int RecordingTimeInSeconds = 5)
        {
            string filePath = $"Commands{Path.DirectorySeparatorChar}Edit{Path.DirectorySeparatorChar}audioFromVoice.bin";

            int          recordingTime = RecordingTimeInSeconds;
            MemoryStream memOut        = new MemoryStream();

            if (recordingTime > 10)
            {
                throw new Exception("Thats too long UwU");
            }

            using (MemoryStream mem = new MemoryStream())
            {
                SocketGuild         g       = Program.GetGuildFromChannel(m.Channel);
                ISocketAudioChannel channel = g.VoiceChannels.FirstOrDefault(x => x.Users.Select(y => y.Id).Contains(m.Author.Id));

                if (channel == null)
                {
                    throw new Exception("You are not in an AudioChannel on this server!");
                }

                try { channel.DisconnectAsync().Wait(); } catch { }

                bool doneListening = false;

                new Action(async() =>
                {
                    try
                    {
                        Thread.CurrentThread.Name = "F**k";
                        IAudioClient client       = await channel.ConnectAsync();

                        using (WaveStream naudioStream = WaveFormatConversionStream.CreatePcmStream(
                                   new StreamMediaFoundationReader(
                                       new FileStream($"Commands{Path.DirectorySeparatorChar}Edit{Path.DirectorySeparatorChar}StartListeningSoundEffect.mp3", FileMode.Open))))
                            await MultiMediaHelper.SendAudioAsync(client, naudioStream);

                        var u = (SocketGuildUser)(await channel.GetUsersAsync().FlattenAsync()).FirstOrDefault(x => userID == 0 ? !x.IsBot : !x.IsBot && x.Id == userID);

                        if (u == null)
                        {
                            throw new Exception("I cant find that user!");
                        }

                        var streamMeUpScotty = (InputStream)u.AudioStream;

                        var buffer = new byte[4096];
                        DateTime startListeningTime = DateTime.Now;
                        while (await streamMeUpScotty.ReadAsync(buffer, 0, buffer.Length) > 0 && (DateTime.Now - startListeningTime).TotalSeconds < recordingTime)
                        {
                            mem.Write(buffer, 0, buffer.Length);
                        }

                        try { channel.DisconnectAsync().Wait(); } catch { }

                        mem.Position = 0;
                        using (FileStream f = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                            mem.CopyTo(f);

                        using (Process P = MultiMediaHelper.CreateFfmpegOut(filePath))
                            P.StandardOutput.BaseStream.CopyTo(memOut);

                        File.Delete(filePath);
                    }
                    catch { }
                    finally
                    {
                        try { channel.DisconnectAsync().Wait(); } catch { }
                        doneListening = true;
                    }
                }).Invoke();

                while (!doneListening)
                {
                    Thread.Sleep(100);
                }

                memOut.Position = 0;
                return(WaveFormatConversionStream.CreatePcmStream(new StreamMediaFoundationReader(memOut)));
            }
        }