You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用discord4j实现Discord消息回复功能?

解决Discord4J中消息回复的问题

在Discord4J中,要实现消息回复,你需要使用MessageCreateSpec来配置消息的回复目标,而非直接传入字符串调用createMessage。以下是修改后的完整代码:

@PostMapping("/say")
public @ResponseBody String say(
    @RequestParam("message") String message,
    @RequestParam("channelId") String channelID,
    @RequestParam("replyMessageId") String replyMessageId
) { 
    // 直接用消息ID指定回复目标,无需提前阻塞获取Message对象,提升性能
    Snowflake targetMessageId = Snowflake.of(replyMessageId);
        
    client.getChannelById(Snowflake.of(channelID)).ofType(MessageChannel.class)
        .flatMap(channel -> channel.createMessage(spec -> {
            spec.content(message);
            // 指定要回复的消息ID
            spec.replyTo(targetMessageId);
            // 可选:关闭回复时对原消息发送者的提及,默认开启
            // spec.allowedMentions(mentions -> mentions.repliedUser(false));
        })).subscribe();
        
    return "OK";
}

关键说明

  • MessageCreateSpec是Discord4J构建消息的核心配置类,支持设置内容、嵌入、附件、回复目标等多种属性。
  • 若你已获取到messageToReply对象,也可替换为spec.replyTo(messageToReply.getId())来指定回复目标。
  • 通过allowedMentions()可以控制回复消息的提及规则,比如关闭对原发送者的@提醒。

内容的提问来源于stack exchange,提问作者Óscar Raya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 13:30:21