-
Create an Application
-
Give the application an awesome name (this will be used as the bots initial username)
-
Click Save Changes
-
Open the Bot tab
-
Make sure to make your bot public, this allows others to invite your bot to your server.
You only want "Requires OAuth2 code grant" enabled if you plan to use an OAuth2 flow, the general user will not need this.
-
Retrieve your application/client ID from the General Information tab
-
Create an OAuth2 authorization URL (reference docs).
This is the URL to invite your bot to a server, for example,
https://discord.com/api/oauth2/authorize?client_id=492747769036013578&scope=bot!!! note 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).
-
Open the authorization dialogue (click link from step 2)
-
Select your Server (Requires permission to manage server)
-
Click Authorize
-
Retrieve your Bot Token from your application dashboard (https://discord.com/developers/applications)
!!! danger You must not show this token to anyone, ever.
-
Set up your JDA project:
-
Create
JDABuilderinstance, using the static methods and your token!!! tip
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. -
Enable the "Message content intent" on your bot's dashboard, and add the
MESSAGE_CONTENTintent on yourJDABuilderWhile receiving messages is enabled by default, Discord requires an additional privileged intent for the content of the message.
-
Build JDA using
JDABuilder.build()public static void main(String[] arguments) throws Exception { JDA api = JDABuilder.createDefault(BOT_TOKEN) .enableIntents(GatewayIntent.MESSAGE_CONTENT) .build(); }
!!! note You must not build JDA more than once.
-
Setup your JDA instance (see Connecting To Discord)
-
Implement an
EventListeneror extendListenerAdapterpublic class MyListener extends ListenerAdapter { @Override 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(...) } } }
!!! info More information about RestActions can be found here
-
Register your listener with either
JDABuilder.addEventListeners(new MyListener())orJDA.addEventListeners(new MyListener())(see Events)





