Search in sources :

Example 1 with GlobalUserEvent

use of com.freya02.botcommands.api.application.context.user.GlobalUserEvent in project BotCommands by freya022.

the class ApplicationCommandsBuilder method processUserCommand.

private void processUserCommand(ApplicationCommand applicationCommand, Method method) {
    if (method.getAnnotation(JDAUserCommand.class).guildOnly()) {
        if (!ReflectionUtils.hasFirstParameter(method, GlobalUserEvent.class) && !ReflectionUtils.hasFirstParameter(method, GuildUserEvent.class))
            throw new IllegalArgumentException("User command at " + Utils.formatMethodShort(method) + " must have a GuildUserEvent or GlobalUserEvent as first parameter");
        if (!ReflectionUtils.hasFirstParameter(method, GuildUserEvent.class)) {
            // If type is correct but guild specialization isn't used
            LOGGER.warn("Guild-only user command {} uses GlobalUserEvent, consider using GuildUserEvent to remove warnings related to guild stuff's nullability", Utils.formatMethodShort(method));
        }
    } else {
        if (!ReflectionUtils.hasFirstParameter(method, GlobalUserEvent.class))
            throw new IllegalArgumentException("User command at " + Utils.formatMethodShort(method) + " must have a GlobalUserEvent as first parameter");
    }
    final UserCommandInfo info = new UserCommandInfo(context, applicationCommand, method);
    LOGGER.debug("Adding user command {} for method {}", info.getPath().getName(), Utils.formatMethodShort(method));
    context.addUserCommand(info);
}
Also used : GuildUserEvent(com.freya02.botcommands.api.application.context.user.GuildUserEvent) UserCommandInfo(com.freya02.botcommands.internal.application.context.user.UserCommandInfo) GlobalUserEvent(com.freya02.botcommands.api.application.context.user.GlobalUserEvent) JDAUserCommand(com.freya02.botcommands.api.application.context.annotations.JDAUserCommand)

Example 2 with GlobalUserEvent

use of com.freya02.botcommands.api.application.context.user.GlobalUserEvent in project BotCommands by freya022.

the class UserCommandInfo method execute.

public boolean execute(BContext context, UserContextInteractionEvent event, Consumer<Throwable> throwableConsumer) throws Exception {
    final Object[] objects = new Object[commandParameters.size() + 1];
    if (guildOnly) {
        objects[0] = new GuildUserEvent(context, event);
    } else {
        objects[0] = new GlobalUserEvent(context, event);
    }
    for (int i = 0, commandParametersLength = commandParameters.size(); i < commandParametersLength; i++) {
        ContextCommandParameter<UserContextParameterResolver> parameter = commandParameters.get(i);
        if (parameter.isOption()) {
            objects[i + 1] = parameter.getResolver().resolve(context, this, event);
        // no need to check for unresolved parameters,
        // it is impossible to have other arg types other than User (and custom resolvers)
        } else {
            objects[i + 1] = parameter.getCustomResolver().resolve(context, this, event);
        }
    }
    applyCooldown(event);
    getMethodRunner().invoke(objects, throwableConsumer);
    return true;
}
Also used : GuildUserEvent(com.freya02.botcommands.api.application.context.user.GuildUserEvent) UserContextParameterResolver(com.freya02.botcommands.api.parameters.UserContextParameterResolver) GlobalUserEvent(com.freya02.botcommands.api.application.context.user.GlobalUserEvent)

Aggregations

GlobalUserEvent (com.freya02.botcommands.api.application.context.user.GlobalUserEvent)2 GuildUserEvent (com.freya02.botcommands.api.application.context.user.GuildUserEvent)2 JDAUserCommand (com.freya02.botcommands.api.application.context.annotations.JDAUserCommand)1 UserContextParameterResolver (com.freya02.botcommands.api.parameters.UserContextParameterResolver)1 UserCommandInfo (com.freya02.botcommands.internal.application.context.user.UserCommandInfo)1