Deprecate JDACommandManager and add JDA4CommandManager

This commit is contained in:
broccolai 2020-10-21 04:23:01 +01:00 committed by Alexander Söderberg
parent 530ecc4f73
commit c5873e46e6
4 changed files with 88 additions and 13 deletions

View file

@ -0,0 +1,73 @@
//
// 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.jda;
import cloud.commandframework.CommandTree;
import cloud.commandframework.execution.CommandExecutionCoordinator;
import java.util.function.BiFunction;
import java.util.function.Function;
import net.dv8tion.jda.api.JDA;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Command manager for use with JDA 4
*
* @param <C> Command sender type
* @since 1.1.0
*/
public class JDA4CommandManager<C> extends JDACommandManager<C> {
/**
* Construct a new JDA Command Manager
*
* @param jda JDA instance to register against
* @param prefixMapper Function that maps the sender to a command prefix string
* @param permissionMapper Function used to check if a command sender has the permission to execute a command
* @param commandExecutionCoordinator Coordination provider
* @param commandSenderMapper Function that maps {@link JDACommandSender} to the command sender type
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link JDACommandSender}
* @throws InterruptedException If the jda instance does not ready correctly
*/
public JDA4CommandManager(
final @NonNull JDA jda,
final @NonNull Function<@NonNull C, @NonNull String> prefixMapper,
final @Nullable BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper,
final @NonNull Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull JDACommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull JDACommandSender> backwardsCommandSenderMapper
)
throws InterruptedException {
super(jda,
prefixMapper,
permissionMapper,
commandExecutionCoordinator,
commandSenderMapper.compose(JDACommandSender::of),
backwardsCommandSenderMapper.andThen((sender) -> sender.getEvent().orElseThrow(IllegalStateException::new))
);
}
}

View file

@ -60,7 +60,7 @@ public class JDACommandListener<C> extends ListenerAdapter {
public final void onMessageReceived(final @NonNull MessageReceivedEvent event) {
final Message message = event.getMessage();
final JDACommandSender jdaCommandSender = JDACommandSender.of(event);
final C sender = this.commandManager.getCommandSenderMapper().apply(jdaCommandSender);
final C sender = this.commandManager.getCommandSenderMapper().apply(event);
if (this.commandManager.getBotId() == event.getAuthor().getIdLong()) {
return;

View file

@ -31,7 +31,7 @@ import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.meta.SimpleCommandMeta;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -42,7 +42,9 @@ import java.util.function.Function;
* Command manager for use with JDA
*
* @param <C> Command sender type
* @deprecated Use {@link JDA4CommandManager}
*/
@Deprecated
public class JDACommandManager<C> extends CommandManager<C> {
private final JDA jda;
@ -50,8 +52,8 @@ public class JDACommandManager<C> extends CommandManager<C> {
private final Function<@NonNull C, @NonNull String> prefixMapper;
private final BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper;
private final Function<@NonNull JDACommandSender, @NonNull C> commandSenderMapper;
private final Function<@NonNull C, @NonNull JDACommandSender> backwardsCommandSenderMapper;
private final Function<@NonNull MessageReceivedEvent, @NonNull C> commandSenderMapper;
private final Function<@NonNull C, @NonNull MessageReceivedEvent> backwardsCommandSenderMapper;
/**
* Construct a new JDA Command Manager
@ -60,8 +62,8 @@ public class JDACommandManager<C> extends CommandManager<C> {
* @param prefixMapper Function that maps the sender to a command prefix string
* @param permissionMapper Function used to check if a command sender has the permission to execute a command
* @param commandExecutionCoordinator Coordination provider
* @param commandSenderMapper Function that maps {@link JDACommandSender} to the command sender type
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link Member}
* @param commandSenderMapper Function that maps {@link MessageReceivedEvent} to the command sender type
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link MessageReceivedEvent}
* @throws InterruptedException If the jda instance does not ready correctly
*/
public JDACommandManager(
@ -69,8 +71,8 @@ public class JDACommandManager<C> extends CommandManager<C> {
final @NonNull Function<@NonNull C, @NonNull String> prefixMapper,
final @Nullable BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper,
final @NonNull Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
final @NonNull Function<@NonNull JDACommandSender, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull JDACommandSender> backwardsCommandSenderMapper
final @NonNull Function<@NonNull MessageReceivedEvent, @NonNull C> commandSenderMapper,
final @NonNull Function<@NonNull C, @NonNull MessageReceivedEvent> backwardsCommandSenderMapper
)
throws InterruptedException {
super(commandExecutionCoordinator, CommandRegistrationHandler.nullCommandRegistrationHandler());
@ -110,7 +112,7 @@ public class JDACommandManager<C> extends CommandManager<C> {
*
* @return Command sender mapper
*/
public final @NonNull Function<@NonNull JDACommandSender, @NonNull C> getCommandSenderMapper() {
public final @NonNull Function<@NonNull MessageReceivedEvent, @NonNull C> getCommandSenderMapper() {
return this.commandSenderMapper;
}
@ -119,7 +121,7 @@ public class JDACommandManager<C> extends CommandManager<C> {
*
* @return The backwards command sender mapper
*/
public final @NonNull Function<@NonNull C, @NonNull JDACommandSender> getBackwardsCommandSenderMapper() {
public final @NonNull Function<@NonNull C, @NonNull MessageReceivedEvent> getBackwardsCommandSenderMapper() {
return this.backwardsCommandSenderMapper;
}
@ -142,7 +144,7 @@ public class JDACommandManager<C> extends CommandManager<C> {
return this.permissionMapper.apply(sender, permission);
}
final JDACommandSender jdaSender = this.backwardsCommandSenderMapper.apply(sender);
final JDACommandSender jdaSender = this.backwardsCommandSenderMapper.andThen(JDACommandSender::of).apply(sender);
if (!(jdaSender instanceof JDAGuildSender)) {
return true;