pingex
/
DiscordBot
Archived
1
0
Fork 0

Proper command invokation threading.

Updated dependencies
master
Pingex aka Raphaël 9 years ago
parent 47b06103c3
commit 8fce07792f

@ -5,7 +5,6 @@ version '0.1-dev'
repositories {
mavenCentral()
jcenter()
maven {
url "https://jitpack.io"
}
@ -13,8 +12,8 @@ repositories {
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile "com.github.austinv11:Discord4j:2.4.4"
compile "org.mod4j.org.apache.commons:cli:1.0.0"
compile "com.github.austinv11:Discord4j:2.4.5"
compile "org.ini4j:ini4j:0.5.4"
compile "org.slf4j:slf4j-simple:1.7.9"
compile "org.apache.commons:commons-lang3:3.0"
}

@ -1,17 +0,0 @@
package net.pingex.discordbot;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Marks a function as async, for long-running commands.
* @version 0.1-dev
* @author Raphael "Pingex" NAAS
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Async
{
}

@ -1,5 +1,6 @@
package net.pingex.discordbot;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import sx.blah.discord.api.EventSubscriber;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import sx.blah.discord.util.DiscordException;
@ -9,6 +10,7 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.*;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -37,6 +39,11 @@ public class CommandDispatcher
*/
private String commandPrefix;
/**
* Thread pool used to invoke commands
*/
private ExecutorService threadPool;
/**
* Basic Constructor, automatically rebuilds command list from the modules registry
*/
@ -44,6 +51,7 @@ public class CommandDispatcher
{
logger = Logger.getLogger(this.getClass().getName());
rebuildCommandList();
threadPool = Executors.newCachedThreadPool(new BasicThreadFactory.Builder().namingPattern("CommandDispatcher-%d").build()); // TODO: threadPool.shutdown()
if(Configuration.exists("general", "commandPrefix") && !Configuration.getValue("general", "commandPrefix").isEmpty())
commandPrefix = Configuration.getValue("general", "commandPrefix");
@ -108,39 +116,25 @@ public class CommandDispatcher
// Run command
if(commandAnswer == null)
{
if (commandList.get(fullCommand).getMethod().isAnnotationPresent(Async.class)) // Run async
{
String finalFullCommand = fullCommand;
Object[] finalParsedArray = parsedArray;
new Thread(() -> // TODO: Very hacky
{
try
{
String ans = (String) commandList.get(finalFullCommand).invoke(finalParsedArray);
if(ans != null) event.getMessage().reply(ans);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
{
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
}
}).start();
}
else // Run sync
{
String finalFullCommand = fullCommand;
Object[] finalParsedArray = parsedArray;
threadPool.submit(() -> {
try
{
String ans = (String) commandList.get(fullCommand).invoke(parsedArray);
String ans = (String) commandList.get(finalFullCommand).invoke(finalParsedArray);
if(ans != null) event.getMessage().reply(ans);
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e)
{
e.printStackTrace();
logger.severe("Couldn't call target method (" + e.getClass().getName() + "): " + e.getMessage());
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
{
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
} catch (Exception e)
{
logger.severe("Error in threaded command");
e.printStackTrace();
}
}
});
}
else // Error answer
try