pingex
/
DiscordBot
Archived
1
0
Fork 0

Quick and dirty async commands.

master
Pingex aka Raphaël 9 years ago
parent b21e6c9647
commit 47b06103c3

@ -0,0 +1,17 @@
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
{
}

@ -75,43 +75,81 @@ public class CommandDispatcher
if(!fullCommand.contains(":") && shortList.containsKey(fullCommand))
fullCommand = shortList.get(fullCommand);
try
String commandAnswer = null;
Object[] parsedArray = null;
// Conditions
if(commandList.containsKey(fullCommand))
{
if(commandList.containsKey(fullCommand))
{
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand);
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand);
InvokableMethod foundMethod = commandList.get(fullCommand);
Object[] parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
parsedArray[0] = event;
if(foundMethod.getMethod().getParameterCount()-1 != args.size())
{
event.getMessage().reply("Invalid arguments. " + getHelp(fullCommand));
return;
}
InvokableMethod foundMethod = commandList.get(fullCommand);
parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
parsedArray[0] = event;
if(foundMethod.getMethod().getParameterCount()-1 == args.size())
{
for(int i=1; i < foundMethod.getMethod().getParameterCount(); i++)
try
{
parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args.get(i-1));
} catch (IllegalArgumentException e)
{
event.getMessage().reply("Failed to parse arguments, are they correct ? " + getHelp(fullCommand));
commandAnswer = "Failed to parse arguments, are they correct ? " + getHelp(fullCommand);
break;
}
String answer = (String) commandList.get(fullCommand).invoke(parsedArray);
if(answer != null) event.getMessage().reply(answer);
}
else
event.getMessage().reply("Unknown command");
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
{
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e)
commandAnswer = "Invalid arguments. " + getHelp(fullCommand);
}
else
commandAnswer = "Unknown command";
// Run command
if(commandAnswer == null)
{
logger.severe("Couldn't call target method (" + e.getClass().getName() + "): " + e.getMessage());
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
{
try
{
String ans = (String) commandList.get(fullCommand).invoke(parsedArray);
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());
}
}
}
else // Error answer
try
{
event.getMessage().reply(commandAnswer);
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
{
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
}
}
/**