コード例 #1
0
        private void AddCommand(int index, string[] parts, Command command, bool isAlias)
        {
            if (!command.IsHidden)
            {
                _isVisible = true;
            }

            if (index != parts.Length)
            {
                CommandMap nextGroup;
                string     name     = parts[index].ToLowerInvariant();
                string     fullName = string.Join(" ", parts, 0, index + 1);
                if (!_items.TryGetValue(name, out nextGroup))
                {
                    nextGroup = new CommandMap(this, name, fullName);
                    _items.Add(name, nextGroup);
                    _hasSubGroups = true;
                }
                nextGroup.AddCommand(index + 1, parts, command, isAlias);
            }
            else
            {
                _commands.Add(command);
                if (!isAlias)
                {
                    _hasNonAliases = true;
                }
            }
        }
コード例 #2
0
ファイル: CommandMap.cs プロジェクト: Kusoneko/Nekobot
 public CommandMap(CommandMap parent, string name, string fullName)
     : this()
 {
     _parent = parent;
     _name = name;
     _fullName = fullName;
 }
コード例 #3
0
        internal void AddCommand(Command command)
        {
            _allCommands.Add(command);

            //Get category
            CommandMap category;
            string     categoryName = command.Category ?? "";

            if (!_categories.TryGetValue(categoryName, out category))
            {
                category = new CommandMap();
                _categories.Add(categoryName, category);
            }

            //Add main command
            category.AddCommand(command.Text, command, false);
            _map.AddCommand(command.Text, command, false);

            //Add aliases
            foreach (var alias in command.Aliases)
            {
                category.AddCommand(alias, command, true);
                _map.AddCommand(alias, command, true);
            }
        }
コード例 #4
0
ファイル: CommandMap.cs プロジェクト: Lirusaito/Nekobot
 /*public IEnumerable<Command> SubCommands => _items.Select(x => x.Value._command).Where(x => x != null);
 public IEnumerable<CommandMap> SubGroups => _items.Select(x => x.Value).Where(x => x._items.Count > 0);*/
 public CommandMap(CommandMap parent, string name, string fullName)
 {
     _parent = parent;
     _name = name;
     _fullName = fullName;
     _items = new Dictionary<string, CommandMap>();
     _commands = new List<Command>();
     _isHidden = true;
 }
コード例 #5
0
        public static bool ParseCommand(string input, CommandMap map, out IEnumerable <Command> commands, out int endPos)
        {
            int  startPosition = 0;
            int  endPosition   = 0;
            int  inputLength   = input.Length;
            bool isEscaped     = false;

            commands = null;
            endPos   = 0;

            if (input == "")
            {
                return(false);
            }

            while (endPosition < inputLength)
            {
                char currentChar = input[endPosition++];
                if (isEscaped)
                {
                    isEscaped = false;
                }
                else if (currentChar == '\\')
                {
                    isEscaped = true;
                }

                bool isWhitespace = IsWhiteSpace(currentChar);
                if ((!isEscaped && isWhitespace) || endPosition >= inputLength)
                {
                    int    length = (isWhitespace ? endPosition - 1 : endPosition) - startPosition;
                    string temp   = input.Substring(startPosition, length);
                    if (temp == "")
                    {
                        startPosition = endPosition;
                    }
                    else
                    {
                        var newMap = map.GetItem(temp);
                        if (newMap != null)
                        {
                            map    = newMap;
                            endPos = endPosition;
                        }
                        else
                        {
                            break;
                        }
                        startPosition = endPosition;
                    }
                }
            }
            commands = map.GetCommands(); //Work our way backwards to find a command that matches our input
            return(commands != null);
        }
コード例 #6
0
ファイル: CommandService.cs プロジェクト: Lirusaito/Nekobot
 public CommandService(CommandServiceConfig config, Func<Channel, bool> getNsfwFlag = null, Func<User, bool> getMusicFlag = null, Func<Channel, User, bool> getIgnoredChannelFlag = null)
 {
     _config = config;
     _getNsfwFlag = getNsfwFlag;
     _getMusicFlag = getMusicFlag;
     _getIgnoredChannelFlag = getIgnoredChannelFlag;
     _allCommands = new List<Command>();
     _map = new CommandMap(null, "", "");
     _categories = new Dictionary<string, CommandMap>();
     _root = new CommandGroupBuilder(this, "", null);
 }
