/// <summary> /// Return member description/summary /// </summary> public static string GetDescription(this MemberInfo member) { if (member == null) { return(string.Empty); } string s; if (cached_summary.TryGetValue(member, out s)) { return(s); } SummaryAttribute summary_flag = member.GetAttribute <SummaryAttribute>(false); if (summary_flag != null) { return(cached_summary[member] = summary_flag.summary); } if (member is Type) { return(cached_summary[member] = ((Type)member).GetTypeName(true)); } if (member is MethodInfo) { return(cached_summary[member] = ((MethodInfo)member).GetSignName(true)); } return(string.Empty); }
public static string[] BuildHelpLines(ulong guild) { List <string> arrayList = new List <string>(); Assembly asm = Assembly.GetExecutingAssembly(); // Get assembly var results = from type in asm.GetTypes() where typeof(ModuleBase).IsAssignableFrom(type) select type; // Grab all types that inherit ModuleBase string trigger = Program.Instance.triggerMap[guild]; foreach (Type t in results) // For each type in results { /* Grab MethodInfo of the type where the method has the attribute SummaryAttribute */ MethodInfo info = t.GetMethods().Where(x => x.GetCustomAttributes(typeof(SummaryAttribute), false).Length > 0).First(); /* Grab summary attribute */ SummaryAttribute summary = info.GetCustomAttribute(typeof(SummaryAttribute)) as SummaryAttribute; /* Grab command attribute */ CommandAttribute command = info.GetCustomAttribute(typeof(CommandAttribute)) as CommandAttribute; /* Both objects are non null, valid, so lets grab the attribute text */ if (summary != null && command != null) { if (!Program.IsCommandDisabled(command.Text)) { arrayList.Add(trigger + command.Text + " - " + summary.Text); } } } return(arrayList.ToArray()); // return string[] array }
public void SummaryWorksOnConstructors() { SummaryAttribute attribute = (SummaryAttribute)Helper.GetAttribute ( typeof(SampleClass).GetConstructors().Single(), AttributeConstants.SUMMARYATTRIBUTE ); Assert.Equal(TestConstants.SummaryText, attribute.Summary); }
public void SummaryWorksOnMethods() { SummaryAttribute attribute = (SummaryAttribute)Helper.GetAttribute ( typeof(SampleClass).GetMethod(TestConstants.MethodName), AttributeConstants.SUMMARYATTRIBUTE ); Assert.Equal(TestConstants.SummaryText, attribute.Summary); }
static void Main(string[] args) { var summaryAttributes = typeof(Foo).GetCustomAttributes(typeof(SummaryAttribute), false); if (summaryAttributes.Length != 0) { SummaryAttribute summary = (SummaryAttribute)summaryAttributes[0]; Console.WriteLine(summary.Value); } }
protected string BuildMethodDescription(MethodInfo methodInfo) { //get the method level attributes CommandAttribute cmnAttrib = methodInfo.GetCustomAttribute(typeof(CommandAttribute)) as CommandAttribute; SummaryAttribute sumAttrib = methodInfo.GetCustomAttribute(typeof(SummaryAttribute)) as SummaryAttribute; //get method template string cmdDescTmpl = _botStrings.getString("common", "method_description"); return(String.Format(cmdDescTmpl, cmnAttrib.Text, sumAttrib.Text)); }
protected string BuildParameterDescription(System.Reflection.ParameterInfo parameterInfo) { //get parameter name and attribute string paramName = parameterInfo.Name; SummaryAttribute summAttrib = parameterInfo.GetCustomAttribute(typeof(SummaryAttribute)) as SummaryAttribute; //get parameter template string paramTmpl = _botStrings.getString("common", "parameter_description"); return(String.Format(paramTmpl, paramName, summAttrib.Text)); }
private Command HandleCommandGroup(Type type) { CommandGroupAttribute commandGroupAttribute = type.GetCustomAttribute <CommandGroupAttribute>(false); AliasAttribute aliasAttribute = type.GetCustomAttribute <AliasAttribute>(false); SummaryAttribute summaryAttribute = type.GetCustomAttribute <SummaryAttribute>(false); // Get all methods that have a CommandAttribute IEnumerable <MethodInfo> methods = type.GetMethods() .Where(m => m.IsDefined(typeof(CommandAttribute), false)); // Get all nested classes with a CommandGroupAttribute IEnumerable <Type> subTypes = type.GetNestedTypes() .Where(t => t.IsDefined(typeof(CommandGroupAttribute), false)); // Default command is when a method in a group has a no name meaning its the default method to call List <Method> defaultCommandMethods = new List <Method>(); List <Command> subCommands = new List <Command>(); foreach (MethodInfo method in methods) { Command subCommand = HandleCommand(method); // Check if its the default command if (subCommand.Name == null) { // Should always be 1 method defaultCommandMethods.Add(subCommand.Methods[0]); } else { Command sameCommand = subCommands.FirstOrDefault(c => c.Name == subCommand.Name); if (sameCommand == null) { subCommands.Add(subCommand); } else { sameCommand.Methods.Add(subCommand.Methods[0]); sameCommand.Summary = sameCommand.Summary ?? subCommand.Summary; } } } foreach (Type subType in subTypes) { subCommands.Add(HandleCommandGroup(subType)); } return(new Command( name: commandGroupAttribute.Name, aliases: aliasAttribute?.Aliases, summary: summaryAttribute?.Summary, subCommands: subCommands, methods: defaultCommandMethods)); }
protected string BuildCommandDescription(TypeInfo typeInfo) { //get the class level attributes GroupAttribute grpAttrib = typeInfo.GetCustomAttribute(typeof(GroupAttribute)) as GroupAttribute; SummaryAttribute sumAttrib = typeInfo.GetCustomAttribute(typeof(SummaryAttribute)) as SummaryAttribute; //get description template string cmdDescTmpl = _botStrings.getString("common", "command_description"); //build string with tempate return(String.Format(cmdDescTmpl, grpAttrib.Prefix, sumAttrib.Text)); }
public async Task Help() { EmbedBuilder eb = new EmbedBuilder(); MethodInfo[] mi = GetType().GetMethods(); for (int o = 0; o < mi.Length - 5; o++) { CommandAttribute myAttribute1 = mi[o].GetCustomAttributes(true).OfType <CommandAttribute>().FirstOrDefault(); SummaryAttribute myAttribute2 = mi[o].GetCustomAttributes(true).OfType <SummaryAttribute>().FirstOrDefault(); eb.AddField(myAttribute1.Text, myAttribute2.Text); } await ReplyAsync("", false, eb); }
private Command HandleCommand(MethodInfo methodInfo) { if (!methodInfo.IsStatic) { throw new MethodNotStaticException(methodInfo); } if (methodInfo.ContainsGenericParameters) { throw new GenericParametersException(methodInfo); } CommandAttribute commandAttribute = methodInfo.GetCustomAttribute <CommandAttribute>(false); AliasAttribute aliasAttribute = methodInfo.GetCustomAttribute <AliasAttribute>(false); SummaryAttribute summaryAttribute = methodInfo.GetCustomAttribute <SummaryAttribute>(false); return(new Command( name: commandAttribute.Name, aliases: aliasAttribute?.Aliases, summary: summaryAttribute?.Summary, subCommands: new List <Command>(), methodInfo: methodInfo)); }
public async Task Commandlist(params string[] parameters) { string dm = "\nList of Commands\n" + "━━━━━━━━━━━━━━━━━━━━━━" + "\n\n"; Type type = typeof(Tools); foreach (var method in type.GetMethods()) { var attrs = System.Attribute.GetCustomAttributes(method); foreach (var attrib in attrs) { if (attrib is CommandAttribute) { CommandAttribute a = (CommandAttribute)attrib; dm += "!!" + a.Text + "\n"; } if (attrib is AliasAttribute) { AliasAttribute a = (AliasAttribute)attrib; dm += "aliases: "; foreach (var alias in a.Aliases) { dm += "!!" + alias + ", "; } dm = dm.Remove(dm.Count() - 2); dm += "\n"; } if (attrib is SummaryAttribute) { SummaryAttribute a = (SummaryAttribute)attrib; dm += a.Text + "\n"; } if (attrib is RequireOwnerAttribute) { RequireOwnerAttribute a = (RequireOwnerAttribute)attrib; dm += "Permitted users - Bot owner" + "\n"; } if (attrib is RequireUserPermissionAttribute) { RequireUserPermissionAttribute a = (RequireUserPermissionAttribute)attrib; dm += "Permitted users - "; if (a.GuildPermission.Value == GuildPermission.Administrator) { dm += "Server Admins"; } dm += "\n"; } if (attrib is RequireRoleAttribute) { RequireRoleAttribute a = (RequireRoleAttribute)attrib; dm += "Permitted users - " + a.name + "\n"; } } dm += "\n"; } dm += "\n\n"; await DmSplit(dm); await CommandStatus("DM Sent.", "Command List"); }
public Task HelpAsync() { //List<CommandInfo> commandInfos = new List<CommandInfo>(); //foreach (ModuleInfo item in CommandHandler._commands.Modules) //{ // commandInfos.AddRange(item.GetExecutableCommandsAsync(Context, null).GetAwaiter().GetResult()); //} //List<CommandDescription> Commands = new List<CommandDescription>(); //foreach (CommandInfo info in commandInfos) //{ // info.Attributes //} MemberInfo[] Methodinfos = typeof(BasicModule).GetMethods(); Methodinfos = Methodinfos.Concat(typeof(BotServerManagmentModule).GetMethods()).ToArray(); Methodinfos = Methodinfos.Concat(typeof(ListSortModule).GetMethods()).ToArray(); Methodinfos = Methodinfos.Concat(typeof(MiscCommandsModlue).GetMethods()).ToArray(); Methodinfos = Methodinfos.Where(x => ((MethodBase)x).IsPublic).ToArray(); List <CommandDescription> Commands = new List <CommandDescription>(); foreach (MethodInfo info in Methodinfos) { GroupAttribute GroupName = (GroupAttribute)info.DeclaringType.GetCustomAttribute(typeof(GroupAttribute)); CommandAttribute CommandName = (CommandAttribute)info.GetCustomAttribute(typeof(CommandAttribute)); SummaryAttribute CommandSummary = (SummaryAttribute)info.GetCustomAttribute(typeof(SummaryAttribute)); if (!(CommandName is null) && !(CommandSummary is null)) { Commands.Add(new CommandDescription { Groupname = GroupName?.Prefix ?? string.Empty, CommandName = CommandName.Text, CommandSummary = CommandSummary.Text }); } } EmbedBuilder builder = new EmbedBuilder() { Author = new EmbedAuthorBuilder() { Name = "AutorName", Url = "https://www.emoji.co.uk/files/twitter-emojis/symbols-twitter/11121-negative-squared-latin-capital-letter-a.png" }, Color = Color.Purple, ImageUrl = Properties.AshSettings.Default.ProfilePicture, Title = "Hilfe", Fields = new List <EmbedFieldBuilder>() }; foreach (CommandDescription description in Commands) { builder.AddField($"{description.Groupname} {description.CommandName}", description.CommandSummary, false); } return(ReplyAsync(embed: builder.Build())); }