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
- 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)
- GUILD_BANS – This will only track guild bans and unbans
- 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.
- 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.
- 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.
- 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.
- GUILD_PRESENCES – This is a privileged gateway intent this is only used to track activity and online-status of a user.
- GUILD_MESSAGES – This is used to receive incoming messages in guilds (servers), most bots will need this for commands.
- 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.
- 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.
- DIRECT_MESSAGES – This is used to receive incoming messages in private channels (DMs). You can still send private messages without this intent.
- DIRECT_MESSAGE_REACTIONS – This is used to track reactions on messages in private channels (DMs).
- 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.
https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/utils/cache/CacheFlag.html
ACTIVITY | Enables cache for Member.getActivities() |
---|---|
CLIENT_STATUS | Enables cache for Member.getOnlineStatus(ClientType) |
EMOTE | Enables cache for Guild.getEmoteCache() |
MEMBER_OVERRIDES | Enables cache for GuildChannel.getMemberPermissionOverrides() |
ONLINE_STATUS | |
ROLE_TAGS | Enables cache for Role.getTags() |
VOICE_STATE | Enables cache for Member.getVoiceState() This will always be cached for self member. |
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