示例#1
0
        private string ShowDetail()
        {
            Custom custom;

            Bot.PluginBase.Plugin plugin;
            if (PluginManager.CommandMapStatic.Keys.Contains(CommandName))
            {
                plugin = PluginManager.CommandMapStatic[CommandName];
                custom = new Custom
                {
                    Title   = plugin.Name,
                    Helps   = plugin.Helps,
                    Author  = plugin.Author,
                    Version = plugin.Version,
                    State   = plugin.State,
                    Arg     = new Dictionary <string, string>(),
                    FreeArg = new Dictionary <string, string>()
                };
            }
            else if (PluginManager.ApplicationList.Select(k => k.Name).Contains(CommandName))
            {
                plugin = PluginManager.ApplicationList.First(k => k.Name == CommandName);
                custom = new Custom
                {
                    Title   = plugin.Name,
                    Helps   = plugin.Helps,
                    Author  = plugin.Author,
                    Version = plugin.Version,
                    State   = plugin.State,
                    Arg     = new Dictionary <string, string>(),
                    FreeArg = new Dictionary <string, string>()
                };
            }
            else
            {
                return("未找到相关资源...");
            }

            var sbArg  = new StringBuilder();
            var sbFree = new StringBuilder();

            Type t     = plugin.GetType();
            var  props = t.GetProperties();

            foreach (var prop in props)
            {
                var info = prop.GetCustomAttributes(false);
                if (info.Length == 0)
                {
                    continue;
                }
                string helpStr = "尚无帮助信息。", argStr = null, freeStr = null, argName = null;
                bool   isSwitch = false;
                foreach (var o in info)
                {
                    switch (o)
                    {
                    case ArgAttribute argAttrib:
                        argStr   = $"-{argAttrib.Name}";
                        argName  = prop.Name;
                        isSwitch = argAttrib.IsSwitch;
                        break;

                    case FreeArgAttribute _:
                        freeStr = prop.Name;
                        break;

                    case HelpAttribute helpAttrib:
                        helpStr = string.Join(" ", helpAttrib.Helps);
                        break;
                    }
                }

                if (argStr != null)
                {
                    sbArg.Append($" [{argStr}{(isSwitch ? "" : " " + StringConvert.ToUnderLineStyle(argName))}]");

                    if (!custom.Arg.ContainsKey(argStr))
                    {
                        custom.Arg.Add(argStr, helpStr);
                    }
                    else
                    {
                        int i = 0;
                        while (custom.Arg.ContainsKey(argStr + " (" + i + ")"))
                        {
                            i++;
                        }
                        custom.Arg.Add(argStr + " (" + i + ")", helpStr);
                    }
                }

                if (freeStr != null)
                {
                    sbFree.Append($" [{StringConvert.ToUnderLineStyle(freeStr)}]");
                    custom.FreeArg.Add(StringConvert.ToUnderLineStyle(freeStr), helpStr);
                }
            }

            custom.Usage = plugin is ApplicationPlugin ? "自动触发。" : $"/{CommandName}{sbArg}{sbFree}";
            return(new FileImage(DrawDetail(custom)).ToString());
        }