コード例 #7
0
ファイル: CommandService.cs プロジェクト: ImportTaste/Nekobot
        public CommandService(CommandServiceConfig config, Func <IMessageChannel, bool> getNsfwFlag = null, Func <IVoiceState, bool> getMusicFlag = null, Func <IMessageChannel, IUser, bool> getIgnoredChannelFlag = null)
        {
            Config = config;

            _getNsfwFlag           = getNsfwFlag;
            _getMusicFlag          = getMusicFlag;
            _getIgnoredChannelFlag = getIgnoredChannelFlag;
            _allCommands           = new List <Command>();
            _map        = new CommandMap();
            _categories = new Dictionary <string, CommandMap>();
            Root        = new CommandGroupBuilder(this);
        }
コード例 #8
0
ファイル: CommandParser.cs プロジェクト: Kusoneko/Nekobot
        public static bool ParseCommand(string input, CommandMap map, out IEnumerable<Command> commands, out int endPos)
        {
            int startPosition = 0;
            int endPosition = 0;
            int inputLength = input.Length;
            bool isEscaped = false;
            commands = null;
            endPos = 0;

            if (input == "")
                return false;

            while (endPosition < inputLength)
            {
                char currentChar = input[endPosition++];
                if (isEscaped)
                    isEscaped = false;
                else if (currentChar == '\\')
                    isEscaped = true;

                bool isWhitespace = IsWhiteSpace(currentChar);
                if ((!isEscaped && isWhitespace) || endPosition >= inputLength)
                {
                    int length = (isWhitespace ? endPosition - 1 : endPosition) - startPosition;
                    string temp = input.Substring(startPosition, length);
                    if (temp == "")
                        startPosition = endPosition;
                    else
                    {
                        var newMap = map.GetItem(temp);
                        if (newMap != null)
                        {
                            map = newMap;
                            endPos = endPosition;
                        }
                        else
                            break;
                        startPosition = endPosition;
                    }
                }
            }
            commands = map.GetCommands(); //Work our way backwards to find a command that matches our input
            return commands != null;
        }
コード例 #9
0
ファイル: CommandService.cs プロジェクト: Lirusaito/Nekobot
        internal void AddCommand(Command command)
        {
            _allCommands.Add(command);

            //Get category
            CommandMap category;
            string categoryName = command.Category ?? "";
            if (!_categories.TryGetValue(categoryName, out category))
            {
                category = new CommandMap(null, "", "");
                _categories.Add(categoryName, category);
            }

            //Add main command
            category.AddCommand(command.Text, command);
            _map.AddCommand(command.Text, command);

            //Add aliases
            foreach (var alias in command.Aliases)
            {
                category.AddCommand(alias, command);
                _map.AddCommand(alias, command);
            }
        }
コード例 #10
0
ファイル: CommandService.cs プロジェクト: Lirusaito/Nekobot
        private Task ShowCommandHelp(CommandMap map, User user, Channel channel, Channel replyChannel = null)
        {
            StringBuilder output = new StringBuilder();

            IEnumerable<Command> cmds = map.Commands;
            bool isFirstCmd = true;
            string error;
            if (cmds != null)
            {
                foreach (var cmd in cmds)
                {
                    if (!cmd.CanRun(user, channel, out error)) { }
                        //output.AppendLine(error ?? DefaultPermissionError);
                    else
                    {
                        if (isFirstCmd)
                            isFirstCmd = false;
                        else
                            output.AppendLine();
                        ShowCommandHelpInternal(cmd, user, channel, output);
                    }
                }
            }
            else
            {
                output.Append('`');
                output.Append(map.FullName);
                output.Append("`\n");
            }

            bool isFirstSubCmd = true;
            foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && !x.IsHidden))
            {
                if (isFirstSubCmd)
                {
                    isFirstSubCmd = false;
                    output.Append("**Sub Commands:** ");
                }
                else
                    output.Append(", ");
                output.Append('`');
                output.Append(subCmd.Name);
                if (subCmd.SubGroups.Any())
                    output.Append("*");
                output.Append('`');
            }

            if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands
            {
                output.Clear();
                output.AppendLine("There are no commands you have permission to run.");
            }

            return _client.SendMessage(replyChannel ?? channel, output.ToString());
        }
