diff --git a/cloud-discord/cloud-javacord/build.gradle b/cloud-discord/cloud-javacord/build.gradle new file mode 100644 index 00000000..b95e30c3 --- /dev/null +++ b/cloud-discord/cloud-javacord/build.gradle @@ -0,0 +1,4 @@ +dependencies { + api project(':cloud-core') + implementation 'org.javacord:javacord:3.1.1' +} diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordCommand.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordCommand.java new file mode 100644 index 00000000..d14cabca --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordCommand.java @@ -0,0 +1,154 @@ +// +// 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.javacord; + +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.arguments.StaticArgument; +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 cloud.commandframework.javacord.sender.JavacordCommandSender; +import cloud.commandframework.javacord.sender.JavacordPrivateSender; +import cloud.commandframework.javacord.sender.JavacordServerSender; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.javacord.api.entity.message.MessageAuthor; +import org.javacord.api.event.message.MessageCreateEvent; +import org.javacord.api.listener.message.MessageCreateListener; + +import java.util.concurrent.CompletionException; + +public class JavacordCommand implements MessageCreateListener { + + private static final String MESSAGE_NO_PERMS = "I'm sorry, but you do not have the permission to do this :/"; + + private final JavacordCommandManager manager; + private final CommandArgument command; + private final cloud.commandframework.Command cloudCommand; + + JavacordCommand(final cloud.commandframework.@NonNull Command cloudCommand, + @NonNull final CommandArgument command, + @NonNull final JavacordCommandManager manager) { + this.command = command; + this.manager = manager; + this.cloudCommand = cloudCommand; + } + + @Override + public final void onMessageCreate(final @NonNull MessageCreateEvent event) { + MessageAuthor messageAuthor = event.getMessageAuthor(); + + if (messageAuthor.isWebhook() || !messageAuthor.isRegularUser()) { + return; + } + + JavacordCommandSender commandSender; + if (event.getMessage().isServerMessage()) { + commandSender = new JavacordServerSender(event); + } else if (event.getMessage().isPrivateMessage()) { + commandSender = new JavacordPrivateSender(event); + } else { + commandSender = new JavacordCommandSender(event); + } + + C sender = manager.getCommandSenderMapper().apply(commandSender); + + String messageContent = event.getMessageContent(); + String commandPrefix = manager.getCommandPrefix(sender); + if (!messageContent.startsWith(commandPrefix)) { + return; + } + messageContent = messageContent.replaceFirst(commandPrefix, ""); + + final String finalContent = messageContent; + //noinspection unchecked + if (((StaticArgument) command).getAliases() + .stream() + .map(String::toLowerCase) + .noneMatch(commandAlias -> finalContent.toLowerCase().startsWith(commandAlias))) { + return; + } + + manager.executeCommand(sender, finalContent) + .whenComplete(((commandResult, throwable) -> { + if (throwable == null) { + return; + } + + if (throwable instanceof CompletionException) { + throwable = throwable.getCause(); + } + + if (throwable instanceof NoSuchCommandException) { + //Ignore, should never happen + return; + } + + if (throwable instanceof InvalidSyntaxException) { + manager.handleException(sender, + InvalidSyntaxException.class, + (InvalidSyntaxException) throwable, + (c, e) -> commandSender.sendErrorMessage( + "Invalid Command Syntax. Correct command syntax is: `" + + e.getCorrectSyntax() + + "`") + ); + + return; + } + + if (throwable instanceof InvalidCommandSenderException) { + manager.handleException(sender, + InvalidCommandSenderException.class, + (InvalidCommandSenderException) throwable, + (c, e) -> commandSender.sendErrorMessage(e.getMessage())); + + return; + } + + if (throwable instanceof NoPermissionException) { + manager.handleException(sender, + NoPermissionException.class, + (NoPermissionException) throwable, + (c, e) -> commandSender.sendErrorMessage(MESSAGE_NO_PERMS)); + + return; + } + + if (throwable instanceof ArgumentParseException) { + manager.handleException(sender, + ArgumentParseException.class, + (ArgumentParseException) throwable, + (c, e) -> commandSender.sendErrorMessage( + "Invalid Command Argument: `" + e.getCause().getMessage() + "`")); + + return; + } + + commandSender.sendErrorMessage(throwable.getMessage()); + throwable.printStackTrace(); + })); + } +} diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordCommandManager.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordCommandManager.java new file mode 100644 index 00000000..3670629b --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordCommandManager.java @@ -0,0 +1,111 @@ +// +// 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.javacord; + +import cloud.commandframework.CommandManager; +import cloud.commandframework.CommandTree; +import cloud.commandframework.execution.CommandExecutionCoordinator; +import cloud.commandframework.javacord.sender.JavacordCommandSender; +import cloud.commandframework.meta.SimpleCommandMeta; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.javacord.api.DiscordApi; + +import java.util.function.Function; + +public class JavacordCommandManager extends CommandManager { + + private final DiscordApi discordApi; + private final Function commandSenderMapper; + private final Function backwardsCommandSenderMapper; + + private final Function commandPrefixMapper; + private final Function commandPermissionMapper; + + /** + * Construct a new Javacord command manager + * + * @param discordApi Instance of {@link DiscordApi} used to register listeners + * @param commandExecutionCoordinator Coordinator provider + * @param commandSenderMapper Function that maps {@link Object} to the command sender type + * @param backwardsCommandSenderMapper Function that maps the command sender type to {@link Object} + * @param commandPrefixMapper Function that maps the command sender type to the command prefix + * @param commandPermissionMapper Function used to check if a command sender has the permission to execute a command + * @throws Exception If the construction of the manager fails + */ + public JavacordCommandManager(@NonNull final DiscordApi discordApi, + @NonNull final Function<@NonNull CommandTree, + @NonNull CommandExecutionCoordinator> commandExecutionCoordinator, + @NonNull final Function<@NonNull JavacordCommandSender, @NonNull C> commandSenderMapper, + @NonNull + final Function<@NonNull C, @NonNull JavacordCommandSender> backwardsCommandSenderMapper, + @NonNull final Function<@NonNull C, @NonNull String> commandPrefixMapper, + @NonNull final Function<@NonNull C, @NonNull Boolean> commandPermissionMapper) + throws Exception { + super(commandExecutionCoordinator, new JavacordRegistrationHandler<>()); + ((JavacordRegistrationHandler) this.getCommandRegistrationHandler()).initialize(this); + this.discordApi = discordApi; + this.commandSenderMapper = commandSenderMapper; + this.backwardsCommandSenderMapper = backwardsCommandSenderMapper; + + this.commandPrefixMapper = commandPrefixMapper; + this.commandPermissionMapper = commandPermissionMapper; + } + + @Override + public final boolean hasPermission( + final @NonNull C sender, final @NonNull String permission) { + if (permission.isEmpty()) { + return true; + } + return this.commandPermissionMapper.apply(sender); + } + + @Override + public final @NonNull SimpleCommandMeta createDefaultCommandMeta() { + return SimpleCommandMeta.empty(); + } + + final @NonNull Function<@NonNull JavacordCommandSender, @NonNull C> getCommandSenderMapper() { + return this.commandSenderMapper; + } + + /** + * Gets the current command prefix + * + * @param sender Sender used to get the prefix (probably won't used anyways) + * @return the command prefix + */ + public @NonNull String getCommandPrefix(final @NonNull C sender) { + return this.commandPrefixMapper.apply(sender); + } + + /** + * Gets the DiscordApi instance + * + * @return Current DiscordApi instance + */ + public @NonNull DiscordApi getDiscordApi() { + return this.discordApi; + } +} diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordRegistrationHandler.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordRegistrationHandler.java new file mode 100644 index 00000000..7d55c76d --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/JavacordRegistrationHandler.java @@ -0,0 +1,63 @@ +// +// 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.javacord; + +import cloud.commandframework.Command; +import cloud.commandframework.arguments.CommandArgument; +import cloud.commandframework.internal.CommandRegistrationHandler; +import java.util.HashMap; +import java.util.Map; +import org.checkerframework.checker.nullness.qual.NonNull; + +final class JavacordRegistrationHandler implements CommandRegistrationHandler { + + private final Map, JavacordCommand> registeredCommands = new HashMap<>(); + + private JavacordCommandManager javacordCommandManager; + + JavacordRegistrationHandler() { + } + + void initialize(@NonNull final JavacordCommandManager javacordCommandManager) { + this.javacordCommandManager = javacordCommandManager; + } + + @Override + public boolean registerCommand(@NonNull final Command command) { + /* We only care about the root command argument */ + final CommandArgument commandArgument = command.getArguments().get(0); + if (this.registeredCommands.containsKey(commandArgument)) { + return false; + } + @SuppressWarnings("unchecked") final JavacordCommand javacordCommand = new JavacordCommand<>( + (Command) command, + (CommandArgument) commandArgument, + this.javacordCommandManager); + this.registeredCommands.put(commandArgument, javacordCommand); + this.javacordCommandManager.getDiscordApi().addMessageCreateListener(javacordCommand); + return true; + } + +} + diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/package-info.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/package-info.java new file mode 100644 index 00000000..bcaed434 --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/package-info.java @@ -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 Javacord + */ +package cloud.commandframework.javacord; diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordCommandSender.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordCommandSender.java new file mode 100644 index 00000000..00af7c6b --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordCommandSender.java @@ -0,0 +1,121 @@ +// +// 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.javacord.sender; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; +import org.javacord.api.entity.channel.TextChannel; +import org.javacord.api.entity.message.Message; +import org.javacord.api.entity.message.MessageAuthor; +import org.javacord.api.event.message.MessageCreateEvent; + +import java.util.concurrent.CompletableFuture; + +public class JavacordCommandSender { + + private final MessageCreateEvent event; + + /** + * Commandsender used for all javacord commands executed. + * + * @param event The event which triggered the command + */ + public JavacordCommandSender(@NonNull final MessageCreateEvent event) { + this.event = event; + } + + /** + * Gets the author of the {@link Message message} which triggered the event + * + * @return The author of the message + */ + @NonNull + public MessageAuthor getAuthor() { + return this.event.getMessageAuthor(); + } + + /** + * Gets the message of the event. + * + * @return The message of the event. + */ + @NonNull + public Message getMessage() { + return this.event.getMessage(); + } + + /** + * Gets the textchannel the {@link Message message} was sent in + * + * @return The textchannel of the event + */ + @NonNull + public TextChannel getTextChannel() { + return this.event.getChannel(); + } + + /** + * Gets the event which triggered the command + * + * @return The event of the command + */ + @NonNull + public MessageCreateEvent getEvent() { + return this.event; + } + + /** + * Sends a message to the executor of the command + * + * @param message message which should be sent + * @return The sent message + */ + @NonNull + public CompletableFuture sendMessage(final @Nullable String message) { + return this.event.getChannel().sendMessage(message); + } + + /** + * Sends an error message to the executor of the command + * + * @param message message which should be sent + * @return The sent message + */ + @NonNull + public CompletableFuture sendErrorMessage(final @Nullable String message) { + return sendMessage(":x: " + message); + } + + /** + * Sends a success message to the executor of the command + * + * @param message message which should be sent + * @return The sent message + */ + @NonNull + public CompletableFuture sendSuccessMessage(final @Nullable String message) { + return sendMessage(":white_check_mark: " + message); + } + +} diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordPrivateSender.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordPrivateSender.java new file mode 100644 index 00000000..d75854f1 --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordPrivateSender.java @@ -0,0 +1,52 @@ +// +// 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.javacord.sender; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.javacord.api.entity.channel.PrivateChannel; +import org.javacord.api.event.message.MessageCreateEvent; + +public class JavacordPrivateSender extends JavacordCommandSender { + + /** + * Commandsender used for commands executed in a {@link PrivateChannel} + * + * @param event The event which triggered the command + */ + public JavacordPrivateSender(@NonNull final MessageCreateEvent event) { + super(event); + } + + /** + * Gets the private channel the command was executed in + * + * @return The private channel + */ + @NonNull + public PrivateChannel getPrivateChannel() { + return getEvent().getPrivateChannel() + .orElseThrow(() -> new UnsupportedOperationException( + "PrivateTextChannel not present even though message was sent in a private channel")); + } +} diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordServerSender.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordServerSender.java new file mode 100644 index 00000000..34aa4cab --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/JavacordServerSender.java @@ -0,0 +1,65 @@ +// +// 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.javacord.sender; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.javacord.api.entity.channel.ServerChannel; +import org.javacord.api.entity.server.Server; +import org.javacord.api.event.message.MessageCreateEvent; + +public class JavacordServerSender extends JavacordCommandSender { + + /** + * Commandsender used for commands executed on a {@link Server} + * + * @param event The event which triggered the command + */ + public JavacordServerSender(@NonNull final MessageCreateEvent event) { + super(event); + } + + /** + * Gets the server channel the command was executed in + * + * @return The server channel + */ + @NonNull + public ServerChannel getServerChannel() { + return getEvent().getServerTextChannel() + .orElseThrow(() -> new UnsupportedOperationException( + "ServerTextChannel not present even though message was sent in a server channel")); + } + + /** + * Gets the server the command was executed in + * + * @return The server + */ + @NonNull + public Server getServer() { + return getEvent().getServer() + .orElseThrow(() -> new UnsupportedOperationException( + "Server not present even though message was sent on a server")); + } +} diff --git a/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/package-info.java b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/package-info.java new file mode 100644 index 00000000..242c2d88 --- /dev/null +++ b/cloud-discord/cloud-javacord/src/main/java/cloud/commandframework/javacord/sender/package-info.java @@ -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. +// + +/** + * Javacord specific command senders + */ +package cloud.commandframework.javacord.sender; diff --git a/settings.gradle b/settings.gradle index b64670ff..d78dcb21 100644 --- a/settings.gradle +++ b/settings.gradle @@ -10,6 +10,7 @@ include(':cloud-bungee') include(':cloud-velocity') include(':cloud-minecraft-extras') include(':cloud-cloudburst') +include(':cloud-javacord') include(':cloud-jda') project(':cloud-bukkit').projectDir = file('cloud-minecraft/cloud-bukkit') project(':cloud-paper').projectDir = file('cloud-minecraft/cloud-paper') @@ -18,4 +19,5 @@ project(':cloud-bungee').projectDir = file('cloud-minecraft/cloud-bungee') project(':cloud-velocity').projectDir = file('cloud-minecraft/cloud-velocity') project(':cloud-minecraft-extras').projectDir = file('cloud-minecraft/cloud-minecraft-extras') project(':cloud-cloudburst').projectDir = file('cloud-minecraft/cloud-cloudburst') +project(':cloud-javacord').projectDir = file('cloud-discord/cloud-javacord') project(':cloud-jda').projectDir = file('cloud-discord/cloud-jda')