【问题标题】:"DiscordApi cannot be resolved to a type" Discord Java Eclipse Bot“DiscordApi 无法解析为类型” Discord Java Eclipse Bot
【发布时间】:2021-08-22 22:24:17
【问题描述】:
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 无法解析为类型

【问题讨论】:

  • 你在导入类吗?
  • 我不这么认为。我可能没有导入任何类。我想我必须下载一些 Maven 的东西。不太确定。我也不知道要添加哪些。

标签: java eclipse error-handling discord


【解决方案1】:

您需要做的第一件事是确保正确设置了 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>

如果您已经有 &lt;build&gt;&lt;plugins&gt; 字段,请将其放入其中。

最后一步是将相关的 JavaCord 类导入到您的主类中。如果您尝试再次输入类名,Eclipse 应该提供导入它们的选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-16
    • 2021-01-26
    • 2012-05-27
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多