public async Task RemoveCommandAsync(Command command) { var builder = new ModuleBuilder(); var oldModule = command.Module; builder.AddAliases(oldModule.Aliases?.ToArray()) .AddAttributes(oldModule.Attributes?.ToArray()) .AddChecks(oldModule.Checks?.ToArray()) .WithDescription(oldModule.Description) .WithIgnoreExtraArguments(oldModule.IgnoreExtraArguments) .WithName(oldModule.Name) .WithRemarks(oldModule.Remarks) .WithRunMode(oldModule.RunMode); foreach (var cmd in oldModule.Commands) { if (cmd.FullAliases[0] == command.FullAliases[0]) { continue; } var cmdBuilder = new CommandBuilder() .AddAliases(cmd.Aliases?.ToArray()) .AddAttributes(cmd.Attributes?.ToArray()) .AddChecks(cmd.Checks?.ToArray()) .AddCooldowns(cmd.Cooldowns?.ToArray()) .WithCallback(cmd.Callback) .WithDescription(cmd.Description) .WithIgnoreExtraArguments(cmd.IgnoreExtraArguments) .WithName(cmd.Name) .WithPriority(cmd.Priority) .WithRemarks(cmd.Remarks) .WithRunMode(cmd.RunMode); foreach (var param in cmd.Parameters) { var paramBuilder = new ParameterBuilder() .AddAttributes(param.Attributes?.ToArray()) .AddChecks(param.Checks?.ToArray()) .WithCustomTypeParserType(param.CustomTypeParserType) .WithDefaultValue(param.DefaultValue) .WithDescription(param.Description) .WithIsMultiple(param.IsMultiple) .WithIsOptional(param.IsOptional) .WithIsRemainder(param.IsRemainder) .WithName(param.Name) .WithRemarks(param.Remarks) .WithType(param.Type); cmdBuilder.AddParameter(paramBuilder); } builder.AddCommand(cmdBuilder); } await _commands.RemoveModuleAsync(oldModule); await _commands.AddModuleAsync(builder); }
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 BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service) { //gotta do this until i have the time to move everything from static fields/constructors over to // a service. This will ensure static constructors are ran >.< var instance = ReflectionUtils.CreateBuilder <IModuleBase>(typeInfo, service).Invoke(DependencyMap.Empty); var attributes = typeInfo.GetCustomAttributes(); foreach (var attribute in attributes) { // TODO: C#7 type switch if (attribute is NameAttribute) { builder.Prefix = (attribute as NameAttribute).Text; } else if (attribute is SummaryAttribute) { builder.Summary = (attribute as SummaryAttribute).Text; } else if (attribute is RemarksAttribute) { builder.Remarks = (attribute as RemarksAttribute).Text; } else if (attribute is AliasAttribute) { builder.AddAliases((attribute as AliasAttribute).Aliases); } else if (attribute is GroupAttribute) { var groupAttr = attribute as GroupAttribute; builder.Name = builder.Name ?? groupAttr.Name; builder.AddAliases(groupAttr.Prefix); } else if (attribute is PreconditionAttribute) { builder.AddPrecondition(attribute as PreconditionAttribute); } } //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 void CreateMemeCommand(string trigger, MemeTemplate template, ModuleBuilder module) { module.AddCommand(trigger, CommandCallbackAsync, command => { command .WithName(template.Name) .WithSummary(template.Description); AddParameters(template, command); }); }
private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service) { var attributes = typeInfo.GetCustomAttributes(); foreach (var attribute in attributes) { // TODO: C#7 type switch if (attribute is NameAttribute) { builder.Name = (attribute as NameAttribute).Text; } else if (attribute is SummaryAttribute) { builder.Summary = (attribute as SummaryAttribute).Text; } else if (attribute is RemarksAttribute) { builder.Remarks = (attribute as RemarksAttribute).Text; } else if (attribute is AliasAttribute) { builder.AddAliases((attribute as AliasAttribute).Aliases); } else if (attribute is GroupAttribute) { var groupAttr = attribute as GroupAttribute; builder.Name = builder.Name ?? groupAttr.Prefix; builder.AddAliases(groupAttr.Prefix); } else if (attribute is PreconditionAttribute) { builder.AddPrecondition(attribute as PreconditionAttribute); } } //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 void CreateMemeDebugCommand(string trigger, MemeTemplate template, ModuleBuilder module) { module.AddCommand(trigger, DebugCommandCallbackAsync, command => { command .WithName($"{template.Name} (Debug)") .WithSummary(template.Description) .AddPrecondition(new RequireOwnerAttribute()); AddParameters(template, command); }); }
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()); }
protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder) { // We create a catch-all command that intercepts the first argument, tries to parse it as // the context parameter, then runs the command service AGAIN with that given in a wrapped // context, with the context argument removed so it delegates to the subcommand executor builder.AddCommand("", async(ctx, param, services, info) => { var pkCtx = ctx as PKCommandContext; pkCtx.SetContextEntity(param[0] as T); await commandService.ExecuteAsync(pkCtx, Prefix + " " + param[1] as string, services); }, (cb) => { cb.WithPriority(-9999); cb.AddPrecondition(new MustNotHaveContextPrecondition()); cb.AddParameter <T>("contextValue", (pb) => pb.WithDefault("")); cb.AddParameter <string>("rest", (pb) => pb.WithDefault("").WithIsRemainder(true)); }); }
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); }); } }
protected override void OnModuleBuilding(CommandService commandService, ModuleBuilder builder) { base.OnModuleBuilding(commandService, builder); using var scope = ServiceProvider.CreateScope(); ActionService = ServiceProvider.GetRequiredService <ActionService>(); //Get phrases from items as aliases for the referenced command. foreach (var action in ActionService.GetAll().OfType <BotCommandAction>() .Where(s => s.TextCommandProperties != null)) { foreach (var textProperties in action.TextCommandProperties) { builder.AddCommand(textProperties.Name, RunActionFromTextCommand, builder => { if (textProperties.Aliases != null) { builder.AddAliases(textProperties.Aliases.ToArray()); } builder.Summary = textProperties.Summary; if (action.GuildsOnly) { builder.AddPrecondition(new RequireContextAttribute(ContextType.Guild)); } if (action.RequiredPermissions.HasValue) { var requiredPermissions = action.RequiredPermissions.Value.ToList(); if (!requiredPermissions.Any()) { builder.AddPrecondition(new RequireOwnerAttribute()); } else { GuildPermission permissions = 0; foreach (var permission in requiredPermissions) { permissions |= permission; } builder.AddPrecondition(new RequireUserPermissionAttribute(permissions)); } } if (textProperties.Priority.HasValue) { builder.Priority = textProperties.Priority.Value; } var parameters = action.GetParameters <ActionParameterTextAttribute>() ?.Where(p => p.Attribute.FilterCommandNames == null || p.Attribute.FilterCommandNames.Contains(textProperties.Name)) .OrderBy(p => p.Attribute.Order); if (parameters != null) { foreach (var p in parameters) { builder.AddParameter(p.Attribute.Name, p.Attribute.ParameterType, pb => { pb.Summary = p.Attribute.Description; pb.IsMultiple = p.Attribute.IsMultiple; pb.IsRemainder = p.Attribute.IsRemainder; pb.DefaultValue = p.Attribute.DefaultValue; pb.IsOptional = !p.Attribute.Required; }); } } textProperties.ModifyBuilder?.Invoke(scope.ServiceProvider, builder); } ); } } }
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); }