Skip to content

Latest commit

 

History

History
93 lines (64 loc) · 3.68 KB

Getting_Started.md

File metadata and controls

93 lines (64 loc) · 3.68 KB

This wiki page is migrating to jda.wiki/using-jda/getting-started


Making a Ping-Pong Bot

Creating a Discord Bot

  1. Go to https://discord.com/developers/applications/

  2. Create an Application

  3. Give the application an awesome name (this will be used as the bots initial username)

    name

  4. Click Save Changes

  5. Open the Bot tab

    create bot

  6. Click Add Bot and confirm

  7. 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) 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).

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

  2. Select your Server (Requires permission to manage server)

  3. Click Authorize

    authorize

Connecting to Discord with a Bot Account

  1. Retrieve your Bot Token from your application dashboard (https://discord.com/developers/applications)

    get token

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

  1. [[Setup JDA Project|2)-Setup]]

    Download

  2. Create JDABuilder instance with token

  3. Build JDA using JDABuilder.build()

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

Making a Ping-Pong Protocol

  1. Setup your JDA instance (see [[Connecting To Discord|3)-Getting-Started#Connecting-to-Discord-with-a-Bot-Account]])

  2. Implement an EventListener or extend ListenerAdapter

    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.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 [[RestAction|7)-Using-RestAction]]

  1. Register your listener with either JDABuilder.addEventListeners(new MyListener()) or JDA.addEventListeners(new MyListener()) [ct. [[1) Events|1)-Events]]]