use of de.nikos410.discordbot.framework.CommandWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method discoverCommands.
private List<CommandWrapper> discoverCommands(final ModuleWrapper moduleWrapper) {
LOG.debug("Registering command(s) for module '{}'.", moduleWrapper.getName());
final List<CommandWrapper> commands = new LinkedList<>();
final Method[] allMethods = moduleWrapper.getModuleClass().getMethods();
final List<Method> commandMethods = Arrays.stream(allMethods).filter(module -> module.isAnnotationPresent(CommandSubscriber.class)).collect(Collectors.toList());
for (final Method method : commandMethods) {
// Register methods with the @CommandSubscriber as commands
// All annotations of type CommandSubscriber declared for that Method. Should be exactly 1
final CommandSubscriber annotation = method.getDeclaredAnnotationsByType(CommandSubscriber.class)[0];
// Get command properties from annotation
final String commandName = annotation.command();
final String commandHelp = annotation.help();
final boolean pmAllowed = annotation.pmAllowed();
final PermissionLevel permissionLevel = annotation.permissionLevel();
final boolean passContext = annotation.passContext();
final boolean ignoreParameterCount = annotation.ignoreParameterCount();
final int parameterCount = method.getParameterCount() - 1;
if ((parameterCount >= 0 && parameterCount <= 5) || ignoreParameterCount) {
final CommandWrapper commandWrapper = new CommandWrapper(commandName, commandHelp, moduleWrapper, method, pmAllowed, permissionLevel, parameterCount, passContext, ignoreParameterCount);
commands.add(commandWrapper);
LOG.debug("Saved command '{}'.", commandName);
} else {
LOG.warn("Method '{}' has an invalid number of arguments. Skipping", commandName);
}
}
return commands;
}
use of de.nikos410.discordbot.framework.CommandWrapper in project de-DiscordBot by DACH-Discord.
the class BotSetup method command_help.
@CommandSubscriber(command = "help", help = "Zeigt diese Hilfe an")
public void command_help(final IMessage message) {
final EmbedBuilder helpEmbedBuilder = new EmbedBuilder();
final List<ModuleWrapper> loadedModules = bot.getLoadedModules();
for (ModuleWrapper module : loadedModules) {
final StringBuilder moduleHelpBuilder = new StringBuilder();
for (CommandWrapper command : module.getCommands()) {
// Only list commands that are available to that user
if (bot.getUserPermissionLevel(message.getAuthor(), message.getGuild()).getLevel() >= command.getPermissionLevel().getLevel()) {
moduleHelpBuilder.append(String.format("`%s` - %s%n", command.getName(), command.getHelp()));
}
}
final String helpString = moduleHelpBuilder.toString();
if (moduleHelpBuilder.length() > 0) {
helpEmbedBuilder.appendField(module.getDisplayName(), helpString, false);
}
}
final EmbedObject embedObject = helpEmbedBuilder.build();
DiscordIO.sendEmbed(message.getAuthor().getOrCreatePMChannel(), embedObject);
if (!message.getChannel().isPrivate()) {
DiscordIO.sendMessage(message.getChannel(), ":mailbox_with_mail:");
}
}
use of de.nikos410.discordbot.framework.CommandWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method makeCommandMap.
/**
* Populates global command map, maps a 'CommandWrapper' instance, containing a commands attributes, to each command.
*/
private void makeCommandMap() {
LOG.debug("Creating command map.");
LOG.debug("Clearing old commands.");
this.commands.clear();
final List<ModuleWrapper> loadedModules = getLoadedModules();
for (final ModuleWrapper moduleWrapper : loadedModules) {
for (final CommandWrapper commandWrapper : moduleWrapper.getCommands()) {
this.commands.put(commandWrapper.getName().toLowerCase(), commandWrapper);
}
}
}
use of de.nikos410.discordbot.framework.CommandWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method handleMessage.
/**
* Process a received or edited message. Check if it contains a command and execute the corresponding method.
*
* @param message The received/edited message
*/
private void handleMessage(final IMessage message) {
final String messageContent = message.getContent();
// Check if the message starts with the configured prefix
if (!messageContent.startsWith(this.prefix)) {
return;
}
// Get only the command in lower case without prefix/parameters
final String commandName = (messageContent.contains(" ") ? // Message contains parameters
messageContent.substring(this.prefix.length(), messageContent.indexOf(' ')) : // Message doesn't contain parameters
messageContent.substring(this.prefix.length())).toLowerCase();
// Check if a command with that name is known
if (!commands.containsKey(commandName)) {
return;
}
final CommandWrapper command = commands.get(commandName);
LOG.info("User {} used command {}", UserUtils.makeUserString(message.getAuthor(), message.getGuild()), commandName);
// The command was received in a PM but is only available on guilds
if (message.getChannel().isPrivate() && !command.isPmAllowed()) {
DiscordIO.sendMessage(message.getChannel(), "Dieser Befehl ist nicht in Privatnachrichten verfügbar!");
LOG.info("CommandWrapper {} is not available in PMs.", commandName);
return;
}
// Check if the user is allowed to use that command
final PermissionLevel userPermissionLevel = this.getUserPermissionLevel(message.getAuthor(), message.getGuild());
LOG.debug("Checking permissions. User: {} | Required: {}", userPermissionLevel, command.getPermissionLevel());
if (userPermissionLevel.getLevel() < command.getPermissionLevel().getLevel()) {
DiscordIO.sendMessage(message.getChannel(), String.format("Dieser Befehl ist für deine Gruppe (%s) nicht verfügbar.", userPermissionLevel.getName()));
LOG.info("User {} doesn't have the required permissions for using the command {}.", UserUtils.makeUserString(message.getAuthor(), message.getGuild()), commandName);
return;
}
final int expectedParameterCount = command.getExpectedParameterCount();
final List<String> parameters = parseParameters(messageContent, commandName, expectedParameterCount, command.isPassContext());
// Check if the user used the correct number of parameters
if (parameters.size() < expectedParameterCount) {
if (command.isIgnoreParameterCount()) {
while (parameters.size() < expectedParameterCount) {
parameters.add(null);
}
} else {
DiscordIO.sendMessage(message.getChannel(), String.format("Dieser Befehl benötigt mindestens %s Parameter! (Gegeben: %s)", expectedParameterCount, parameters.size()));
LOG.info("Wrong number of arguments. Expected number: {} Actual number: {}", expectedParameterCount, parameters.size());
return;
}
}
executeCommand(command, parameters, message);
}
use of de.nikos410.discordbot.framework.CommandWrapper in project de-DiscordBot by DACH-Discord.
the class DiscordBot method executeCommand.
/**
* Invoke the method specified in a CommandWrapper with the parameters in a List
*
* @param command The instance containing the attributes of the command, specifically the method to invoke
* @param parameters The parameters to use while invoking the method
* @param message The message that triggered the command
*/
private void executeCommand(final CommandWrapper command, final List<String> parameters, final IMessage message) {
LOG.debug("Executing command {} with {} parameters.", command.getName(), parameters.size());
final Method method = command.getMethod();
final CommandModule instance = command.getModule().getInstance();
try {
switch(parameters.size()) {
case 0:
method.invoke(instance, message);
break;
case 1:
method.invoke(instance, message, parameters.get(0));
break;
case 2:
method.invoke(instance, message, parameters.get(0), parameters.get(1));
break;
case 3:
method.invoke(instance, message, parameters.get(0), parameters.get(1), parameters.get(2));
break;
case 4:
method.invoke(instance, message, parameters.get(0), parameters.get(1), parameters.get(2), parameters.get(3));
break;
case 5:
method.invoke(instance, message, parameters.get(0), parameters.get(1), parameters.get(2), parameters.get(3), parameters.get(4));
break;
default:
throw new IllegalArgumentException("Command has an invalid number of arguments. This should never happen.");
}
} catch (IllegalAccessException | InvocationTargetException e) {
final Throwable cause = e.getCause();
LOG.error("Command '{}' could not be executed.", command.getName(), e.getCause());
DiscordIO.errorNotify(cause.toString(), message.getChannel());
}
}
Aggregations