简体   繁体   English

我的不和谐机器人没有错误的 Java 代码。 但我的机器人仍然处于离线状态

[英]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.我有一个没有错误的 y discord bot 的 java 代码。 But when I run it nothing happens to my Discord Bot.但是当我运行它时,我的 Discord Bot 没有任何反应。 Here is my build.grade code这是我的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'
}

Here is my main.java code.这是我的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();
        }
    }
}

Please help.请帮忙。 I don't know what I am missing.我不知道我错过了什么。 If you have questions or need more info just tell me.如果您有疑问或需要更多信息,请告诉我。

You did not wait until JDA has reached the CONENCTED status.您没有等到 JDA 达到 CONENCTED 状态。 JDA is connected when it finished setting up its internal cache and is ready to be used. JDA 在完成其内部缓存的设置并准备好使用时即已连接。 You should put an .awaitReady() after the .build()你应该在.awaitReady()之后放一个.awaitReady() .build()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM