如何让我的 JDA 机器人在服务器上运行,而不仅仅是 DM?

How do I make my JDA bot work on a server instead of just DMs?

我正在尝试使用 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>

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


JDA jda = JDABuilder.createDefault(token).build();