< Build your Discord Bot />

0
(0)

With this small code snippet, you are able to make your own bot. This snippet will create a working bot without any functionalities. You’ll have to add it yourself. It will give you a headstart when beginning your own bot using the Java Discord API.

import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;

public class DiscordBot {

  public static void main(String[] args) {
    try{
      // Initialize the builder
      JDABuilder builder = JDABuilder.createDefault(YOUR_TOKEN_HERE); 
      // Replace YOUR_TOKEN_HERE with your discord token.
      
      // Your Options Here
      ...

      // Build the JDA Object
      JDA jda = builder.build().awaitReady();
    }catch (LoginException | InterruptedException e){
      e.printStackTrace();
    }
  }

}
// https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/JDABuilder.html 

According the documentation you have to give intents with the arguments. That is what we are going to do apart from the builder.
When you have everything apart from any object it will be more organized and more readable for your own eyes.

// Enable GatewayIntents for which ones you need
builder.enableIntents(GatewayIntent.GUILD_MEMBERS);
builder.enableIntents(GatewayIntent.GUILD_MESSAGE_REACTIONS);
builder.enableIntents(GatewayIntent.GUILD_EMOJIS);
// Enabling caching if wanted
builder.enableCache(CacheFlag.MEMBER_OVERRIDES);
// Filter function for member chunking of guilds. ALL or NONE
builder.setChunkingFilter(ChunkingFilter.ALL); 

https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/requests/GatewayIntent.html

  1. GUILD_MEMBERS – This is a privileged gateway intent that is used to update user information and join/leaves (including kicks). This is required to cache all members of a guild (including chunking)
  2. GUILD_BANS – This will only track guild bans and unbans
  3. GUILD_EMOJIS – This will only track guild emote create/modify/delete. Most bots don’t need this since they just use the emote id anyway.
  4. GUILD_WEBHOOKS – This will only track guild webhook create/update/delete. Most bots don’t need this since related events don’t contain any useful information about webhook changes.
  5. GUILD_INVITES – This will only track invite create/delete. Most bots don’t make use of invites since they are added through OAuth2 authorization by administrators.
  6. GUILD_VOICE_STATES – Required to properly get information of members in voice channels and cache them. You cannot connect to a voice channel without this intent.
  7. GUILD_PRESENCES – This is a privileged gateway intent this is only used to track activity and online-status of a user.
  8. GUILD_MESSAGES – This is used to receive incoming messages in guilds (servers), most bots will need this for commands.
  9. GUILD_MESSAGE_REACTIONS – This is used to track reactions on messages in guilds (servers). Can be useful to make a paginated embed or reaction role management.
  10. GUILD_MESSAGE_TYPING – This is used to track when a user starts typing in guilds (servers). Almost no bot will have a use for this.
  11. DIRECT_MESSAGES – This is used to receive incoming messages in private channels (DMs). You can still send private messages without this intent.
  12. DIRECT_MESSAGE_REACTIONS – This is used to track reactions on messages in private channels (DMs).
  13. DIRECT_MESSAGE_TYPING – This is used to track when a user starts typing in private channels (DMs). Almost no bot will have a use for this.

Finally, add an auto-reconnect to your bot so it will stay reconnected if for any reason it might disconnect. 
According to their documentation: “Sets whether or not JDA should try to reconnect if a connection-error is encountered.”

// Enable auto reconnect if you want to let it auto reconnect when idle
builder.setAutoReconnect(true);
// Set your own activity, like help messages.
builder.setActivity(Activity.watching("!help for help")); 

So after everything added together we have the final class.

import javax.security.auth.login.LoginException;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import net.dv8tion.jda.api.requests.GatewayIntent;
import net.dv8tion.jda.api.utils.ChunkingFilter;
import net.dv8tion.jda.api.utils.cache.CacheFlag;

public class DiscordBot {

  public static void main(String[] args) {
    try{
      // Initialize the builder
      JDABuilder builder = JDABuilder.createDefault(YOUR_TOKEN_HERE);
      // Replace YOUR_TOKEN_HERE with your discord token.

      // Enable GatewayIntents for which ones you need
      builder.enableIntents(GatewayIntent.GUILD_MEMBERS);
      builder.enableIntents(GatewayIntent.GUILD_MESSAGE_REACTIONS);
      builder.enableIntents(GatewayIntent.GUILD_EMOJIS);
      // Enabling caching if wanted
      builder.enableCache(CacheFlag.MEMBER_OVERRIDES);
      // Filter function for member chunking of guilds. ALL or NONE
      builder.setChunkingFilter(ChunkingFilter.ALL);

      // Enable auto reconnect if you want to let it auto reconnect when idle
      builder.setAutoReconnect(true);
      // Set your own activity, like help messages.
      builder.setActivity(Activity.watching("!help for help"));

      // Build the JDA Object
      JDA jda = builder.build().awaitReady();
    }catch (LoginException | InterruptedException e){
      e.printStackTrace();
    }
  }

}
// https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/JDABuilder.html 

Did this help you?

Average rating 0 / 5. Vote count: 0

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on telegram
Telegram
Share on whatsapp
WhatsApp
Share on print
Print

Did this help you?

Average rating 0 / 5. Vote count: 0

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Justin van lier

Justin van lier

I am a self taught Software Engineer with experience in managing and developing game servers, websites, applications & backend systems. I began to work on a professional level since April, 2014. I'm always excited to teach people about Computer Science and the advantages that come along.