如何为变量User newUser
分配登录服务器的用户?
我的程序的一部分:
Main.java
:
public static void main(String[] args) throws Exception{
try {
JDA api = new JDABuilder(AccountType.BOT).setToken(token).build();
api.addEventListener(new MyEventListener(api));
} catch (Exception e) {
e.printStackTrace();
}
}
MyEventListener.java
:
public void onMessageReceived(MessageReceivedEvent event) {
User newUser;
}
我阅读了Javadoc,也许我需要使用GuildMemberJoinEvent
。如果这是我的需要,请提供示例代码以帮助如何在我的程序中正确使用此功能,以提供帮助。
答案 0 :(得分:1)
private static User newUser;
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
newUser = event.getUser();
}
@Override
public void onMessageReceived(MessageReceivedEvent event) {
System.out.println(newUser.getName()); //...your code
}