Exemplo n.º 1
0
        public static EmbedBuilder GetCommandCollectionEmbed(IDMCommandContext context, CommandCollection collection)
        {
            string contextType = "";

            if (GuildCommandContext.TryConvert(context, out IGuildCommandContext guildContext))
            {
                contextType = "Guild";
            }
            else
            {
                contextType = "PM";
            }

            string embedTitle = $"Command Collection \"{collection.Name}\"";
            string embedDesc  = "This list only shows commands where all preconditions have been met!";

            List <EmbedFieldBuilder> helpFields = new List <EmbedFieldBuilder>();

            foreach (Command command in collection.Commands)
            {
                if (command.CanView(context, guildContext, context.UserInfo.IsBotAdmin, out _))
                {
                    helpFields.Add(Macros.EmbedField(command.Syntax, command.Summary, true));
                }
            }

            if (helpFields.Count == 0)
            {
                embedDesc = "No command's precondition has been met!";
                return(new EmbedBuilder()
                {
                    Title = embedTitle, Description = embedDesc, Color = BotCore.ErrorColor, Footer = new EmbedFooterBuilder()
                    {
                        Text = "Context: " + contextType
                    }
                });
            }
            else
            {
                return(new EmbedBuilder()
                {
                    Title = embedTitle, Description = embedDesc, Color = BotCore.EmbedColor, Footer = new EmbedFooterBuilder()
                    {
                        Text = "Context: " + contextType
                    }, Fields = helpFields
                });
            }
        }
        private static bool tryGetCommandCollectionEmbedField(IDMCommandContext context, IGuildCommandContext guildContext, CommandCollection collection, out EmbedFieldBuilder embedField)
        {
            bool collectionAllowed = true;

            if (context.IsGuildContext)
            {
                collectionAllowed = guildContext.ChannelMeta.allowedCommandCollections.Count == 0 || guildContext.ChannelMeta.allowedCommandCollections.Contains(collection.Name);
            }
            int availableCommands = collection.ViewableCommands(context, guildContext);

            if (availableCommands > 0 && collectionAllowed)
            {
                embedField = Macros.EmbedField($"Collection \"{collection.Name}\"", $"{availableCommands} commands.{(string.IsNullOrEmpty(collection.Description) ? string.Empty : $" {collection.Description}.")} Use `{MessageHandler.CommandParser.CommandSyntax("man", collection.Name)}` to see a summary of commands in this command family!", true);
                return(true);
            }
Exemplo n.º 3
0
 public static bool TryGetCommandCollection(string name, out CommandCollection collection)
 {
     return(collectionDict.TryGetValue(name, out collection));
 }
Exemplo n.º 4
0
        public virtual ICommandContext ParseCommand(IMessageContext dmContext)
        {
            string message = dmContext.Content.Substring(Prefix.Length).Trim();
            string commandIdentifier;
            string argumentSection;
            IndexArray <string> arguments;
            Command             interpretedCommand;
            CommandSearchResult commandSearch;

            int argStartPointer = message.IndexOf(':', Prefix.Length);

            if (argStartPointer == -1 || argStartPointer == message.Length - 1)
            {
                argumentSection   = string.Empty;
                arguments         = new string[0];
                commandIdentifier = message;
            }
            else
            {
                argumentSection   = message.Substring(argStartPointer + 1);
                commandIdentifier = message.Substring(0, argStartPointer);
                int argcnt = 1;
                for (int i = 0; i < argumentSection.Length; i++)
                {
                    bool isUnescapedComma = argumentSection[i] == ',';
                    if (i > 0 && isUnescapedComma)
                    {
                        isUnescapedComma = argumentSection[i - 1] != '\\';
                    }
                    if (isUnescapedComma)
                    {
                        argcnt++;
                    }
                }
                arguments = new string[argcnt];
                int argindex  = 0;
                int lastindex = 0;
                for (int i = 0; i < argumentSection.Length; i++)
                {
                    bool isUnescapedComma = argumentSection[i] == ',';
                    if (i > 0 && isUnescapedComma)
                    {
                        isUnescapedComma = argumentSection[i - 1] != '\\';
                    }
                    if (isUnescapedComma)
                    {
                        if (lastindex < i)
                        {
                            arguments[argindex] = argumentSection.Substring(lastindex, i - lastindex).Trim().Replace("\\,", ",");
                        }
                        else
                        {
                            arguments[argindex] = string.Empty;
                        }
                        argindex++;
                        lastindex = i + 1;
                    }
                }
                if (lastindex <= argumentSection.Length - 1)
                {
                    arguments[argindex] = argumentSection.Substring(lastindex, argumentSection.Length - lastindex).Trim().Replace("\\,", ",");
                }
                else
                {
                    arguments[argindex] = string.Empty;
                }
            }

            commandSearch = CommandCollection.TryFindCommand(commandIdentifier, arguments.TotalCount, out interpretedCommand);

            return(new CommandContext(interpretedCommand, commandSearch, argumentSection, arguments));
        }
Exemplo n.º 5
0
 internal static void AddCollection(CommandCollection collection)
 {
     collectionDict.TryAdd(collection.Name, collection);
 }