-
-
Notifications
You must be signed in to change notification settings - Fork 90
Adding context commands
This tutorial shows how to add custom context command to the bot. That is, a command that can be selected when right clicking an user or message.
Please read Add a new command first.
- add a custom user context command
- add a custom message context command
To create a command that can be selected when right clicking a user, we have to implement the UserContextCommand
interface. The class BotCommandAdapter
simplifies this process heavily.
We will create a very simple command that just greets an user:
The code is really simple:
public final class HelloUserCommand extends BotCommandAdapter implements UserContextCommand {
public HelloUserCommand() {
super(Commands.user("say-hello"), CommandVisibility.GUILD);
}
@Override
public void onUserContext(UserContextInteractionEvent event) {
event.reply("Hello " + event.getTargetMember().getAsMention()).queue();
}
}
Finally, we have to add an instance of the class to the system. We do so in the file Features.java
:
features.add(new HelloUserCommand());
To create a command that can be selected when right clicking a message, we have to implement the MessageContextCommand
interface. BotCommandAdapter
helps us out here again.
We will create a very simple command that just repeats the given message:
The code is very similar:
public final class RepeatMessageCommand extends BotCommandAdapter implements MessageContextCommand {
public RepeatMessageCommand() {
super(Commands.message("repeat"), CommandVisibility.GUILD);
}
@Override
public void onMessageContext(MessageContextInteractionEvent event) {
String content = event.getTarget().getContentRaw();
event.reply(content).queue();
}
}
And we add it to Features.java
as well:
features.add(new RepeatMessageCommand());