Jonathan Jones
Jonathan Jones

Reputation: 27

I have a no error Java code for my discord bot. But my bot is still offline

I have a java code for y discord bot that has no errors. But when I run it nothing happens to my Discord Bot. Here is my build.grade code

plugins {
    id 'java'
    id 'application'
    id 'com.github.johnrengelman.shadow' version'5.1.0'
}

mainClassName = "Main"

group 'BlueBot'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'net.dv8tion:JDA:4.0.0_62'
}

Here is my main.java code.

import net.dv8tion.jda.api.AccountType;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import javax.security.auth.login.LoginException;


public class Main extends ListenerAdapter {
    public static void main(String[] args) throws LoginException {
        JDABuilder builder = new JDABuilder(AccountType.BOT);
        String token = "Enter token here";
        builder.setToken(token);
        builder.addEventListeners(new Main());
        builder.build();
    }

    @Override
    public void onMessageReceived(MessageReceivedEvent event) {
        System.out.println("We received a message from " +
                event.getAuthor().getName() + ": " +
                event.getMessage().getContentDisplay()
        );

        if (event.getMessage().getContentRaw().equals("I am lonely")) {
            event.getChannel().sendMessage("Who isn't?").queue();
        }
    }
}

Please help. I don't know what I am missing. If you have questions or need more info just tell me.

Upvotes: 0

Views: 668

Answers (1)

lireoy
lireoy

Reputation: 36

You did not wait until JDA has reached the CONENCTED status. JDA is connected when it finished setting up its internal cache and is ready to be used. You should put an .awaitReady() after the .build()

JDABuilder builder = new JDABuilder(AccountType.BOT);
String token = "Enter token here";
builder.setToken(token);
builder.addEventListeners(new Main());
builder.build().awaitReady();

Upvotes: 1

Related Questions