-
-
Notifications
You must be signed in to change notification settings - Fork 726
3) Getting Started
Florian Spieß edited this page Mar 3, 2017
·
23 revisions
- Go to https://discordapp.com/developers/applications/me
- Create an Application
- Give the application an awesome name (this will be used as the bots initial username)
- Click Create App
- Once you are in your application review page, click Create Bot User (Confirm pop-up)
-
[OPTIONAL]
If you want your bot to be invited to servers by other discord users check Public Bot (DO NOT CHECK Require OAuth2 Code Grant unless you know what you're doing!)
- Retrieve your application/client ID
- Create an OAuth2 authorization URL (reference docs)
Example:
https://discordapp.com/api/oauth2/authorize?client_id=287329057639497740&scope=bot
- 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://discordapp.com/developers/applications/me)
- Setup JDA Project
-
Create
JDABuilder
instance with token typeAccountType.BOT
-
Set your token
-
Build JDA using either
JDABuilder.buildBlocking()
orJDABuilder.buildAsync()
depending on your needspublic static void main(String[] arguments) { JDA api = new JDABuilder(AccountType.BOT).setToken("[REDACTED]").buildAsync(); }
-
Setup your JDA instance (see Connecting To Discord)
-
Implement an
EventListener
or extendListenerAdapter
public 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.getRawContent(); // getRawContent() is an atomic getter // getContent() 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(...) } } }
-
Register your listener with either
JDABuilder.addListener(new MyListener())
orJDA.addEventListener(new MyListener())
Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß