欢迎听众不工作(Java Discord JDA)
Welcome Listener Does Not Work(Java Discord JDA)
我试图让我的机器人在有人加入时欢迎某人,但我似乎无法让它工作。例如(顺便说一句,这将出现在嵌入中):
@Jason joined. You must construct additional pylons.
有人可以帮我编辑我的代码使其正常工作吗?
这是我的主要代码:
import Events.*;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) throws LoginException {
JDABuilder jda = JDABuilder.createDefault("I imported key here");
jda.setActivity(Activity.watching("baldness"));
jda.addEventListeners(new Help());
jda.addEventListeners(new PingPong());
jda.addEventListeners(new Clear());
jda.addEventListeners(new Welcome());
jda.build();
}
}
这是我的欢迎代码:
package Events;
import java.util.Random;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Welcome extends ListenerAdapter {
public class GuildMemberJoin extends ListenerAdapter {
String[] messages = {
"[member] joined. You must construct additional pylons.",
"Never gonna give [member] up. Never let [member] down!",
"Hey! Listen! [member] has joined!",
"Ha! [member] has joined! You activated my trap card!",
"We've been expecting you, [member].",
"It's dangerous to go alone, take [member]!",
"Swoooosh. [member] just landed.",
"Brace yourselves. [member] just joined the server.",
"A wild [member] appeared."
};
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Random rand = new Random();
int number = rand.nextInt(messages.length);
EmbedBuilder join = new EmbedBuilder();
join.setColor(0x66d8ff);
join.setDescription(messages[number].replace("[member]", event.getMember().getAsMention()));
event.getGuild().getDefaultChannel().sendMessage(join.build()).queue();
}
}
}
documentation fo GuildMemberJoinEvent 明确指出:
Requirements
This event requires the GUILD_MEMBERS intent to be enabled.
createDefault(String) and createLight(String) disable this by default!
因此您必须启用意图。在 wiki 指南中阅读更多内容 Gateway Intents and Member Cache Policy
此外,您无缘无故地嵌套了 class,这意味着您将封闭的 class 注册为侦听器,而不是实际实现它的嵌套 class。
更好:
public class Welcome extends ListenerAdapter {
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
...
}
}
我试图让我的机器人在有人加入时欢迎某人,但我似乎无法让它工作。例如(顺便说一句,这将出现在嵌入中):
@Jason joined. You must construct additional pylons.
有人可以帮我编辑我的代码使其正常工作吗?
这是我的主要代码:
import Events.*;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.entities.Activity;
import javax.security.auth.login.LoginException;
public class Main {
public static void main(String[] args) throws LoginException {
JDABuilder jda = JDABuilder.createDefault("I imported key here");
jda.setActivity(Activity.watching("baldness"));
jda.addEventListeners(new Help());
jda.addEventListeners(new PingPong());
jda.addEventListeners(new Clear());
jda.addEventListeners(new Welcome());
jda.build();
}
}
这是我的欢迎代码:
package Events;
import java.util.Random;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class Welcome extends ListenerAdapter {
public class GuildMemberJoin extends ListenerAdapter {
String[] messages = {
"[member] joined. You must construct additional pylons.",
"Never gonna give [member] up. Never let [member] down!",
"Hey! Listen! [member] has joined!",
"Ha! [member] has joined! You activated my trap card!",
"We've been expecting you, [member].",
"It's dangerous to go alone, take [member]!",
"Swoooosh. [member] just landed.",
"Brace yourselves. [member] just joined the server.",
"A wild [member] appeared."
};
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Random rand = new Random();
int number = rand.nextInt(messages.length);
EmbedBuilder join = new EmbedBuilder();
join.setColor(0x66d8ff);
join.setDescription(messages[number].replace("[member]", event.getMember().getAsMention()));
event.getGuild().getDefaultChannel().sendMessage(join.build()).queue();
}
}
}
documentation fo GuildMemberJoinEvent 明确指出:
Requirements
This event requires the GUILD_MEMBERS intent to be enabled. createDefault(String) and createLight(String) disable this by default!
因此您必须启用意图。在 wiki 指南中阅读更多内容 Gateway Intents and Member Cache Policy
此外,您无缘无故地嵌套了 class,这意味着您将封闭的 class 注册为侦听器,而不是实际实现它的嵌套 class。
更好:
public class Welcome extends ListenerAdapter {
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
...
}
}