-
Notifications
You must be signed in to change notification settings - Fork 0
CommandListener
CommandListener is an extension (i.e. subclass) of ExtendedListener with support for commands. All modules with commands extend CommandListener and benefit from the same improvements made by ExtendedListener.
There are 3 types of commands corresponding to their default permission level: Generic, Admin, and Owner. (See Permissions for more details.) Additionally, commands may be restricted to guilds (i.e. servers), if necessary. Each CommandListener may create new commands using one of the following methods:
Creates a new Generic command. By default, all users can run this command, even in DMs.
Creates a new Admin command. By default, only members of permission groups with Admin privileges can run this command, even in DMs.
Same as newAdminCommand, but restricted to guilds.
Creates a new Owner command. By default, only members of permission groups with Owner privileges can run this command, even in DMs.
Each method takes two arguments: a one-word name preceded by a prefix and a Consumer written in lambda form pointing to the method to run. E.g. this.newCommand("help", event -> help(event));
Every time a command is received, the CommandListener will evaluate if it matches one of its commands. If this is the case, it will check if the user has permission to run this command, and if the command is sent in a guild (if set to be guild-only). If either check fails, the appropriate error message will be relayed.
If all requirements to run the command are met, the corresponding JDA event (either SlashCommandInteractionEvent or MessageReceivedEvent) will be wrapped into a new CommandEvent and passed to the Consumer. The CommandEvent is a wrapper object for the Command object and the JDA event.
Exceptions thrown while running Commands are caught by the CommandListener. For most exception types, the exception is relayed to the user, and the error message either pings all bot owners (if in a guild) or instructs the user to contact one of the bot owners (if in DMs). The exception is rethrown and further handled by ExtendedListener to inform the owners of the error.
CommandExceptions are thrown as intentional error messages for the user, and so are handled differently. Instead, the accompanying message is sent in response to the user, and the owners are not notified of the exception. CommandExceptions are most useful in common utility methods used by multiple commands, such as parsing time or finding a user, role, or channel.