Add a PircBotX implementation of cloud

This commit is contained in:
Alexander Söderberg 2020-10-22 10:51:33 +02:00 committed by Alexander Söderberg
parent c74ac64e5f
commit 979d1079c6
9 changed files with 484 additions and 3 deletions

View file

@ -0,0 +1,99 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.pircbotx;
import cloud.commandframework.exceptions.ArgumentParseException;
import cloud.commandframework.exceptions.InvalidCommandSenderException;
import cloud.commandframework.exceptions.InvalidSyntaxException;
import cloud.commandframework.exceptions.NoPermissionException;
import cloud.commandframework.exceptions.NoSuchCommandException;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.types.GenericMessageEvent;
final class CloudListenerAdapter<C> extends ListenerAdapter {
private static final String MESSAGE_INVALID_SYNTAX = "Invalid Command Syntax. Correct command syntax is: ";
private static final String MESSAGE_NO_PERMS = "I'm sorry, but you do not have permission to perform this command. "
+ "Please contact the server administrators if you believe that this is in error.";
private static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
private final PircBotXCommandManager<C> manager;
CloudListenerAdapter(final @NonNull PircBotXCommandManager<C> manager) {
this.manager = manager;
}
@Override
public void onGenericMessage(final @NonNull GenericMessageEvent event) {
final String message = event.getMessage();
if (!message.startsWith(this.manager.getCommandPrefix())) {
return;
}
final C sender = this.manager.getUserMapper().apply(event.getUser());
manager.executeCommand(sender, message.substring(this.manager.getCommandPrefix().length()))
.whenComplete((commandResult, throwable) -> {
if (throwable == null) {
return;
}
if (throwable instanceof InvalidSyntaxException) {
this.manager.handleException(sender,
InvalidSyntaxException.class,
(InvalidSyntaxException) throwable, (c, e) -> event.respondWith(
MESSAGE_INVALID_SYNTAX + this.manager.getCommandPrefix()
+ ((InvalidSyntaxException) throwable).getCorrectSyntax()
)
);
} else if (throwable instanceof InvalidCommandSenderException) {
this.manager.handleException(sender,
InvalidCommandSenderException.class,
(InvalidCommandSenderException) throwable, (c, e) ->
event.respondWith(throwable.getMessage())
);
} else if (throwable instanceof NoPermissionException) {
this.manager.handleException(sender,
NoPermissionException.class,
(NoPermissionException) throwable, (c, e) ->
event.respondWith(MESSAGE_NO_PERMS)
);
} else if (throwable instanceof NoSuchCommandException) {
this.manager.handleException(sender,
NoSuchCommandException.class,
(NoSuchCommandException) throwable, (c, e) ->
event.respondWith(MESSAGE_UNKNOWN_COMMAND)
);
} else if (throwable instanceof ArgumentParseException) {
this.manager.handleException(sender, ArgumentParseException.class,
(ArgumentParseException) throwable, (c, e) -> event.respondWith(
"Invalid Command Argument: " + throwable.getCause().getMessage()
)
);
} else {
event.respondWith(throwable.getMessage());
}
});
}
}

View file

@ -0,0 +1,141 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.pircbotx;
import cloud.commandframework.CommandManager;
import cloud.commandframework.CommandTree;
import cloud.commandframework.captions.Caption;
import cloud.commandframework.captions.FactoryDelegatingCaptionRegistry;
import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.internal.CommandRegistrationHandler;
import cloud.commandframework.meta.CommandMeta;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import java.util.function.BiFunction;
import java.util.function.Function;
/**
* Command manager implementation for PircBotX 2.0
*
* @param <C> Command sender type
* @since 1.1.0
*/
public class PircBotXCommandManager<C> extends CommandManager<C> {
/**
* Meta key for accessing the {@link org.pircbotx.PircBotX} instance from a
* {@link cloud.commandframework.context.CommandContext} instance
*/
public static final String PIRCBOTX_META_KEY = "__internal_pircbotx__";
/**
* Variables: {input}
*/
public static final Caption ARGUMENT_PARSE_FAILURE_USER_KEY = Caption.of("argument.parse.failure.use");
/**
* Default caption for {@link #ARGUMENT_PARSE_FAILURE_USER_KEY}
*/
public static final String ARGUMENT_PARSE_FAILURE_USER = "'{input}' is not a valid user";
private final String commandPrefix;
private final BiFunction<C, String, Boolean> permissionFunction;
private final Function<User, C> userMapper;
private final PircBotX pircBotX;
/**
* Create a new command manager instance
*
* @param pircBotX PircBotX instance. This is used to register the
* {@link org.pircbotx.hooks.ListenerAdapter} that will forward commands to the
* command dispatcher
* @param commandExecutionCoordinator Execution coordinator instance. The coordinator is in charge of executing incoming
* commands. Some considerations must be made when picking a suitable execution coordinator
* for your platform. For example, an entirely asynchronous coordinator is not suitable
* when the parsers used in that particular platform are not thread safe. If you have
* commands that perform blocking operations, however, it might not be a good idea to
* use a synchronous execution coordinator. In most cases you will want to pick between
* {@link CommandExecutionCoordinator#simpleCoordinator()} and
* {@link AsynchronousCommandExecutionCoordinator}
* @param commandRegistrationHandler Command registration handler. This will get called every time a new command is
* registered to the command manager. This may be used to forward command registration
* @param permissionFunction Function used to determine whether or not a sender is permitted to use a certain
* command. The first input is the sender of the command, and the second parameter is
* the the command permission string. The return value should be {@code true} if the
* sender is permitted to use the command, else {@code false}
* @param userMapper Function that maps {@link User users} to the custom command sender type
* @param commandPrefix The prefix that must be applied to all commands for the command to be valid
*/
public PircBotXCommandManager(
final @NonNull PircBotX pircBotX,
final @NonNull Function<@NonNull CommandTree<C>, @NonNull CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull CommandRegistrationHandler commandRegistrationHandler,
final @NonNull BiFunction<C, String, Boolean> permissionFunction,
final @NonNull Function<User, C> userMapper,
final @NonNull String commandPrefix
) {
super(commandExecutionCoordinator, commandRegistrationHandler);
this.pircBotX = pircBotX;
this.permissionFunction = permissionFunction;
this.commandPrefix = commandPrefix;
this.userMapper = userMapper;
this.pircBotX.getConfiguration().getListenerManager().addListener(new CloudListenerAdapter<>(this));
if (this.getCaptionRegistry() instanceof FactoryDelegatingCaptionRegistry) {
((FactoryDelegatingCaptionRegistry<C>) this.getCaptionRegistry()).registerMessageFactory(
ARGUMENT_PARSE_FAILURE_USER_KEY,
(caption, user) -> ARGUMENT_PARSE_FAILURE_USER
);
}
this.registerCommandPreProcessor(context -> context.getCommandContext().store(PIRCBOTX_META_KEY, pircBotX));
}
@Override
public final boolean hasPermission(
@NonNull final C sender,
@NonNull final String permission
) {
return this.permissionFunction.apply(sender, permission);
}
@Override
public final @NonNull CommandMeta createDefaultCommandMeta() {
return CommandMeta.simple().build();
}
/**
* Get the command prefix. A message should be classed as a command if, and only if, it is prefixed
* with this prefix
*
* @return Command prefix
*/
public final @NonNull String getCommandPrefix() {
return this.commandPrefix;
}
@NonNull final Function<User, C> getUserMapper() {
return this.userMapper;
}
}

