Esempio n. 1
0
        /// <summary>
        /// Do NOT run on main thread or syncronously
        /// </summary>
        /// <param name="song"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public async Task UpdateSaidDownloading(Music.ISong song, IUserMessage message)
        {
            TimeSpan  timeSpan  = new TimeSpan(0, 0, 1, 0);
            var       seconds   = timeSpan.Seconds;
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            while (stopwatch.Elapsed.Seconds < seconds)
            {
                if (song.FileExists())
                {
                    try
                    {
                        if (song.HasTitle())
                        {
                            await message.ModifyAsync(n => n.Content = "Now playing `" + song.title + "`");
                        }
                        else
                        {
                            await message.ModifyAsync(n => n.Content = "Now playing `" + song.url + "`");
                        }
                    }
                    catch (Exception) { }
                    return;
                }
                await Task.Delay(5000);
            }
        }
Esempio n. 2
0
        public string ClearQueue(Music.ISong song, int count = Int32.MaxValue)
        {
            if (count > 0)
            {
                int queuenum = queue.songs.Count(n => n == song);
                int actnum   = 0;
                for (int i = 0; i < count || i < queuenum; i++)
                {
                    queue.songs.Remove(song);

                    actnum++;
                }
                if (actnum > 1)
                {
                    return("Cleared `" + actnum + "` songs");
                }
                else if (queuenum == 1)
                {
                    return("Cleared `1` song");
                }
                else
                {
                    return("Cleared no songs");
                }
            }
            else
            {
                return("Cleared no songs");
            }
        }
Esempio n. 3
0
 public void RemoveSong(ISong song)
 {
     if (song != null && songs.Contains(song))
     {
         songs.Remove(song);
     }
 }
Esempio n. 4
0
 public void Enqueue(ISong song)
 {
     if (song != null)
     {
         songs.Add(song);
     }
 }
Esempio n. 5
0
 public async Task AddSong(Music.ISong song)
 {
     //queue.AddSong(song);
     if (!song.FileExists())
     {
         var song_ = (await GuildHandler.Music.Download.GetSongAsync(song.url));
         song_.requester = song.requester;
         queue.Enqueue(song_);
     }
     else
     {
         queue.Enqueue(song);
     }
 }
