public async Task AutoPlayAudioAsync(IGuild guild, IMessageChannel channel) { if (guild == null || Shared.AutoPlayRunning) { return; } do { bool autoPlay = Shared.AutoPlay; bool flag = autoPlay; Shared.AutoPlayRunning = autoPlay; if (!flag) { break; } if (Shared.AudioPlayer.IsRunning()) { await Task.Delay(1000); } if (Shared.Playlist.IsEmpty || !Shared.AutoPlayRunning || !Shared.AutoPlay) { break; } if (Shared.ConnectedChannels.TryGetValue(guild.Id, out IAudioClient value)) { AudioFile audioFile = PlaylistNext(); if (audioFile != null) { string thumbURL = ""; string title = audioFile.Title; foreach (RadioStation station in Program.config.Stations) { if (audioFile.Title == station.Filename) { title = station.Name; thumbURL = station.Thumbnail; } } if (title != "") { EmbedBuilder emb = new EmbedBuilder(); emb.WithAuthor(Context.Client.CurrentUser); emb.WithColor(Color.Red); if (audioFile.Link.Contains("soundcloud.com")) { emb.WithThumbnailUrl(SCDownloader.GetTrack(audioFile.Link)["artwork_url"].ToString().Replace("large", "t500x500")); emb.WithTitle("Now Playing:"); emb.AddField(SCDownloader.GetTrack(audioFile.Link)["user"]["username"].ToString(), SCDownloader.GetTrack(audioFile.Link)["title"].ToString()); } else if (audioFile.Link.Contains("youtube.com")) { string[] url = audioFile.Link.Split(new string[] { "watch?v=" }, StringSplitOptions.None); title = SCDownloader.GetYouTubeInfo(url[1])["items"][0]["snippet"]["title"].ToString().Replace("—", "-"); thumbURL = "https://i.ytimg.com/vi/" + url[1] + "/maxresdefault.jpg"; emb.WithThumbnailUrl(thumbURL); emb.AddField("Now Playing from YouTube:", title); } else if (audioFile.Link.Contains("youtu.be")) { string[] url = audioFile.Link.Split(new string[] { "youtu.be/" }, StringSplitOptions.None); title = SCDownloader.GetYouTubeInfo(url[1])["items"][0]["snippet"]["title"].ToString().Replace("—", "-"); thumbURL = "https://i.ytimg.com/vi/" + url[1] + "/maxresdefault.jpg"; emb.WithThumbnailUrl(thumbURL); emb.AddField("Now Playing from YouTube:", title); } else if (audioFile.Link.Contains("play.cz") && audioFile.Link.Contains("icecast")) { emb.WithThumbnailUrl(thumbURL); emb.AddField("Now Playing from PLAY.cz:", Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(title))); } else { emb.AddField("Now Playing:", title); } await ReplyAsync(null, false, emb.Build()); await Program.LogText(LogSeverity.Info, "AudioModule", "Now Playing: " + title + ""); await Context.Client.SetGameAsync(title, null, ActivityType.Listening); await Shared.AudioPlayer.Play(value, audioFile); } else { await ReplyAsync("Sorry, but i cannot play this link."); await Program.LogText(LogSeverity.Warning, "AudioModule", "Cannot play media from link!"); } await Context.Client.SetGameAsync("Just chilling"); } else { await Program.LogText(LogSeverity.Warning, "AudioModule", $"Cannot play the audio source specified: {audioFile}"); } continue; } await Program.LogText(LogSeverity.Warning, "AudioModule", "Unable to play in the proper channel. Make sure the audio client is connected."); break; }while (!Shared.Playlist.IsEmpty && Shared.AutoPlayRunning && Shared.AutoPlay); if (AutoStop) { Shared.AutoPlay = false; } Shared.AutoPlayRunning = false; }
public async Task PlaylistAddAsync(string path) { AudioFile audioFile = await GetAudioFileAsync(path); if (audioFile == null) { return; } bool isStream = false; string thumbURL = ""; string title = audioFile.Title; EmbedBuilder emb = new EmbedBuilder(); foreach (RadioStation station in Program.config.Stations) { if (audioFile.Title == station.Filename) { title = station.Name; thumbURL = station.Thumbnail; } } if (title != "") { emb.WithAuthor(Context.Client.CurrentUser); emb.WithColor(Color.Red); if (path.Contains("soundcloud.com")) { emb.WithThumbnailUrl(SCDownloader.GetTrack(path)["artwork_url"].ToString().Replace("large", "t500x500")); emb.WithTitle("Added to playlist:"); emb.AddField(SCDownloader.GetTrack(path)["user"]["username"].ToString(), SCDownloader.GetTrack(path)["title"].ToString()); await Program.LogText(LogSeverity.Warning, "AudioModule", "Added to playlist: " + title + ""); } else if (path.Contains("youtube.com")) { string[] url = path.Split(new string[] { "watch?v=" }, StringSplitOptions.None); title = SCDownloader.GetYouTubeInfo(url[1])["items"][0]["snippet"]["title"].ToString().Replace("—", "-"); thumbURL = "https://i.ytimg.com/vi/" + url[1] + "/maxresdefault.jpg"; emb.WithThumbnailUrl(thumbURL); emb.AddField("Added to playlist from YouTube:", title); await Program.LogText(LogSeverity.Warning, "AudioModule", "Added to playlist: " + title + ""); } else if (path.Contains("youtu.be")) { string[] url = path.Split(new string[] { "youtu.be/" }, StringSplitOptions.None); title = SCDownloader.GetYouTubeInfo(url[1])["items"][0]["snippet"]["title"].ToString().Replace("—", "-"); thumbURL = "https://i.ytimg.com/vi/" + url[1] + "/maxresdefault.jpg"; emb.WithThumbnailUrl(thumbURL); emb.AddField("Added to playlist from YouTube:", title); await Program.LogText(LogSeverity.Warning, "AudioModule", "Added to playlist: " + title + ""); } else if (path.Contains("play.cz") && path.Contains("icecast")) { emb.WithThumbnailUrl(thumbURL); isStream = true; emb.AddField("Error", "Cannot add audio stream to playlist!"); await Program.LogText(LogSeverity.Warning, "AudioModule", "Cannot add audio streams to the playlist!"); } else { emb.AddField("Added to playlist:", title); await Program.LogText(LogSeverity.Info, "AudioModule", "Added to playlist: " + title + ""); } audioFile.Link = path; } if (isStream) { Shared.Playlist.Enqueue(audioFile); } await ReplyAsync(null, false, emb.Build()); if (AutoDownload && !isStream) { if (audioFile.IsNetwork) { Shared.AudioDownloader.Push(audioFile); } await Shared.AudioDownloader.StartDownloadAsync(); } }
public async Task ForcePlayAudioAsync(IGuild guild, IMessageChannel channel, string path) { if (guild == null) { return; } AudioFile song = await GetAudioFileAsync(path); if (song != null) { Interlocked.Increment(ref NumPlaysCalled); if (Shared.AudioPlayer.IsRunning()) { StopAudio(); } while (Shared.AudioPlayer.IsRunning()) { await Task.Delay(1000); } if (Shared.ConnectedChannels.TryGetValue(guild.Id, out IAudioClient value)) { string thumbURL = ""; string title = song.Title; foreach (RadioStation station in Program.config.Stations) { if (song.Title == station.Filename) { title = station.Name; thumbURL = station.Thumbnail; } } if (title != "") { EmbedBuilder emb = new EmbedBuilder(); emb.WithAuthor(Context.Client.CurrentUser); emb.WithColor(Color.Red); if (path.Contains("soundcloud.com") && !Program.config.SCSettings.Enabled) { await ReplyAsync("Soundcloud playback is disabled by admin.", false); return; } else if ((path.Contains("youtube.com") || path.Contains("youtu.be")) && !Program.config.YTSettings.Enabled) { await ReplyAsync("YouTube playback is disabled by admin.", false); return; } else { if (path.Contains("soundcloud.com")) { emb.WithThumbnailUrl(SCDownloader.GetTrack(path)["artwork_url"].ToString().Replace("large", "t500x500")); emb.WithTitle("Now Playing:"); emb.AddField(SCDownloader.GetTrack(path)["user"]["username"].ToString(), SCDownloader.GetTrack(path)["title"].ToString()); } else if (path.Contains("youtube.com")) { string[] url = path.Split(new string[] { "watch?v=" }, StringSplitOptions.None); title = SCDownloader.GetYouTubeInfo(url[1])["items"][0]["snippet"]["title"].ToString(); thumbURL = "https://i.ytimg.com/vi/" + url[1] + "/maxresdefault.jpg"; emb.WithThumbnailUrl(thumbURL); emb.AddField("Now Playing from YouTube:", title); } else if (path.Contains("youtu.be")) { string[] url = path.Split(new string[] { "youtu.be/" }, StringSplitOptions.None); title = SCDownloader.GetYouTubeInfo(url[1])["items"][0]["snippet"]["title"].ToString(); thumbURL = "https://i.ytimg.com/vi/" + url[1] + "/maxresdefault.jpg"; emb.WithThumbnailUrl(thumbURL); emb.AddField("Now Playing from YouTube:", title); } else if (path.Contains("play.cz") && path.Contains("icecast")) { emb.WithThumbnailUrl(thumbURL); emb.AddField("Now Playing from PLAY.cz:", title); } else { emb.AddField("Now Playing:", title); } await ReplyAsync(null, false, emb.Build()); await Program.LogText(LogSeverity.Info, "AudioModule", "Now Playing: " + title + ""); await Context.Client.SetGameAsync(title, null, ActivityType.Listening); await Shared.AudioPlayer.Play(value, song); } } else if (path.Contains("spotify.com")) { await ReplyAsync("I cannot play songs from Spotify, because they have an really hard API for me. 😦", false); return; } else { await ReplyAsync("Media not found on this link."); await Program.LogText(LogSeverity.Warning, "AudioModule", "Cannot play media from link!"); } await Context.Client.SetGameAsync("Just chilling"); } else { await Program.LogText(LogSeverity.Warning, "AudioModule", "Unable to play in the proper channel. Make sure the audio client is connected."); await ReplyAsync("I'm not connected to the voice channel."); } Interlocked.Decrement(ref NumPlaysCalled); } }