Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Commit

Permalink
Merge pull request #41 from Bastian/development
Browse files Browse the repository at this point in the history
Merge development branch
  • Loading branch information
Bastian authored Aug 6, 2018
2 parents 9534b3a + 8ecd4f5 commit b4a370d
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 69 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# sdcf4j <a href="#"><img src="https://img.shields.io/github/release/BtoBastian/sdcf4j.svg?colorB=brightgreen&label=Version" alt="Latest version"></a> <a href="http://ci.ketrwu.de/job/sdcf4j/job/master/javadoc/"><img src="https://img.shields.io/badge/JavaDoc-latest-yellow.svg" alt="Latest JavaDocs"></a> <a href="https://github.com/BtoBastian/sdcf4j/wiki"><img src="https://img.shields.io/badge/Wiki-Home-red.svg" alt="Latest JavaDocs"></a>
# sdcf4j <a href="#"><img src="https://shields.javacord.org/github/release/BtoBastian/sdcf4j.svg?colorB=brightgreen&label=Version" alt="Latest version"></a> <a href="http://ci.ketrwu.de/job/sdcf4j/job/master/javadoc/"><img src="https://shields.javacord.org/badge/JavaDoc-latest-yellow.svg" alt="Latest JavaDocs"></a> <a href="https://github.com/BtoBastian/sdcf4j/wiki"><img src="https://shields.javacord.org/badge/Wiki-Home-red.svg" alt="Latest JavaDocs"></a>

