Search in sources :

Example 1 with GuildUserEvent

use of com.freya02.botcommands.api.application.context.user.GuildUserEvent 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 GuildUserEvent

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

the class ContextAvatar method execute.

@JDAUserCommand(name = "Get avatar")
public void execute(GuildUserEvent event) {
    final User targetUser = event.getTarget();
    event.reply(targetUser.getEffectiveAvatarUrl()).queue();
}
Also used : User(net.dv8tion.jda.api.entities.User) JDAUserCommand(com.freya02.botcommands.api.application.context.annotations.JDAUserCommand)

Example 3 with GuildUserEvent

use of com.freya02.botcommands.api.application.context.user.GuildUserEvent 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

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