Esempio n. 6
0
        public static async Task <ISong> GetSongAsync(string url, string path = null)
        {
            try
            {
                //Youtube link
                if (getid.IsMatch(url) || id.IsMatch(url))
                {
                    ISong Song = new ISong();
                    Song.attemptedDownload = true;
                    //Tag tag = TagLib.File.Create(path).Tag;
                    if (System.IO.File.Exists(Program.config.musicPath + ID(url) + ".m4a"))
                    {
                    }
                    else
                    {
                        var song = await YouTube_DL(url, Program.config.musicPath + ID(url) + ".m4a");
                    }
                    Song.fileName = ID(url) + ".m4a";
                    File file       = null;
                    int  numoftries = 0;
                    while (numoftries < 5)
                    {
                        try
                        {
                            file = TagLib.File.Create(Program.config.musicPath + ID(url) + ".m4a");
                            break;
                        }
                        catch (Exception)
                        {
                            await Task.Delay(500);

                            numoftries++;
                        }
                    }
                    Song.title  = System.Web.HttpUtility.HtmlDecode(file.Tag.Title);
                    Song.url    = url;
                    Song.length = new TimeSpan(0, 0, 0, 0, (int)(file.Properties.Duration.TotalMilliseconds));
                    return(Song);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception)
            {
                return(null);
            }
        }
Esempio n. 7
0
 public async Task SayNowPlaying(Music.ISong song)
 {
     if (song != null)
     {
         if (musicChannel is ITextChannel)
         {
             if (song.HasTitle())
             {
                 await(musicChannel as ITextChannel).SendMessageAsync("Now playing `" + song.title + "`");
             }
             else
             {
                 await(musicChannel as ITextChannel).SendMessageAsync("Now playing `" + song.url + "`");
             }
         }
     }
 }
Esempio n. 8
0
        static public List <ISong> SearchYTGetSongs(string query)
        {
            List <ISong> songs  = new List <ISong>();
            var          search = new VideoSearch().SearchQuery(query, 1);

            foreach (var result in search)
            {
                try
                {
                    TimeSpan duration = TimeSpan.FromMilliseconds(0);
                    var      song     = new ISong().WithURL(result.Url).WithTitle(result.Title).WithDuration(duration);
                    song.fileName = Download.ID(result.Url) + ".m4a";
                    songs.Add(song);
                }
                catch (Exception) { }
            }
            return(songs);
        }
Esempio n. 9
0
        public async Task SayDownloading(Music.ISong song)
        {
            IUserMessage userMessage = null;

            if (song != null)
            {
                if (musicChannel is ITextChannel)
                {
                    if (song.HasTitle())
                    {
                        userMessage = await(musicChannel as ITextChannel).SendMessageAsync("Downloading `" + song.title + "`");
                        await Task.Run(() => UpdateSaidDownloading(song, userMessage));
                    }
                    else
                    {
                        userMessage = await(musicChannel as ITextChannel).SendMessageAsync("Downloading `" + song.url + "`");
                        await Task.Run(() => UpdateSaidDownloading(song, userMessage));
                    }
                }
            }
        }
Esempio n. 10
0
 public async Task RemoveSong(Music.ISong song)
 {
     queue.RemoveSong(queue.songs.Where(n => n.fileName == song.fileName).First());
 }
Esempio n. 11
0
        //public async Task MusicThread()
        //{
        //    while (true)
        //    {
        //        while (queue.songs.Count > 0 && audioClient != null && audioChannel != null)
        //        {
        //            if (audioClient != null)
        //            {
        //                _nowPlaying = queue.songs[0];
        //                //Consume a song
        //                queue.songs.RemoveAt(0);
        //                if (_nowPlaying.attemptedDownload || _nowPlaying.FileExists())
        //                {
        //                    try
        //                    {
        //                        _currentFFMpegProc = new Music.Play();
        //                        if (_nowPlaying.HasTitle())
        //                        {
        //                            await SendMessage("Now playing `" + _nowPlaying.title + "`");
        //                        }
        //                        else
        //                        {
        //                            await SendMessage("Now playing `" + _nowPlaying.url + "`");
        //                        }
        //                        await _currentFFMpegProc.PlaySongNAudio(0, _nowPlaying.fileName, audioClient);
        //                    } catch(Exception e)
        //                    {
        //                        Logger.Error("Playing song, maybe track skip?");
        //                    }
        //                }
        //                else
        //                {
        //                    try
        //                    {
        //                        //We need to download the song
        //                        _nowPlaying = await GuildHandler.Music.Download.GetSongAsync(_nowPlaying.url);
        //                        if (_nowPlaying.FileExists())
        //                        {
        //                            _currentFFMpegProc = new Music.Play();
        //                            if (_nowPlaying.HasTitle())
        //                            {
        //                                await SendMessage("Now playing `" + _nowPlaying.title + "`");
        //                            }
        //                            else
        //                            {
        //                                await SendMessage("Now playing `" + _nowPlaying.url + "`");
        //                            }
        //                            await _currentFFMpegProc.PlaySongNAudio(0, _nowPlaying.fileName, audioClient);
        //                        }
        //                        else
        //                        {
        //                            //Video was unavailable, notify
        //                            //Logger.Warning("Video: " + _nowPlaying.url + " was unnavailable");
        //                            if (Program.mainHandler.guildHandles[guild.Id].database.musicChannel != 0)
        //                            {
        //                                var musicChannel = guild.GetChannelAsync(Program.mainHandler.guildHandles[guild.Id].database.musicChannel) as ITextChannel;
        //                                if (musicChannel != null)
        //                                {
        //                                    if (_nowPlaying.HasTitle())
        //                                    {
        //                                        await musicChannel.SendMessageAsync("Song `" + _nowPlaying.title + "` is unavailable");
        //                                    }
        //                                    else
        //                                    {
        //                                        await musicChannel.SendMessageAsync("Song `" + _nowPlaying.url + "` is unavailable");
        //                                    }
        //                                }
        //                            }
        //                        }

        //                        /*
        //                        await new Music.Play().PlaySong(0,
        //                        (await GuildHandler.Music.Download.GetSongAsync(_nowPlaying.url)).fileName, audioClient);
        //                        */
        //                    } catch(Exception e)
        //                    {
        //                        Logger.Error("Playing song, maybe track skip?");
        //                    }
        //                }
        //            }
        //            else
        //            {
        //                if (!queue.songs[0].FileExists())
        //                {
        //                    queue.songs[0] = await GuildHandler.Music.Download.GetSongAsync(queue.songs[0].url);
        //                }
        //            }
        //        }
        //        await Task.Delay(1000);
        //    }
        //}

        public async Task MusicThread()
        {
            while (true)
            {
                await Task.Delay(1000);

                if (audioClient != null)
                {
                    if (audioStream == null)
                    {
                        audioStream = audioClient.CreatePCMStream(AudioApplication.Music);
                    }
                    Music.ISong song = _nowPlaying;

                    while (queue.songs.Count > 0 && song == null && _nowPlaying == null)
                    {
                        song = queue.Dequeue();
                    }
                    if (song == null && queue.songs.Count < 1)
                    {
                        try
                        {
                            audioStream.Close();
                            await audioClient.StopAsync();

                            audioClient  = null;
                            audioStream  = null;
                            AudioPlaying = false;
                            AudioStopped = false;
                        }
                        catch (Exception)
                        {
                            audioClient  = null;
                            audioStream  = null;
                            AudioPlaying = false;
                            AudioStopped = false;
                        }
                        if (musicChannel is ITextChannel)
                        {
                            await(musicChannel as ITextChannel).SendMessageAsync("Queue finished playing");
                        }
                    }
                    else
                    {
                        if (song != null)
                        {
                            if (_nowPlaying == null)
                            {
                                _nowPlaying = song;
                            }
                            if (_nowPlaying.FileExists())
                            {
                                try
                                {
                                    try
                                    {
                                        AudioPlaying = true;
                                        if (loopType != Database.LoopType.SingleLoop)
                                        {
                                            await SayNowPlaying(_nowPlaying);
                                        }
                                        var time = _currentFFMpegProc.PlaySongNAudio(_nowPlaying.currentTrackTimeOffset.Seconds, _nowPlaying.fileName, audioStream);
                                        if (!AudioPaused)
                                        {
                                            if (loopType == Database.LoopType.NoLoop)
                                            {
                                                _nowPlaying = null;
                                            }
                                            else if (loopType == Database.LoopType.SingleLoop)
                                            {
                                                _nowPlaying.currentTrackTimeOffset = new TimeSpan(0, 0, 0, 0, 0);
                                            }
                                            else if (loopType == Database.LoopType.QueueLoop)
                                            {
                                                queue.Enqueue(_nowPlaying);
                                            }
                                        }
                                        else
                                        {
                                            _nowPlaying.currentTrackTimeOffset = time;
                                        }
                                    }
                                    catch (Exception e)
                                    {
                                        Logger.Error("Playing music: ", e);
                                        if (!AudioPaused)
                                        {
                                            _nowPlaying = null;
                                        }
                                        else
                                        {
                                            _nowPlaying.currentTrackTimeOffset = new TimeSpan(0, 0, 0, 0, 0);
                                        }
                                    }
                                }
                                catch (Exception) { }
                                AudioPlaying = false;
                                if (AudioStopped)
                                {
                                    try
                                    {
                                        audioStream.Close();
                                        await audioClient.StopAsync();

                                        audioClient = null;
                                        audioStream = null;
                                    }
                                    catch (Exception)
                                    {
                                        audioClient = null;
                                        audioStream = null;
                                    }
                                }
                            }
                            else
                            {
                                if (_nowPlaying.url != null)
                                {
                                    await SayDownloading(_nowPlaying);

                                    _nowPlaying = await Music.Download.GetSongAsync(_nowPlaying.url);
                                }
                                else
                                {
                                    _nowPlaying = null;
                                }
                            }
                        }
                    }
                }

                while (AudioPaused && audioClient != null)
                {
                    await Task.Delay(1000);
                }
            }
        }
Esempio n. 12
0
        /*
         * public static bool HasPermission(string CommandTree, IGuildUser user)
         * {
         *  string[] permissions = Program.mainHandler.guildHandles[user.GuildId].permissionsHandler.getPerms(user).perms;
         *  string[] permissionsExclusion = Program.mainHandler.guildHandles[user.GuildId].permissionsHandler.getPerms(user).permsExclude;
         *  string command = CommandTree.Split('\'').Last();
         *  string commandFamily = CommandTree.Split('\'')[0];
         *  if (Program.config.botAdmins.Any(user.Id.ToString().Equals))
         *  { return true; }
         *
         *  //if (permissions.Any(command.Equals))
         *  //{
         *  //    return true;
         *  //}
         *
         *  //Return true if it is the guild owner, but not if they sent a botadmin command
         *  if (user.Guild.OwnerId == user.Id)
         *  {
         *      if (commandFamily == "Commands.BotAdmin")
         *      {
         *          if (permissions.Any(commandFamily.Equals))
         *          {
         *              return true;
         *          }
         *          else
         *          {
         *              return false;
         *          }
         *      }
         *      else
         *      {
         *          return true;
         *      }
         *  }
         *  if (permissions.Any(commandFamily.Equals) && !permissionsExclusion.Any(CommandTree.Equals))
         *  {
         *      return true;
         *  }
         *  return false;
         * }
         */
        public static async Task HandleCommand(SocketMessage message)
        {
            string content = null;
            string args    = null;

            if (message.Channel is IGuildChannel)
            {
                content = message.Content.Remove(0, Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].database.prefix.Length);
                args    = content.Remove(0, content.Split(' ')[0].Length).Trim();
            }
            else
            {
                content = message.Content.Remove(0, 1);
                args    = content.Remove(0, content.Split(' ')[0].Length).Trim();
            }
            string[]      splitmessage = content.Split(' ');
            IGuildChannel guildChannel = (message.Channel as IGuildChannel);
            IGuildUser    guildUser    = (message.Author as IGuildUser);

            //Guild only commands
            if (message.Channel is IGuildChannel)
            {
                switch (splitmessage[0].ToLower())
                {
                case "prefix":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'Prefix", message.Author as IGuildUser))
                    {
                        await Commands.Guild.Prefix.Prefix_(args, message);
                    }
                    return;

                case "welcomemessage":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'WelcomeMessage", message.Author as IGuildUser))
                    {
                        await Commands.Guild.WelcomeMessage.WelcomeMessage_(args, message);
                    }
                    return;

                case "leavemessage":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'WelcomeMessage", message.Author as IGuildUser))
                    {
                        await Commands.Guild.LeaveMessage.LeaveMessage_(args, message);
                    }
                    return;

                case "whoami":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.User'WhoAmI", message.Author as IGuildUser))
                    {
                        await message.Channel.SendMessageAsync(null, false, new EmbedBuilder()
                        {
                            Title = "WhoAmI?", Description = $"You are {message.Author.Username}"
                        }.Build());
                    }
                    return;

                case "testing":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.BotAdmin'Testing", message.Author as IGuildUser))
                    {
                        await message.Channel.SendMessageAsync("perms");
                    }
                    return;

                case "permissions":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'Permissions", message.Author as IGuildUser))
                    {
                        await message.Channel.SendMessageAsync(Commands.Guild.Permissions.Permissions_(args, message));
                    }
                    return;

                case "avatar":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.User'Avatar", message.Author as IGuildUser))
                    {
                        await Commands.User.Avatar.Avatar_(args, message);
                    }
                    return;

                case "defaultrole":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'DefaultRole", message.Author as IGuildUser))
                    {
                        await Commands.Guild.DefaultRole.DefaultRole_(args, message);
                    }
                    return;

                case "level":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'Level", message.Author as IGuildUser))
                    {
                        await Commands.Guild.Level.Level_(args, message);
                    }
                    return;

                case "privatevc":
                    if (Program.mainHandler.guildHandles[guildChannel.GuildId].permissionsHandler.HasPermission("Commands.Guild'PrivateVC", message.Author as IGuildUser))
                    {
                        await Commands.Guild.PrivateVC.PrivateVC_(args, message);
                    }
                    return;


                //Remove these
                case "purge":
                    await Commands.Guild.Purge.Purge_(args, message);

                    return;

                case "loop":
                    await Commands.Music.Loop.Loop_(args, message);

                    return;

                case "nowplaying":
                case "np":
                    await Commands.Music.NowPlaying.NowPlaying_(null, message);

                    return;

                case "dequeue":
                    var musichandle = Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle;
                    if ((message.MentionedRoles != null && message.MentionedRoles.Count > 0) || (message.MentionedUsers != null && message.MentionedUsers.Count > 0))
                    {
                        if (message.MentionedRoles != null && message.MentionedRoles.Count > 0)
                        {
                            await message.Channel.SendMessageAsync(musichandle.ClearQueue(musichandle.guild.Roles.Where(n => message.MentionedRoles.Any(m => m.Id == n.Id))));
                        }
                        if (message.MentionedUsers != null && message.MentionedUsers.Count > 0)
                        {
                            var users = await musichandle.guild.GetUsersAsync();

                            await message.Channel.SendMessageAsync(musichandle.ClearQueue(users.Where(n => message.MentionedUsers.Any(m => m.Id == n.Id))));
                        }
                    }
                    else
                    if (args.Length > 0)
                    {
                        try
                        {
                            await message.Channel.SendMessageAsync(musichandle.ClearQueue(Convert.ToInt16(args)));
                        }
                        catch (Exception)
                        {
                            await message.Channel.SendMessageAsync(musichandle.ClearQueue(GuildHandler.Music.Search.SearchYTGetSong(args)));
                        }
                    }
                    return;

                case "ping":
                    await message.Channel.SendMessageAsync("pong!");

                    return;

                case "pause":
                    await message.Channel.SendMessageAsync(Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Pause());

                    return;

                case "unpause":
                    await message.Channel.SendMessageAsync(Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Pause(true));

                    return;

                case "play":
                    if (splitmessage.Length > 1)
                    {
                        //var song = await GuildHandler.Music.Download.GetSongAsync(splitmessage[1]);
                        if (!GuildHandler.Music.Download.playList.IsMatch(splitmessage[1]) && GuildHandler.Music.Download.getid.IsMatch(splitmessage[1]))
                        {
                            var song = new GuildHandler.Music.ISong().WithURL(splitmessage[1]);
                            song.requester = message.Author as IGuildUser;
                            if (song.FileExists())
                            {
                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                                {
                                    await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");

                                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                }
                                else
                                {
                                    if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                                    {
                                        await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");

                                        await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                        await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                    }
                                    else
                                    {
                                        await message.Channel.SendMessageAsync("Downloading `" + song.title + "`");

                                        await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                        await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                    }
                                }
                            }
                            else
                            {
                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                                {
                                    if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.songs.Count > 0)
                                    {
                                        await message.Channel.SendMessageAsync("Added `" + song.url + "` to the queue");
                                    }
                                    else
                                    {
                                        await message.Channel.SendMessageAsync("Downloading `" + song.url + "`");
                                    }
                                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                }
                                else
                                {
                                    if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                                    {
                                        await message.Channel.SendMessageAsync("Added `" + song.url + "` to the queue");

                                        await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                        await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                    }
                                    else
                                    {
                                        await message.Channel.SendMessageAsync("Downloading `" + song.url + "`");

                                        await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                        await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                    }
                                }
                            }
                        }
                        else if (!GuildHandler.Music.Download.getid.IsMatch(splitmessage[1]))
                        {
                            var song = GuildHandler.Music.Search.SearchYTGetSong(message.Content.Substring(splitmessage[0].Length, message.Content.Length - splitmessage[0].Length));
                            song.requester = message.Author as IGuildUser;
                            if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                            {
                                await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");

                                await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                            }
                            else
                            {
                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                                {
                                    await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");
                                }
                                else
                                {
                                    await message.Channel.SendMessageAsync("Now playing `" + song.title + "`");
                                }
                                await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                            }
                        }
                        else if (GuildHandler.Music.Download.playList.IsMatch(splitmessage[1]))
                        {
                            var songs = GuildHandler.Music.Download.Playlist(splitmessage[1]);
                            songs.ForEach(n => n.requester = message.Author as IGuildUser);
                            await message.Channel.SendMessageAsync("Added playlist");

                            if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                            {
                                //Add to queue
                                //foreach (var song in songs)
                                //{
                                //    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                //}
                                Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.AddMany(songs);
                            }
                            else
                            {
                                await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                                {
                                    //Queue contains items, but nobody is in vc
                                    Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.AddMany(songs);
                                }
                                else
                                {
                                    //Nothing in the queue, play first song
                                    Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.AddMany(songs);
                                }
                            }
                        }
                    }
                    else if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.songs.Count > 0)
                    {
                        await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));
                    }
                    return;

                case "connect":
                    await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                    return;

                case "queue":
                    await message.Channel.SendMessageAsync(null, false, Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.GetQueueEmbed());

                    return;

                case "stop":
                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Disconnect();
                    await message.Channel.SendMessageAsync("Audio stopped");

                    return;

                case "skip":
                    if (splitmessage.Count() > 1)
                    {
                        try
                        {
                            int num = Convert.ToInt32(splitmessage[1]);
                            await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Skip(num);
                        }
                        catch (Exception) { }
                    }
                    else
                    {
                        await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Skip(1);
                    }
                    return;
                }
            }

            //User Commands
            switch (splitmessage[0].ToLower())
            {
            case "help":
                await Commands.User.Help.Help_(args, message);

                return;
            }
        }
