✨ Add Javacord Support (#23)
Co-authored-by: Alexander Söderberg <Sauilitired@users.noreply.github.com>
This commit is contained in:
parent
212145cc6b
commit
72e578ff22
10 changed files with 628 additions and 0 deletions
4
cloud-discord/cloud-javacord/build.gradle
Normal file
4
cloud-discord/cloud-javacord/build.gradle
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
dependencies {
|
||||
api project(':cloud-core')
|
||||
implementation 'org.javacord:javacord:3.1.1'
|
||||
}
|
||||
|
|
@ -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<C> 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<C> manager;
|
||||
private final CommandArgument<C, ?> command;
|
||||
private final cloud.commandframework.Command<C> cloudCommand;
|
||||
|
||||
JavacordCommand(final cloud.commandframework.@NonNull Command<C> cloudCommand,
|
||||
@NonNull final CommandArgument<C, ?> command,
|
||||
@NonNull final JavacordCommandManager<C> 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<C>) 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();
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
@ -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<C> extends CommandManager<C> {
|
||||
|
||||
private final DiscordApi discordApi;
|
||||
private final Function<JavacordCommandSender, C> commandSenderMapper;
|
||||
private final Function<C, JavacordCommandSender> backwardsCommandSenderMapper;
|
||||
|
||||
private final Function<C, String> commandPrefixMapper;
|
||||
private final Function<C, Boolean> 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<C>,
|
||||
@NonNull CommandExecutionCoordinator<C>> 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<C>) 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<C> implements CommandRegistrationHandler {
|
||||
|
||||
private final Map<CommandArgument<?, ?>, JavacordCommand<C>> registeredCommands = new HashMap<>();
|
||||
|
||||
private JavacordCommandManager<C> javacordCommandManager;
|
||||
|
||||
JavacordRegistrationHandler() {
|
||||
}
|
||||
|
||||
void initialize(@NonNull final JavacordCommandManager<C> 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<C> javacordCommand = new JavacordCommand<>(
|
||||
(Command<C>) command,
|
||||
(CommandArgument<C, ?>) commandArgument,
|
||||
this.javacordCommandManager);
|
||||
this.registeredCommands.put(commandArgument, javacordCommand);
|
||||
this.javacordCommandManager.getDiscordApi().addMessageCreateListener(javacordCommand);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
@ -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<Message> 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<Message> 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<Message> sendSuccessMessage(final @Nullable String message) {
|
||||
return sendMessage(":white_check_mark: " + message);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
@ -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"));
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
Loading…
Add table
Add a link
Reference in a new issue