private void BuildModule(ModuleBuilder builder, TypeInfo moduleType, MetadataPath currentPath) { var attributes = moduleType.GetCustomAttributes() .ToImmutableArray(); var preconditions = attributes.Where(a => a is PreconditionAttribute); foreach (var precondition in preconditions) { builder.AddPrecondition(precondition as PreconditionAttribute); } builder.AddAttributes(attributes.Where(a => !(a is PreconditionAttribute)).ToArray()); builder.WithName(this._metadataProvider.GetModuleValue(m => m.Name)) .WithSummary(this._metadataProvider.GetModuleValue(m => m.Summary)) .WithRemarks(this._metadataProvider.GetModuleValue(m => m.Remarks)) .AddAliases(this._metadataProvider.GetModuleValue(m => m.Aliases) ?? new String[0]); if (builder.Name.IsEmpty()) { builder.Name = moduleType.Name; } var commands = moduleType.DeclaredMethods.Where(IsValidCommandDefinition); foreach (var command in commands) { currentPath.CurrentCommand = command.Name; currentPath.CurrentArgument = null; this._metadataProvider.SetCurrentPath(currentPath); this.BuildCommand(builder, moduleType, command, currentPath); } }
private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service) { var attributes = typeInfo.GetCustomAttributes(); foreach (var attribute in attributes) { switch (attribute) { case NameAttribute name: builder.Name = name.Text; break; case SummaryAttribute summary: builder.Summary = summary.Text; break; case RemarksAttribute remarks: builder.Remarks = remarks.Text; break; case AliasAttribute alias: builder.AddAliases(alias.Aliases); break; case GroupAttribute group: builder.Name = builder.Name ?? group.Prefix; builder.AddAliases(group.Prefix); break; case PreconditionAttribute precondition: builder.AddPrecondition(precondition); break; default: builder.AddAttributes(attribute); break; } } //Check for unspecified info if (builder.Aliases.Count == 0) { builder.AddAliases(""); } if (builder.Name == null) { builder.Name = typeInfo.Name; } var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x)); foreach (var method in validCommands) { builder.AddCommand((command) => { BuildCommand(command, typeInfo, method, service); }); } }
private static void CreateCommand <TData>( this ModuleBuilder module, string message, string group, string key, PropertyInfo property, Action <PropertyInfo, TData, object[]> propertyFunc) where TData : class, IGroupSetting, new() { module.AddCommand(key, (ctx, args, service, command) => { GetData(ctx, args, service, property, propertyFunc); return(ctx.Channel.SendMessageAsync(message)); }, c => c.WithSettings(property.GetRealType(), group, key)); module.AddAttributes(new HiddenAttribute()); }
private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service, IServiceProvider services) { var attributes = typeInfo.GetCustomAttributes(); builder.TypeInfo = typeInfo; foreach (var attribute in attributes) { switch (attribute) { case NameAttribute name: builder.Name = name.Text; break; case SummaryAttribute summary: builder.Summary = summary.Text; break; case RemarksAttribute remarks: builder.Remarks = remarks.Text; break; case AliasAttribute alias: builder.AddAliases(alias.Aliases); break; case GroupAttribute group: builder.Name = builder.Name ?? group.Prefix; builder.Group = group.Prefix; builder.AddAliases(group.Prefix); break; case PreconditionAttribute precondition: builder.AddPrecondition(precondition); break; default: builder.AddAttributes(attribute); break; } } //Check for unspecified info if (builder.Aliases.Count == 0) { builder.AddAliases(""); } if (builder.Name == null) { builder.Name = typeInfo.Name; } // Get all methods (including from inherited members), that are valid commands var validCommands = typeInfo.GetMethods().Where(IsValidCommandDefinition); foreach (var method in validCommands) { builder.AddCommand((command) => { BuildCommand(command, typeInfo, method, service, services); }); } }
private void BuildCommand(ModuleBuilder builder, TypeInfo moduleType, MethodInfo method, MetadataPath currentPath) { var name = this._metadataProvider.GetCommandValue(c => c.Name); var command = this._metadataProvider.GetCommandValue(c => c.Command); var isDefault = command == null && !builder.Aliases[0].IsEmpty(); // This command is not configured properly if (!isDefault && command.IsEmpty()) { return; } async Task <IResult> ExecuteCommand(ICommandContext context, Object[] args, IServiceProvider services, CommandInfo cmd) { var instance = (IModule)services.GetRequiredService(moduleType); instance.SetContext(context as KuuhakuCommandContext); try { instance.BeforeExecute(cmd); var task = method.Invoke(instance, args) as Task ?? Task.CompletedTask; if (task is Task <RuntimeResult> resultTask) { return(await resultTask); } await task; return(ExecuteResult.FromSuccess()); } finally { instance.AfterExecute(cmd); (instance as IDisposable)?.Dispose(); if (instance is IAsyncDisposable disposable) { await disposable.DisposeAsync(); } } } void CreateCommand(CommandBuilder builder) { var attributes = method.GetCustomAttributes() .ToImmutableArray(); var preconditions = attributes.Where(a => a is PreconditionAttribute); foreach (var precondition in preconditions) { builder.AddPrecondition(precondition as PreconditionAttribute); } builder.AddAttributes(attributes.Where(a => !(a is PreconditionAttribute)).ToArray()); // TODO: Permissions // TODO: Ratelimiting // TODO: Generic Precondition Values? builder.WithPriority(this._metadataProvider.GetCommandValue(c => c.Priority)) .WithSummary(this._metadataProvider.GetCommandValue(c => c.Summary)) .WithRemarks(this._metadataProvider.GetCommandValue(c => c.Remarks)) .AddAliases(this._metadataProvider.GetCommandValue(c => c.Aliases) ?? new String[0]); if (builder.Name.IsEmpty()) { builder.Name = method.Name.Replace("Async", ""); } var parameters = method.GetParameters(); for (var i = 0; i < parameters.Length; i++) { currentPath.CurrentArgument = i; this._metadataProvider.SetCurrentPath(currentPath); this.BuildArgument(builder, parameters[i], (current: i, total: parameters.Length)); } } var primaryAlias = isDefault ? "" : command.IsEmpty() ? name : command; builder.AddCommand(primaryAlias, ExecuteCommand, CreateCommand); }