Javacord / Javacord

An easy to use multithreaded library for creating Discord bots in Java.
https://discord.gg/javacord
Apache License 2.0
762 stars 180 forks source link

java.lang.IllegalStateException: No DelegateFactoryDelegate implementation was found! #677

Closed ghost closed 3 years ago

ghost commented 3 years ago

Hello, I was trying to create a discord bot. In my IDE it worked fine but as I exported it there was an error:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.javacord.api.DiscordApiBuilder.<init>(DiscordApiBuilder.java:33)
    at me.sirlennox.selfy.Selfy.buildBot(Selfy.java:40)
    at me.sirlennox.selfy.Selfy.<init>(Selfy.java:35)
    at me.sirlennox.selfy.Main.main(Main.java:22)
Caused by: java.lang.IllegalStateException: No DelegateFactoryDelegate implementation was found!
    at org.javacord.api.util.internal.DelegateFactory.<clinit>(DelegateFactory.java:74)
    ... 4 more

I hope you can help me, thanks :)

ghost commented 3 years ago

I tried it with maven and gradle (Build > Build Artifact > Build)

ShindouMihou commented 3 years ago

Do you mind showing some code snippets?

ghost commented 3 years ago

yes

    public Selfy(String name, String version, String prefix, String token, ArrayList<String> developers, me.sirlennox.selfy.AccountType accountType) {
        this.NAME = name;
        this.VERSION = version;
        this.PREFIX = prefix;
        this.TOKEN = token;
        this.DEVELOPERS = developers;
        this.commandManager = new CommandManager();
        this.moduleManager = new ModuleManager();
        this.API = addEventListeners(buildBot(token));
        this.accountType = accountType;
    }

public DiscordApi addEventListeners(DiscordApi api) {
        api.addMessageCreateListener(event -> {
            if(event.getMessage().getAuthor().getId() == api.getYourself().getId()) {
                String msg = event.getMessageContent();
                if(msg.startsWith(PREFIX)) {
                    String msgWithoutPrefix = msg.substring(PREFIX.length());
                    String[] args = new String[msgWithoutPrefix.split(" ").length - 1];
                    for(int i = 1; i < msgWithoutPrefix.split(" ").length; i++) {
                        args[i - 1] = msgWithoutPrefix.split(" ")[i];
                    }
                    String c = msgWithoutPrefix.split(" ")[0];
                    for(Command cmd : this.commandManager.commands) {
                        if(cmd.cmd.equalsIgnoreCase(c) || isAliasOfCommand(cmd, c)) {
                            if(cmd.onlyPremium && !Utils.hasAccessToPremiumFeatures()) {
                                MessageUtils.editMessage("Error", "This command is premium only and you don't have premium!", Color.RED.getRGB(), event.getMessage());
                                return;
                            }
                            cmd.onCommand(args, event);
                            return;
                        }
                    }
                }
            }
        });
        return api;
    }

    public DiscordApi buildBot(String token) {
        return new DiscordApiBuilder().setAccountType(AccountType.CLIENT).setToken(token).login().join();
    }
Vampire commented 3 years ago

You probably try to build a fat jar and do it wrongly, which is one of the reasons fat jars are evil. You miss the javacord-core artifact in your fat jar, or at least the relevant files from META-INF. As you mentioned Gradle, I'd strongly recommend building a proper distribution instead, by applying the application plugin. Then (and after configuring the main class properly) you can use distTar or distZip to create a distributable archive with everyting you need and nifty start scripts in the bin directory of that archive.

ghost commented 3 years ago

I have javacord-core and in my META-INF is only a MANIFEST.MF

ghost commented 3 years ago

so is this wrong?

Vampire commented 3 years ago

Yes, that is wrong

Vampire commented 3 years ago

Btw. you are trying to build a self-bot, that is a violation of the Discord ToS, will in near future be removed from Javacord completely and is already broken in large parts already due to incompatible changes on Discord side.

ghost commented 3 years ago

Ok, and what do I have to put into my META-INF?

Vampire commented 3 years ago

The javacord-core artifact has files in there in services that are essential. If you repack the artifact but leave out those files, you should not wonder that it does not work. That's one of the reasons fat jars are evil, you are modifying libraries in unsupported ways and then go to the maintainers and complain that their library does not work, while you actively broke it by manipulating the files. ;-)

ghost commented 3 years ago

Thank you very much, it worked fine. Please stay supporting selfbots, because there aren't any java libraries with selfbot support, and I like java (I don't want to switch to python) :)