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;
            }
        }
Exemple #2
0
        public static IAudioClient ConnectToVoice(this SocketGuild guild, ISocketAudioChannel channel)
        {
            var voiceChannel = channel;

            if (voiceChannel == null)
            {
                Logging.LogError(LogType.Bot, $"Voice channel is null in server {guild.Name}");
                return(null);
            }
            else
            {
                Logging.LogInfo(LogType.Bot, $"Found Channel {channel.Name} in server {guild.Name} attempting to connect...");

                IAudioClient client  = null;
                bool         success = Utilities.ExecuteAndWait(async(token) =>
                {
                    try
                    {
                        client = await voiceChannel.ConnectAsync().ConfigureAwait(false);
                    }
                    catch (Exception ex)
                    {
                        Logging.LogException(LogType.Discord, ex, "Failed to connect to voice channel");
                    }
                }, 30.Second());

                if (!success)
                {
                    Logging.LogError(LogType.Bot, "Failed to connect to voice channel (timed out after 30 seconds).");
                }
                else
                {
                    Logging.LogInfo(LogType.Bot, "Connected to voice channel.");
                }

                if (client != null && (client.ConnectionState == ConnectionState.Connected || client.ConnectionState == ConnectionState.Connecting))
                {
                    Logging.LogInfo(LogType.Bot, $"Successfully connected to Channel {channel.Name} in server {guild.Name}.");
                    return(client);
                }
                else
                {
                    Logging.LogError(LogType.Bot, $"Failed to connected to Channel {channel.Name} in server {guild.Name}.");
                    return(null);
                }
            }
        }
Exemple #3
0
        public async Task getIn()
        {
            SocketGuildUser user = Context.User as SocketGuildUser;

            if (user.VoiceState == null)
            {
                await ReplyAsync("Conecta num voice-chat ae mein ");

                return;
            }
            ISocketAudioChannel channel = user.VoiceChannel;

            Console.WriteLine(channel);
            await channel.ConnectAsync();

            Console.WriteLine("Conectado");
        }
        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)));
            }
        }