如何使我的JDA机器人在服务器上而不是仅在DM上工作?

时间:2020-11-12 16:10:21

标签: java discord discord-jda

我正在尝试使用Java(JDA)设置Discord机器人,但是尝试在服务器上使用它时遇到了一些麻烦。它适用于直接消息,但由于某种原因它不能在服务器上使用。我通过intellij将项目设置为Maven项目。

我从一个开始

public void onMessageReceived(MessageReceivedEvent event) {
        event.getChannel().sendMessage("test").queue();
}

哪个在DM上工作正常,但在服务器上却不行。

这是我到目前为止尝试过的:

1。

我将其从收到的消息更改为:

public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        event.getChannel().sendMessage("test").queue();
}

这仍然无法在我的服务器上正常运行,并且在dms中也不起作用。

2。 我更改了机器人的权限(我先拥有了我需要的权限,但现在它具有管理员权限,因此具有所有权限)

不过,该漫游器只能在DM中没有任何公会消息的情况下运行。我希望你们能提供帮助,以防万一,在您需要的时候,这是我现在的全部代码(出于隐私目的,我将令牌替换为“ TOKEN”)

Main.java:

import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;

public class Main {

    public static JDA jda;
    public static String[] prefixes = {"!", "-", "~", "/"};

    public static void main(String args[]) throws Exception{

        //jda settings
        jda = new JDABuilder("TOKEN").build();
        jda.getPresence().setStatus(OnlineStatus.IDLE);

        jda.addEventListener(new Commands());

    }

}

这是Commands.java:

import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;

import java.util.*;

public class Commands extends ListenerAdapter {

    String[] info = {"info", "zuipbot", "zuipen", "jo", "hoi", "hallo", "30en"};
    List<String> infoList = Arrays.asList(info);

    public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
        event.getChannel().sendMessage("adsfdsf").queue();
        Main.jda.getPresence().setStatus(OnlineStatus.ONLINE);
        String[] args = event.getMessage().getContentRaw().split(" ");
        for (int i = 0; i < Main.prefixes.length; i++) {

            //check if the message starts with one of the correct prefixes
            if (args[0].startsWith(Main.prefixes[i])) {

                //remove the prefix
                args[0] = args[0].substring(1);

                //set the idle timer
                TimerTask task = new TimerTask() {
                    public void run() {
                        Main.jda.getPresence().setStatus(OnlineStatus.IDLE);
                    }
                };

                Timer timer = new Timer();

                long delay = 120000L;
                timer.schedule(task, delay);

                //commands
                if (infoList.contains(args[0].toLowerCase())) {
                    event.getChannel().sendTyping().queue();
                    event.getChannel().sendMessage("info").queue();
                }

                if (args[0].toLowerCase() == "stop") {
                    event.getChannel().sendMessage("Are you sure you want to stop?").queue();
                    Main.jda.shutdown();
                }
            }
        }
    }
}

编辑:这是我的pom.xml(因此我使用的是JDA的最新版本)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Me.LoudMines</groupId>
    <artifactId>ZuipBot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.15</maven.compiler.source>
        <maven.compiler.target>1.15</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.dv8tion</groupId>
            <artifactId>JDA</artifactId>
            <version>4.2.0_214</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>jcenter</id>
            <name>jcenter-bintray</name>
            <url>https://jcenter.bintray.com</url>
        </repository>
    </repositories>

</project>

1 个答案:

答案 0 :(得分:1)

不要使用不推荐使用的JDABuilder构造函数,要获取JDA实例,请改用以下方法: https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/JDABuilder.html#createDefault(java.lang.String)


keytool -list -v -keystore ~/.android/debug.keystore  -alias androiddebugkey -storepass android -keypass android