DiscordBot - Redstonecrafter0/RedstoneAPI GitHub Wiki

DiscordBot

The DiscordBot<> class can have a generic class of a CommandManager that does what it says. There are the DefaultCommandManager for Spigot like commands and the SimpleCommandManager for annotated commands with automatic type conversion. There is also a Spigot like eventmanager. Argument parsing can be escaped using quotation marks which can be escaped by backslashes.

EventManager

The EventManager is always in the DiscordBot class. The EventManager could also be added to JDA as a EventListener like normal.

To create Listeners you need a class that extends DiscordEventListener and add events like this by the normal JDA event classes.

bot.getEventManager().addEventListener(new DiscordEventListener() {

  @DiscordEvent
  public void onMessage(GuildMessageReceivedEvent event) {
    // do stuff
  }

});

CommandManager

By extending the CommandManager class you can create your own CommandManager however there are already two. There are private commands and server commands. Private commands are commands that gets executed when a private message is incomming. It is defined by PrivateContext. Server commands are commands executed on Discord servers. It is defined by ServerContext.

SimpleCommandManager

Here is assumed that the bot instance is of type DiscordBot. A simple CommandManager using Annotations for commands. If the command name is not provided in the Annotation the method name will be used. If the permission is not set in the Annotation everyone will be able to use the command. The usage and a info for the help command must be provided. It returns a boolean whether the command was successfully executed.

Create the bot instance using this.

DiscordBot<SimpleCommandManager> bot = new DiscordBot<>("bottoken", "commandprefix", new SimpleCommandManager("embed title"));

####Commands

Custom Commands
bot.getCommandManger().registerCommands(new SimpleCommands() {

  @SimpleCommand(usage = "privateTest <string> <integer> <userid or ping>", info = "This is a test private command")
  public boolean privateTest(PrivateContext ctx, String arg1, Integer arg2, User arg3) {
    ctx.getChannel().sendMessage(arg1 + " is a String. " + arg2 + " is an Integer. " + arg3.getId() + " is a UserId.").queue();
    return true;
  }

  @SimpleCommand(name = "serverTest", usage = "serverTest <string> <integer> <memberid or ping>", info = "This is a test private command", permission = Permission.ADMINISTRATOR)
  public boolean test(ServerContext ctx, String arg1, Integer arg2, Member arg3) {
    ctx.getChannel().sendMessage(arg1 + " is a String. " + arg2 + " is an Integer. " + arg3.getGuild().getId() + " the id the member is on.").queue();
    return true;
  }

});
Predefined Help Commands

There is a private help command and server help command predefined but not automatically used. To implement these commands just use this code.

bot.getCommandManager().registerCommands(
//                                                  bot instance|use full width of embed|max helps per page|embed title|embed color | invalid page message
  new SimpleCommandManager.DefaultServerHelpCommand(bot         , true                  , 9                , "Title"   , Color.BLACK, "The maximum page is %d."),
  new SimpleCommandManager.DefaultPrivateHelpCommand(bot        , true                  , 9                , "Help"    , Color.BLACK, "The maximum page is %d.")
);

Custom converter

You can use custom converters like the ones privided already. It could convert a String input to whatever specified in the command method. You only need to define and register it.

bot.getCommandManager().registerConverters(converter);

DefaultCommandManager

Here is assumed that the bot instance is of type DiscordBot. The command are like them from Spigot.

Create the bot instance using this.

DiscordBot<DefaultCommandManager> bot = new DiscordBot<>("bottoken", "commandprefix", new DefaultCommandManager("embed title"));

A command must extend ServerCommand or PrivateCommand whatever you want to use.

bot.getCommandManager().registerServerCommand("commandName", new ServerCommand() {
  @Override
  public String usage() {
    return "commandName <arg1> <arg2>";
  }

  @Override
  public boolean onCommand(ServerContext ctx, String[] args) {
    ctx.getChannel().sendMessage(String.join(" ", args)).queue();
    return true;
  }
});

bot.getCommandManager().registerPrivateCommand("commandName", new PrivateCommand() {
  @Override
  public String usage() {
    return "commandName <arg1> <arg2>";
  }

  @Override
  public boolean onCommand(PrivateContext ctx, String[] args) {
    ctx.getChannel().sendMessage(String.join(" ", args)).queue();
    return true;
  }
});
⚠️ **GitHub.com Fallback** ⚠️