要在 Java Discord Bot 中添加按钮,您需要使用 JDA(Java Discord API)。以下是一个简单的示例代码,用于创建一个包含一个按钮的消息:

public void onMessageReceived(MessageReceivedEvent event) {
    if (event.getMessage().getContentRaw().equals('!button')) {
        // Create a new message builder
        MessageBuilder builder = new MessageBuilder();
        
        // Create a new action row with a button
        ActionRow row = ActionRow.of(Button.primary('button', 'Click me!'));
        
        // Add the action row to the message builder
        builder.setActionRows(row);
        
        // Send the message to the channel
        event.getChannel().sendMessage(builder.build()).queue();
    }
}

在这个示例中,当用户发送'!button'消息时,Bot会创建一个新的消息,并在其中添加一个名为'button'的主按钮。您可以根据需要更改按钮的样式和标签。

Java Discord Bot: 如何在消息中添加按钮

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

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