Search in sources :

Example 1 with GlobalMessageEvent

use of com.freya02.botcommands.api.application.context.message.GlobalMessageEvent in project BotCommands by freya022.

the class ApplicationCommandsBuilder method processMessageCommand.

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

Example 2 with GlobalMessageEvent

use of com.freya02.botcommands.api.application.context.message.GlobalMessageEvent in project BotCommands by freya022.

the class MessageCommandInfo method execute.

public boolean execute(BContext context, MessageContextInteractionEvent event, Consumer<Throwable> throwableConsumer) throws Exception {
    final Object[] objects = new Object[commandParameters.size() + 1];
    if (guildOnly) {
        objects[0] = new GuildMessageEvent(context, event);
    } else {
        objects[0] = new GlobalMessageEvent(context, event);
    }
    for (int i = 0, commandParametersLength = commandParameters.size(); i < commandParametersLength; i++) {
        ContextCommandParameter<MessageContextParameterResolver> 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 Message (and custom resolvers)
        } else {
            objects[i + 1] = parameter.getCustomResolver().resolve(context, this, event);
        }
    }
    applyCooldown(event);
    getMethodRunner().invoke(objects, throwableConsumer);
    return true;
}
Also used : MessageContextParameterResolver(com.freya02.botcommands.api.parameters.MessageContextParameterResolver) GlobalMessageEvent(com.freya02.botcommands.api.application.context.message.GlobalMessageEvent) GuildMessageEvent(com.freya02.botcommands.api.application.context.message.GuildMessageEvent)

Aggregations

GlobalMessageEvent (com.freya02.botcommands.api.application.context.message.GlobalMessageEvent)2 GuildMessageEvent (com.freya02.botcommands.api.application.context.message.GuildMessageEvent)2 JDAMessageCommand (com.freya02.botcommands.api.application.context.annotations.JDAMessageCommand)1 MessageContextParameterResolver (com.freya02.botcommands.api.parameters.MessageContextParameterResolver)1 MessageCommandInfo (com.freya02.botcommands.internal.application.context.message.MessageCommandInfo)1