View file

@ -0,0 +1,178 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package cloud.commandframework.pircbotx.arguments;
import cloud.commandframework.arguments.CommandArgument;
import cloud.commandframework.arguments.parser.ArgumentParseResult;
import cloud.commandframework.arguments.parser.ArgumentParser;
import cloud.commandframework.captions.CaptionVariable;
import cloud.commandframework.context.CommandContext;
import cloud.commandframework.exceptions.parsing.NoInputProvidedException;
import cloud.commandframework.exceptions.parsing.ParserException;
import cloud.commandframework.pircbotx.PircBotXCommandManager;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.exception.DaoException;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.function.BiFunction;
/**
* {@link CommandArgument Command argument} that parses PircBotX {@link User users}
*
* @param <C> Command sender type
* @since 1.1.0
*/
public final class UserArgument<C> extends CommandArgument<C, User> {
private UserArgument(
final boolean required,
final @NonNull String name,
final @Nullable BiFunction<CommandContext<C>, String, List<String>> suggestionsProvider
) {
super(
required,
name,
new UserArgumentParser<>(),
"",
TypeToken.get(User.class),
suggestionsProvider,
new LinkedList<>()
);
}
/**
* Create a new user argument builder
*
* @param name Argument name
* @param <C> Command sender type
* @return Builder instance
*/
public static <C> @NonNull Builder<C> newBuilder(final @NonNull String name) {
return new Builder<>(name);
}
/**
* Create a new required user argument
*
* @param name Argument name
* @param <C> Command sender type
* @return Argument instance
*/
public static <C> @NonNull CommandArgument<C, User> of(final @NonNull String name) {
return UserArgument.<C>newBuilder(name).asRequired().build();
}
/**
* Create a optional user argument
*
* @param name Argument name
* @param <C> Command sender type
* @return Argument instance
*/
public static <C> @NonNull CommandArgument<C, User> optional(final @NonNull String name) {
return UserArgument.<C>newBuilder(name).asOptional().build();
}
public static final class Builder<C> extends CommandArgument.Builder<C, User> {
private Builder(
final @NonNull String name
) {
super(
TypeToken.get(User.class),
name
);
}
@Override
public @NonNull CommandArgument<@NonNull C, @NonNull User> build() {
return new UserArgument<>(
this.isRequired(),
this.getName(),
this.getSuggestionsProvider()
);
}
}
public static final class UserArgumentParser<C> implements ArgumentParser<C, User> {
@Override
public @NonNull ArgumentParseResult<@NonNull User> parse(
@NonNull final CommandContext<@NonNull C> commandContext,
@NonNull final Queue<@NonNull String> inputQueue
) {
final String input = inputQueue.peek();
if (input == null) {
return ArgumentParseResult.failure(new NoInputProvidedException(
UserArgumentParser.class,
commandContext
));
}
final PircBotX pircBotX = commandContext.get(PircBotXCommandManager.PIRCBOTX_META_KEY);
final User user;
try {
user = pircBotX.getUserChannelDao().getUser(input);
} catch (final DaoException exception) {
return ArgumentParseResult.failure(
new UserParseException(
commandContext,
input
)
);
}
return ArgumentParseResult.success(user);
}
}
public static final class UserParseException extends ParserException {
private UserParseException(
final @NonNull CommandContext<?> context,
final @NonNull String input
) {
super(
UserArgumentParser.class,
context,
PircBotXCommandManager.ARGUMENT_PARSE_FAILURE_USER_KEY,
CaptionVariable.of(
"input",
input
)
);
}
}
}

View file

@ -0,0 +1,28 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
/**
* PircBotX specific arguments
*/
package cloud.commandframework.pircbotx.arguments;

View file

@ -0,0 +1,28 @@
//
// MIT License
//
// Copyright (c) 2020 Alexander Söderberg & Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
/**
* PircBotX 2.0 implementation of Cloud
*/
package cloud.commandframework.pircbotx;