如何修复不工作的不和谐机器人? java-lang

问题描述 投票:0回答:1

我最近在chatgpt的帮助下用Java语言创建了一个discord机器人。该机器人正在运行并且在不和谐服务器上在线,但没有执行我想要的操作。目的是在某人上线到服务器时向他们打招呼。机器人并没有这样做。我不知道代码中是否有问题或者我是否错误地将其设置到服务器。我可以提到,机器人具有以下权限:(存在意图、服务器成员意图和消息内容意图)已启用。我还认为我也有最新版本的 Maven、JDA 等。我很了解编码,所以我相信这是逻辑错误,但我要告诉谁,请看一下,我将不胜感激:)

这是我的java文件和pom.xml文件

import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import net.dv8tion.jda.api.events.user.update.UserUpdateOnlineStatusEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import net.dv8tion.jda.api.requests.GatewayIntent;

import javax.security.auth.login.LoginException;

public class OnlineTrackerBot extends ListenerAdapter
{

    public static void main(String[] args) throws LoginException {
        String token = "my_token";  // Replaced with my bot token

        JDABuilder builder = JDABuilder.createDefault(token);
        builder.enableIntents(GatewayIntent.GUILD_PRESENCES, GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT);  // Enable necessary intents
        builder.addEventListeners(new OnlineTrackerBot());
        System.out.println("Event listeners added.");
        builder.build();
    }

    @Override
    public void onUserUpdateOnlineStatus(UserUpdateOnlineStatusEvent event) {
        System.out.println("Received UserUpdateOnlineStatusEvent for user: " + event.getUser().getName());

        // Get the member whose presence updated
        Member member = event.getGuild().getMember(event.getUser());
        if (member == null) {
            System.out.println("Member not found in the guild.");
            return;
        }
        System.out.println("Member found: " + member.getEffectiveName());

        // Check if the member's status is now ONLINE
        if (event.getNewOnlineStatus() == OnlineStatus.ONLINE) {
            // Get the general channel (replace with your actual channel name)
            TextChannel generalChannel = event.getGuild().getTextChannelsByName("inte-dansband-förhelvete", true).get(0);

            // Send a welcome message in the general channel
            if (generalChannel != null) {
                System.out.println("Sending message to channel: " + generalChannel.getName());
                generalChannel.sendMessage("Welcome back online, " + member.getAsMention() + "!").queue();
            } else {
                System.out.println("General channel not found.");
            }
        }
    }
}

Pom.xml 文件

<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.Carllundholm</groupId>
  <artifactId>OnlineTrackerBot</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>OnlineTrackerBot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>net.dv8tion</groupId>
      <artifactId>JDA</artifactId>
      <version>5.1.0</version> <!-- Use a stable release of JDA -->
    </dependency>
    <!-- JUnit 5 Dependency -->
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-api</artifactId>
      <version>5.9.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter-engine</artifactId>
      <version>5.9.3</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>2.0.9</version>
    </dependency>

提前致谢!

我尝试过更改不同的方法和版本的 JDA,因为很多次我收到消息:无法解析符号或无法解析方法。

java maven intellij-idea discord discord-jda
1个回答
0
投票

问题在于您创建机器人的方式,这是实际可以工作的版本:

            public static JDA builder;

    public static void main(String[] args) {
        String token = "token";  // Replaced with my bot token

        builder  = JDABuilder.create(GatewayIntent.GUILD_PRESENCES, GatewayIntent.GUILD_MEMBERS, GatewayIntent.MESSAGE_CONTENT).setToken(token).build();
        builder.addEventListener(new OnlineTracker());
        System.out.println("Event listeners added.");
}

对于监听器,您可以在单独的类中创建它并以与以前相同的方式添加它 它只是看起来更有条理,你无法将所有内容都放在一堂课中

还有关于你的监听器,我看到你正在使用 .getTextChannelsByName().get(0) 发送消息时, 更好的方法是使用 .getTextChannelById()

因为 .getTextChannelsByName() 返回一个可能包含多个具有相同名称的通道的列表。 所以让他们有各自的id会更好 无论如何,如果您现在遇到任何问题,请随时询问!

© www.soinside.com 2019 - 2024. All rights reserved.