用户状态更新消息 JDA

UserStatusUpdateMessage JDA

我想在管理员在线、空闲、....离线时发送状态更新消息。我的代码有效,但有一个错误。 该机器人将为服务器上的所有用户发送更新消息..我只想获取管理员的状态更新..

这是我的代码:

public class LoginMessageListener extends ListenerAdapter {

    public void onUserUpdateOnlineStatus(UserUpdateOnlineStatusEvent event) {
        OnlineStatus os = event.getGuild().getMember(event.getUser()).getOnlineStatus();

        if (event.getGuild().getRoleById("628650613152153640") != null) {
            TextChannel channel = event.getGuild().getTextChannelById("628645905955815454");

            EmbedBuilder builder = new EmbedBuilder();
            builder.setThumbnail("http://i.epvpimg.com/t43Pfab.gif");
            builder.setFooter("Powered by Backxtar.");
            builder.setTimestamp(OffsetDateTime.now());
            builder.setColor(0xf22613);
            builder.setDescription("Admin **" + event.getUser().getAsMention() + "** is now **" + os + "**!\nI'm __always available__ for questions,\nsuggestions or problems!\n:point_right: [**Da Hood - Join NOW!**](https://discord.gg/xyz) :point_left:");

            channel.sendMessage(builder.build()).complete().delete().queueAfter(15, TimeUnit.SECONDS);
        } 

        else {
            return;
        }
    }
}

您对 if (event.getGuild().getRoleById("628650613152153640") != null) 的检查仅检查该角色是否存在于服务器中。要检查用户是否具有角色,您必须使用 event.getMember():

检查角色
private static boolean isAdmin(Member member) {
    return member.getRoles().stream()
        .mapToLong(Role::getIdLong) // we check by id
        .anyMatch(id -> id == 628650613152153640L); // true if any role has that id
}

@Override
public void onUserUpdateOnlineStatus(UserUpdateOnlineStatusEvent event) {
        OnlineStatus os = event.getGuild().getMember(event.getUser()).getOnlineStatus();

        if (isAdmin(event.getMember())) {
            // code
        }
// the else here is unnecessary
}