コード例 #11
0
ファイル: CommandService.cs プロジェクト: ImportTaste/Nekobot
        private Task ShowCommandHelp(CommandMap map, IUser user, IMessageChannel channel, IMessageChannel replyChannel = null)
        {
            EmbedBuilder output = EmbedBuilder();

            IEnumerable <Command> cmds = map.Commands;
            bool   isFirstCmd          = true;
            string error;

            if (cmds.Any())
            {
                foreach (var cmd in cmds)
                {
                    if (!cmd.CanRun(user, channel, out error))
                    {
                    }
                    //output.AppendLine(error ?? DefaultPermissionError);
                    else
                    {
                        if (isFirstCmd)
                        {
                            isFirstCmd = false;
                        }
                        ShowCommandHelpInternal(cmd, user, channel, output);
                    }
                }
            }
            else
            {
                output.WithTitle(map.FullName);
            }

            bool isFirstSubCmd = true;

            var field = new EmbedFieldBuilder();

            foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && x.IsVisible))
            {
                if (isFirstSubCmd)
                {
                    isFirstSubCmd = false;
                    field.WithName("Sub Commands");
                }
                else
                {
                    field.Value += ", ";
                }
                field.Value += $"`{subCmd.Name}";
                if (subCmd.SubGroups.Any())
                {
                    field.Value += "*";
                }
                field.Value += "`";
            }
            if (!string.IsNullOrEmpty(field.Name))
            {
                output.AddField(field);
            }

            if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands
            {
                output = EmbedBuilder().WithDescription("There are no commands you have permission to run.");
            }

            return((replyChannel ?? channel).SendMessageAsync(string.Empty, embed: output.Build()));
        }
コード例 #12
0
ファイル: CommandMap.cs プロジェクト: Kusoneko/Nekobot
        private void AddCommand(int index, string[] parts, Command command, bool isAlias)
        {
            if (!command.IsHidden)
                _isVisible = true;

            if (index != parts.Length)
            {
                CommandMap nextGroup;
                string name = parts[index].ToLowerInvariant();
                string fullName = string.Join(" ", parts, 0, index + 1);
                if (!_items.TryGetValue(name, out nextGroup))
                {
                    nextGroup = new CommandMap(this, name, fullName);
                    _items.Add(name, nextGroup);
                    _hasSubGroups = true;
                }
                nextGroup.AddCommand(index + 1, parts, command, isAlias);
            }
            else
            {
                _commands.Add(command);
                if (!isAlias)
                    _hasNonAliases = true;
            }
        }
コード例 #13
0
        private Task ShowCommandHelp(CommandMap map, User user, Channel channel, Channel replyChannel = null)
        {
            StringBuilder output = new StringBuilder();

            IEnumerable <Command> cmds = map.Commands;
            bool   isFirstCmd          = true;
            string error;

            if (cmds.Any())
            {
                foreach (var cmd in cmds)
                {
                    if (!cmd.CanRun(user, channel, out error))
                    {
                    }
                    //output.AppendLine(error ?? DefaultPermissionError);
                    else
                    {
                        if (isFirstCmd)
                        {
                            isFirstCmd = false;
                        }
                        else
                        {
                            output.AppendLine();
                        }
                        ShowCommandHelpInternal(cmd, user, channel, output);
                    }
                }
            }
            else
            {
                output.Append('`');
                output.Append(map.FullName);
                output.Append("`\n");
            }

            bool isFirstSubCmd = true;

            foreach (var subCmd in map.SubGroups.Where(x => x.CanRun(user, channel, out error) && x.IsVisible))
            {
                if (isFirstSubCmd)
                {
                    isFirstSubCmd = false;
                    output.Append("**Sub Commands:** ");
                }
                else
                {
                    output.Append(", ");
                }
                output.Append('`');
                output.Append(subCmd.Name);
                if (subCmd.SubGroups.Any())
                {
                    output.Append("*");
                }
                output.Append('`');
            }

            if (isFirstCmd && isFirstSubCmd) //Had no commands and no subcommands
            {
                output.Clear();
                output.AppendLine("There are no commands you have permission to run.");
            }

            return((replyChannel ?? channel).SendMessage(output.ToString()));
        }
コード例 #14
0
ファイル: CommandMap.cs プロジェクト: Lirusaito/Nekobot
        private void AddCommand(int index, string[] parts, Command command)
        {
            if (!command.IsHidden && _isHidden)
                _isHidden = false;

            if (index != parts.Length)
            {
                CommandMap nextGroup;
                string name = parts[index].ToLowerInvariant();
                string fullName = string.Join(" ", parts, 0, index + 1);
                if (!_items.TryGetValue(name, out nextGroup))
                {
                    nextGroup = new CommandMap(this, name, fullName);
                    _items.Add(name, nextGroup);
                }
                nextGroup.AddCommand(index + 1, parts, command);
            }
            else
                _commands.Add(command);
        }