Skip to content

Making a Ping-Pong Bot

Creating a Discord Bot

  1. Go to
  2. Create an Application
  3. Give the application an awesome name (this will be used as the bots initial username)


  4. Click Save Changes

  5. Open the Bot tab

    create bot

  6. Make sure to make your bot public, this allows others to invite your bot to your server.

    public bot

    You only want require code grant enabled if you plan to use an oauth2 flow, the general user will not need this.

Add your Discord Bot to a Server

  1. Retrieve your application/client ID from the General Information tab

    client id

  2. Create an OAuth2 authorization URL (reference docs). Users who want to use Interaction Commands should also add the applications.commands scope. Some example URLs:



    This can be done from the Bot tab at the very bottom. Here you can select the scope bot and some permissions required for your bots functionality (optional).

  3. Open the authorization dialogue (click link from step 2)

  4. Select your Server (Requires permission to manage server)
  5. Click Authorize


Connecting to Discord with a Bot Account

  1. Retrieve your Bot Token from your application dashboard (

    get token


    Note that it is very important not to show this token to anyone, ever.

  2. Set up your JDA project:


  3. Create JDABuilder instance with token

  4. Build JDA using

    public static void main(String[] arguments) throws Exception
        JDA api = JDABuilder.createDefault(BOT_TOKEN).build();


    It is often better to load your token in from an external file or environment variable, especially if you plan on publishing the source code.

Making a Ping-Pong Protocol

  1. Setup your JDA instance (see Connecting To Discord)
  2. Implement an EventListener or extend ListenerAdapter

    public class MyListener extends ListenerAdapter 
        public void onMessageReceived(MessageReceivedEvent event)
            if (event.getAuthor().isBot()) return;
            // We don't want to respond to other bot accounts, including ourself
            Message message = event.getMessage();
            String content = message.getContentRaw(); 
            // getContentRaw() is an atomic getter
            // getContentDisplay() is a lazy getter which modifies the content for e.g. console view (strip discord formatting)
            if (content.equals("!ping"))
                MessageChannel channel = event.getChannel();
                channel.sendMessage("Pong!").queue(); // Important to call .queue() on the RestAction returned by sendMessage(...)


    More information about RestActions can be found here

  3. Register your listener with either JDABuilder.addEventListeners(new MyListener()) or JDA.addEventListeners(new MyListener()) (see Events)