嘿,我想向新朋友发送欢迎消息。 这是我的代码。我是Java的新手,请帮助我使此功能正常运行:
package de.backxtar.listener;
import java.time.OffsetDateTime;
import net.dv8tion.jda.api.EmbedBuilder;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.events.guild.member.GuildMemberJoinEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class JoinListener extends ListenerAdapter {
@Override
public void onGuildMemberJoin(GuildMemberJoinEvent event) {
Member member = event.getMember();
if((event.getGuild().getDefaultChannel()) != null) {
EmbedBuilder builder = new EmbedBuilder();
builder.setColor(0xf22613);
builder.setThumbnail("http://image.png");
builder.setTitle("Willkommen auf Da Hood!");
builder.setFooter("Powered by Backxtar.");
builder.setTimestamp(OffsetDateTime.now());
builder.setDescription("Herzlich willkommen" + member.getAsMention() + "auf **Da Hood**!\n"
+ "[**Da Hood - The Best Gaming-Discord!**](https://xyz.gg)");
member.getUser().openPrivateChannel().queue((ch) -> {
ch.sendMessage(builder.build()).queue();
});
}
}
}
此功能如何? if((event.getGuild().getDefaultChannel()) != null) {
谢谢!
答案 0 :(得分:0)
首先,event.getGuild().getDefaultChannel()
将为您返回服务器设置中为“ 系统消息通道”设置的通道,如果未设置,则返回Discord Server上的第一个文本通道
不幸的是,我无法告诉您为什么您的代码对我不起作用,因为它对我来说完美无缺,也许是因为您的Discord服务器没有 TextChannel ,如果是这种情况,请尝试您在Discord服务器上创建 TextChannel 时,代码将起作用。