Repository navigation
CommandHandler #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
CommandHandler #30
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/org/togetherjava/tjbot/commands/AbstractCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,53 @@ | ||||||||||||||
| package org.togetherjava.tjbot.commands; | ||||||||||||||
|
|
||||||||||||||
| import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||||||||||||||
| import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * The class all commands have to abstract. | ||||||||||||||
| * Look at {@link org.togetherjava.tjbot.commands.example.ExampleCommand} for an example. | ||||||||||||||
| * A second example will come for sub-commands. | ||||||||||||||
| */ | ||||||||||||||
| public abstract class AbstractCommand { | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * The commands name. | ||||||||||||||
| * @return | ||||||||||||||
| * The commands name as a {@link String} | ||||||||||||||
| */ | ||||||||||||||
| public abstract String getCommandName(); | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * The commands description. | ||||||||||||||
| * @return | ||||||||||||||
| * The commands description as a {@link String} | ||||||||||||||
| */ | ||||||||||||||
| public abstract String getDescription(); | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * Whenever the command is only for guilds, optional method. | ||||||||||||||
| * @return | ||||||||||||||
| * Whenever the command is only for guilds as a {@link Boolean} | ||||||||||||||
| */ | ||||||||||||||
| public boolean isGuildOnly() { | ||||||||||||||
| return false; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+32
to
+34
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * This command will add all the options to the slash-command. | ||||||||||||||
| * Look at {@link org.togetherjava.tjbot.commands.example.ExampleCommand#addOptions(CommandCreateAction)} for an example. | ||||||||||||||
| * @param commandCreateAction | ||||||||||||||
| * The {@link CommandCreateAction} where the options need to be added. | ||||||||||||||
| * @return | ||||||||||||||
| * The changed {@link CommandCreateAction} | ||||||||||||||
| */ | ||||||||||||||
| public abstract CommandCreateAction addOptions(CommandCreateAction commandCreateAction); | ||||||||||||||
|
|
||||||||||||||
| /** | ||||||||||||||
| * The execute method for when the command gets executed. | ||||||||||||||
| * Check <a href="https://github.com/DV8FromTheWorld/JDA/wiki/Interactions#slash-commands">JDA's slash-commands Wiki article</a> for more information. | ||||||||||||||
| * @param event | ||||||||||||||
| * The relating {@link SlashCommandEvent} | ||||||||||||||
| */ | ||||||||||||||
| public abstract void execute(SlashCommandEvent event); | ||||||||||||||
| } | ||||||||||||||
83 changes: 83 additions & 0 deletions
83
src/main/java/org/togetherjava/tjbot/commands/example/ExampleCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package org.togetherjava.tjbot.commands.example; | ||
|
|
||
| import net.dv8tion.jda.api.entities.User; | ||
| import net.dv8tion.jda.api.events.interaction.SlashCommandEvent; | ||
| import net.dv8tion.jda.api.interactions.commands.Command; | ||
| import net.dv8tion.jda.api.interactions.commands.OptionType; | ||
| import net.dv8tion.jda.api.interactions.commands.build.OptionData; | ||
| import net.dv8tion.jda.api.requests.restaction.CommandCreateAction; | ||
| import org.togetherjava.tjbot.commands.AbstractCommand; | ||
|
|
||
| /** | ||
| * Example command | ||
| */ | ||
| public class ExampleCommand extends AbstractCommand { | ||
|
|
||
| /** | ||
| * The command name is example, so it returns example. | ||
| * @return | ||
| * "Example" as a {@link String} | ||
| */ | ||
| @Override | ||
| public String getCommandName() { | ||
| return "example"; | ||
| } | ||
|
|
||
| /** | ||
| * The description | ||
| * @return | ||
| * "Example command! Allows you to spam ping someone!" as a {@link String} | ||
| */ | ||
| @Override | ||
| public String getDescription() { | ||
| return "Example command! Allows you to spam ping someone!"; | ||
| } | ||
|
|
||
| /** | ||
| * You can make commands guild only by overriding this method. | ||
| * You can only ping people in a guild, so this command is guild only. | ||
| * @return | ||
| * "true" as a {@link Boolean} | ||
| */ | ||
| @Override | ||
| public boolean isGuildOnly() { | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Adds the "User" and "Amount of pings" option. | ||
| * This is for the GUI, so the user sees what options it may give. | ||
| * @param commandCreateAction | ||
| * The thing we will add our options to! | ||
| * @return | ||
| * A {@link CommandCreateAction} with the required options for this command | ||
| */ | ||
| @Override | ||
| public CommandCreateAction addOptions(CommandCreateAction commandCreateAction) { | ||
| return commandCreateAction.addOptions( | ||
| new OptionData(OptionType.USER, "User", "User that will be pinged!", true), | ||
| new OptionData(OptionType.STRING, "Amount of pings", "Amount of times the user will be pinged, default 1").addChoices( | ||
| new Command.Choice("Once", "1"), | ||
| new Command.Choice("Twice", "2") | ||
| ).setRequired(true) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * The execute method. | ||
| * Here it loads the options the users made. | ||
| * After that it pings the user ... times. | ||
| * And it calls the person who ran the command out, spammer they are! | ||
| * @param event | ||
| * A {@link SlashCommandEvent} to respond to. | ||
| */ | ||
| @Override | ||
| public void execute(SlashCommandEvent event) { | ||
|
|
||
| User userToMention = event.getOption("User").getAsUser(); | ||
| int timesToPing = Integer.parseInt(event.getOption("Amount of pings").getAsString()); | ||
|
|
||
| event.reply("It'll only ping once, you spammer!").setEphemeral(true).queue(); | ||
| event.getChannel().sendMessage(userToMention.getAsMention().repeat(timesToPing)).queue(); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be able to be an interface if we never need local state:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.