Deprecate JDACommandManager and add JDA4CommandManager
This commit is contained in:
parent
530ecc4f73
commit
c5873e46e6
4 changed files with 88 additions and 13 deletions
|
|
@ -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))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -60,7 +60,7 @@ public class JDACommandListener<C> extends ListenerAdapter {
|
||||||
public final void onMessageReceived(final @NonNull MessageReceivedEvent event) {
|
public final void onMessageReceived(final @NonNull MessageReceivedEvent event) {
|
||||||
final Message message = event.getMessage();
|
final Message message = event.getMessage();
|
||||||
final JDACommandSender jdaCommandSender = JDACommandSender.of(event);
|
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()) {
|
if (this.commandManager.getBotId() == event.getAuthor().getIdLong()) {
|
||||||
return;
|
return;
|
||||||
|
|
|
||||||
|
|
@ -31,7 +31,7 @@ import cloud.commandframework.meta.CommandMeta;
|
||||||
import cloud.commandframework.meta.SimpleCommandMeta;
|
import cloud.commandframework.meta.SimpleCommandMeta;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.Permission;
|
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.NonNull;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
|
||||||
|
|
@ -42,7 +42,9 @@ import java.util.function.Function;
|
||||||
* Command manager for use with JDA
|
* Command manager for use with JDA
|
||||||
*
|
*
|
||||||
* @param <C> Command sender type
|
* @param <C> Command sender type
|
||||||
|
* @deprecated Use {@link JDA4CommandManager}
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public class JDACommandManager<C> extends CommandManager<C> {
|
public class JDACommandManager<C> extends CommandManager<C> {
|
||||||
|
|
||||||
private final JDA jda;
|
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 Function<@NonNull C, @NonNull String> prefixMapper;
|
||||||
private final BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper;
|
private final BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper;
|
||||||
private final Function<@NonNull JDACommandSender, @NonNull C> commandSenderMapper;
|
private final Function<@NonNull MessageReceivedEvent, @NonNull C> commandSenderMapper;
|
||||||
private final Function<@NonNull C, @NonNull JDACommandSender> backwardsCommandSenderMapper;
|
private final Function<@NonNull C, @NonNull MessageReceivedEvent> backwardsCommandSenderMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a new JDA Command Manager
|
* 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 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 permissionMapper Function used to check if a command sender has the permission to execute a command
|
||||||
* @param commandExecutionCoordinator Coordination provider
|
* @param commandExecutionCoordinator Coordination provider
|
||||||
* @param commandSenderMapper Function that maps {@link JDACommandSender} to the command sender type
|
* @param commandSenderMapper Function that maps {@link MessageReceivedEvent} to the command sender type
|
||||||
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link Member}
|
* @param backwardsCommandSenderMapper Function that maps the command sender type to {@link MessageReceivedEvent}
|
||||||
* @throws InterruptedException If the jda instance does not ready correctly
|
* @throws InterruptedException If the jda instance does not ready correctly
|
||||||
*/
|
*/
|
||||||
public JDACommandManager(
|
public JDACommandManager(
|
||||||
|
|
@ -69,8 +71,8 @@ public class JDACommandManager<C> extends CommandManager<C> {
|
||||||
final @NonNull Function<@NonNull C, @NonNull String> prefixMapper,
|
final @NonNull Function<@NonNull C, @NonNull String> prefixMapper,
|
||||||
final @Nullable BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper,
|
final @Nullable BiFunction<@NonNull C, @NonNull String, @NonNull Boolean> permissionMapper,
|
||||||
final @NonNull Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
final @NonNull Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
||||||
final @NonNull Function<@NonNull JDACommandSender, @NonNull C> commandSenderMapper,
|
final @NonNull Function<@NonNull MessageReceivedEvent, @NonNull C> commandSenderMapper,
|
||||||
final @NonNull Function<@NonNull C, @NonNull JDACommandSender> backwardsCommandSenderMapper
|
final @NonNull Function<@NonNull C, @NonNull MessageReceivedEvent> backwardsCommandSenderMapper
|
||||||
)
|
)
|
||||||
throws InterruptedException {
|
throws InterruptedException {
|
||||||
super(commandExecutionCoordinator, CommandRegistrationHandler.nullCommandRegistrationHandler());
|
super(commandExecutionCoordinator, CommandRegistrationHandler.nullCommandRegistrationHandler());
|
||||||
|
|
@ -110,7 +112,7 @@ public class JDACommandManager<C> extends CommandManager<C> {
|
||||||
*
|
*
|
||||||
* @return Command sender mapper
|
* @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;
|
return this.commandSenderMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -119,7 +121,7 @@ public class JDACommandManager<C> extends CommandManager<C> {
|
||||||
*
|
*
|
||||||
* @return The backwards command sender mapper
|
* @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;
|
return this.backwardsCommandSenderMapper;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -142,7 +144,7 @@ public class JDACommandManager<C> extends CommandManager<C> {
|
||||||
return this.permissionMapper.apply(sender, permission);
|
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)) {
|
if (!(jdaSender instanceof JDAGuildSender)) {
|
||||||
return true;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ package cloud.commandframework.examples.jda;
|
||||||
import cloud.commandframework.Command;
|
import cloud.commandframework.Command;
|
||||||
import cloud.commandframework.arguments.standard.StringArgument;
|
import cloud.commandframework.arguments.standard.StringArgument;
|
||||||
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||||
import cloud.commandframework.jda.JDACommandManager;
|
import cloud.commandframework.jda.JDA4CommandManager;
|
||||||
import cloud.commandframework.jda.JDAGuildSender;
|
import cloud.commandframework.jda.JDAGuildSender;
|
||||||
import cloud.commandframework.jda.JDAPrivateSender;
|
import cloud.commandframework.jda.JDAPrivateSender;
|
||||||
import cloud.commandframework.jda.parsers.UserArgument;
|
import cloud.commandframework.jda.parsers.UserArgument;
|
||||||
|
|
@ -66,7 +66,7 @@ public final class ExampleBot {
|
||||||
|
|
||||||
final PermissionRegistry permissionRegistry = new PermissionRegistry();
|
final PermissionRegistry permissionRegistry = new PermissionRegistry();
|
||||||
|
|
||||||
final JDACommandManager<CustomUser> commandManager = new JDACommandManager<>(
|
final JDA4CommandManager<CustomUser> commandManager = new JDA4CommandManager<>(
|
||||||
jda,
|
jda,
|
||||||
message -> "!",
|
message -> "!",
|
||||||
(sender, permission) -> permissionRegistry.hasPermission(sender.getUser().getIdLong(), permission),
|
(sender, permission) -> permissionRegistry.hasPermission(sender.getUser().getIdLong(), permission),
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue