|
|
|
@ -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
|
|
|
|
|