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

Scala3使用discord4j 3.2斜杠命令报Bad symbolic reference错误如何解决?

问题描述

我尝试在Scala 3代码中使用Discord4J 3.2开发斜杠命令,添加斜杠命令相关代码后,sbt编译时抛出如下错误:

[error] Bad symbolic reference. A signature
[error] refers to Value/T in package org.immutables.value which is not available.
[error] It may be completely missing from the current classpath, or the version on
[error] the classpath might be incompatible with the version used when compiling the signature.
[warn] Caught: java.lang.AssertionError: assertion failed: failure to resolve inner class:
[warn] externalName = org.immutables.value.Value$Immutable,
[warn] outerName = org.immutables.value.Value,
[warn] innerName = Immutable
[warn] owner.fullName = org.immutables.value.Value
[warn] while parsing ~/.cache/coursier/v1/https/oss.sonatype.org/content/repositories/snapshots/com/discord4j/discord-json/1.6.10-SNAPSHOT/discord-json-1.6.10-20210908.025209-4.jar(discord4j/discordjson/json/InteractionApplicationCommandCallbackData.class) while parsing annotations in ~/.cache/coursier/v1/https/oss.sonatype.org/content/repositories/snapshots/com/discord4j/discord-json/1.6.10-SNAPSHOT/discord-json-1.6.10-20210908.025209-4.jar(discord4j/discordjson/json/InteractionApplicationCommandCallbackData.class)
[error] Bad symbolic reference. A signature

相同版本的Discord4J在Scala 2中编写斜杠命令代码时,编译没有任何问题。
我的sbt配置如下:

val scala3Version = "3.0.2"

val scala2deps = Seq(
  "com.typesafe.akka" %% "akka-actor" % "2.6.0",
  "com.typesafe.akka" %% "akka-actor-typed" % "2.6.0",
  "com.typesafe.akka" %% "akka-stream" % "2.6.8",
  "com.typesafe.akka" %% "akka-http" % "10.2.0",

  "org.scalaz" %% "scalaz-core" % "7.2.29",

  "io.projectreactor" %% "reactor-scala-extensions" % "0.8.+",

  "org.json4s" %% "json4s-jackson" % "3.+",

  "com.lihaoyi" %% "requests" % "0.2.0",
).map(d ⇒ d.cross(CrossVersion.for3Use2_13))

lazy val root = project
  .in(file("."))
  .settings(
    name := "PUGBot",
    version := "0.1.0",

    scalacOptions ++= Seq(
      "-language:postfixOps",
      "-language:implicitConversions",
      ),


    scalaVersion := scala3Version,

    resolvers += "d4j-snapshot" at "https://oss.sonatype.org/content/repositories/snapshots",

    libraryDependencies ++= Seq(
      "com.discord4j" % "discord4j-core" % "3.2.0-RC3",
    ) ++ scala2deps,

  )

请问如何修复这个编译错误?


解决方案

这个错误的根本原因是Scala 3的符号解析逻辑比Scala 2更严格,Discord4J底层依赖的discord-json模块使用org.immutables注解生成类,该依赖默认仅作为编译时依赖引入,不会自动进入编译classpath,Scala 2会直接忽略找不到的注解类,而Scala 3会抛出编译错误。

你可以选择以下任意一种方案修复:

  • 方案1:手动添加immutables依赖到依赖列表
    在libraryDependencies中加入以下配置即可:
    "org.immutables" % "value" % "2.9.0-beta3" % Provided
    
    这个版本和Discord4J 3.2版本使用的immutables版本完全兼容,添加后不会引入冗余的运行时依赖。
  • 方案2:添加Scala 3编译参数跳过缺失注解校验
    在scalacOptions中添加参数-Yignore-missing-annotations,让编译器直接忽略找不到的注解,不会强制校验相关符号。
  • 方案3:升级Discord4J到正式版
    你当前使用的是3.2.0-RC3预览版,依赖的是快照版本的discord-json,升级到Discord4J 3.2.x正式版即可解决快照版本的注解兼容性问题。

内容的提问来源于stack exchange,提问作者nefas

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 13:36:00