Sdcf4j is a **s**imple **D**iscord **c**ommand **f**ramework **for** **J**ava, supporting [Javacord](https://github.com/BtoBastian/Javacord), [JDA](https://github.com/DV8FromTheWorld/JDA) and [Discord4J](https://github.com/austinv11/Discord4J). It helps you creating commands within seconds in a clean and simple way.

Expand Down Expand Up @@ -33,7 +33,7 @@ public String onPingCommand() {
```
Make sure to replace `%version%` with the latest version number, e.g. `v1.0.0` (don't use this one!).

Latest version: <a href="#"><img src="https://img.shields.io/github/release/BtoBastian/sdcf4j.svg?colorB=brightgreen&label=Version" alt="Latest version"></a>
Latest version: <a href="#"><img src="https://shields.javacord.org/github/release/BtoBastian/sdcf4j.svg?colorB=brightgreen&label=Version" alt="Latest version"></a>
## Support

* [Javacord server](https://discord.gg/0qJ2jjyneLEgG7y3)
Expand All @@ -47,7 +47,7 @@ For those of you how don't use maven: [Jenkins](http://ci.ketrwu.de/job/sdcf4j/b
Thanks to ketrwu (https://github.com/KennethWussmann).

## Javadocs
The javadocs can be found here: [JavaDocs](http://ci.ketrwu.de/job/sdcf4j/branch/master/de.btobastian.sdcf4j$sdcf4j-core/javadoc/)
The javadocs can be found here: [JavaDocs](http://ci.ketrwu.de/job/sdcf4j/job/master/javadoc/)

Thanks to ketrwu, too.

Expand Down
16 changes: 16 additions & 0 deletions sdcf4j-core/src/main/java/de/btobastian/sdcf4j/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

/**
* The basic command handler.
Expand All @@ -35,6 +36,21 @@ public abstract class CommandHandler {

protected String defaultPrefix = "";

// From Javacord's DiscordRegexPattern
protected static final Pattern USER_MENTION =
Pattern.compile("(?x) # enable comment mode \n"
+ "(?<! # negative lookbehind \n"
+ " # (do not have uneven amount of backslashes before) \n"
+ " (?<!\\\\) # negative lookbehind (do not have one backslash before) \n"
+ " (?:\\\\{2}+) # exactly two backslashes \n"
+ " {0,1000000000}+ # 0 to 1_000_000_000 times \n"
+ " # (basically *, but a lookbehind has to have a maximum length) \n"
+ " \\\\ # the one escaping backslash \n"
+ ") # \n"
+ "<@!?+ # '<@' or '<@!' \n"
+ "(?<id>[0-9]++) # the user id as named group \n"
+ "> # '>'");

/**
* Registers an executor.
*
Expand Down
15 changes: 3 additions & 12 deletions sdcf4j-discord4j/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@
</plugins>
</build>

<repositories>
<!-- Discord4J uses jitpack -->
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<!-- The core module -->
<dependency>
Expand All @@ -76,10 +68,9 @@
</dependency>
<!-- Discord4J -->
<dependency>
<groupId>com.github.austinv11</groupId>
<artifactId>Discord4j</artifactId>
<version>2.9.3</version>
<scope>provided</scope>
<groupId>com.discord4j</groupId>
<artifactId>Discord4J</artifactId>
<version>2.10.1</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.regex.Matcher;

/**
* A command handler for the Discord4J library.
Expand All @@ -48,12 +49,7 @@ public class Discord4JHandler extends CommandHandler {
* @param client The discord client.
*/
public Discord4JHandler(IDiscordClient client) {
client.getDispatcher().registerListener(new IListener<MessageReceivedEvent> () {
@Override
public void handle(MessageReceivedEvent event) {
handleMessageCreate(event);
}
});
client.getDispatcher().registerListener((IListener<MessageReceivedEvent>) this::handleMessageCreate);
}

/**
Expand Down Expand Up @@ -100,9 +96,11 @@ private void handleMessageCreate(final MessageReceivedEvent event) {
}
}
Command commandAnnotation = command.getCommandAnnotation();
if (commandAnnotation.requiresMention() &&
!commandString.equals("<@" + event.getClient().getOurUser().getStringID() + ">")) {
return;
if (commandAnnotation.requiresMention()) {
Matcher matcher = USER_MENTION.matcher(commandString);
if (!matcher.find() || !matcher.group("id").equals(event.getClient().getOurUser().getStringID())) {
return;
}
}
if (event.getMessage().getChannel().isPrivate() && !commandAnnotation.privateMessages()) {
return;
Expand Down Expand Up @@ -180,7 +178,9 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
}
} else if (type == String[].class) {
parameters[i] = args;
} else if (type == IMessage.class) {
} else if (type == MessageReceivedEvent.class) {
parameters[i] = event;
} else if (type == IMessage.class) {
parameters[i] = event.getMessage();
} else if (type == IDiscordClient.class) {
parameters[i] = event.getClient();
Expand All @@ -201,7 +201,7 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
}

/**
* Tries to get objects (like channel, user, integer) from the given strings.
* Tries to get objects (like channel, user, long) from the given strings.
*
* @param client The client.
* @param args The string array.
Expand All @@ -216,7 +216,7 @@ private Object[] getObjectsFromString(IDiscordClient client, String[] args) {
}

/**
* Tries to get an object (like channel, user, integer) from the given string.
* Tries to get an object (like channel, user, long) from the given string.
*
* @param client The client.
* @param arg The string.
Expand All @@ -228,8 +228,9 @@ private Object getObjectFromString(IDiscordClient client, String arg) {
return Long.valueOf(arg);
} catch (NumberFormatException ignored) {}
// test user
if (arg.matches("<@([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
Matcher matcher = USER_MENTION.matcher(arg);
if (matcher.find()) {
String id = matcher.group("id");
IUser user = client.getUserByID(Long.valueOf(id));
if (user != null) {
return user;
Expand Down
19 changes: 9 additions & 10 deletions sdcf4j-javacord/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@
</plugins>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<!-- The core module -->
<dependency>
Expand All @@ -75,9 +68,15 @@
</dependency>
<!-- Javacord -->
<dependency>
<groupId>de.btobastian</groupId>
<artifactId>Javacord</artifactId>
<version>8ded4aa</version>
<groupId>org.javacord</groupId>
<artifactId>javacord-api</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.javacord</groupId>
<artifactId>javacord-core</artifactId>
<version>3.0.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,27 @@
*/
package de.btobastian.sdcf4j.handler;

import de.btobastian.javacord.DiscordApi;
import de.btobastian.javacord.entities.Server;
import de.btobastian.javacord.entities.User;
import de.btobastian.javacord.entities.channels.*;
import de.btobastian.javacord.entities.message.Message;
import de.btobastian.javacord.entities.message.MessageAuthor;
import de.btobastian.javacord.utils.logging.LoggerUtil;
import de.btobastian.sdcf4j.Command;
import de.btobastian.sdcf4j.CommandHandler;
import de.btobastian.sdcf4j.Sdcf4jMessage;
import org.slf4j.Logger;
import org.apache.logging.log4j.Logger;
import org.javacord.api.DiscordApi;
import org.javacord.api.entity.channel.Channel;
import org.javacord.api.entity.channel.GroupChannel;
import org.javacord.api.entity.channel.PrivateChannel;
import org.javacord.api.entity.channel.ServerChannel;
import org.javacord.api.entity.channel.ServerTextChannel;
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.entity.server.Server;
import org.javacord.api.entity.user.User;
import org.javacord.api.event.message.MessageCreateEvent;
import org.javacord.core.util.logging.LoggerUtil;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.regex.Matcher;

/**
* A command handler for the Javacord library.
Expand All @@ -49,7 +56,7 @@ public class JavacordHandler extends CommandHandler {
* @param api The api.
*/
public JavacordHandler(DiscordApi api) {
api.addMessageCreateListener(event -> handleMessageCreate(api, event.getMessage()));
api.addMessageCreateListener(event -> handleMessageCreate(api, event));
}

/**
Expand Down Expand Up @@ -77,9 +84,10 @@ public boolean hasPermission(User user, String permission) {
* Handles a received message.
*
* @param api The api.
* @param message The received message.
* @param event The received event.
*/
private void handleMessageCreate(DiscordApi api, final Message message) {
private void handleMessageCreate(DiscordApi api, final MessageCreateEvent event) {
Message message = event.getMessage();
if (message.getUserAuthor().map(User::isYourself).orElse(false)) {
return;
}
Expand All @@ -100,8 +108,11 @@ private void handleMessageCreate(DiscordApi api, final Message message) {
}
}
Command commandAnnotation = command.getCommandAnnotation();
if (commandAnnotation.requiresMention() && !commandString.equals(api.getYourself().getMentionTag())) {
return;
if (commandAnnotation.requiresMention()) {
Matcher matcher = USER_MENTION.matcher(commandString);
if (!matcher.find() || !matcher.group("id").equals(api.getYourself().getIdAsString())) {
return;
}
}
if (message.getPrivateChannel().isPresent() && !commandAnnotation.privateMessages()) {
return;
Expand All @@ -115,7 +126,7 @@ private void handleMessageCreate(DiscordApi api, final Message message) {
}
return;
}
final Object[] parameters = getParameters(splitMessage, command, message, api);
final Object[] parameters = getParameters(splitMessage, command, event, api);
if (commandAnnotation.async()) {
final SimpleCommand commandFinal = command;
api.getThreadPool().getExecutorService().submit(() -> invokeMethod(commandFinal, message, parameters));
Expand Down Expand Up @@ -150,11 +161,12 @@ private void invokeMethod(SimpleCommand command, Message message, Object[] param
*
* @param splitMessage The spit message (index 0: command, index > 0: arguments)
* @param command The command.
* @param message The original message.
* @param event The received event.
* @param api The api.
* @return The parameters which are used to invoke the executor's method.
*/
private Object[] getParameters(String[] splitMessage, SimpleCommand command, Message message, DiscordApi api) {
private Object[] getParameters(String[] splitMessage, SimpleCommand command, MessageCreateEvent event, DiscordApi api) {
Message message = event.getMessage();
String[] args = Arrays.copyOfRange(splitMessage, 1, splitMessage.length);
Class<?>[] parameterTypes = command.getMethod().getParameterTypes();
final Object[] parameters = new Object[parameterTypes.length];
Expand All @@ -172,6 +184,8 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
}
} else if (type == String[].class) {
parameters[i] = args;
} else if (type == MessageCreateEvent.class) {
parameters[i] = event;
} else if (type == Message.class) {
parameters[i] = message;
} else if (type == DiscordApi.class) {
Expand Down Expand Up @@ -205,7 +219,7 @@ private Object[] getParameters(String[] splitMessage, SimpleCommand command, Mes
}

/**
* Tries to get objects (like channel, user, integer) from the given strings.
* Tries to get objects (like channel, user, long) from the given strings.
*
* @param api The api.
* @param args The string array.
Expand All @@ -220,7 +234,7 @@ private Object[] getObjectsFromString(DiscordApi api, String[] args) {
}

/**
* Tries to get an object (like channel, user, integer) from the given string.
* Tries to get an object (like channel, user, long) from the given string.
*
* @param api The api.
* @param arg The string.
Expand All @@ -232,9 +246,10 @@ private Object getObjectFromString(DiscordApi api, String arg) {
return Long.valueOf(arg);
} catch (NumberFormatException ignored) {}
// test user
if (arg.matches("<@([0-9]*)>")) {
String id = arg.substring(2, arg.length() - 1);
User user = api.getUserById(id).orElse(null);
Matcher matcher = USER_MENTION.matcher(arg);
if (matcher.find()) {
String id = matcher.group("id");
User user = api.getCachedUserById(id).orElse(null);
if (user != null) {
return user;
}
Expand Down
2 changes: 1 addition & 1 deletion sdcf4j-jda3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>3.5.0_327</version>
<version>3.7.1_385</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Loading

0 comments on commit b4a370d

Please sign in to comment.