Esempio n. 13
0
        public static async Task HandleCommand_OLD(SocketMessage message)
        {
            var Iguild = (message.Author as IGuildUser)?.Guild;

            string[] splitmessage;

            if (Iguild != null)
            {
                splitmessage = message.Content.Remove(0, Program.mainHandler.guildHandles[Iguild.Id].database.prefix.Length).Split(' ');
            }
            else
            {
                splitmessage = message.Content.TrimStart('$').Split(' ');
            }

            //Any user
            switch (splitmessage[0])
            {
            case "ping":
                await message.Channel.SendMessageAsync("pong!");

                return;

            case "play":
                if (splitmessage.Length > 1)
                {
                    //var song = await GuildHandler.Music.Download.GetSongAsync(splitmessage[1]);
                    if (!GuildHandler.Music.Download.playList.IsMatch(splitmessage[1]) && GuildHandler.Music.Download.getid.IsMatch(splitmessage[1]))
                    {
                        var song = new GuildHandler.Music.ISong().WithURL(splitmessage[1]);
                        if (song.FileExists())
                        {
                            if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                            {
                                await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");

                                await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                            }
                            else
                            {
                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                                {
                                    await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");

                                    await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                }
                                else
                                {
                                    await message.Channel.SendMessageAsync("Downloading `" + song.title + "`");

                                    await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                }
                            }
                        }
                        else
                        {
                            if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                            {
                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.songs.Count > 0)
                                {
                                    await message.Channel.SendMessageAsync("Added `" + song.url + "` to the queue");
                                }
                                else
                                {
                                    await message.Channel.SendMessageAsync("Downloading `" + song.url + "`");
                                }
                                await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                            }
                            else
                            {
                                if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                                {
                                    await message.Channel.SendMessageAsync("Added `" + song.url + "` to the queue");

                                    await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                }
                                else
                                {
                                    await message.Channel.SendMessageAsync("Downloading `" + song.url + "`");

                                    await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                                }
                            }
                        }
                    }
                    else if (!GuildHandler.Music.Download.getid.IsMatch(splitmessage[1]))
                    {
                        var song = GuildHandler.Music.Search.SearchYTGetSong(message.Content.Substring(splitmessage[0].Length, message.Content.Length - splitmessage[0].Length));

                        if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                        {
                            await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");

                            await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                        }
                        else
                        {
                            if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                            {
                                await message.Channel.SendMessageAsync("Added `" + song.title + "` to the queue");
                            }
                            else
                            {
                                await message.Channel.SendMessageAsync("Now playing `" + song.title + "`");
                            }
                            await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                            await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                        }
                    }
                    else if (GuildHandler.Music.Download.playList.IsMatch(splitmessage[1]))
                    {
                        var songs = GuildHandler.Music.Download.Playlist(splitmessage[1]);
                        await message.Channel.SendMessageAsync("Added playlist");

                        if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.audioClient != null)
                        {
                            //Add to queue
                            //foreach (var song in songs)
                            //{
                            //    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.AddSong(song);
                            //}
                            Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.AddMany(songs);
                        }
                        else
                        {
                            await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                            if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.NowPlaying != null)
                            {
                                //Queue contains items, but nobody is in vc
                                Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.AddMany(songs);
                            }
                            else
                            {
                                //Nothing in the queue, play first song
                                Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.AddMany(songs);
                            }
                        }
                    }
                }
                else if (Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.queue.songs.Count > 0)
                {
                    await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));
                }
                return;

            case "connect":
                await message.Channel.SendMessageAsync(await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Connect((message.Author as IGuildUser)));

                return;

            case "queue":
                await message.Channel.SendMessageAsync(null, false, Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.GetQueueEmbed());

                return;

            case "stop":
                await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Disconnect();
                return;

            case "skip":
                if (splitmessage.Count() > 1)
                {
                    try
                    {
                        int num = Convert.ToInt32(splitmessage[1]);
                        await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Skip(num);
                    }
                    catch (Exception) { }
                }
                else
                {
                    await Program.mainHandler.guildHandles[(message.Channel as IGuildChannel).GuildId].musicHandle.Skip(1);
                }
                return;
            }

            //Bot admin
            if (Program.config.botAdmins.Any(message.Author.Id.ToString().Contains))
            {
                switch (splitmessage[0])
                {
                case "reparse":
                    foreach (var ghandle in Program.mainHandler.guildHandles)
                    {
                        ghandle.Value.database.ParseConfig();
                    }
                    await message.Channel.SendMessageAsync("Reparsed all");

                    return;

                case "relay":
                    if (TCPChatRelay.open)
                    {
                        await message.Channel.SendMessageAsync("Relay is already open!");
                    }
                    else
                    {
                        Task.Run(() => TCPChatRelay.Relay(message.Channel as ITextChannel));
                        await message.Channel.SendMessageAsync("Relay is now open");
                    }
                    return;
                }
            }
        }
