Discord4J可以使用Interactions API来添加按钮。

以下是一个示例,演示如何在Discord4J中创建一个按钮,并在用户单击时触发事件:

import discord4j.core.DiscordClient;
import discord4j.core.GatewayDiscordClient;
import discord4j.core.event.domain.interaction.ButtonInteractEvent;
import discord4j.core.event.domain.interaction.InteractionCreateEvent;
import discord4j.core.object.component.ActionRow;
import discord4j.core.object.component.Button;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.channel.MessageChannel;
import discord4j.discordjson.json.ApplicationCommandOptionData;
import discord4j.rest.util.ApplicationCommandOptionType;
import reactor.core.publisher.Mono;

public class ButtonExample {
    public static void main(String[] args) {
        DiscordClient client = DiscordClient.create("YOUR_BOT_TOKEN");
        GatewayDiscordClient gateway = client.login().block();

        gateway.on(InteractionCreateEvent.class)
                .flatMap(event -> {
                    if (event.getInteraction().getCommandName().equals("button")) {
                        // Create a button with a custom ID
                        Button button = Button.primary("my_button", "Click me!");

                        // Create an action row with the button
                        ActionRow actionRow = ActionRow.of(button);

                        // Create a message with the action row
                        return event.reply(spec -> spec.setContent("Here's a button:").addComponent(actionRow));
                    } else {
                        return Mono.empty();
                    }
                })
                .flatMap(event -> {
                    // Get the message that was just sent
                    Message message = event.getMessage().block();

                    if (message != null) {
                        // Get the message channel
                        MessageChannel channel = message.getChannel().block();

                        if (channel != null) {
                            // Listen for button interactions in the message channel
                            return gateway.on(ButtonInteractEvent.class)
                                    .filter(interaction -> interaction.getMessage().getId().equals(message.getId()))
                                    .next()
                                    .flatMap(interaction -> {
                                        // Respond to the button click
                                        return interaction.reply("You clicked the button!").then();
                                    });
                        } else {
                            return Mono.empty();
                        }
                    } else {
                        return Mono.empty();
                    }
                })
                .subscribe();

        gateway.onDisconnect().block();
    }
}

在上面的示例中,我们创建了一个按钮并将其添加到消息中。然后,我们使用on(ButtonInteractEvent.class)方法来监听按钮交互事件。在监听到按钮点击事件后,我们可以在ButtonInteractEvent对象中获取按钮的ID和消息ID,并回复用户。

注意,按钮的ID必须是唯一的。如果您的应用程序需要使用多个按钮,请分配不同的ID。

此外,要使用Interactions API,您需要在Discord开发者门户中注册您的应用程序,并启用Interactions功能。您还需要在代码中设置应用程序的公共密钥,以便Discord可以验证您的交互请求。

java使用discord4j 在消息中添加按钮

原文地址: https://www.cveoy.top/t/topic/bjkr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录