这是我第一次尝试JDA。 (Java Discord API)我正在尝试制作一个简单的Discord机器人,该机器人在有人通过指定频道加入服务器时说一条自定义消息(因为我认为这将是一种很好的学习体验)。但是,我的程序似乎并没有检测到用户已加入。我尝试使用打印语句对其进行调试,发现它甚至无法输入该方法。
这是我加入活动的代码:
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event){
User user=event.getUser();
TextChannel channel=event.getGuild().getTextChannelById("769208577205731328");
channel.sendMessage("everyone welcome "+user.getName());
}
我还有另外两个类(一个包含main方法,一个包含事件的实际侦听器)
Bot.java(具有主要方法):
import events.JoinLeave;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import javax.security.auth.login.LoginException;
public class Bot {
public static void main(String[] args) throws LoginException{
JDA jda= JDABuilder.createDefault("mytoken").build();
jda.addEventListener(new JoinLeave());
}
}
JoinLeave.java:
package events;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.guild.GuildJoinEvent;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import javax.security.auth.login.LoginException;
public class JoinLeave extends ListenerAdapter {
//this works normally (checks for messages)
@Override
public void onMessageReceived(MessageReceivedEvent event){
if(event.getMessage().getContentRaw().equals("!ping")){
event.getChannel().sendMessage("Pong!").queue();
}
}
//this one is the problematic one
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event){
User user=event.getUser();
// channel id want to send to
TextChannel channel=event.getGuild().getTextChannelById("769208577205731328");
channel.sendMessage("everyone welcome "+user.getName());
}
}