Skip to content

Commit

Permalink
Add a JDA 5 module
Browse files Browse the repository at this point in the history
Eventually will drop the JDA 4 module - but it still functions so no
reason to arbitrarily upgrade it or drop without bumping acf version
  • Loading branch information
chickeneer committed Dec 14, 2024
1 parent 43323e6 commit 623048b
Show file tree
Hide file tree
Showing 23 changed files with 875 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions jda5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2016-2017 Daniel Ennis (Aikar) - MIT License
~
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>co.aikar</groupId>
<artifactId>acf-parent</artifactId>
<version><!--VERSION-->0.5.1-SNAPSHOT<!--VERSION--></version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>acf-jda5</artifactId>
<version><!--VERSION-->0.5.1-SNAPSHOT<!--VERSION--></version>

<name>ACF (JDA 5)</name>

<dependencies>
<dependency>
<groupId>co.aikar</groupId>
<artifactId>acf-core</artifactId>
<version><!--VERSION-->0.5.1-SNAPSHOT<!--VERSION--></version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>5.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<resources>
<!--resource>
<directory>${project.basedir}/../languages/minecraft/</directory>
</resource-->
</resources>
</build>
</project>
15 changes: 15 additions & 0 deletions jda5/src/main/java/co/aikar/commands/CommandConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package co.aikar.commands;

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public interface CommandConfig extends CommandConfigProvider {
@NotNull List<String> getCommandPrefixes();

@Override
default CommandConfig provide(MessageReceivedEvent event) {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package co.aikar.commands;

import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

public interface CommandConfigProvider {
CommandConfig provide(MessageReceivedEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.aikar.commands;


public interface CommandPermissionResolver {
boolean hasPermission(JDACommandManager manager, JDACommandEvent event, String permission);
}
42 changes: 42 additions & 0 deletions jda5/src/main/java/co/aikar/commands/JDACommandCompletions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package co.aikar.commands;

import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.List;

public class JDACommandCompletions extends CommandCompletions<CommandCompletionContext<?>> {
private boolean initialized;

public JDACommandCompletions(CommandManager manager) {
super(manager);
this.initialized = true;
}

@Override
public CommandCompletionHandler registerCompletion(String id, CommandCompletionHandler<CommandCompletionContext<?>> handler) {
if (initialized) {
throw new UnsupportedOperationException("JDA Doesn't support Command Completions");
}
return null;
}

@Override
public CommandCompletionHandler registerAsyncCompletion(String id, AsyncCommandCompletionHandler<CommandCompletionContext<?>> handler) {
if (initialized) {
throw new UnsupportedOperationException("JDA Doesn't support Command Completions");
}
return null;
}

@NotNull
@Override
List<String> of(RegisteredCommand command, CommandIssuer sender, String[] args, boolean isAsync) {
return Collections.emptyList();
}

@Override
List<String> getCompletionValues(RegisteredCommand command, CommandIssuer sender, String completion, String[] args, boolean isAsync) {
return Collections.emptyList();
}
}
19 changes: 19 additions & 0 deletions jda5/src/main/java/co/aikar/commands/JDACommandConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package co.aikar.commands;

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class JDACommandConfig implements CommandConfig {
protected @NotNull List<String> commandPrefixes = new CopyOnWriteArrayList<>(new String[]{"!"});

public JDACommandConfig() {

}

@NotNull
public List<String> getCommandPrefixes() {
return commandPrefixes;
}
}
115 changes: 115 additions & 0 deletions jda5/src/main/java/co/aikar/commands/JDACommandContexts.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package co.aikar.commands;

import co.aikar.commands.annotation.Author;
import co.aikar.commands.annotation.CrossGuild;
import co.aikar.commands.annotation.SelfUser;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.Role;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;

import java.util.List;

// TODO: Message Keys !!!
public class JDACommandContexts extends CommandContexts<JDACommandExecutionContext> {
private final JDACommandManager manager;
private final JDA jda;

public JDACommandContexts(JDACommandManager manager) {
super(manager);
this.manager = manager;
this.jda = this.manager.getJDA();
this.registerIssuerOnlyContext(JDACommandEvent.class, CommandExecutionContext::getIssuer);
this.registerIssuerOnlyContext(MessageReceivedEvent.class, c -> c.getIssuer().getIssuer());
this.registerIssuerOnlyContext(Message.class, c -> c.issuer.getIssuer().getMessage());
this.registerIssuerOnlyContext(ChannelType.class, c -> c.issuer.getIssuer().getChannelType());
this.registerIssuerOnlyContext(JDA.class, c -> jda);
this.registerIssuerOnlyContext(Guild.class, c -> {
MessageReceivedEvent event = c.getIssuer().getIssuer();
if (event.isFromType(ChannelType.PRIVATE) && !c.isOptional()) {
throw new InvalidCommandArgument("This command can only be executed in a Guild.", false);
} else {
return event.getGuild();
}
});
this.registerIssuerAwareContext(MessageChannel.class, c -> {
if (c.hasAnnotation(Author.class)) {
return c.issuer.getIssuer().getChannel();
}
boolean isCrossGuild = c.hasAnnotation(CrossGuild.class);
String argument = c.popFirstArg(); // we pop because we are only issuer aware if we are annotated
MessageChannel channel = null;
if (argument.startsWith("<#")) {
String id = argument.substring(2, argument.length() - 1);
channel = isCrossGuild ? jda.getTextChannelById(id) : c.issuer.getIssuer().getGuild().getTextChannelById(id);
} else {
List<TextChannel> channelList = isCrossGuild ? jda.getTextChannelsByName(argument, true) :
c.issuer.getEvent().getGuild().getTextChannelsByName(argument, true);
if (channelList.size() > 1) {
throw new InvalidCommandArgument("Too many channels were found with the given name. Try with the `#channelname` syntax.", false);
} else if (channelList.size() == 1) {
channel = channelList.get(0);
}
}
if (channel == null) {
throw new InvalidCommandArgument("Couldn't find a channel with that name or ID.");
}
return channel;
});
this.registerIssuerAwareContext(User.class, c -> {
if (c.hasAnnotation(SelfUser.class)) {
return jda.getSelfUser();
}
String arg = c.getFirstArg();
if (c.isOptional() && (arg == null || arg.isEmpty())) {
return null;
}
arg = c.popFirstArg(); // we pop because we are only issuer aware if we are annotated
User user = null;
if (arg.startsWith("<@!")) { // for some reason a ! is added when @'ing and clicking their name.
user = jda.getUserById(arg.substring(3, arg.length() - 1));
} else if (arg.startsWith("<@")) { // users can /also/ be mentioned like this...
user = jda.getUserById(arg.substring(2, arg.length() - 1));
} else {
List<User> users = jda.getUsersByName(arg, true);
if (users.size() > 1) {
throw new InvalidCommandArgument("Too many users were found with the given name. Try with the `@username#0000` syntax.", false);
}
if (!users.isEmpty()) {
user = users.get(0);
}
}
if (user == null) {
throw new InvalidCommandArgument("Could not find a user with that name or ID.");
}
return user;
});
this.registerContext(Role.class, c -> {
boolean isCrossGuild = c.hasAnnotation(CrossGuild.class);
String arg = c.popFirstArg();
Role role = null;
if (arg.startsWith("<@&")) {
String id = arg.substring(3, arg.length() - 1);
role = isCrossGuild ? jda.getRoleById(id) : c.issuer.getIssuer().getGuild().getRoleById(id);
} else {
List<Role> roles = isCrossGuild ? jda.getRolesByName(arg, true)
: c.issuer.getIssuer().getGuild().getRolesByName(arg, true);
if (roles.size() > 1) {
throw new InvalidCommandArgument("Too many roles were found with the given name. Try with the `@role` syntax.", false);
}
if (!roles.isEmpty()) {
role = roles.get(0);
}
}
if (role == null) {
throw new InvalidCommandArgument("Could not find a role with that name or ID.");
}
return role;
});
}
}
67 changes: 67 additions & 0 deletions jda5/src/main/java/co/aikar/commands/JDACommandEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package co.aikar.commands;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.utils.messages.MessageCreateData;
import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public class JDACommandEvent implements CommandIssuer {

private MessageReceivedEvent event;
private JDACommandManager manager;

public JDACommandEvent(JDACommandManager manager, MessageReceivedEvent event) {

this.manager = manager;
this.event = event;
}

public MessageReceivedEvent getEvent() {
return event;
}

@Override
public MessageReceivedEvent getIssuer() {
return event;
}

@Override
public CommandManager getManager() {
return this.manager;
}

@Override
public boolean isPlayer() {
return false;
}

@Override
public @NotNull UUID getUniqueId() {
// Discord id only have 64 bit width (long) while UUIDs have twice the size.
// In order to keep it unique we use 0L for the first 64 bit.
long authorId = event.getAuthor().getIdLong();
return new UUID(0, authorId);
}

@Override
public boolean hasPermission(String permission) {
CommandPermissionResolver permissionResolver = this.manager.getPermissionResolver();
return permissionResolver == null || permissionResolver.hasPermission(manager, this, permission);
}

@Override
public void sendMessageInternal(String message) {
this.event.getChannel().sendMessage(message).queue();
}

public void sendMessage(Message message) {
this.event.getChannel().sendMessage(MessageCreateData.fromMessage(message)).queue();
}

public void sendMessage(MessageEmbed message) {
this.event.getChannel().sendMessage(MessageCreateData.fromEmbeds(message)).queue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package co.aikar.commands;

import java.util.List;
import java.util.Map;

public class JDACommandExecutionContext extends CommandExecutionContext<JDACommandExecutionContext, JDACommandEvent> {
JDACommandExecutionContext(RegisteredCommand cmd, CommandParameter param, JDACommandEvent sender, List<String> args, int index, Map<String, Object> passedArgs) {
super(cmd, param, sender, args, index, passedArgs);
}
}
Loading

0 comments on commit 623048b

Please sign in to comment.