我的Discord机器人没有错误的Java代码。但是我的机器人仍然离线

时间:2019-12-23 00:55:11

标签: java discord discord-jda

我有一个用于y discord bot的Java代码,没有错误。但是当我运行它时,我的Discord Bot没有任何反应。这是我的build.grade代码

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'
}

这是我的main.java代码。

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();
        }
    }
}

请帮助。我不知道我在想什么。如果您有疑问或需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:1)

您没有等到JDA达到CONENCTED状态。 JDA完成设置其内部缓存并准备使用时,已连接。 您应该在.awaitReady()

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