状态更新不和谐机器人

时间:2019-11-23 23:41:24

标签: java discord-jda

这是我更新状态的代码:

String[] status = new String[] {"Version: 1.5.0", "https://discord.gg/arWEM2h", "Love Backxtar", "You want me!", "Type: ~help", "User Counter: %members"};
    int next = 60;

    public void onSecond() {

        if(next%5 == 0) {

            if(!hasStarted) {
                hasStarted = true;
                StatChannelCommand.onStartUp();
            }

            Random rand = new Random();
            int i = rand.nextInt(status.length);

            shardMan.getShards().forEach(jda -> {
                String text = status[i].replaceAll("%members", "" + jda.getUsers().size());

                jda.getPresence().setActivity(Activity.playing(text));
            });

            StatChannelCommand.checkStats();

            if(next == 0) {
            next = 60;

            }
        }
        else {
            next--;
        }
    }

但是String每秒钟运行一次。我以为是每5秒一次。我做了60秒%5。这段代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

首次输入方法onSecond()时,条件next%5 == 0将为true。变量next将不会更新,因为这仅发生在else部分中。因此,方法next的下一次运行仍将是60