Skip to content

Creating bots with JG_API

Jared Scarito edited this page Oct 18, 2026 · 7 revisions

JG_API needs Java 17+, see Installation on how to add it to your project.

Step 1: Getting your bot token from Guilded

  1. Go to the server settings page
  2. Click "Bots" on the left side and you will see all the bots in the server
  3. Click "Create a bot" to create a new bot
  4. Click the Kebab menu which is the icon that has 3 dots on top of each other to the right of this newly created bot
  5. Click "Generate token" and click "Copy"
  6. This is the token you will use for initializing your JG_API instance

Step 2: Creating the JG_API instance

public class Bot {
    public static JG_API jg_api;

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

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

    }
}

Step 3: Creating an event handler

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) {}
}

Step 4: Register the event handler

public class Bot {
    private static JG_API jg_api;

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

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

    }
}

Step 5: Executing RestClient methods via the queue

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 messageId = message.getId();
        String channelId = message.getChannel().getId();
        // Note the order of the arguments: the channel first, then the message
        event.getJGAPI().getRestClient().deleteMessage(channelId, messageId).queue(
           (isDeleted) -> {
               if (isDeleted) System.out.println("The channel message was deleted successfully...");
            }, Throwable::printStackTrace);
    }
}

Step 6: Executing RestClient methods via completion

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();
        try {
            boolean wasDeleted = event.getJGAPI().getRestClient()
                    .deleteMessage(message.getChannel().getId(), message.getId())
                    .complete(); // Blocks until Guilded answered
        } catch (IOException | RestActionException e) {
            e.printStackTrace();
        }
    }
}

Next steps

Clone this wiki locally