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

Creating bots with JG_API

Jared edited this page Jul 30, 2022 · 6 revisions

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) throws IOException, RestActionException {
        ChatMessage message = event.getMessage();
        String messageId = message.getId();
        String channelId = message.getChannel().getId();
        event.getJGAPI().getRestClient().deleteMessage(messageId, channelId).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) throws IOException, RestActionException {
        ChatMessage message = event.getMessage();
        String messageId = message.getId();
        String channelId = message.getChannel().getId();
        boolean wasDeleted = event.getJGAPI().getRestClient().deleteMessage(messageId, channelId).complete();
    }
}