Skip to content

Instantly share code, notes, and snippets.

@Hopefuls
Created March 25, 2021 17:48
Show Gist options
  • Save Hopefuls/253c6721c7e65d415d8cda32b50c65f9 to your computer and use it in GitHub Desktop.
Save Hopefuls/253c6721c7e65d415d8cda32b50c65f9 to your computer and use it in GitHub Desktop.
Basic Example of implementing a ping command using the CommandHandler
package me.hopedev.testproj;
import me.hopedev.commandhandler.CommandBuilder;
import org.javacord.api.DiscordApi;
import org.javacord.api.DiscordApiBuilder;
public class Main {
public static void main(String[] args) {
// init javacord
DiscordApi api = new DiscordApiBuilder().setToken("token").login().join();
//CommandBuilder
CommandBuilder builder = new CommandBuilder("!", api);
// add !ping
builder.addCommand("ping", new PingCommand(), "Gets the ping of the bot", "!ping");
//build
builder.build();
}
}
package me.hopedev.testproj;
import me.hopedev.commandhandler.Command;
import me.hopedev.commandhandler.CommandData;
import me.hopedev.commandhandler.CommandExecutor;
import org.javacord.api.entity.message.Message;
import java.util.ArrayList;
public class PingCommand implements CommandExecutor {
@Override
public void execute(CommandData data, ArrayList<Command> commands) {
long ping1, ping2, ms;
ping1 = System.currentTimeMillis();
Message message = data.getChannel().sendMessage("Ping..").join();
ping2 = System.currentTimeMillis();
ms = ping2-ping1;
message.edit("Pong! `"+ms+"ms`");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment