Add JDA implementation
This commit is contained in:
parent
cd274fd032
commit
212145cc6b
11 changed files with 732 additions and 0 deletions
|
|
@ -58,6 +58,7 @@ subprojects {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
jcenter()
|
||||||
|
|
||||||
maven {
|
maven {
|
||||||
url = 'https://oss.sonatype.org/content/repositories/snapshots'
|
url = 'https://oss.sonatype.org/content/repositories/snapshots'
|
||||||
|
|
|
||||||
4
cloud-discord/cloud-jda/build.gradle
Normal file
4
cloud-discord/cloud-jda/build.gradle
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
dependencies {
|
||||||
|
api project(':cloud-core')
|
||||||
|
compileOnly 'net.dv8tion:JDA:4.2.0_207'
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,125 @@
|
||||||
|
//
|
||||||
|
// 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.exceptions.ArgumentParseException;
|
||||||
|
import cloud.commandframework.exceptions.InvalidCommandSenderException;
|
||||||
|
import cloud.commandframework.exceptions.InvalidSyntaxException;
|
||||||
|
import cloud.commandframework.exceptions.NoPermissionException;
|
||||||
|
import cloud.commandframework.exceptions.NoSuchCommandException;
|
||||||
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
import net.dv8tion.jda.api.hooks.ListenerAdapter;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JDA Command Listener
|
||||||
|
*
|
||||||
|
* @param <C> Command sender type
|
||||||
|
*/
|
||||||
|
public class JDACommandListener<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 JDACommandManager<C> commandManager;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new JDA Command Listener
|
||||||
|
*
|
||||||
|
* @param commandManager Command Manager instance
|
||||||
|
*/
|
||||||
|
public JDACommandListener(final @NonNull JDACommandManager<C> commandManager) {
|
||||||
|
this.commandManager = commandManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final void onMessageReceived(final @NonNull MessageReceivedEvent event) {
|
||||||
|
Message message = event.getMessage();
|
||||||
|
C sender = commandManager.getCommandSenderMapper().apply(event);
|
||||||
|
|
||||||
|
if (commandManager.getBotId() == event.getAuthor().getIdLong()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String prefix = commandManager.getPrefixMapper().apply(sender);
|
||||||
|
String content = message.getContentRaw();
|
||||||
|
|
||||||
|
if (!content.startsWith(prefix)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
content = content.substring(prefix.length());
|
||||||
|
|
||||||
|
commandManager.executeCommand(sender, content)
|
||||||
|
.whenComplete((commandResult, throwable) -> {
|
||||||
|
if (throwable == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (throwable instanceof InvalidSyntaxException) {
|
||||||
|
commandManager.handleException(sender,
|
||||||
|
InvalidSyntaxException.class,
|
||||||
|
(InvalidSyntaxException) throwable, (c, e) -> {
|
||||||
|
sendMessage(event,
|
||||||
|
MESSAGE_INVALID_SYNTAX + prefix + ((InvalidSyntaxException) throwable)
|
||||||
|
.getCorrectSyntax());
|
||||||
|
});
|
||||||
|
} else if (throwable instanceof InvalidCommandSenderException) {
|
||||||
|
commandManager.handleException(sender,
|
||||||
|
InvalidCommandSenderException.class,
|
||||||
|
(InvalidCommandSenderException) throwable, (c, e) ->
|
||||||
|
sendMessage(event, throwable.getMessage())
|
||||||
|
);
|
||||||
|
} else if (throwable instanceof NoPermissionException) {
|
||||||
|
commandManager.handleException(sender,
|
||||||
|
NoPermissionException.class,
|
||||||
|
(NoPermissionException) throwable, (c, e) ->
|
||||||
|
sendMessage(event, MESSAGE_NO_PERMS)
|
||||||
|
);
|
||||||
|
} else if (throwable instanceof NoSuchCommandException) {
|
||||||
|
commandManager.handleException(sender,
|
||||||
|
NoSuchCommandException.class,
|
||||||
|
(NoSuchCommandException) throwable, (c, e) ->
|
||||||
|
sendMessage(event, MESSAGE_UNKNOWN_COMMAND)
|
||||||
|
);
|
||||||
|
} else if (throwable instanceof ArgumentParseException) {
|
||||||
|
commandManager.handleException(sender, ArgumentParseException.class,
|
||||||
|
(ArgumentParseException) throwable, (c, e) -> {
|
||||||
|
sendMessage(event,
|
||||||
|
"Invalid Command Argument: " + throwable.getCause()
|
||||||
|
.getMessage());
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
sendMessage(event, throwable.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMessage(final @NonNull MessageReceivedEvent event, final @NonNull String message) {
|
||||||
|
event.getChannel().sendMessage(message).queue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,124 @@
|
||||||
|
//
|
||||||
|
// 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.CommandManager;
|
||||||
|
import cloud.commandframework.CommandTree;
|
||||||
|
import cloud.commandframework.execution.CommandExecutionCoordinator;
|
||||||
|
import cloud.commandframework.internal.CommandRegistrationHandler;
|
||||||
|
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 java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command manager for use with JDA
|
||||||
|
*
|
||||||
|
* @param <C> Command sender type
|
||||||
|
*/
|
||||||
|
public class JDACommandManager<C> extends CommandManager<C> {
|
||||||
|
private final long botId;
|
||||||
|
|
||||||
|
private final Function<@NonNull C, @NonNull String> prefixMapper;
|
||||||
|
private final Function<@NonNull MessageReceivedEvent, @NonNull C> commandSenderMapper;
|
||||||
|
private final Function<@NonNull C, @NonNull MessageReceivedEvent> backwardsCommandSenderMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* final
|
||||||
|
* 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 commandExecutionCoordinator Coordination provider
|
||||||
|
* @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(final @NonNull JDA jda,
|
||||||
|
final @NonNull Function<@NonNull C, @NonNull String> prefixMapper,
|
||||||
|
final @NonNull Function<CommandTree<C>, CommandExecutionCoordinator<C>> commandExecutionCoordinator,
|
||||||
|
final @NonNull Function<@NonNull MessageReceivedEvent, @NonNull C> commandSenderMapper,
|
||||||
|
final @NonNull Function<@NonNull C, @NonNull MessageReceivedEvent> backwardsCommandSenderMapper)
|
||||||
|
throws InterruptedException {
|
||||||
|
super(commandExecutionCoordinator, CommandRegistrationHandler.nullCommandRegistrationHandler());
|
||||||
|
this.prefixMapper = prefixMapper;
|
||||||
|
this.commandSenderMapper = commandSenderMapper;
|
||||||
|
this.backwardsCommandSenderMapper = backwardsCommandSenderMapper;
|
||||||
|
jda.addEventListener(new JDACommandListener<>(this));
|
||||||
|
jda.awaitReady();
|
||||||
|
this.botId = jda.getSelfUser().getIdLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the prefix mapper
|
||||||
|
*
|
||||||
|
* @return Prefix mapper
|
||||||
|
*/
|
||||||
|
public final @NonNull Function<C, String> getPrefixMapper() {
|
||||||
|
return prefixMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the command sender mapper
|
||||||
|
*
|
||||||
|
* @return Command sender mapper
|
||||||
|
*/
|
||||||
|
public final @NonNull Function<@NonNull MessageReceivedEvent, @NonNull C> getCommandSenderMapper() {
|
||||||
|
return this.commandSenderMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the bots discord id
|
||||||
|
*
|
||||||
|
* @return Bots discord id
|
||||||
|
*/
|
||||||
|
public final long getBotId() {
|
||||||
|
return botId;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final boolean hasPermission(final @NonNull C sender, final @NonNull String permission) {
|
||||||
|
if (permission.isEmpty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageReceivedEvent message = backwardsCommandSenderMapper.apply(sender);
|
||||||
|
Member member = message.getMember();
|
||||||
|
if (member == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return member.hasPermission(Permission.valueOf(permission));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public final @NonNull CommandMeta createDefaultCommandMeta() {
|
||||||
|
return SimpleCommandMeta.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,67 @@
|
||||||
|
//
|
||||||
|
// 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 net.dv8tion.jda.api.entities.ChannelType;
|
||||||
|
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Wrapper for {@link MessageReceivedEvent}
|
||||||
|
*/
|
||||||
|
public class JDACommandSender {
|
||||||
|
private final MessageReceivedEvent event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a JDA Command Sender using an event
|
||||||
|
*
|
||||||
|
* @param event Message Received Event
|
||||||
|
*/
|
||||||
|
public JDACommandSender(final @NonNull MessageReceivedEvent event) {
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the {@link MessageReceivedEvent}
|
||||||
|
*
|
||||||
|
* @return Message Received Event
|
||||||
|
*/
|
||||||
|
public @NonNull MessageReceivedEvent getEvent() {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a JDA Command Sender from a {@link MessageReceivedEvent}
|
||||||
|
*
|
||||||
|
* @param event Message Received Event
|
||||||
|
* @return Constructed JDA Command Sender
|
||||||
|
*/
|
||||||
|
public static JDACommandSender of(final @NonNull MessageReceivedEvent event) {
|
||||||
|
if (event.isFromType(ChannelType.PRIVATE)) {
|
||||||
|
return new JDAPrivateSender(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new JDAGuildSender(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// 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 net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Guild specific JDA Command Sender
|
||||||
|
*/
|
||||||
|
public class JDAGuildSender extends JDACommandSender {
|
||||||
|
|
||||||
|
JDAGuildSender(final @NonNull MessageReceivedEvent event) {
|
||||||
|
super(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
//
|
||||||
|
// 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 net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private message specific JDA Command Sender
|
||||||
|
*/
|
||||||
|
public class JDAPrivateSender extends JDACommandSender {
|
||||||
|
|
||||||
|
JDAPrivateSender(final @NonNull MessageReceivedEvent event) {
|
||||||
|
super(event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* cloud implementation for Bukkit 1.8-1.16
|
||||||
|
*/
|
||||||
|
package cloud.commandframework.jda;
|
||||||
|
|
@ -0,0 +1,279 @@
|
||||||
|
//
|
||||||
|
// 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.parsers;
|
||||||
|
|
||||||
|
import cloud.commandframework.arguments.CommandArgument;
|
||||||
|
import cloud.commandframework.arguments.parser.ArgumentParseResult;
|
||||||
|
import cloud.commandframework.arguments.parser.ArgumentParser;
|
||||||
|
import cloud.commandframework.context.CommandContext;
|
||||||
|
import net.dv8tion.jda.api.JDA;
|
||||||
|
import net.dv8tion.jda.api.entities.User;
|
||||||
|
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Queue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command Argument for {@link User}
|
||||||
|
*
|
||||||
|
* @param <C> Command sender type
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public final class UserArgument<C> extends CommandArgument<C, User> {
|
||||||
|
private final List<ParserMode> modes;
|
||||||
|
|
||||||
|
private UserArgument(final boolean required, final @NonNull String name,
|
||||||
|
final @NonNull JDA jda, final @NonNull List<ParserMode> modes) {
|
||||||
|
super(required, name, new UserParser<>(jda, modes), User.class);
|
||||||
|
this.modes = modes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new builder
|
||||||
|
*
|
||||||
|
* @param name Name of the component
|
||||||
|
* @param jda JDA instance
|
||||||
|
* @param <C> Command sender type
|
||||||
|
* @return Created builder
|
||||||
|
*/
|
||||||
|
public static <C> @NonNull Builder<C> newBuilder(final @NonNull String name, final @NonNull JDA jda) {
|
||||||
|
return new Builder<>(name, jda);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new required command component
|
||||||
|
*
|
||||||
|
* @param name Component name
|
||||||
|
* @param jda JDA instance
|
||||||
|
* @param <C> Command sender type
|
||||||
|
* @return Created component
|
||||||
|
*/
|
||||||
|
public static <C> @NonNull CommandArgument<C, User> of(final @NonNull String name, final @NonNull JDA jda) {
|
||||||
|
return UserArgument.<C>newBuilder(name, jda).asRequired().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new optional command component
|
||||||
|
*
|
||||||
|
* @param name Component name
|
||||||
|
* @param jda JDA instance
|
||||||
|
* @param <C> Command sender type
|
||||||
|
* @return Created component
|
||||||
|
*/
|
||||||
|
public static <C> @NonNull CommandArgument<C, User> optional(final @NonNull String name, final @NonNull JDA jda) {
|
||||||
|
return UserArgument.<C>newBuilder(name, jda).asOptional().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public enum ParserMode {
|
||||||
|
MENTION,
|
||||||
|
ID,
|
||||||
|
NAME
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the modes enabled on the parser
|
||||||
|
*
|
||||||
|
* @return List of Modes
|
||||||
|
*/
|
||||||
|
public @NotNull List<ParserMode> getModes() {
|
||||||
|
return modes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class Builder<C> extends CommandArgument.Builder<C, User> {
|
||||||
|
private final JDA jda;
|
||||||
|
private List<ParserMode> modes = new ArrayList<>();
|
||||||
|
|
||||||
|
protected Builder(final @NonNull String name, final @NonNull JDA jda) {
|
||||||
|
super(User.class, name);
|
||||||
|
this.jda = jda;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the modes for the parsers to use
|
||||||
|
*
|
||||||
|
* @param modes List of Modes
|
||||||
|
* @return Builder instance
|
||||||
|
*/
|
||||||
|
public @NonNull Builder<C> withParsers(final @NonNull List<ParserMode> modes) {
|
||||||
|
this.modes = modes;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builder a new example component
|
||||||
|
*
|
||||||
|
* @return Constructed component
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public @NonNull UserArgument<C> build() {
|
||||||
|
return new UserArgument<>(this.isRequired(), this.getName(), jda, modes);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class UserParser<C> implements ArgumentParser<C, User> {
|
||||||
|
private final JDA jda;
|
||||||
|
private final List<ParserMode> modes;
|
||||||
|
|
||||||
|
private UserParser(final @NonNull JDA jda, final @NonNull List<ParserMode> modes) {
|
||||||
|
this.jda = jda;
|
||||||
|
this.modes = modes;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull ArgumentParseResult<User> parse(
|
||||||
|
final @NonNull CommandContext<C> commandContext,
|
||||||
|
final @NonNull Queue<@NonNull String> inputQueue) {
|
||||||
|
final String input = inputQueue.peek();
|
||||||
|
if (input == null) {
|
||||||
|
return ArgumentParseResult.failure(new NullPointerException("No input was provided"));
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception exception = null;
|
||||||
|
|
||||||
|
if (modes.contains(ParserMode.MENTION)) {
|
||||||
|
if (input.endsWith(">")) {
|
||||||
|
String id;
|
||||||
|
if (input.startsWith("<@!")) {
|
||||||
|
id = input.substring(3, input.length() - 1);
|
||||||
|
} else {
|
||||||
|
id = input.substring(2, input.length() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
final ArgumentParseResult<User> result = userFromId(input, id);
|
||||||
|
inputQueue.remove();
|
||||||
|
return result;
|
||||||
|
} catch (UserNotFoundParseException | NumberFormatException e) {
|
||||||
|
exception = e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modes.contains(ParserMode.ID)) {
|
||||||
|
try {
|
||||||
|
final ArgumentParseResult<User> result = userFromId(input, input);
|
||||||
|
inputQueue.remove();
|
||||||
|
return result;
|
||||||
|
} catch (UserNotFoundParseException | NumberFormatException e) {
|
||||||
|
exception = e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (modes.contains(ParserMode.NAME)) {
|
||||||
|
List<User> users = jda.getUsersByName(input, true);
|
||||||
|
|
||||||
|
if (users.size() == 0) {
|
||||||
|
exception = new UserNotFoundParseException(input);
|
||||||
|
} else if (users.size() > 1) {
|
||||||
|
exception = new TooManyUsersFoundParseException(input);
|
||||||
|
} else {
|
||||||
|
inputQueue.remove();
|
||||||
|
return ArgumentParseResult.success(users.get(0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert exception != null;
|
||||||
|
return ArgumentParseResult.failure(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isContextFree() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private @NonNull ArgumentParseResult<User> userFromId(final @NonNull String input, final @NonNull String id)
|
||||||
|
throws UserNotFoundParseException, NumberFormatException {
|
||||||
|
User user = jda.getUserById(id);
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
throw new UserNotFoundParseException(input);
|
||||||
|
} else {
|
||||||
|
return ArgumentParseResult.success(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class UserParseException extends IllegalArgumentException {
|
||||||
|
|
||||||
|
private final String input;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new UUID parse exception
|
||||||
|
*
|
||||||
|
* @param input String input
|
||||||
|
*/
|
||||||
|
public UserParseException(final @NonNull String input) {
|
||||||
|
this.input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the users input
|
||||||
|
*
|
||||||
|
* @return Users input
|
||||||
|
*/
|
||||||
|
public final @NonNull String getInput() {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class TooManyUsersFoundParseException extends UserParseException {
|
||||||
|
/**
|
||||||
|
* Construct a new UUID parse exception
|
||||||
|
*
|
||||||
|
* @param input String input
|
||||||
|
*/
|
||||||
|
public TooManyUsersFoundParseException(final @NonNull String input) {
|
||||||
|
super(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull String getMessage() {
|
||||||
|
return String.format("Too many users found for '%s'.", getInput());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static final class UserNotFoundParseException extends UserParseException {
|
||||||
|
/**
|
||||||
|
* Construct a new UUID parse exception
|
||||||
|
*
|
||||||
|
* @param input String input
|
||||||
|
*/
|
||||||
|
public UserNotFoundParseException(final @NonNull String input) {
|
||||||
|
super(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public @NonNull String getMessage() {
|
||||||
|
return String.format("User not found for '%s'.", getInput());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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.
|
||||||
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* JDA specific command arguments
|
||||||
|
*/
|
||||||
|
package cloud.commandframework.jda.parsers;
|
||||||
|
|
@ -10,6 +10,7 @@ include(':cloud-bungee')
|
||||||
include(':cloud-velocity')
|
include(':cloud-velocity')
|
||||||
include(':cloud-minecraft-extras')
|
include(':cloud-minecraft-extras')
|
||||||
include(':cloud-cloudburst')
|
include(':cloud-cloudburst')
|
||||||
|
include(':cloud-jda')
|
||||||
project(':cloud-bukkit').projectDir = file('cloud-minecraft/cloud-bukkit')
|
project(':cloud-bukkit').projectDir = file('cloud-minecraft/cloud-bukkit')
|
||||||
project(':cloud-paper').projectDir = file('cloud-minecraft/cloud-paper')
|
project(':cloud-paper').projectDir = file('cloud-minecraft/cloud-paper')
|
||||||
project(':cloud-brigadier').projectDir = file('cloud-minecraft/cloud-brigadier')
|
project(':cloud-brigadier').projectDir = file('cloud-minecraft/cloud-brigadier')
|
||||||
|
|
@ -17,3 +18,4 @@ project(':cloud-bungee').projectDir = file('cloud-minecraft/cloud-bungee')
|
||||||
project(':cloud-velocity').projectDir = file('cloud-minecraft/cloud-velocity')
|
project(':cloud-velocity').projectDir = file('cloud-minecraft/cloud-velocity')
|
||||||
project(':cloud-minecraft-extras').projectDir = file('cloud-minecraft/cloud-minecraft-extras')
|
project(':cloud-minecraft-extras').projectDir = file('cloud-minecraft/cloud-minecraft-extras')
|
||||||
project(':cloud-cloudburst').projectDir = file('cloud-minecraft/cloud-cloudburst')
|
project(':cloud-cloudburst').projectDir = file('cloud-minecraft/cloud-cloudburst')
|
||||||
|
project(':cloud-jda').projectDir = file('cloud-discord/cloud-jda')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue