"DiscordApi cannot be resolved to a type" Discord Java Eclipse 机器人
"DiscordApi cannot be resolved to a type" Discord Java Eclipse Bot
package Bots;
public class FirstBot {
public static void main(String[] args) {
// Insert your bot's token here
String token = "TheToken";
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
String prefix = "!";
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessageContent().equalsIgnoreCase(""+prefix+"ping")) {
event.getChannel().sendMessage("Pong!");
}
});
// Print the invite url of your bot
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
}
}
我是在 Java 中创建 Discord 机器人的新手。我正在使用 Eclipse 并且我使用了这个入门代码 ^
它给我一个错误,即 DiscordApi 无法解析为类型 并且 DiscordApiBuilder 无法解析为类型
您需要做的第一件事是确保正确设置了 JavaCord Maven 依赖项。
将此添加到 pom.xml 的 <dependencies>
字段中:
<dependency>
<groupId>org.javacord</groupId>
<artifactId>javacord</artifactId>
<version>3.3.0</version>
<type>pom</type>
</dependency>
下一步是将 JavaCord 包隐藏到您的最终 jar 中,以便您可以直接 运行 它。将此添加到您的 pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<relocations>
<relocation>
<pattern>org.javacord</pattern>
<shadedPattern>your.package.name.here.dependencies.javacord</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果您已有 <build>
或 <plugins>
字段,请将其放入其中。
最后一步是将相关的 JavaCord class 导入到您的主 class 中。如果您尝试再次输入 class 名称,Eclipse 应该会提供导入它们的选项。
package Bots;
public class FirstBot {
public static void main(String[] args) {
// Insert your bot's token here
String token = "TheToken";
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
String prefix = "!";
// Add a listener which answers with "Pong!" if someone writes "!ping"
api.addMessageCreateListener(event -> {
if (event.getMessageContent().equalsIgnoreCase(""+prefix+"ping")) {
event.getChannel().sendMessage("Pong!");
}
});
// Print the invite url of your bot
System.out.println("You can invite the bot by using the following url: " + api.createBotInvite());
}
}
我是在 Java 中创建 Discord 机器人的新手。我正在使用 Eclipse 并且我使用了这个入门代码 ^ 它给我一个错误,即 DiscordApi 无法解析为类型 并且 DiscordApiBuilder 无法解析为类型
您需要做的第一件事是确保正确设置了 JavaCord Maven 依赖项。
将此添加到 pom.xml 的 <dependencies>
字段中:
<dependency>
<groupId>org.javacord</groupId>
<artifactId>javacord</artifactId>
<version>3.3.0</version>
<type>pom</type>
</dependency>
下一步是将 JavaCord 包隐藏到您的最终 jar 中,以便您可以直接 运行 它。将此添加到您的 pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<configuration>
<relocations>
<relocation>
<pattern>org.javacord</pattern>
<shadedPattern>your.package.name.here.dependencies.javacord</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
如果您已有 <build>
或 <plugins>
字段,请将其放入其中。
最后一步是将相关的 JavaCord class 导入到您的主 class 中。如果您尝试再次输入 class 名称,Eclipse 应该会提供导入它们的选项。