gpt4 book ai didi

java - 用户状态更新消息 JDA

转载 作者:行者123 更新时间:2023-12-01 19:58:54 27 4
gpt4 key购买 nike

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

这是我的代码:

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
}

关于java - 用户状态更新消息 JDA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59018084/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com