Esempio n. 14
0
 public void ParseConfig()
 {
     if (_json.ContainsKey("guildName"))
     {
         guildName = (string)_json["guildName"];
     }
     if (_json.ContainsKey("guildId"))
     {
         guildId = (ulong)_json["guildId"];
     }
     if (_json.ContainsKey("prefix"))
     {
         prefix = (string)_json["prefix"];
     }
     if (_json.ContainsKey("sendLeaveMessage"))
     {
         sendLeaveMessage = (bool)_json["sendLeaveMessage"];
     }
     if (_json.ContainsKey("sendWelcomeMessage"))
     {
         sendWelcomeMessage = (bool)_json["sendWelcomeMessage"];
     }
     if (_json.ContainsKey("welcomeMessage"))
     {
         welcomeMessage = (string)_json["welcomeMessage"];
     }
     if (_json.ContainsKey("leaveMessage"))
     {
         leaveMessage = (string)_json["leaveMessage"];
     }
     if (_json.ContainsKey("welcomeLeaveChannel"))
     {
         welcomeLeaveChannel = (ulong)_json["welcomeLeaveChannel"];
     }
     if (_json.ContainsKey("musicChannel"))
     {
         musicChannel = (ulong)_json["musicChannel"];
     }
     if (_json.ContainsKey("giveDefaultRole"))
     {
         giveDefaultRole = (bool)_json["giveDefaultRole"];
     }
     if (_json.ContainsKey("useLevels"))
     {
         useLevels = (bool)_json["useLevels"];
     }
     if (_json.ContainsKey("showLevelUpMessage"))
     {
         showLevelUpMessage = (bool)_json["showLevelUpMessage"];
     }
     if (_json.ContainsKey("voiceChannelTimeOut"))
     {
         voiceChannelTimeOut = (int)_json["voiceChannelTimeOut"];
     }
     if (_json.ContainsKey("defaultRole"))
     {
         if (guild.Roles.Any(n => n.Id == (ulong)_json["defaultRole"]))
         {
             defaultRole = (ulong)_json["defaultRole"];
         }
         else
         {
             giveDefaultRole = false;
         }
     }
     if (_json.ContainsKey("loop"))
     {
         loop = (LoopType)(int)_json["loop"];
     }
     if (_json.ContainsKey("queue"))
     {
         queue = new Music.Queue();
         foreach (var song in _json.Properties().Where(n => n.Name == "queue").First().Values())
         {
             var so = new Music.ISong();
             so.fileName = (string)song;
             if (so.FileExists())
             {
                 queue.Enqueue(so);
             }
         }
     }
     permissions = new PermissionsHandler();
     if (_json.ContainsKey("permissions"))
     {
         try
         {
             foreach (var perm in _json["permissions"].Children <JProperty>())
             {
                 ulong.TryParse(perm.Name, out ulong id);
                 if (!permissions.userPerms.ContainsKey(id))
                 {
                     permissions.userPerms.Add(id, new Permissions()
                     {
                         perms = new string[] { "" }
                     });
                     List <string> permstoadd = new List <string>(new string[] { "" });
                     if (perm.HasValues)
                     {
                         foreach (var pee in perm.Values())
                         {
                             permstoadd.Add((string)pee);
                         }
                     }
                     permissions.userPerms[id].perms = permstoadd.ToArray();
                 }
             }
         }
         catch (Exception e)
         {
             Logger.Error("Could not load old permissions for guild " + guild.Name + " (" + guild.Id + ")" + e);
         }
     }
     if (_json.ContainsKey("rolepermissions"))
     {
         try
         {
             foreach (var roleperm in _json["rolepermissions"].Children <JProperty>())
             {
                 ulong.TryParse(roleperm.Name, out ulong roleid);
                 if (!permissions.rolePerms.ContainsKey(roleid))
                 {
                     permissions.rolePerms.Add(roleid, new Permissions()
                     {
                         perms = new string[] { "" }
                     });
                     List <string> rolestoadd = new List <string>(new string[] { "" });
                     if (roleperm.HasValues)
                     {
                         foreach (var rolepee in roleperm.Values())
                         {
                             rolestoadd.Add((string)rolepee);
                         }
                     }
                     permissions.rolePerms[roleid].perms = rolestoadd.ToArray();
                 }
             }
         } catch (Exception e)
         {
             Logger.Error("Could not load old role permissions for guild " + guild.Name + " (" + guild.Id + ")" + e);
         }
     }
     userlevels = new Dictionary <ulong, Levels>();
     foreach (var user in guild.GetUsersAsync().GetAwaiter().GetResult())
     {
         userlevels.Add(user.Id, new Levels());
     }
     if (_json.ContainsKey("userLevels"))
     {
         try
         {
             foreach (var userlevel in _json["userLevels"].Children <JProperty>())
             {
                 ulong.TryParse(userlevel.Name, out ulong userid);
                 if (!userlevels.ContainsKey(userid))
                 {
                     userlevels.Add(userid, new Levels()
                     {
                         level = (int)userlevel.Value["level"], messagesSentSinceLevelUp = (int)userlevel.Value["messagesSentSinceLevelUp"], totalMessageSent = (int)userlevel.Value["totalMessagesSent"]
                     });
                 }
                 else
                 {
                     userlevels[userid].level = (int)userlevel.Value["level"];
                     userlevels[userid].messagesSentSinceLevelUp = (int)userlevel.Value["messagesSentSinceLevelUp"];
                     userlevels[userid].totalMessageSent         = (int)userlevel.Value["totalMessagesSent"];
                 }
             }
         } catch (Exception e)
         {
             Logger.Error($"Could not load old userLevels for guild {guild.Name} ({guild.Id})" + e);
         }
     }
     rolesPerLevel = new Dictionary <int, IRole>();
     if (_json.ContainsKey("roleLevels"))
     {
         try
         {
             var roleids = guild.Roles;
             foreach (var rolelevel in _json["roleLevels"].Children <JProperty>())
             {
                 ulong.TryParse(rolelevel.Name, out ulong roleid);
                 int key = (int)rolelevel.Values().First();
                 if (roleids.Select(n => n.Id).Any(n => n.Equals(roleid)))
                 {
                     rolesPerLevel.Add(key, roleids.First(n => n.Id == roleid));
                 }
             }
         } catch (Exception e)
         {
             Logger.Error($"Could not load old roleLevels for guild {guild.Name} ({guild.Id})", e);
         }
     }
 }