Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Bot Examples

Jared edited this page Jul 26, 2022 · 14 revisions

Ping Pong Bot

Main Class

public class Bot {
    private static JG_API jg_api;

    public static void main(String[] args) {
        try {
            jg_api = new JG_API.ClientBuilder()
                    .setParentServerId(ConfigManager.getInstance().getProperty("GUILDED_SERVER_ID"))
                    .setToken(ConfigManager.getInstance().getProperty("GUILDED_TOKEN"))
                    .addListenerAdapter(new EventHandler())
                    .build();

            jg_api.login();
            jg_api.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

EventHandler

public class EventHandler extends ListenerAdapter {
    @Override
    public void onReadyEvent(ReadyEvent event) {
        System.out.println(event.getJGAPI().getClientUser().getName() + " is now ready!");
    }

    @Override
    public void onChatMessageCreatedEvent(ChatMessageCreatedEvent event) {
        ChatMessage message = event.getMessage();
        String content = message.getContent();
        String[] args = content.split(" ");
        if (args.length < 1) return; // It's not enough arguments, disregard
        String command = args[0];

        if (command.equalsIgnoreCase("ping")) {
            message.getChannel().sendMessage("pong!").queue();
        }
    }
}

Commands with arguments

Main Class

public class Bot {
    private static JG_API jg_api;

    public static void main(String[] args) {
        try {
            jg_api = new JG_API.ClientBuilder()
                    .setParentServerId(ConfigManager.getInstance().getProperty("GUILDED_SERVER_ID"))
                    .setToken(ConfigManager.getInstance().getProperty("GUILDED_TOKEN"))
                    .addListenerAdapter(new EventHandler())
                    .build();

            jg_api.login();
            jg_api.start();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

EventHandler

public class EventHandler extends ListenerAdapter {
    @Override
    public void onReadyEvent(ReadyEvent event) {
        System.out.println(event.getJGAPI().getClientUser().getName() + " is now ready!");
    }

    @Override
    public void onChatMessageCreatedEvent(ChatMessageCreatedEvent event) {
        ChatMessage message = event.getMessage();
        String content = message.getContent();
        String[] args = content.split(" ");
        if (args.length < 1) return; // It's not enough arguments, disregard
        String command = args[0];

        if (command.equalsIgnoreCase("+jgapi")) {
            if (args[1].equalsIgnoresCase("help")) {
                message.getChannel().sendMessage("This is a help menu for the command... You can use `+jgapi founder`, `+jgapi website` commands as well.").queue();
            }
            if (args[1].equalsIgnoresCase("founder")) {
                message.getChannel().sendMessage("This is the founder message. JG_API was founded by Jared and Nathaniel...").queue();
            }
            if (args[1].equalsIgnoresCase("website")) {
                message.getChannel().sendMessage("You can find our website here: jgapi.dev").queue();
            }
            message.getChannel().sendMessage("pong!").queue();
        }
    }
}

Message Deletion

TBD

Using consumers for success and failure of RestActions

TBD

Using RestClient methods

Clone this wiki locally