[EN] Java - Discord Bot Development

in #tutorial7 years ago

Hello!

Welcome to my Java Discord Bot Development Tutorial. The necessary links are linked in the bottom section.

What is Discord!

Discord is an innovative all-in-one text and voice chat. The servers are free of charge, have DDoS protection and offer unlimited slots. The applications can be downloaded free of charge on all platforms.

How do I create a Discord bot?

First you create a Discord application on the Discord "My Apps" page. Then you are on the overview page of the application. After that you can convert your application with the following button to a bot. 

How do I add the bot to my server?

First you need the client ID of the bot, which you can find on the overview page.

If you have your client ID, you can use an OAuth link to add the bot to your own servers. Instead of *clientid* you just write your own client ID in there and open the link.  

After that you can select in a dialog which server the bot should be on. You can only select those servers for which you have the necessary permissions. 

How do I program a Discord bot in Java?

You can choose between the two APIs, JDA and Discord4J. In this example JDA was used, but the choice is up to the developer.

Go to the overview page and display the client token.  

In the IDE of your choice you have to add JDA as a library.

In this example I'll show you how to program a bot that returns the message "Test back" to the command ".test". Using JDA#addEventListener you can add your own events.

public static void main(String[] args) {
    try {
        JDA jda = new JDABuilder(AccountType.BOT).setToken("Zensiert").buildAsync();
        jda.addEventListener(new MessageReceivedListener());
    } catch (LoginException | IllegalArgumentException | RateLimitedException ex) {
        Logger.getLogger(DiscordBot.class.getName()).log(Level.SEVERE, null, ex);
    }
}

Main Class 

public class MessageReceivedListener extends ListenerAdapter {
    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        if (!event.getAuthor().isBot()) {
            String content = event.getMessage().getRawContent();
            if (content.equals(".test")) {
                event.getChannel().sendMessage("Test back").queue();
            }
        }
    }
}

MessageReceivedListener Class 

 Links

You could expand the bot now, as there are many more events, but this tutorial is over here. I wish you the best of luck for the future.

With kind regards
Thomas

Sort:  

Congratulations @batthomas! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @batthomas! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.25
TRX 0.20
JST 0.035
BTC 95037.22
ETH 3446.03
USDT 1.00
SBD 3.46