Discord bot

... by Bit in Java Bot August 18, 2018

Hello, in this article I will be teaching you how to create a discord bot in java using Discord4j API.

To start off you need to download the API at: https://discord4j.com/downloads.html and import it into your project.If you don't know how to import a library into your project check out the java tutorials Environment Setup!


If you prefer adding the API in an alternative way you can do the following:

With Maven

pom.xml 
<repositories>
  <repository>
    <id>jcenter</id>
    <url>http://jcenter.bintray.com</url>
  </repository>
</repositories>

<dependencies>
  <dependency>
    <groupId>com.discord4j</groupId> <!-- However our packages are all under sx.blah in version 2.x! -->
    <artifactId>Discord4J</artifactId>
    <version>@VERSION@</version>
  </dependency>
</dependencies>

With Gradle

build.gradle
repositories {
  jcenter()
}

dependencies {
  compile "com.discord4j:Discord4J:@VERSION@"
}

With SBT

build.sbt
libraryDependencies ++= Seq(
  "com.discord4j" % "Discord4J" % "@VERSION@"
)

resolvers += "jcenter" at "http://jcenter.bintray.com"

After importing the library you will want to head over to https://discordapp.com/developers/applications/ to create a bot application for discord.

After creating a bot application you will want to click reveal (clients secret) and that will inform you about your bots token copy and paste it and we can jump into coding the bot.

Logging into the bots client:

public class DiscordBot {
    public static final IDiscordClient bot = createClient("Token",true);
    public static void main (String args []) throws Exception {
               //register listener so it can recieve messages 
                EventDispatcher ds = bot.getDispatcher();
                ds.registerListener(new BotListener()); 
            }
                public static IDiscordClient createClient(String token, boolean login) { // Returns a new instance of the Discord client
                ClientBuilder clientBuilder = new ClientBuilder(); // Creates the ClientBuilder instance
                clientBuilder.withToken(token); // Adds the login info to the builder
                try {
                    if (login) {
                        return clientBuilder.login(); // Creates the client instance and logs the client in
                    } else {
                        return clientBuilder.build(); // Creates the client instance but it doesn't log the client in yet
                    }


                    } catch (DiscordException e) { // This is thrown if there was a problem building the client
                    e.printStackTrace();
                    return null;
                }
            }
        }

Finally, make another class named the same class that you passed within the parameters of the ds.registerListener() like shown in the example above.

public class BotListener {
//NOTE: Different types of Listeners
/*MessageUpdateEvent
 MessageSendEvent
 MessageDeleteEvent
 MessageEditEvent
*/
@EventSubscriber
public void onMessageEvent(MessageReceivedEvent e){
String message = e.getMessage().getContent();
Channel channel = e.getChannel();
if(message.equalsIgnoreCase("hello")){
 channel.sendMessage("World");
  }
 }
}


if you have any questions comment below!

Comments (0)

Search Here