统计特定用户jda的消息数

Count the number of messages for specific user jda

我找到了很多关于如何计算特定用户的消息数量的教程,但所有教程都是针对 discord.py 的。现在我需要这样做,但对于 discord jda。我尝试了很多不同的方法,但没有奏效。有帮助吗?

好的,这就是我到目前为止所尝试的。虽然我知道这样是错误的:

public class ExpSystem extends ListenerAdapter {

    HashMap<User, Integer> playerXp = new HashMap<>();
    HashMap<User, Integer> playerMessages = new HashMap<>();
    HashMap<User, Integer> playerTimer = new HashMap<>();
    int counter = 0;
    public void onGuildMessageReceived(GuildMessageReceivedEvent event){
        String[] args = event.getMessage().getContentRaw().split(" ");
        String command = args[0];
        System.out.println(event.getMessage());



        if(command.equalsIgnoreCase("tr!" + "xp")){
            System.out.println("command");
            if (canGetXp(event.getMember().getUser())){
                if (event.getMember().getUser() == event.getMessage().getMember().getUser()){
                    counter ++;
                    setPlayerMessages(event.getMember().getUser(),counter);
                    System.out.println("cangetxp");
                    randXp(event.getMember().getUser());
                    setPlayerTime(event.getMember().getUser(), 3);
                    event.getChannel().sendMessage("You have " + getPlayerXp(event.getMember().getUser()) + " xp"+ "And " + getPlayerMessages(event.getMember().getUser()) + " Messages").queue();
                }

            }else {
                event.getChannel().sendMessage("You have " + getPlayerXp(event.getMember().getUser()) + " xp" + "And " + getPlayerMessages(event.getMember().getUser()) + " Messages").queue();
            }

        }


    }

    private int getPlayerXp(User member){
        return playerXp.get(member);
    }

    private int getPlayerMessages(User member){
        return playerMessages.get(member);
    }

    private void setPlayerXp(User member, int num){
        playerXp.put(member, num);
    }

    private void setPlayerMessages(User member, int num){
        playerXp.put(member, num);
    }

    private int getPlayerTime(User member){
        return playerTimer.get(member);
    }

    private void setPlayerTime(User member, int num){
        playerTimer.put(member,num);
    }

    private void randXp(User member){
        Random r = new Random();
        if (!playerXp.containsKey(member)){
          setPlayerXp(member, 0);
        }
        setPlayerXp(member, getPlayerXp(member) + r.nextInt(25));
    }

    private boolean canGetXp(User member){
        if(!playerTimer.containsKey(member)){
            return true;
        }
        return false;
    }

    public void startTimer(){
        System.out.println("TIMER STARTED");
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                for (User member : playerTimer.keySet()){

                    setPlayerTime(member,getPlayerTime(member) - 1);
                    if (getPlayerTime(member) == 0){
                        playerTimer.remove(member);
                    }
                }
            }
        };
        timer.schedule(task,1000,1000);
    }



}

我的目标是在发送消息时获得 xp。但我总是得到:

[JDA MainWS-ReadThread] ERROR JDA - One of the EventListeners had an uncaught exception
java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.HashMap.get(Object)" is null
    at ExpSystem.getPlayerMessages(ExpSystem.java:50)
    at ExpSystem.onGuildMessageReceived(ExpSystem.java:33)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at net.dv8tion.jda.api.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:405)
    at net.dv8tion.jda.api.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:96)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handleInternally(EventManagerProxy.java:88)
    at net.dv8tion.jda.internal.hooks.EventManagerProxy.handle(EventManagerProxy.java:70)
    at net.dv8tion.jda.internal.JDAImpl.handleEvent(JDAImpl.java:149)
    at net.dv8tion.jda.internal.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:97)
    at net.dv8tion.jda.internal.handle.SocketHandler.handle(SocketHandler.java:36)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onDispatch(WebSocketClient.java:952)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onEvent(WebSocketClient.java:839)
    at net.dv8tion.jda.internal.requests.WebSocketClient.handleEvent(WebSocketClient.java:817)
    at net.dv8tion.jda.internal.requests.WebSocketClient.onBinaryMessage(WebSocketClient.java:990)
    at com.neovisionaries.ws.client.ListenerManager.callOnBinaryMessage(ListenerManager.java:385)
    at com.neovisionaries.ws.client.ReadingThread.callOnBinaryMessage(ReadingThread.java:276)
    at com.neovisionaries.ws.client.ReadingThread.handleBinaryFrame(ReadingThread.java:996)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:755)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:108)
    at com.neovisionaries.ws.client.ReadingThread.runMain(ReadingThread.java:64)
    at com.neovisionaries.ws.client.WebSocketThread.run(WebSocketThread.java:45)

编辑: 我已经替换了 playerXp.put(member, num); setPlayerMessages 方法中的 platerMessages.put(member, num);。 但是现在所有用户的计数器都是一样的,所以它给了他们相同数量的消息。有什么办法可以解决这个问题吗?

您实际上从未将任何东西放入 playerMessages 地图中。 替换

private void setPlayerMessages(User member, int num){
        playerXp.put(member, num);
}

private void setPlayerMessages(User member, int num){
        playerMessages.put(member, num);
}