TrueMB / DiscordNotify

Connects your Discord Server with your Minecraft Server!
https://www.spigotmc.org/resources/discordnotify-notifications-and-utils-for-discord.94230/
GNU General Public License v2.0
7 stars 5 forks source link

[ISSUE] Bot isn't connecting, if server loads for a long time #3

Closed TrueMB closed 2 years ago

TrueMB commented 2 years ago

Describe the bug If too many plugins are installed and the Server gets started, it won't connect to the Discord Bot. https://github.com/TrueMB/DiscordNotify/blob/f28331380bd47cff597b93606bd14432b3d69aef/src/me/truemb/disnotify/spigot/main/Main.java#L139

Is it possible to wait for the Spicord Bot and not with a timer?

Server (please complete the following information):

OopsieWoopsie commented 2 years ago

Hi! I didn't knew that this plugin existed, if you want I can add it to the list of addons on the spigotmc page ^^

Is it possible to wait for the Spicord Bot and not with a timer?

Yes, I have some code tips for you. This snippet is for getting the Spicord instance without running Spicord.getInstance() as is not safe because it can return null if Spicord has not loaded when the method is executed.

import org.spicord.SpicordLoader;

public void onEnable() {
    // This is an async callback, your code will not stop waiting, it will only be executed once Spicord loads.
    SpicordLoader.addStartupListener(spicord -> {
        // Spicord has loaded, and you can now use the valid instance from the "spicord" parameter.
        // As running Spicord.getInstance() is not safe.
    });
}

Now, to know when a bot has been loaded or when the bot is fully ready, you can implement one (or both) of these methods in your addon class (DC_PlayerInfoCommand.java / DC_VerifyCommand.java)

public class YourAddon extends SimpleAddon {
    @Override
    public void onLoad(DiscordBot bot) {
        // A bot that is using your addon has been loaded, but it may be not ready
        // and may have not been connected yet to Discord, so it is not usable.
    }

    @Override
    public void onReady(DiscordBot bot) {
        // A bot that is using your addon is now fully ready to be used.
    }
}

Hope that helps, have fun coding :smile:

TrueMB commented 2 years ago

Hi! I didn't knew that this plugin existed, if you want I can add it to the list of addons on the spigotmc page ^^

That would be awsome! The funny thing is, I just saw your latest update, were you mentioned adding Plugins to your list. After that I went on your discord to find a chatroom for that. But I couldnt find any :'D

But how did you find the plugin? I never expected an answer of somebody else here and I am kinda suprised :D

Main Topic: Yes I know the Method and it would be one way to get it. But my plugin works so, that you can disable not needed features. So if somebody doesnt need the playerinfo and verify command, I still need to get the Bot.

OopsieWoopsie commented 2 years ago

That would be awsome! The funny thing is, I just saw your latest update, were you mentioned adding Plugins to your list. After that I went on your discord to find a chatroom for that. But I couldnt find any :'D

I have added it on the list ^^ you could have asked it on #general, as it is related to Spicord. In case you need help regarding the Spicord API or something related you can join the Discord server and ask, you are not bothering.

But how did you find the plugin? I never expected an answer of somebody else here and I am kinda suprised :D

Just coincidence :sweat_smile:

So if somebody doesnt need the playerinfo and verify command, I still need to get the Bot.

Oh- well, at least you can now get the Spicord instance safely :smile:

TrueMB commented 2 years ago

Oh- well, at least you can now get the Spicord instance safely 😄

You mean, if somebody uses the verify or Playerinfo command? Or is there another way?

It's 3am right now, so I will take a closer look tomorrow, since I finished the update of my other plugin. But I might just ask more often for the bot and not only once after server start, how it currently is.

OopsieWoopsie commented 2 years ago

You mean, if somebody uses the verify or Playerinfo command? Or is there another way?

Quote from the original issue:

Is it possible to wait for the Spicord Bot and not with a timer?

By using the code snippet I sent you, you won't need to use a timer anymore, and you will get the valid Spicord instance as soon as it loads, and then getting the bot instance from it.

TrueMB commented 2 years ago

You mean, if somebody uses the verify or Playerinfo command? Or is there another way?

Quote from the original issue:

Is it possible to wait for the Spicord Bot and not with a timer?

By using the code snippet I sent you, you won't need to use a timer anymore, and you will get the valid Spicord instance as soon as it loads, and then getting the bot instance from it.

But for that, your config needs to know the Addon, doesnt it? What I mean: If a player doesnt want to use the verify and playerinfo command, he just doesnt add the Addons in your Config. That would mean for my plugin that the Addon doesnt get loaded, only registered. But this doesnt get the correct Bot.

Even if somebody doesnt uses the Addons, I need the Bot. F.e. for messages that I send to the Discord Server.

OopsieWoopsie commented 2 years ago

But for that, your config needs to know the Addon, doesnt it? What I mean: If a player doesnt want to use the verify and playerinfo command, he just doesnt add the Addons in your Config. That would mean for my plugin that the Addon doesnt get loaded, only registered. But this doesnt get the correct Bot.

Even if somebody doesnt uses the Addons, I need the Bot. F.e. for messages that I send to the Discord Server.

I'm talking about the first code snippet, the one that doesn't uses any addon class, as you don't need to register any addon for using the SpicordLoader.addStartupListener method

TrueMB commented 2 years ago

OHH Now I get what you mean. I thought the first Code registered the Addon. Didnt read correctly. Sorry!

TrueMB commented 2 years ago

Should be fixed in cdad59e308db3ecd23ca67a671bf6c4ced439fd4