JDA-新用户加入公会时发生的事件

时间:2020-06-14 11:33:11

标签: java discord-jda

我有问题。 Ima使用Java Discord API(JDA)制作了一个机器人。因此,当新用户加入服务器时,我需要运行。机器人发送消息。但是我的代码无法正常工作。

代码:

#kubectl version
Client Version: version.Info{Major:"1", Minor:"15", GitVersion:"v1.15.0", GitCommit:"e8462b5b5dc2584fdcd18e6bcfe9f1e4d970a529", GitTreeState:"clean", BuildDate:"2019-06-19T16:32:14Z", GoVersion:"go1.12.5", Compiler:"gc", Platform:"linux/amd64"}

# kubectl get pods -A  | grep -i istio | grep pilot
istio-platform      istio-pilot-7c5adrgcd89-wt9k                       2/2     Running       4          1d

#istioctl version
2020-06-14T11:26:13.636825Z warn    will use `--remote=false` to retrieve version info due to `no Istio pods in namespace "istio-system"`
1.4.8

# istioctl version -c istio-platform
2020-06-14T11:27:59.121013Z warn    will use `--remote=false` to retrieve version info due to `no Istio pods in namespace "istio-system"`
1.4.8

因此,当新用户加入我的公会时,它不会发送任何消息吗?你能帮我吗?

我也将其添加到事件监听器中

4 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

public class UserJoinModule extends ListenerAdapter {
    @Override // USE THIS WHEN YOU WANT TO OVERRIDE A METHOD
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        String user = event.getMember().getAsMention();
        JDA client = event.getJDA(); // DO NOT CREATE A NEW JDA INSTANCE EVERY TIME
        List<TextChannel> channels = client.getTextChannelsByName("awesome channel name", true);
        for (TextChannel ch : channels) {
            ch.sendMessage("New member joined: " + user).queue();
        }
    }
}

并且您必须在JDABuilder实例中注册此侦听器,最好在整个代码库中只有一个。参见addEventListeners

答案 1 :(得分:0)

您的代码中有2个问题。

  1. 每次成员加入时,您都在创建一个新的JDA客户端。
  2. 您正在向每个公会中具有该名称的每个频道发送消息。不只是用户加入的行会。

这是您想要做的:

public class UserJoinModule extends ListenerAdapter {
    @Override
    public void onGuildMemberJoin(GuildMemberJoinEvent event) {
        Guild guild = event.getGuild(); // Get the guild that the user joined.
        User user = event.getUser();    // Get the user that joined.
        JDA client = event.getJDA();    // Get the already existing JDA instance.

        List<TextChannel> channels = guild.getTextChannelsByName("awesome channel name", true); // Get the list of channels in the guild that matches that name.

        for (TextChannel channel : channels) { // Loops through the channels and sends a message to each one.
            channel.sendMessage("New member joined: " + user).queue();
        }
    }
}

答案 2 :(得分:0)

对我来说,问题不在于我所覆盖的侦听器和方法。 我相信您必须将ILIKE添加到您的JDABuilder中。

GatewayIntent.GUILD_MEMBERS

这为我解决了同一问题。

答案 3 :(得分:0)

在您的 Main.java 或任何文件中,有一个 JDABuilder 类型的变量,在它的同一行代码中,有您的令牌,最后是 .build() 等...

将此代码插入该行:

.enableIntents(GatewayIntent.GUILD_MEMBERS)

所以它看起来像这样:

jda = JDABuilder.createDefault("TOKEN").enableIntents(GatewayIntent.GUILD_MEMBERS).build();

要使其工作,请转到您的 Discord Developer Portal,单击您的机器人,从左侧菜单中,单击 Bot,然后向下滚动并启用:

  • 服务器成员意图

仍然存在明显的错误,例如在每条消息上注册新客户端和其他问题,修复它们,然后启动您的机器人,它应该可以工作。