/// <summary> /// Gets a <see cref="ServerMusicItem"/> /// </summary> /// <param name="guildId"></param> /// <returns></returns> public static ServerMusicItem GetMusicList(ulong guildId) { IEnumerable <ServerMusicItem> result = from a in currentChannels where a.GuildId == guildId select a; ServerMusicItem list = result.FirstOrDefault(); return(list); }
/// <summary> /// Checks if server is currently already playing music /// </summary> /// <param name="guild"></param> /// <param name="serverMusic"></param> /// <returns></returns> private bool CheckIfServerIsPlayingMusic(IGuild guild, out ServerMusicItem serverMusic) { ServerMusicItem serverMusicList = GetMusicList(guild.Id); if (serverMusicList == null) { serverMusic = null; return(false); } serverMusic = serverMusicList; return(true); }
/// <summary> /// Stops a song playing on a guild /// </summary> /// <param name="serverMusic"></param> /// <returns></returns> public static async Task StopPlayingAudioOnServer(ServerMusicItem serverMusic) { if (serverMusic.IsPlaying) { serverMusic.CancellationSource.Cancel(); while (serverMusic.CancellationSource != null) { //Wait until CancellationSource is null await Task.Delay(100); } } }
/// <summary> /// Pauses the current music playback /// </summary> /// <param name="guild">The <see cref="IGuild"/> that it is in</param> /// <param name="channel">The <see cref="IMessageChannel"/> to log messages to</param> /// <param name="user">The <see cref="IUser"/> who requested the command</param> /// <returns></returns> public async Task PauseAudio(IGuild guild, IMessageChannel channel, IUser user) { if (guild == null) { return; } ServerMusicItem musicList = GetMusicList(guild.Id); if (musicList == null) //The bot isn't in any voice channels { await channel.SendMessageAsync(":musical_note: There is no music being played!"); return; } //Check to see if the user is in the playing audio channel if (musicList.AudioChannel.GetUser(user.Id) == null) { await channel.SendMessageAsync(":musical_note: You are not in the current playing channel!"); return; } //Toggle pause status musicList.IsPlaying = !musicList.IsPlaying; if (musicList.IsPlaying) { await channel.SendMessageAsync(":musical_note: Current song has been un-paused."); } else { await channel.SendMessageAsync(":musical_note: Current song has been paused."); } }
/// <summary> /// Gets, or downloads (if necessary) a song /// </summary> /// <param name="search"></param> /// <param name="message"></param> /// <param name="musicList"></param> /// <returns>Returns a path to the song, or null if it failed</returns> private async Task <string> GetOrDownloadSong(string search, IUserMessage message, ServerMusicItem musicList) { string songFileLocation; if (musicList.Downloader != null) { musicList.Downloader.CancelTask(); await Task.Delay(100); musicList.Downloader = null; } musicList.Downloader = new StandardMusicDownloader(MusicDir, fileFormat, Global.HttpClient, new CancellationTokenSource()); if (WebUtils.IsStringValidUrl(search)) { songFileLocation = await musicList.Downloader.GetSongViaYouTubeUrl(search, message); } else { songFileLocation = await musicList.Downloader.GetOrDownloadSong(search, message); } return(songFileLocation); }
private async Task <bool> CheckIfUserInChat(IUser user, IMessageChannel channel, ServerMusicItem serverMusic) { //Check to see if the user is in the playing voice channel if (serverMusic.AudioChannel.GetUser(user.Id) == null) { await channel.SendMessageAsync(":musical_note: You are not in the current playing channel!"); return(false); } return(true); }
/// <summary> /// Gets, or downloads (if necessary) a song /// </summary> /// <param name="search"></param> /// <param name="message"></param> /// <param name="guild"></param> /// <param name="serverMusicList"></param> /// <returns>Returns a path to the song, or null if it failed</returns> private async Task <string> GetOrDownloadSong(string search, IUserMessage message, SocketGuild guild, ServerMusicItem serverMusicList) { string songFileLocation; //We are downloading a direct URL if (WebUtils.IsStringValidUrl(search)) { //Check to make sure the URL has youtube in it if (search.Contains("www.youtube.com/")) { await MessageUtils.ModifyMessage(message, ":musical_note: Processing YouTube URL..."); Uri uri = new Uri(search); //Get video ID NameValueCollection query = HttpUtility.ParseQueryString(uri.Query); string videoId = query.AllKeys.Contains("v") ? query["v"] : uri.Segments.Last(); if (videoId == "/") { await MessageUtils.ModifyMessage(message, ":musical_note: The imputed URL is not a valid YouTube URL!"); return(null); } //Stop any pre-existing downloader await StopMusicFileDownloader(); //Create the new MusicDownloader serverMusicList.AudioMusicFilesDownloader = new MusicDownloader(message, guild, Config.bot.AudioSettings.MaxVideoTime, MusicDir, fileFormat); //Download the song songFileLocation = serverMusicList.AudioMusicFilesDownloader.DownloadAudioById(videoId); serverMusicList.AudioMusicFilesDownloader.Dispose(); } else { await MessageUtils.ModifyMessage(message, ":musical_note: The imputed URL is not a YouTube URL!"); return(null); } } else //Search and download normally { await MessageUtils.ModifyMessage(message, $":musical_note: Searching my audio banks for '{search}'"); //First, search to see if we have the song already downloaded songFileLocation = SearchMusicDirectory(search, fileFormat); //Search YouTube if (!string.IsNullOrWhiteSpace(songFileLocation)) { return(songFileLocation); } //Stop any pre-existing downloader await StopMusicFileDownloader(); //Create the new MusicDownloader serverMusicList.AudioMusicFilesDownloader = new MusicDownloader(message, guild, Config.bot.AudioSettings.MaxVideoTime, MusicDir, fileFormat); //Download the song songFileLocation = serverMusicList.AudioMusicFilesDownloader.DownloadAudioByTitle(search); serverMusicList.AudioMusicFilesDownloader.Dispose(); } return(songFileLocation); async Task StopMusicFileDownloader() { if (serverMusicList.AudioMusicFilesDownloader != null) { serverMusicList.AudioMusicFilesDownloader.Dispose(); await Task.Delay(100); //Wait a moment so the previous download can cancel and clean up } } }
public async Task UserVoiceStateUpdated(SocketUser user, SocketVoiceState before, SocketVoiceState after) { try { ServerList server = ServerListsManager.GetServer(((SocketGuildUser)user).Guild); //If we are adding an auto voice channel if (after.VoiceChannel != null) { ServerAudioVoiceChannel audioVoiceChannel = server.GetAutoVoiceChannel(after.VoiceChannel.Id); if (audioVoiceChannel.Name != null) { RestVoiceChannel createdChannel = await after.VoiceChannel.Guild.CreateVoiceChannelAsync( $"{audioVoiceChannel.Name} #" + (server.ActiveAutoVoiceChannels.Count + 1), x => { x.CategoryId = after.VoiceChannel.CategoryId; x.Bitrate = after.VoiceChannel.Bitrate; x.Position = after.VoiceChannel.Position + 1; }); if (createdChannel.CategoryId != null) { await createdChannel.SyncPermissionsAsync(); } //Move the user who created the channel to the new channel await((SocketGuildUser)user).ModifyAsync(x => { x.ChannelId = createdChannel.Id; }); server.ActiveAutoVoiceChannels.Add(createdChannel.Id); ServerListsManager.SaveServerList(); } } //If we are removing an auto voice channel if (before.VoiceChannel != null) { ulong activeChannel = server.GetActiveVoiceChannel(before.VoiceChannel.Id); if (activeChannel != 0) { //There are no user on the active auto voice channel if (before.VoiceChannel.Users.Count == 0) { await before.VoiceChannel.DeleteAsync(); server.ActiveAutoVoiceChannels.Remove(before.VoiceChannel.Id); ServerListsManager.SaveServerList(); } } } //Only check channel user count if the audio services are enabled. if (Config.bot.AudioSettings.AudioServicesEnabled) { //There is an active song playing on this guild ServerMusicItem musicItem = MusicService.GetMusicList(((SocketGuildUser)user).Guild.Id); if (musicItem != null && musicItem.AudioChannel.Id == before.VoiceChannel?.Id) { //It is literally just us in the voice channel if (before.VoiceChannel.Users.Count == 1) { await MusicService.StopPlayingAudioOnServer(musicItem); //Leave this voice channel await musicItem.AudioClient.StopAsync(); await musicItem.StartChannel.SendMessageAsync( ":musical_note: Left the audio channel due to there being no one there :("); MusicService.currentChannels.Remove(musicItem); } } } } catch (Exception ex) { Logger.Error("An error occured while managing a user voice updated event! {@Exception}", ex); } }