pingex
/
DiscordBot
Archived
1
0
Fork 0

Converted command storage type from HashMap to TreeMap, so commands are sorted.

Now prints stacktrace when a command methods fails.
getCommandList() and getShortList() are no longer cloned.
master
Pingex aka Raphaël 9 years ago
parent 47ca96889c
commit c8054c0a1b

@ -9,7 +9,8 @@ import sx.blah.discord.util.MissingPermissionsException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.*; import java.util.concurrent.*;
import java.util.logging.Logger; import java.util.logging.Logger;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -32,12 +33,12 @@ class CommandDispatcher
/** /**
* Contains all available commands, built using `rebuildCommandList()` * Contains all available commands, built using `rebuildCommandList()`
*/ */
private HashMap<String, InvokableMethod> commandList; private Map<String, InvokableMethod> commandList;
/** /**
* Contains all the shorthanded commands * Contains all the shorthanded commands
*/ */
private HashMap<String, String> shortList; private Map<String, String> shortList;
/** /**
* Thread pool used to invoke commands * Thread pool used to invoke commands
@ -147,9 +148,13 @@ class CommandDispatcher
{ {
String ans = (String) commandList.get(finalFullCommand).invoke(finalParsedArray); String ans = (String) commandList.get(finalFullCommand).invoke(finalParsedArray);
if(ans != null) event.getMessage().getChannel().sendMessage("```\n" + ans + "\n```"); if(ans != null) event.getMessage().getChannel().sendMessage("```\n" + ans + "\n```");
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) } catch (IllegalArgumentException | IllegalAccessException e)
{ {
logger.severe("Couldn't call target method (" + e.getClass().getName() + "): " + e.getMessage()); logger.severe("Couldn't call target method (" + e.getClass().getName() + "): " + e.getMessage());
} catch (InvocationTargetException e)
{
logger.severe("An error occurred inside the target command: " + e.getCause().getMessage());
e.getCause().printStackTrace();
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e) } catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
{ {
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage()); logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
@ -183,8 +188,8 @@ class CommandDispatcher
logger.info("Rebuilding command list..."); logger.info("Rebuilding command list...");
ArrayList<AbstractModule> registry = ModulesRegistry.getRegistry(); ArrayList<AbstractModule> registry = ModulesRegistry.getRegistry();
commandList = new HashMap<>(); commandList = new TreeMap<>();
shortList = new HashMap<>(); shortList = new TreeMap<>();
for(AbstractModule i : registry) for(AbstractModule i : registry)
if(i.getClass().isAnnotationPresent(Controllable.class)) if(i.getClass().isAnnotationPresent(Controllable.class))
@ -237,22 +242,20 @@ class CommandDispatcher
} }
/** /**
* Gets the command list (cloned to avoid modifications) * Gets the command list
* @return The current cloned command list * @return The current command list
*/ */
@SuppressWarnings("unchecked") public Map<String, InvokableMethod> getCommandList()
public HashMap<String, InvokableMethod> getCommandList()
{ {
return (HashMap<String, InvokableMethod>) commandList.clone(); return commandList;
} }
/** /**
* Gets the shorthands list (cloned to avoid edits) * Gets the shorthands list
* @return The current cloned short list * @return The current short list
*/ */
@SuppressWarnings("unchecked") public Map<String, String> getShortList()
public HashMap<String, String> getShortList()
{ {
return (HashMap<String, String>) shortList.clone(); return shortList;
} }
} }